antlr4-runtime 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +65 -0
- data/Rakefile +6 -0
- data/antlr4-runtime.gemspec +30 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ext/rumourhash/Makefile +264 -0
- data/ext/rumourhash/extconf.rb +3 -0
- data/ext/rumourhash/rumourhash.c +59 -0
- data/lib/antlr4/runtime.rb +37 -0
- data/lib/antlr4/runtime/abstract_parse_tree_visitor.rb +43 -0
- data/lib/antlr4/runtime/abstract_predicate_transition.rb +11 -0
- data/lib/antlr4/runtime/action_transition.rb +29 -0
- data/lib/antlr4/runtime/ambiguity_info.rb +10 -0
- data/lib/antlr4/runtime/antlr_error_listener.rb +15 -0
- data/lib/antlr4/runtime/antlr_error_strategy.rb +24 -0
- data/lib/antlr4/runtime/antlr_file_stream.rb +17 -0
- data/lib/antlr4/runtime/antlr_input_stream.rb +6 -0
- data/lib/antlr4/runtime/array_2d_hash_set.rb +471 -0
- data/lib/antlr4/runtime/array_prediction_context.rb +76 -0
- data/lib/antlr4/runtime/atn.rb +100 -0
- data/lib/antlr4/runtime/atn_config.rb +140 -0
- data/lib/antlr4/runtime/atn_config_set.rb +150 -0
- data/lib/antlr4/runtime/atn_deserialization_options.rb +48 -0
- data/lib/antlr4/runtime/atn_deserializer.rb +737 -0
- data/lib/antlr4/runtime/atn_simulator.rb +69 -0
- data/lib/antlr4/runtime/atn_state.rb +118 -0
- data/lib/antlr4/runtime/atn_type.rb +8 -0
- data/lib/antlr4/runtime/atom_transition.rb +27 -0
- data/lib/antlr4/runtime/bail_error_strategy.rb +31 -0
- data/lib/antlr4/runtime/base_error_listener.rb +18 -0
- data/lib/antlr4/runtime/basic_block_start_state.rb +12 -0
- data/lib/antlr4/runtime/basic_state.rb +11 -0
- data/lib/antlr4/runtime/bit_set.rb +54 -0
- data/lib/antlr4/runtime/block_end_state.rb +15 -0
- data/lib/antlr4/runtime/block_start_state.rb +12 -0
- data/lib/antlr4/runtime/buffered_token_stream.rb +335 -0
- data/lib/antlr4/runtime/char_stream.rb +6 -0
- data/lib/antlr4/runtime/char_streams.rb +12 -0
- data/lib/antlr4/runtime/chunk.rb +4 -0
- data/lib/antlr4/runtime/code_point_char_stream.rb +83 -0
- data/lib/antlr4/runtime/common_token.rb +125 -0
- data/lib/antlr4/runtime/common_token_factory.rb +30 -0
- data/lib/antlr4/runtime/common_token_stream.rb +63 -0
- data/lib/antlr4/runtime/console_error_listener.rb +12 -0
- data/lib/antlr4/runtime/context_sensitivity_info.rb +7 -0
- data/lib/antlr4/runtime/decision_event_info.rb +19 -0
- data/lib/antlr4/runtime/decision_info.rb +36 -0
- data/lib/antlr4/runtime/decision_state.rb +15 -0
- data/lib/antlr4/runtime/default_error_strategy.rb +314 -0
- data/lib/antlr4/runtime/dfa.rb +97 -0
- data/lib/antlr4/runtime/dfa_serializer.rb +62 -0
- data/lib/antlr4/runtime/dfa_state.rb +109 -0
- data/lib/antlr4/runtime/diagnostic_error_listener.rb +58 -0
- data/lib/antlr4/runtime/double_key_map.rb +49 -0
- data/lib/antlr4/runtime/empty_prediction_context.rb +35 -0
- data/lib/antlr4/runtime/epsilon_transition.rb +27 -0
- data/lib/antlr4/runtime/equality_comparator.rb +4 -0
- data/lib/antlr4/runtime/error_info.rb +7 -0
- data/lib/antlr4/runtime/error_node.rb +5 -0
- data/lib/antlr4/runtime/error_node_impl.rb +12 -0
- data/lib/antlr4/runtime/failed_predicate_exception.rb +33 -0
- data/lib/antlr4/runtime/flexible_hash_map.rb +232 -0
- data/lib/antlr4/runtime/input_mismatch_exception.rb +20 -0
- data/lib/antlr4/runtime/int_stream.rb +31 -0
- data/lib/antlr4/runtime/integer.rb +14 -0
- data/lib/antlr4/runtime/interval.rb +111 -0
- data/lib/antlr4/runtime/interval_set.rb +540 -0
- data/lib/antlr4/runtime/lexer.rb +257 -0
- data/lib/antlr4/runtime/lexer_action.rb +12 -0
- data/lib/antlr4/runtime/lexer_action_executor.rb +75 -0
- data/lib/antlr4/runtime/lexer_action_type.rb +12 -0
- data/lib/antlr4/runtime/lexer_atn_config.rb +50 -0
- data/lib/antlr4/runtime/lexer_atn_simulator.rb +522 -0
- data/lib/antlr4/runtime/lexer_channel_action.rb +51 -0
- data/lib/antlr4/runtime/lexer_custom_action.rb +49 -0
- data/lib/antlr4/runtime/lexer_dfa_serializer.rb +12 -0
- data/lib/antlr4/runtime/lexer_indexed_custom_action.rb +49 -0
- data/lib/antlr4/runtime/lexer_mode_action.rb +51 -0
- data/lib/antlr4/runtime/lexer_more_action.rb +41 -0
- data/lib/antlr4/runtime/lexer_no_viable_alt_exception.rb +4 -0
- data/lib/antlr4/runtime/lexer_pop_mode_action.rb +41 -0
- data/lib/antlr4/runtime/lexer_push_mode_action.rb +51 -0
- data/lib/antlr4/runtime/lexer_skip_action.rb +43 -0
- data/lib/antlr4/runtime/lexer_type_action.rb +51 -0
- data/lib/antlr4/runtime/ll1_analyzer.rb +133 -0
- data/lib/antlr4/runtime/lookahead_event_info.rb +10 -0
- data/lib/antlr4/runtime/loop_end_state.rb +15 -0
- data/lib/antlr4/runtime/murmur_hash.rb +99 -0
- data/lib/antlr4/runtime/no_viable_alt_exception.rb +7 -0
- data/lib/antlr4/runtime/not_set_transition.rb +20 -0
- data/lib/antlr4/runtime/object_equality_comparator.rb +18 -0
- data/lib/antlr4/runtime/ordered_atn_config_set.rb +15 -0
- data/lib/antlr4/runtime/parse_cancellation_exception.rb +5 -0
- data/lib/antlr4/runtime/parse_tree.rb +7 -0
- data/lib/antlr4/runtime/parse_tree_listener.rb +4 -0
- data/lib/antlr4/runtime/parse_tree_visitor.rb +4 -0
- data/lib/antlr4/runtime/parser.rb +522 -0
- data/lib/antlr4/runtime/parser_atn_simulator.rb +1171 -0
- data/lib/antlr4/runtime/parser_rule_context.rb +186 -0
- data/lib/antlr4/runtime/plus_block_start_state.rb +11 -0
- data/lib/antlr4/runtime/plus_loopback_state.rb +12 -0
- data/lib/antlr4/runtime/precedence_predicate_transition.rb +31 -0
- data/lib/antlr4/runtime/predicate.rb +6 -0
- data/lib/antlr4/runtime/predicate_eval_info.rb +16 -0
- data/lib/antlr4/runtime/predicate_transition.rb +35 -0
- data/lib/antlr4/runtime/prediction_context.rb +103 -0
- data/lib/antlr4/runtime/prediction_context_cache.rb +28 -0
- data/lib/antlr4/runtime/prediction_context_utils.rb +407 -0
- data/lib/antlr4/runtime/prediction_mode.rb +213 -0
- data/lib/antlr4/runtime/profiling_atn_simulator.rb +149 -0
- data/lib/antlr4/runtime/proxy_error_listener.rb +33 -0
- data/lib/antlr4/runtime/range_transition.rb +29 -0
- data/lib/antlr4/runtime/recognition_exception.rb +17 -0
- data/lib/antlr4/runtime/recognizer.rb +136 -0
- data/lib/antlr4/runtime/rule_context.rb +131 -0
- data/lib/antlr4/runtime/rule_context_with_alt_num.rb +11 -0
- data/lib/antlr4/runtime/rule_node.rb +8 -0
- data/lib/antlr4/runtime/rule_start_state.rb +17 -0
- data/lib/antlr4/runtime/rule_stop_state.rb +12 -0
- data/lib/antlr4/runtime/rule_tag_token.rb +64 -0
- data/lib/antlr4/runtime/rule_transition.rb +29 -0
- data/lib/antlr4/runtime/semantic_context.rb +313 -0
- data/lib/antlr4/runtime/set_transition.rb +29 -0
- data/lib/antlr4/runtime/singleton_prediction_context.rb +56 -0
- data/lib/antlr4/runtime/star_block_start_state.rb +12 -0
- data/lib/antlr4/runtime/star_loop_entry_state.rb +17 -0
- data/lib/antlr4/runtime/star_loopback_state.rb +16 -0
- data/lib/antlr4/runtime/syntax_tree.rb +6 -0
- data/lib/antlr4/runtime/tag_chunk.rb +22 -0
- data/lib/antlr4/runtime/terminal_node.rb +5 -0
- data/lib/antlr4/runtime/terminal_node_impl.rb +50 -0
- data/lib/antlr4/runtime/text_chunk.rb +16 -0
- data/lib/antlr4/runtime/token.rb +13 -0
- data/lib/antlr4/runtime/token_stream.rb +13 -0
- data/lib/antlr4/runtime/token_tag_token.rb +22 -0
- data/lib/antlr4/runtime/tokens_start_state.rb +14 -0
- data/lib/antlr4/runtime/transition.rb +51 -0
- data/lib/antlr4/runtime/tree.rb +4 -0
- data/lib/antlr4/runtime/trees.rb +195 -0
- data/lib/antlr4/runtime/triple.rb +40 -0
- data/lib/antlr4/runtime/utils.rb +117 -0
- data/lib/antlr4/runtime/uuid.rb +46 -0
- data/lib/antlr4/runtime/version.rb +5 -0
- data/lib/antlr4/runtime/vocabulary.rb +12 -0
- data/lib/antlr4/runtime/vocabulary_impl.rb +82 -0
- data/lib/antlr4/runtime/wildcard_transition.rb +20 -0
- data/lib/antlr4/runtime/writable_token.rb +7 -0
- metadata +243 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
#include <ruby.h>
|
|
2
|
+
|
|
3
|
+
#define c1 0xCC9E2D51
|
|
4
|
+
#define c2 0x1B873593
|
|
5
|
+
#define r1 15
|
|
6
|
+
#define r2 13
|
|
7
|
+
#define m 5
|
|
8
|
+
#define n 0xE6546B64
|
|
9
|
+
|
|
10
|
+
static VALUE rumour_hash_update_int(VALUE self, VALUE hashv, VALUE valuev) {
|
|
11
|
+
long hash = NUM2LONG(hashv);
|
|
12
|
+
long value = NUM2LONG(valuev);
|
|
13
|
+
|
|
14
|
+
long k = value;
|
|
15
|
+
k *= c1;
|
|
16
|
+
k = (k << r1) | (k >> (32 - r1));
|
|
17
|
+
k *= c2;
|
|
18
|
+
|
|
19
|
+
hash = hash ^ k;
|
|
20
|
+
hash = (hash << r2) | (hash >> (32 - r2));
|
|
21
|
+
hash *= m + n;
|
|
22
|
+
|
|
23
|
+
return LONG2NUM(hash);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static VALUE rumour_hash_finish(VALUE self, VALUE hashv, VALUE n_wordsv) {
|
|
27
|
+
long hash = NUM2LONG(hashv);
|
|
28
|
+
long n_words = NUM2LONG(n_wordsv);
|
|
29
|
+
|
|
30
|
+
hash = hash ^ (n_words * 4);
|
|
31
|
+
hash = hash ^ (hash >> 16);
|
|
32
|
+
hash *= 0x85EBCA6B;
|
|
33
|
+
hash = hash ^ (hash >> 13);
|
|
34
|
+
hash *= 0xC2B2AE35;
|
|
35
|
+
hash ^= (hash >> 16);
|
|
36
|
+
|
|
37
|
+
return LONG2NUM(hash);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static VALUE bit_count(VALUE self, VALUE v) {
|
|
41
|
+
long num = NUM2LONG(v);
|
|
42
|
+
int count = 0;
|
|
43
|
+
|
|
44
|
+
while (num)
|
|
45
|
+
{
|
|
46
|
+
num &= (num-1) ;
|
|
47
|
+
count++;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return INT2NUM(count);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
void Init_rumourhash() {
|
|
54
|
+
VALUE mod = rb_define_module("RumourHash");
|
|
55
|
+
rb_define_method(mod, "rumour_hash_update_int", rumour_hash_update_int, 2);
|
|
56
|
+
rb_define_method(mod, "rumour_hash_finish", rumour_hash_finish, 2);
|
|
57
|
+
rb_define_method(mod, "bit_count", bit_count, 1);
|
|
58
|
+
}
|
|
59
|
+
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
require 'set'
|
|
2
|
+
require 'singleton'
|
|
3
|
+
require 'ostruct'
|
|
4
|
+
require 'weakref'
|
|
5
|
+
|
|
6
|
+
require "rumourhash/rumourhash"
|
|
7
|
+
|
|
8
|
+
module Antlr4
|
|
9
|
+
module Runtime
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
module RumourHash
|
|
14
|
+
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
module BitCount
|
|
18
|
+
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
require 'antlr4/runtime/interval'
|
|
22
|
+
require 'antlr4/runtime/char_streams'
|
|
23
|
+
require 'antlr4/runtime/common_token_stream'
|
|
24
|
+
require 'antlr4/runtime/prediction_context_cache'
|
|
25
|
+
require 'antlr4/runtime/vocabulary_impl'
|
|
26
|
+
require 'antlr4/runtime/dfa'
|
|
27
|
+
require 'antlr4/runtime/atn_deserializer'
|
|
28
|
+
require 'antlr4/runtime/lexer_atn_simulator'
|
|
29
|
+
require 'antlr4/runtime/parser_atn_simulator'
|
|
30
|
+
require 'antlr4/runtime/parser_rule_context'
|
|
31
|
+
require 'antlr4/runtime/lexer'
|
|
32
|
+
require 'antlr4/runtime/parser'
|
|
33
|
+
require 'antlr4/runtime/parse_tree_listener'
|
|
34
|
+
require 'antlr4/runtime/parse_tree_visitor'
|
|
35
|
+
require 'antlr4/runtime/abstract_parse_tree_visitor'
|
|
36
|
+
require 'antlr4/runtime/recognition_exception'
|
|
37
|
+
require 'antlr4/runtime/utils'
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
module Antlr4::Runtime
|
|
2
|
+
class AbstractParseTreeVisitor
|
|
3
|
+
def visit(tree)
|
|
4
|
+
tree.accept(self)
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def visit_children(node)
|
|
8
|
+
result = default_result
|
|
9
|
+
n = node.child_count
|
|
10
|
+
i = 0
|
|
11
|
+
while i < n
|
|
12
|
+
break unless should_visit_next_child(node, result)
|
|
13
|
+
|
|
14
|
+
c = node.child_at(i)
|
|
15
|
+
child_result = c.accept(self)
|
|
16
|
+
result = aggregate_result(result, child_result)
|
|
17
|
+
i += 1
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
result
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def visit_terminal(_node)
|
|
24
|
+
default_result
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def visit_error_node(_node)
|
|
28
|
+
default_result
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def default_result
|
|
32
|
+
nil
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def aggregate_result(_aggregate, next_result)
|
|
36
|
+
next_result
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def should_visit_next_child(_node, _current_result)
|
|
40
|
+
true
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module Antlr4::Runtime
|
|
2
|
+
|
|
3
|
+
class ActionTransition < Transition
|
|
4
|
+
attr_reader :action_index
|
|
5
|
+
|
|
6
|
+
def initialize(target, rule_index, action_index, is_ctx_dependent)
|
|
7
|
+
super(target)
|
|
8
|
+
@rule_index = rule_index
|
|
9
|
+
@action_index = action_index
|
|
10
|
+
@is_ctx_dependent = is_ctx_dependent
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def serialization_type
|
|
14
|
+
ACTION
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def epsilon?
|
|
18
|
+
true
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def matches(_symbol, _min_vocab_symbol, _max_vocab_symbol)
|
|
22
|
+
false
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def to_s
|
|
26
|
+
'action_' + @rule_index + ':' + @action_index
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
module Antlr4::Runtime
|
|
2
|
+
|
|
3
|
+
class AmbiguityInfo < DecisionEventInfo
|
|
4
|
+
def initialize(decision, configs, ambig_alts, input, start_index, stop_index, full_ctx)
|
|
5
|
+
super(decision, configs, input, start_index, stop_index, full_ctx)
|
|
6
|
+
@ambig_alts = ambig_alts
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module Antlr4::Runtime
|
|
2
|
+
class ANTLRErrorListener
|
|
3
|
+
def syntax_error(_recognizer, _offending_symbol, _line, _char_position_in_line, _msg, _e)
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
def report_ambiguity(_recognizer, _dfa, _start_index, _stop_index, _exact, _ambig_alts, _configs)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def report_attempting_full_context(_recognizer, _dfa, _start_index, _stop_index, _conflicting_alts, _configs)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def report_context_sensitivity(_recognizer, _dfa, _start_index, _stop_index, _prediction, _configs)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module Antlr4::Runtime
|
|
2
|
+
class ANTLRErrorStrategy
|
|
3
|
+
def reset(_recognizer)
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
def recover_in_line(_recognizer)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def recover(_recognizer, _e)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def sync(_recognizer)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def in_error_recovery_mode(_recognizer)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def report_match(_recognizer)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def report_error(_recognizer, _e)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module Antlr4::Runtime
|
|
2
|
+
class ANTLRFileStream < ANTLRInputStream
|
|
3
|
+
def initialize(file_name, encoding)
|
|
4
|
+
@file_name = file_name
|
|
5
|
+
load(file_name, encoding)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def load(file_name, encoding)
|
|
9
|
+
data = Utils.read_file(file_name, encoding)
|
|
10
|
+
@n_items = data.length
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def source_name
|
|
14
|
+
@file_name
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
require 'antlr4/runtime/object_equality_comparator'
|
|
2
|
+
require 'antlr4/runtime/murmur_hash'
|
|
3
|
+
|
|
4
|
+
module Antlr4::Runtime
|
|
5
|
+
|
|
6
|
+
class Array2DHashSet
|
|
7
|
+
INITIAL_CAPACITY = 16 # must be power of 2
|
|
8
|
+
INITIAL_BUCKET_CAPACITY = 8
|
|
9
|
+
LOAD_FACTOR = 0.75
|
|
10
|
+
|
|
11
|
+
def initialize(comparator = nil, initial_capacity = INITIAL_CAPACITY, initial_bucket_capacity = INITIAL_BUCKET_CAPACITY)
|
|
12
|
+
comparator.nil? ? @comparator = ObjectEqualityComparator.instance : @comparator = comparator
|
|
13
|
+
|
|
14
|
+
@n_elements = 0
|
|
15
|
+
@initial_bucket_capacity = initial_bucket_capacity
|
|
16
|
+
@threshold = (initial_bucket_capacity * LOAD_FACTOR).floor # when to expand
|
|
17
|
+
@current_prime = 1 # jump by 4 primes each expand or whatever
|
|
18
|
+
@buckets = create_buckets(initial_capacity)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def get_or_add(o)
|
|
22
|
+
expand if @n_elements > @threshold
|
|
23
|
+
get_or_add_impl(o)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def get_or_add_impl(o)
|
|
27
|
+
b = get_bucket(o)
|
|
28
|
+
bucket = @buckets[b]
|
|
29
|
+
|
|
30
|
+
if bucket.nil?
|
|
31
|
+
bucket = create_bucket(@initial_bucket_capacity)
|
|
32
|
+
bucket[0] = o
|
|
33
|
+
@buckets[b] = bucket
|
|
34
|
+
@n_elements += 1
|
|
35
|
+
return o
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# LOOK FOR IT IN BUCKET
|
|
39
|
+
i = 0
|
|
40
|
+
while i < bucket.length
|
|
41
|
+
existing = bucket[i]
|
|
42
|
+
if existing.nil? # empty slot not there, add.
|
|
43
|
+
bucket[i] = o
|
|
44
|
+
@n_elements += 1
|
|
45
|
+
return o
|
|
46
|
+
end
|
|
47
|
+
if @comparator.equals(existing, o)
|
|
48
|
+
return existing # found existing, quit
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
i += 1
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# FULL BUCKET, add to end
|
|
55
|
+
@buckets[b] = bucket
|
|
56
|
+
bucket << o # add to end
|
|
57
|
+
@n_elements += 1
|
|
58
|
+
o
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def get(o)
|
|
62
|
+
return o if o.nil?
|
|
63
|
+
|
|
64
|
+
b = get_bucket(o)
|
|
65
|
+
bucket = @buckets[b]
|
|
66
|
+
if bucket.nil?
|
|
67
|
+
return nil # no bucket
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
i = 0
|
|
71
|
+
while i < bucket.length
|
|
72
|
+
e = bucket[i]
|
|
73
|
+
if e.nil?
|
|
74
|
+
return nil # empty slot not there
|
|
75
|
+
end
|
|
76
|
+
return e if @comparator.equals(e, o)
|
|
77
|
+
i += 1
|
|
78
|
+
end
|
|
79
|
+
nil
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def get_bucket(o)
|
|
83
|
+
hash = @comparator.hash(o)
|
|
84
|
+
hash & (@buckets.length - 1) # assumes len is power of 2
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def hash
|
|
88
|
+
objs = []
|
|
89
|
+
i = 0
|
|
90
|
+
while i < @buckets.length
|
|
91
|
+
bucket = @buckets[i]
|
|
92
|
+
if bucket.nil?
|
|
93
|
+
i += 1
|
|
94
|
+
next
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
j = 0
|
|
98
|
+
while j < bucket.length
|
|
99
|
+
o = bucket[j]
|
|
100
|
+
break if o.nil?
|
|
101
|
+
|
|
102
|
+
objs << o
|
|
103
|
+
j += 1
|
|
104
|
+
end
|
|
105
|
+
i += 1
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
hash_code = MurmurHash.hash_objs(objs)
|
|
109
|
+
|
|
110
|
+
if !@_hash.nil?
|
|
111
|
+
if hash_code == @_hash
|
|
112
|
+
puts 'Same hash_code for Array2DHashSet'
|
|
113
|
+
else
|
|
114
|
+
puts 'Different hash_code for Array2DHashSet'
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
@_hash = hash_code
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def equals(o)
|
|
121
|
+
return true if o == self
|
|
122
|
+
return false unless o.is_a? Array2DHashSet
|
|
123
|
+
|
|
124
|
+
other = o
|
|
125
|
+
return false if other.size != size
|
|
126
|
+
|
|
127
|
+
contains_all(other)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def add(t)
|
|
131
|
+
existing = get_or_add(t)
|
|
132
|
+
existing == t
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def size
|
|
136
|
+
@n_elements
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def empty?
|
|
140
|
+
@n_elements == 0
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def contains(o)
|
|
144
|
+
contains_fast(o)
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def contains_fast(obj)
|
|
148
|
+
return false if obj.nil?
|
|
149
|
+
|
|
150
|
+
!get(obj).nil?
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def iterator
|
|
154
|
+
a = to_a
|
|
155
|
+
a.sort(@comparator) unless @comparator.nil?
|
|
156
|
+
SetIterator.new(a, self)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def to_a
|
|
160
|
+
a = create_bucket(size)
|
|
161
|
+
i = 0
|
|
162
|
+
j = 0
|
|
163
|
+
while j < @buckets.length
|
|
164
|
+
bucket = @buckets[j]
|
|
165
|
+
if bucket.nil?
|
|
166
|
+
j += 1
|
|
167
|
+
next
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
k = 0
|
|
171
|
+
while k < bucket.length
|
|
172
|
+
o = bucket[k]
|
|
173
|
+
break if o.nil?
|
|
174
|
+
|
|
175
|
+
a[i] = o
|
|
176
|
+
i += 1
|
|
177
|
+
k += 1
|
|
178
|
+
end
|
|
179
|
+
j += 1
|
|
180
|
+
end
|
|
181
|
+
a
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def remove(o)
|
|
185
|
+
remove_fast(o)
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def remove_fast(obj)
|
|
189
|
+
return false if obj.nil?
|
|
190
|
+
|
|
191
|
+
b = get_bucket(obj)
|
|
192
|
+
bucket = @buckets[b]
|
|
193
|
+
if bucket.nil?
|
|
194
|
+
# no bucket
|
|
195
|
+
return false
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
i = 0
|
|
199
|
+
while i < bucket.length
|
|
200
|
+
e = bucket[i]
|
|
201
|
+
if e.nil?
|
|
202
|
+
# empty slot not there
|
|
203
|
+
return false
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
if @comparator.eql?(e, obj) # found it
|
|
207
|
+
bucket[i] = nil
|
|
208
|
+
return true
|
|
209
|
+
end
|
|
210
|
+
i += 1
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
false
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def contains_all(collection)
|
|
217
|
+
if collection.is_a? Array2DHashSet
|
|
218
|
+
s = collection
|
|
219
|
+
i = 0
|
|
220
|
+
while i < s.buckets.length
|
|
221
|
+
bucket = s.buckets[i]
|
|
222
|
+
if bucket.nil?
|
|
223
|
+
i += 1
|
|
224
|
+
next
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
j = 0
|
|
228
|
+
while j < bucket.length
|
|
229
|
+
o = bucket[j]
|
|
230
|
+
break if o.nil?
|
|
231
|
+
return false unless contains_fast(o)
|
|
232
|
+
j += 1
|
|
233
|
+
end
|
|
234
|
+
i += 1
|
|
235
|
+
end
|
|
236
|
+
else
|
|
237
|
+
i = 0
|
|
238
|
+
while i < collection.length
|
|
239
|
+
o = collection[i]
|
|
240
|
+
return false unless contains_fast(o)
|
|
241
|
+
i += 1
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
true
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def add_all(c)
|
|
248
|
+
changed = false
|
|
249
|
+
i = 0
|
|
250
|
+
while i < c.length
|
|
251
|
+
o = c[i]
|
|
252
|
+
existing = get_or_add(o)
|
|
253
|
+
changed = true if existing != o
|
|
254
|
+
i += 1
|
|
255
|
+
end
|
|
256
|
+
changed
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def retain_all(c)
|
|
260
|
+
newsize = 0
|
|
261
|
+
k = 0
|
|
262
|
+
while k < @buckets.length
|
|
263
|
+
bucket = @buckets[k]
|
|
264
|
+
if bucket.nil?
|
|
265
|
+
k += 1
|
|
266
|
+
next
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
i = 0
|
|
270
|
+
j = 0
|
|
271
|
+
while i < bucket.length
|
|
272
|
+
break if bucket[i].nil?
|
|
273
|
+
|
|
274
|
+
if c.contains(bucket[i])
|
|
275
|
+
# keep
|
|
276
|
+
bucket[j] = bucket[i] if i != j
|
|
277
|
+
|
|
278
|
+
j += 1
|
|
279
|
+
newsize += 1
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
i += 1
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
newsize += j
|
|
286
|
+
|
|
287
|
+
while j < i
|
|
288
|
+
bucket[j] = nil
|
|
289
|
+
j += 1
|
|
290
|
+
end
|
|
291
|
+
k += 1
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
changed = newsize != @n_elements
|
|
295
|
+
@n_elements = newsize
|
|
296
|
+
changed
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
def remove_all(c)
|
|
300
|
+
changed = false
|
|
301
|
+
i = 0
|
|
302
|
+
while i < c.length
|
|
303
|
+
o = c[i]
|
|
304
|
+
changed |= remove_fast(o)
|
|
305
|
+
i += 1
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
changed
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
def clear
|
|
312
|
+
@buckets = create_buckets(INITIAL_CAPACITY)
|
|
313
|
+
@n_elements = 0
|
|
314
|
+
@threshold = (INITIAL_CAPACITY * LOAD_FACTOR).floor
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
def to_s
|
|
318
|
+
return '{}' if size == 0
|
|
319
|
+
|
|
320
|
+
buf = ''
|
|
321
|
+
buf << '{'
|
|
322
|
+
first = true
|
|
323
|
+
i = 0
|
|
324
|
+
while i < @buckets.length
|
|
325
|
+
bucket = @buckets[i]
|
|
326
|
+
if bucket.nil?
|
|
327
|
+
i += 1
|
|
328
|
+
next
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
j = 0
|
|
332
|
+
while j < bucket.length
|
|
333
|
+
o = bucket[j]
|
|
334
|
+
break if o.nil?
|
|
335
|
+
|
|
336
|
+
if first
|
|
337
|
+
first = false
|
|
338
|
+
else
|
|
339
|
+
buf << ', '
|
|
340
|
+
buf << o.to_s
|
|
341
|
+
end
|
|
342
|
+
j += 1
|
|
343
|
+
end
|
|
344
|
+
i += 1
|
|
345
|
+
end
|
|
346
|
+
buf << '}'
|
|
347
|
+
buf
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
def to_table_string
|
|
351
|
+
buf = ''
|
|
352
|
+
i = 0
|
|
353
|
+
while i < @buckets.length
|
|
354
|
+
bucket = @buckets[i]
|
|
355
|
+
if bucket.nil?
|
|
356
|
+
buf << "null\n"
|
|
357
|
+
else
|
|
358
|
+
buf << '['
|
|
359
|
+
first = true
|
|
360
|
+
j = 0
|
|
361
|
+
while j < bucket.length
|
|
362
|
+
o = bucket[j]
|
|
363
|
+
if first
|
|
364
|
+
first = false
|
|
365
|
+
else
|
|
366
|
+
buf << ' '
|
|
367
|
+
end
|
|
368
|
+
buf << if o.nil?
|
|
369
|
+
'_'
|
|
370
|
+
else
|
|
371
|
+
o.to_s
|
|
372
|
+
end
|
|
373
|
+
j += 1
|
|
374
|
+
end
|
|
375
|
+
buf << "]\n"
|
|
376
|
+
end
|
|
377
|
+
i += 1
|
|
378
|
+
end
|
|
379
|
+
buf
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
def create_buckets(capacity)
|
|
383
|
+
Array.new(capacity)
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
def create_bucket(capacity)
|
|
387
|
+
Array.new(capacity)
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
class SetIterator
|
|
391
|
+
def initialize(data, parent)
|
|
392
|
+
@data = data
|
|
393
|
+
@parent = parent
|
|
394
|
+
@next_index = 0
|
|
395
|
+
@removed = true
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
def has_next
|
|
399
|
+
@next_index < @data.length
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
def next
|
|
403
|
+
raise StandardError unless has_next
|
|
404
|
+
|
|
405
|
+
@removed = false
|
|
406
|
+
result = @data[@next_index]
|
|
407
|
+
@next_index += 1
|
|
408
|
+
result
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
def remove
|
|
412
|
+
raise IllegalStateException if @removed
|
|
413
|
+
|
|
414
|
+
parent.remove(@data[@next_index - 1])
|
|
415
|
+
@removed = true
|
|
416
|
+
end
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
def expand
|
|
420
|
+
old = @buckets
|
|
421
|
+
@current_prime += 4
|
|
422
|
+
new_capacity = @buckets.length * 2
|
|
423
|
+
new_table = create_buckets(new_capacity)
|
|
424
|
+
new_bucket_lengths = Array.new(new_table.length, 0)
|
|
425
|
+
@buckets = new_table
|
|
426
|
+
@threshold = (new_capacity * LOAD_FACTOR).floor
|
|
427
|
+
|
|
428
|
+
old_size = size
|
|
429
|
+
j = 0
|
|
430
|
+
while j < old.length
|
|
431
|
+
bucket = old[j]
|
|
432
|
+
if bucket.nil?
|
|
433
|
+
j += 1
|
|
434
|
+
next
|
|
435
|
+
end
|
|
436
|
+
|
|
437
|
+
k = 0
|
|
438
|
+
while k < bucket.length
|
|
439
|
+
o = bucket[k]
|
|
440
|
+
break if o.nil?
|
|
441
|
+
|
|
442
|
+
b = get_bucket(o)
|
|
443
|
+
bucket_length = new_bucket_lengths[b]
|
|
444
|
+
if bucket_length == 0
|
|
445
|
+
new_bucket = create_bucket(@initial_bucket_capacity)
|
|
446
|
+
new_table[b] = new_bucket
|
|
447
|
+
else
|
|
448
|
+
new_bucket = new_table[b]
|
|
449
|
+
if bucket_length == new_bucket.length
|
|
450
|
+
tmp = Array.new(new_bucket.length * 2)
|
|
451
|
+
i = 0
|
|
452
|
+
while i < new_bucket.length
|
|
453
|
+
tmp[i] = new_bucket[i]
|
|
454
|
+
i += 1
|
|
455
|
+
end
|
|
456
|
+
new_bucket = tmp
|
|
457
|
+
new_table[b] = new_bucket
|
|
458
|
+
end
|
|
459
|
+
end
|
|
460
|
+
|
|
461
|
+
new_bucket[bucket_length] = o
|
|
462
|
+
new_bucket_lengths[b] += 1
|
|
463
|
+
k += 1
|
|
464
|
+
end
|
|
465
|
+
j += 1
|
|
466
|
+
end
|
|
467
|
+
|
|
468
|
+
raise StandardError, '@nElements != oldSize' if @n_elements != old_size
|
|
469
|
+
end
|
|
470
|
+
end
|
|
471
|
+
end
|