Class: Antlr4::Runtime::IntervalSet

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

Constant Summary collapse

@@complete_char_set =
IntervalSet.of(Lexer::MIN_CHAR_VALUE, Lexer::MAX_CHAR_VALUE)
@@empty_set =
IntervalSet.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a = nil, b = nil) ⇒ IntervalSet

Returns a new instance of IntervalSet



8
9
10
11
12
# File 'lib/antlr4/runtime/interval_set.rb', line 8

def initialize(a = nil, b = nil)
  @readonly = false
  @intervals = []
  add(a, b) unless a.nil?
end

Instance Attribute Details

#intervalsObject

Returns the value of attribute intervals



6
7
8
# File 'lib/antlr4/runtime/interval_set.rb', line 6

def intervals
  @intervals
end

Class Method Details

.of(a, b = nil) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/antlr4/runtime/interval_set.rb', line 14

def self.of(a, b = nil)
  if b.nil?
    IntervalSet.new(a)
  else
    IntervalSet.new(a, b)
  end
end

Instance Method Details

#==(obj) ⇒ Object



328
329
330
331
332
333
# File 'lib/antlr4/runtime/interval_set.rb', line 328

def ==(obj)
  return false if obj.nil? || !(obj.is_a? IntervalSet)

  other = obj
  @intervals == other.intervals
end

#add(el1, el2 = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/antlr4/runtime/interval_set.rb', line 28

def add(el1, el2 = nil)
  raise IllegalStateException, "can't alter readonly IntervalSet" if @readonly

  if el1.is_a? Interval
    add_interval(el1)
  elsif el2.nil?
    add_interval(Interval.of(el1, el1))
  else
    add_interval(Interval.of(el1, el2))
  end
end

#add_all(set) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/antlr4/runtime/interval_set.rb', line 89

def add_all(set)
  return this if set.nil?

  if set.is_a? IntervalSet
    other = set

    n = other.intervals.length
    i = 0
    while i < n
      interval = other.intervals[i]
      add(interval.a, interval.b)
      i += 1
    end
  else
    i = 0
    list = set.to_list
    while i < list.length
      add(list[i])
      i += 1
    end
  end

  self
end

#add_interval(addition) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/antlr4/runtime/interval_set.rb', line 40

def add_interval(addition)
  raise IllegalStateException, "can't alter readonly IntervalSet" if @readonly

  return if addition.b < addition.a

  # Find where to insert the Interval and remember where it went

  i = 0 # An index into @intervals
  while i < @intervals.length
    r = @intervals[i]
    return if addition == r

    if addition.adjacent(r) || !addition.disjoint(r)
      bigger = addition.union(r)
      @intervals[i] = bigger

      while i < (@intervals.length - 1)
        i += 1
        next_interval = @intervals[i]
        if !bigger.adjacent(next_interval) && bigger.disjoint(next_interval)
          break
        end

        @intervals.delete_at i
        i -= 1
        @intervals[i] = bigger.union(next_interval)
      end
      return
    end

    if addition.starts_before_disjoint r
      @intervals.insert(i, addition)
      return
    end
    i += 1
  end
  @intervals << addition
end

#and(other) ⇒ Object



211
212
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/antlr4/runtime/interval_set.rb', line 211

def and(other)
  if other.nil? # || !(other.is_a? IntervalSet) )
    return nil # nothing in common with nil set
  end

  my_intervals = @intervals
  their_intervals = other.intervals
  intersection = nil
  my_size = my_intervals.length
  their_size = their_intervals.length
  i = 0
  j = 0

  while i < my_size && j < their_size
    mine = my_intervals[i]
    theirs = their_intervals[j]
    # System.out.println("mine="+mine+" and theirs="+theirs)
    if mine.starts_before_disjoint(theirs)
      # move this iterator looking for interval that might overlap
      i += 1
    elsif theirs.starts_before_disjoint(mine)
      # move other iterator looking for interval that might overlap
      j += 1
    elsif mine.properlyContains(theirs)
      # overlap, add intersection, get next theirs
      intersection = IntervalSet.new if intersection.nil?
      intersection.add(mine.intersection(theirs))
      j += 1
    elsif theirs.properlyContains(mine)
      # overlap, add intersection, get next mine
      intersection = IntervalSet.new if intersection.nil?
      intersection.add(mine.intersection(theirs))
      i += 1
    elsif !mine.disjoint(theirs)
      # overlap, add intersection
      intersection = IntervalSet.new if intersection.nil?
      intersection.add(mine.intersection(theirs))
      # Move the iterator of lower range [a..b], but not
      # the upper range as it may contain elements that will collide
      # with the next iterator. So, if mine=[0..115] and
      # theirs=[115..200], then intersection is 115 and move mine
      # but not theirs as theirs may collide with the next range
      # in thisIter.
      # move both iterators to next ranges
      if mine.startsAfterNonDisjoint(theirs)
        j += 1
      elsif theirs.startsAfterNonDisjoint(mine)
        i += 1
      end
    end
  end
  return IntervalSet.new if intersection.nil?

  intersection
end

#clearObject



22
23
24
25
26
# File 'lib/antlr4/runtime/interval_set.rb', line 22

def clear
  raise IllegalStateException, "can't alter readonly IntervalSet" if @readonly

  @intervals.clear
end

#complement(min_element, max_element) ⇒ Object



114
115
116
# File 'lib/antlr4/runtime/interval_set.rb', line 114

def complement(min_element, max_element)
  complement_interval_set(IntervalSet.of(min_element, max_element))
end

#complement_interval_set(vocabulary) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/antlr4/runtime/interval_set.rb', line 118

def complement_interval_set(vocabulary)
  if vocabulary.nil? || vocabulary.is_nil
    return nil # nothing in common with nil set
  end

  vocabularyIS = nil
  if vocabulary.is_a? IntervalSet
    vocabularyIS = vocabulary
  else
    vocabularyIS = IntervalSet.new
    vocabularyIS.add_all(vocabulary)
  end

  vocabularyIS.subtract(self)
end

#contains(el) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/antlr4/runtime/interval_set.rb', line 267

def contains(el)
  n = @intervals.length
  l = 0
  r = n - 1
  # Binary search for the element in the (sorted,
  # disjoint) array of @intervals.
  while l <= r
    m = (l + r) / 2
    interval = @intervals[m]
    a = interval.a
    b = interval.b
    if b < el
      l = m + 1
    elsif a > el
      r = m - 1
    else # el >= a && el <= b
      return true
    end
  end
  false
end

#element_name_in_vocabulary(vocabulary, a) ⇒ Object



399
400
401
402
403
404
405
406
407
# File 'lib/antlr4/runtime/interval_set.rb', line 399

def element_name_in_vocabulary(vocabulary, a)
  if a == Token::EOF
    '<EOF>'
  elsif a == Token::EPSILON
    '<EPSILON>'
  else
    vocabulary.display_name(a)
  end
end

#get(i) ⇒ Object



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/antlr4/runtime/interval_set.rb', line 464

def get(i)
  n = @intervals.length
  index = 0
  j = 0
  while j < n
    interval = @intervals[j]
    a = interval.a
    b = interval.b
    v = a
    while v <= b
      return v if index == i

      index += 1
      v += 1
    end
    j += 1
  end
  -1
end

#hashObject



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/antlr4/runtime/interval_set.rb', line 306

def hash
  ints = []
  i = 0
  while i < @intervals.length
    interval = @intervals[i]
    ints <<  interval.a
    ints <<  interval.b
    i += 1
  end

  hash_code = MurmurHash.hash_ints(ints)

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

#is_nilObject



289
290
291
# File 'lib/antlr4/runtime/interval_set.rb', line 289

def is_nil
  @intervals.nil? || @intervals.empty?
end

#max_elementObject

Raises:

  • (StandardEror)


293
294
295
296
297
298
# File 'lib/antlr4/runtime/interval_set.rb', line 293

def max_element
  raise StandardEror, 'set is empty' if is_nil

  last = @intervals[-1]
  last.b
end

#min_elementObject

Raises:

  • (StandardEror)


300
301
302
303
304
# File 'lib/antlr4/runtime/interval_set.rb', line 300

def min_element
  raise StandardEror, 'set is empty' if is_nil

  @intervals[0].a
end

#or_list(a) ⇒ Object



204
205
206
207
208
209
# File 'lib/antlr4/runtime/interval_set.rb', line 204

def or_list(a)
  o = IntervalSet.new
  o.add_all(self)
  o.add_all(a)
  o
end

#or_sets(sets) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/antlr4/runtime/interval_set.rb', line 79

def or_sets(sets)
  r = IntervalSet.new
  i = 0
  while i < sets.length
    r.add_all(sets[i])
    i += 1
  end
  r
end

#readonly(readonly) ⇒ Object



526
527
528
529
530
531
532
# File 'lib/antlr4/runtime/interval_set.rb', line 526

def readonly(readonly)
  if @readonly && !readonly
    raise IllegalStateException, "can't alter readonly IntervalSet"
  end

  @readonly = readonly
end

#remove(el) ⇒ Object



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'lib/antlr4/runtime/interval_set.rb', line 488

def remove(el)
  raise IllegalStateException, "can't alter readonly IntervalSet" if @readonly

  n = @intervals.length
  i = 0
  while i < n
    interval = @intervals[i]
    a = interval.a
    b = interval.b
    if el < a
      break # list is sorted and el is before this interval not here
    end

    # if whole interval x..x, rm
    if el == a && el == b
      @intervals.delete_at(i)
      break
    end
    # if on left edge x..b, adjust left
    if el == a
      interval.a += 1
      break
    end
    # if on right edge a..x, adjust right
    if el == b
      interval.b -= 1
      break
    end
    # if in middle a..x..b, split interval
    if el > a && el < b # found in this interval
      int oldb = interval.b
      interval.b = el - 1 # [a..x-1]
      add(el + 1, oldb) # add [x+1..b]
    end
    i += 1
  end
end

#sizeObject



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/antlr4/runtime/interval_set.rb', line 409

def size
  n = 0
  num_intervals = @intervals.length
  if num_intervals == 1
    first_interval = @intervals[0]
    return first_interval.b - first_interval.a + 1
  end
  i = 0
  while i < num_intervals
    interval = @intervals[i]
    n += (interval.b - interval.a + 1)
    i += 1
  end
  n
end

#subtract(a) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/antlr4/runtime/interval_set.rb', line 134

def subtract(a)
  return IntervalSet.new self if a.nil? || a.is_nil

  return subtract_interval_sets(self, a) if a.is_a? IntervalSet

  other = IntervalSet.new
  other.add_all(a)
  subtract_interval_sets(self, other)
end

#subtract_interval_sets(left, right) ⇒ Object



144
145
146
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/antlr4/runtime/interval_set.rb', line 144

def subtract_interval_sets(left, right)
  return new IntervalSet if left.nil? || left.is_nil

  result = IntervalSet.new(left)
  return result if right.nil? || right.is_nil

  result_i = 0
  right_i = 0
  while result_i < result.intervals.length && right_i < right.intervals.length
    result_interval = result.intervals[result_i]
    right_interval = right.intervals[right_i]

    if right_interval.b < result_interval.a
      right_i += 1
      next
    end

    if right_interval.a > result_interval.b
      result_i += 1
      next
    end

    before_current = nil
    after_current = nil
    if right_interval.a > result_interval.a
      before_current = Interval.new(result_interval.a, right_interval.a - 1)
    end

    if right_interval.b < result_interval.b
      after_current = Interval.new(right_interval.b + 1, result_interval.b)
    end

    if !before_current.nil?
      if !after_current.nil?
        # split the current interval into two
        result.intervals.set(result_i, before_current)
        result.intervals.add(result_i + 1, after_current)
        result_i += 1
        right_i += 1
        next
      else # replace the current interval
        result.intervals.set(result_i, before_current)
        result_i += 1
        next
      end
    else
      if !after_current.nil?
        result.intervals.set(result_i, after_current)
        right_i += 1
        next
      else
        result.intervals.remove(result_i)
        next
      end
    end
  end

  result
end

#to_aObject



484
485
486
# File 'lib/antlr4/runtime/interval_set.rb', line 484

def to_a
  to_integer_list
end

#to_integer_listObject



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/antlr4/runtime/interval_set.rb', line 425

def to_integer_list
  values = IntegerList.new
  n = @intervals.length
  i = 0
  while i < n
    interval = @intervals[i]
    a = interval.a
    b = interval.b
    v = a
    while v <= b
      values.add(v)
      v += 1
    end
    i += 1
  end
  values
end

#to_listObject



443
444
445
# File 'lib/antlr4/runtime/interval_set.rb', line 443

def to_list
  to_integer_list
end

#to_setObject



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/antlr4/runtime/interval_set.rb', line 447

def to_set
  s = Set.new
  k = 0
  while k < @intervals.length
    i = @intervals[k]
    a = i.a
    b = i.b
    v = a
    while v <= b
      s.add(v)
      v += 1
    end
    k += 1
  end
  s
end

#to_string(elem_are_char = false) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/antlr4/runtime/interval_set.rb', line 335

def to_string(elem_are_char = false)
  buf = ''
  return 'end' if @intervals.nil? || @intervals.empty?

  buf << '' if size > 1

  i = 0
  while i < @intervals.length
    interval = @intervals[i]
    a = interval.a
    b = interval.b
    if a == b
      if a == Token::EOF
        buf << '<EOF>'
      elsif elem_are_char
        buf << "'" << a << "'"
      else
        buf << a
      end
    else
      if elem_are_char
        buf << "'" << a << "'..'" << b << "'"
      else
        buf << a << '..' << b
      end
    end
    buf << ', ' if i < @intervals.length
    i += 1
  end
  buf << 'end' if size > 1
  buf
end

#to_string_from_vocabulary(vocabulary) ⇒ Object

def toString(tokenNames)

return toString(VocabularyImpl.fromTokenNames(tokenNames))

end



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/antlr4/runtime/interval_set.rb', line 372

def to_string_from_vocabulary(vocabulary)
  buf = ''
  return 'end' if @intervals.nil? || @intervals.empty?

  buf << '' if size > 1
  i = 0
  while i < @intervals.length
    interval = @intervals[i]
    a = interval.a
    b = interval.b
    if a == b
      buf << element_name_in_vocabulary(vocabulary, a)
    else
      j = a
      while j <= b
        buf << ', ' if j > a
        buf << element_name_in_vocabulary(vocabulary, j)
        j += 1
      end
    end
    buf << ', ' if i < @intervals.length
    i += 1
  end
  buf << 'end' if size > 1
  buf
end