Class: Antlr4::Runtime::Array2DHashSet

Inherits:
Object
  • Object
show all
Defined in:
lib/antlr4/runtime/array_2d_hash_set.rb

Defined Under Namespace

Classes: SetIterator

Constant Summary collapse

INITIAL_CAPACITY =

must be power of 2

16
INITIAL_BUCKET_CAPACITY =
8
LOAD_FACTOR =
0.75

Instance Method Summary collapse

Constructor Details

#initialize(comparator = nil, initial_capacity = INITIAL_CAPACITY, initial_bucket_capacity = INITIAL_BUCKET_CAPACITY) ⇒ Array2DHashSet

Returns a new instance of Array2DHashSet


8
9
10
11
12
13
14
15
16
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 8

def initialize(comparator = nil, initial_capacity = INITIAL_CAPACITY, initial_bucket_capacity = INITIAL_BUCKET_CAPACITY)
  comparator.nil? ? @comparator = ObjectEqualityComparator.instance : @comparator = comparator

  @n_elements = 0
  @initial_bucket_capacity = initial_bucket_capacity
  @threshold = (initial_bucket_capacity * LOAD_FACTOR).floor # when to expand
  @current_prime = 1 # jump by 4 primes each expand or whatever
  @buckets = create_buckets(initial_capacity)
end

Instance Method Details

#add(t) ⇒ Object


127
128
129
130
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 127

def add(t)
  existing = get_or_add(t)
  existing == t
end

#add_all(c) ⇒ Object


244
245
246
247
248
249
250
251
252
253
254
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 244

def add_all(c)
  changed = false
  i = 0
  while i < c.length
    o = c[i]
    existing = get_or_add(o)
    changed = true if existing != o
    i += 1
  end
  changed
end

#clearObject


308
309
310
311
312
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 308

def clear
  @buckets = create_buckets(INITIAL_CAPACITY)
  @n_elements = 0
  @threshold = (INITIAL_CAPACITY * LOAD_FACTOR).floor
end

#contains(o) ⇒ Object


140
141
142
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 140

def contains(o)
  contains_fast(o)
end

#contains_all(collection) ⇒ Object


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 213

def contains_all(collection)
  if collection.is_a? Array2DHashSet
    s = collection
    i = 0
    while i < s.buckets.length
      bucket = s.buckets[i]
      if bucket.nil?
        i += 1
        next
      end

      j = 0
      while j < bucket.length
        o = bucket[j]
        break if o.nil?
        return false unless contains_fast(o)
        j += 1
      end
      i += 1
    end
  else
    i = 0
    while i < collection.length
      o = collection[i]
      return false unless contains_fast(o)
      i += 1
    end
  end
  true
end

#contains_fast(obj) ⇒ Object


144
145
146
147
148
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 144

def contains_fast(obj)
  return false if obj.nil?

  !get(obj).nil?
end

#create_bucket(capacity) ⇒ Object


383
384
385
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 383

def create_bucket(capacity)
  Array.new(capacity)
end

#create_buckets(capacity) ⇒ Object


379
380
381
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 379

def create_buckets(capacity)
  Array.new(capacity)
end

#empty?Boolean

Returns:

  • (Boolean)

136
137
138
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 136

def empty?
  @n_elements == 0
end

#equals(o) ⇒ Object


117
118
119
120
121
122
123
124
125
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 117

def equals(o)
  return true if o == self
  return false unless o.is_a? Array2DHashSet

  other = o
  return false if other.size != size

  contains_all(other)
end

#expandObject

Raises:

  • (StandardError)

416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 416

def expand
  old = @buckets
  @current_prime += 4
  new_capacity = @buckets.length * 2
  new_table = create_buckets(new_capacity)
  new_bucket_lengths = Array.new(new_table.length, 0)
  @buckets = new_table
  @threshold = (new_capacity * LOAD_FACTOR).floor

  old_size = size
  j = 0
  while j < old.length
    bucket = old[j]
    if bucket.nil?
      j += 1
      next
    end

    k = 0
    while k < bucket.length
      o = bucket[k]
      break if o.nil?

      b = get_bucket(o)
      bucket_length = new_bucket_lengths[b]
      if bucket_length == 0
        new_bucket = create_bucket(@initial_bucket_capacity)
        new_table[b] = new_bucket
      else
        new_bucket = new_table[b]
        if bucket_length == new_bucket.length
          tmp = Array.new(new_bucket.length * 2)
          i = 0
          while i < new_bucket.length
            tmp[i] = new_bucket[i]
            i += 1
          end
          new_bucket = tmp
          new_table[b] = new_bucket
        end
      end

      new_bucket[bucket_length] = o
      new_bucket_lengths[b] += 1
      k += 1
    end
    j += 1
  end

  raise StandardError, '@nElements != oldSize' if @n_elements != old_size
end

#get(o) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 58

def get(o)
  return o if o.nil?

  b = get_bucket(o)
  bucket = @buckets[b]
  if bucket.nil?
    return nil # no bucket
  end

  i = 0
  while i < bucket.length
    e = bucket[i]
    if e.nil?
      return nil # empty slot not there
    end
    return e if @comparator.equals(e, o)
    i += 1
  end
  nil
end

#get_bucket(o) ⇒ Object


79
80
81
82
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 79

def get_bucket(o)
  hash = @comparator.hash(o)
  hash & (@buckets.length - 1) # assumes len is power of 2
end

#get_or_add(o) ⇒ Object


18
19
20
21
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 18

def get_or_add(o)
  expand if @n_elements > @threshold
  get_or_add_impl(o)
end

#get_or_add_impl(o) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 23

def get_or_add_impl(o)
  b = get_bucket(o)
  bucket = @buckets[b]

  if bucket.nil?
    bucket = create_bucket(@initial_bucket_capacity)
    bucket[0] = o
    @buckets[b] = bucket
    @n_elements += 1
    return o
  end

  # LOOK FOR IT IN BUCKET
  i = 0
  while i < bucket.length
    existing = bucket[i]
    if existing.nil? # empty slot not there, add.
      bucket[i] = o
      @n_elements += 1
      return o
    end
    if @comparator.equals(existing, o)
      return existing # found existing, quit
    end

    i += 1
  end

  # FULL BUCKET, add to end
  @buckets[b] = bucket
  bucket << o # add to end
  @n_elements += 1
  o
end

#hashObject


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 84

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
      o = bucket[j]
      break if o.nil?

      objs << o
      j += 1
    end
    i += 1
  end

  hash_code = MurmurHash.hash_objs(objs)

  if !@_hash.nil?
    if hash_code == @_hash
      puts 'Same hash_code for Array2DHashSet'
    else
      puts 'Different hash_code for Array2DHashSet'
    end
  end
  @_hash = hash_code
end

#iteratorObject


150
151
152
153
154
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 150

def iterator
  a = to_a
  a.sort(@comparator) unless @comparator.nil?
  SetIterator.new(a, self)
end

#remove(o) ⇒ Object


181
182
183
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 181

def remove(o)
  remove_fast(o)
end

#remove_all(c) ⇒ Object


296
297
298
299
300
301
302
303
304
305
306
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 296

def remove_all(c)
  changed = false
  i = 0
  while i < c.length
    o = c[i]
    changed |= remove_fast(o)
    i += 1
  end

  changed
end

#remove_fast(obj) ⇒ Object


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 185

def remove_fast(obj)
  return false if obj.nil?

  b = get_bucket(obj)
  bucket = @buckets[b]
  if bucket.nil?
    # no bucket
    return false
  end

  i = 0
  while i < bucket.length
    e = bucket[i]
    if e.nil?
      # empty slot not there
      return false
    end

    if @comparator.eql?(e, obj) # found it
      bucket[i] = nil
      return true
    end
    i += 1
  end

  false
end

#retain_all(c) ⇒ Object


256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 256

def retain_all(c)
  newsize = 0
  k = 0
  while k < @buckets.length
    bucket = @buckets[k]
    if bucket.nil?
      k += 1
      next
    end

    i = 0
    j = 0
    while i < bucket.length
      break if bucket[i].nil?

      if c.contains(bucket[i])
        # keep
        bucket[j] = bucket[i] if i != j

        j += 1
        newsize += 1
      end

      i += 1
    end

    newsize += j

    while j < i
      bucket[j] = nil
      j += 1
    end
    k += 1
  end

  changed = newsize != @n_elements
  @n_elements = newsize
  changed
end

#sizeObject


132
133
134
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 132

def size
  @n_elements
end

#to_aObject


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 156

def to_a
  a = create_bucket(size)
  i = 0
  j = 0
  while j < @buckets.length
    bucket = @buckets[j]
    if bucket.nil?
      j += 1
      next
    end

    k = 0
    while k < bucket.length
      o = bucket[k]
      break if o.nil?

      a[i] = o
      i += 1
      k += 1
    end
    j += 1
  end
  a
end

#to_sObject


314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 314

def to_s
  return '{}' if size == 0

  buf = ''
  buf << '{'
  first = true
  i = 0
  while i < @buckets.length
    bucket = @buckets[i]
    if bucket.nil?
      i += 1
      next
    end

    j = 0
    while j < bucket.length
      o = bucket[j]
      break if o.nil?

      if first
        first = false
      else
        buf << ', '
        buf << o.to_s
      end
      j += 1
    end
    i += 1
  end
  buf << '}'
  buf
end

#to_table_stringObject


347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/antlr4/runtime/array_2d_hash_set.rb', line 347

def to_table_string
  buf = ''
  i = 0
  while i < @buckets.length
    bucket = @buckets[i]
    if bucket.nil?
      buf << "null\n"
    else
      buf << '['
      first = true
      j = 0
      while j < bucket.length
        o = bucket[j]
        if first
          first = false
        else
          buf << ' '
        end
        buf << if o.nil?
                 '_'
               else
                 o.to_s
               end
        j += 1
      end
      buf << "]\n"
    end
    i += 1
  end
  buf
end