Class: Antlr4::Runtime::FlexibleHashMap
- Inherits:
-
Object
- Object
- Antlr4::Runtime::FlexibleHashMap
show all
- Defined in:
- lib/antlr4/runtime/flexible_hash_map.rb
Defined Under Namespace
Classes: Entry
Constant Summary
collapse
- INITIAL_CAPACITY =
16
- INITIAL_BUCKET_CAPACITY =
8
- LOAD_FACTOR =
0.75
Instance Method Summary
collapse
Constructor Details
#initialize(comparator = nil, initial_capacity = nil, initial_bucket_capacity = nil) ⇒ FlexibleHashMap
Returns a new instance of FlexibleHashMap.
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/antlr4/runtime/flexible_hash_map.rb', line 21
def initialize(comparator = nil, initial_capacity = nil, initial_bucket_capacity = nil)
comparator = ObjectEqualityComparator.instance if comparator.nil?
initial_capacity = INITIAL_CAPACITY if initial_capacity.nil?
initial_bucket_capacity = INITIAL_BUCKET_CAPACITY if initial_bucket_capacity.nil?
@n_items = 0
@threshold = initial_capacity * LOAD_FACTOR @current_prime = 1 @initial_bucket_capacity = initial_bucket_capacity
@comparator = comparator
@buckets = create_entry_list_array(initial_bucket_capacity)
end
|
Instance Method Details
#bucket(key) ⇒ Object
39
40
41
42
|
# File 'lib/antlr4/runtime/flexible_hash_map.rb', line 39
def bucket(key)
hash = @comparator.hash(key)
hash & (@buckets.length - 1) end
|
#clear ⇒ Object
185
186
187
188
|
# File 'lib/antlr4/runtime/flexible_hash_map.rb', line 185
def clear
@buckets = create_entry_list_array(INITIAL_CAPACITY)
@n_items = 0
end
|
#contains_key(key) ⇒ Object
110
111
112
|
# File 'lib/antlr4/runtime/flexible_hash_map.rb', line 110
def contains_key(key)
!get(key).nil?
end
|
#create_entry_list_array(length) ⇒ Object
35
36
37
|
# File 'lib/antlr4/runtime/flexible_hash_map.rb', line 35
def create_entry_list_array(length)
Array.new(length)
end
|
#empty? ⇒ Boolean
181
182
183
|
# File 'lib/antlr4/runtime/flexible_hash_map.rb', line 181
def empty?
@n_items == 0
end
|
#expand ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/antlr4/runtime/flexible_hash_map.rb', line 147
def expand
old = @buckets
@current_prime += 4
new_capacity = @buckets.length * 2
new_table = create_entry_list_array(new_capacity)
@buckets = new_table
@threshold = new_capacity * LOAD_FACTOR
old_size = size
i = 0
while i < old.length
bucket = old[i]
if bucket.nil?
i += 1
next
end
j = 0
while j < bucket.length
e = bucket[j]
break if e.nil?
put(e.key, e.value)
j += 1
end
i += 1
end
@n_items = old_size
end
|
#get(key) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/antlr4/runtime/flexible_hash_map.rb', line 44
def get(key)
typed_key = key
return nil if key.nil?
b = bucket(typed_key)
bucket = @buckets[b]
if bucket.nil?
return nil end
i = 0
while i < bucket.length
e = bucket[i]
return e.value if @comparator.equals(e.key, typed_key)
i += 1
end
nil
end
|
#hash ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/antlr4/runtime/flexible_hash_map.rb', line 114
def hash
objs = []
i = 0
while i < @buckets.length
bucket = @buckets[i]
if bucket.nil?
i += 1
next
end
j = 0
while j < bucket.length
e = bucket[j]
break if e.nil?
objs << e.key
j += 1
end
i += 1
end
hash_code = RumourHash.calculate(objs)
unless @_hash.nil?
if hash_code == @_hash
puts 'Same hash_code for FlexibleHashMap'
else
puts 'Different hash_code for FlexibleHashMap'
end
end
@_hash = hash_code
end
|
#put(key, value) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/antlr4/runtime/flexible_hash_map.rb', line 63
def put(key, value)
return nil if key.nil?
expand if @n_items > @threshold
b = bucket(key)
bucket = @buckets[b]
bucket = @buckets[b] = [] if bucket.nil?
i = 0
while i < bucket.length
e = bucket[i]
unless @comparator.equals(e.key, key)
i += 1
next
end
prev = e.value
e.value = value
@n_items += 1
return prev
end
bucket << Entry.new(key, value)
@n_items += 1
nil
end
|
#size ⇒ Object
177
178
179
|
# File 'lib/antlr4/runtime/flexible_hash_map.rb', line 177
def size
@n_items
end
|
#to_s ⇒ Object
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
# File 'lib/antlr4/runtime/flexible_hash_map.rb', line 190
def to_s
return 'end' if size == 0
buf = ''
buf << ''
first = true
@buckets.each do |bucket|
next if bucket.nil?
bucket.each do |e|
break if e.nil?
if first
first = false
else
buf << ', '
end
buf << e.to_s
end
end
buf << 'end'
buf.to_s
end
|
#to_table_string ⇒ Object
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
# File 'lib/antlr4/runtime/flexible_hash_map.rb', line 214
def to_table_string
buf = ''
@buckets.each do |bucket|
if bucket.nil?
buf << "nil\n"
next
end
buf << '['
first = true
bucket.each do |e|
first ? first = false : buf << ' '
buf << (e.nil? ? '_' : e.to_s)
end
buf << "]\n"
end
buf.to_s
end
|
#values ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/antlr4/runtime/flexible_hash_map.rb', line 89
def values
a = []
i = 0
while i < @buckets.length
bucket = @buckets[i]
if bucket.nil?
i += 1
next
end
j = 0
while j < bucket.length
e = bucket[j]
a << e.value
j += 1
end
i += 1
end
a
end
|