Class: Antlr4::Runtime::IntervalSet
- Inherits:
-
Object
- Object
- Antlr4::Runtime::IntervalSet
- 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
-
#intervals ⇒ Object
Returns the value of attribute intervals.
Class Method Summary collapse
Instance Method Summary collapse
- #==(obj) ⇒ Object
- #add(el1, el2 = nil) ⇒ Object
- #add_all(set) ⇒ Object
- #add_interval(addition) ⇒ Object
- #and(other) ⇒ Object
- #clear ⇒ Object
- #complement(min_element, max_element) ⇒ Object
- #complement_interval_set(intervalSet) ⇒ Object
- #contains(el) ⇒ Object
- #element_name_in_vocabulary(vocabulary, a) ⇒ Object
- #hash ⇒ Object
-
#initialize(a = nil, b = nil) ⇒ IntervalSet
constructor
A new instance of IntervalSet.
- #is_nil ⇒ Object
- #max_element ⇒ Object
- #min_element ⇒ Object
- #or(a) ⇒ Object
- #readonly(readonly) ⇒ Object
- #remove(el) ⇒ Object
- #size ⇒ Object
- #subtract(a) ⇒ Object
- #subtract_interval_sets(left, right) ⇒ Object
- #to_a ⇒ Object
- #to_integer_list ⇒ Object
- #to_list ⇒ Object
- #to_set ⇒ Object
- #to_string(elem_are_char = false) ⇒ Object
- #to_string_from_vocabulary(vocabulary) ⇒ Object
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
#intervals ⇒ Object
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 |
.or_sets(sets) ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/antlr4/runtime/interval_set.rb', line 81 def self.or_sets(sets) r = IntervalSet.new i = 0 while i < sets.length r.add_all(sets[i]) i += 1 end r end |
Instance Method Details
#==(obj) ⇒ Object
310 311 312 313 314 315 |
# File 'lib/antlr4/runtime/interval_set.rb', line 310 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 39 40 |
# 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 el1.is_a? IntervalSet add_all(el1) elsif el2.nil? add_interval(Interval.of(el1, el1)) else add_interval(Interval.of(el1, el2)) end end |
#add_all(set) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/antlr4/runtime/interval_set.rb', line 91 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 end self end |
#add_interval(addition) ⇒ Object
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 78 79 |
# File 'lib/antlr4/runtime/interval_set.rb', line 42 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
197 198 199 200 201 202 203 204 205 206 207 208 209 210 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 |
# File 'lib/antlr4/runtime/interval_set.rb', line 197 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 |
#clear ⇒ Object
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
109 110 111 |
# File 'lib/antlr4/runtime/interval_set.rb', line 109 def complement(min_element, max_element) complement_interval_set(IntervalSet.of(min_element, max_element)) end |
#complement_interval_set(intervalSet) ⇒ Object
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/antlr4/runtime/interval_set.rb', line 113 def complement_interval_set(intervalSet) if intervalSet.nil? || intervalSet.is_nil return nil # nothing in common with nil set end if intervalSet.is_a? IntervalSet intervalSet.subtract(self) end end |
#contains(el) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/antlr4/runtime/interval_set.rb', line 253 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
377 378 379 380 381 382 383 384 385 |
# File 'lib/antlr4/runtime/interval_set.rb', line 377 def element_name_in_vocabulary(vocabulary, a) if a == Token::EOF '<EOF>' elsif a == Token::EPSILON '<EPSILON>' else vocabulary.display_name(a) end end |
#hash ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/antlr4/runtime/interval_set.rb', line 292 def hash ints = @intervals.each_with_object([]) do |interval, ret| ret << interval.a ret << interval.b end hash_code = RumourHash.calculate(ints) unless @_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_nil ⇒ Object
275 276 277 |
# File 'lib/antlr4/runtime/interval_set.rb', line 275 def is_nil @intervals.nil? || @intervals.empty? end |
#max_element ⇒ Object
279 280 281 282 283 284 |
# File 'lib/antlr4/runtime/interval_set.rb', line 279 def max_element raise StandardEror, 'set is empty' if is_nil last = @intervals[-1] last.b end |
#min_element ⇒ Object
286 287 288 289 290 |
# File 'lib/antlr4/runtime/interval_set.rb', line 286 def min_element raise StandardEror, 'set is empty' if is_nil @intervals[0].a end |
#or(a) ⇒ Object
190 191 192 193 194 195 |
# File 'lib/antlr4/runtime/interval_set.rb', line 190 def or(a) o = IntervalSet.new o.add_all(self) o.add_all(a) o end |
#readonly(readonly) ⇒ Object
484 485 486 487 488 489 490 |
# File 'lib/antlr4/runtime/interval_set.rb', line 484 def readonly(readonly) if @readonly && !readonly raise IllegalStateException, "can't alter readonly IntervalSet" end @readonly = readonly end |
#remove(el) ⇒ Object
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 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 446 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 oldb = interval.b interval.b = el - 1 # [a..x-1] add(el + 1, oldb) # add [x+1..b] end i += 1 end end |
#size ⇒ Object
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/antlr4/runtime/interval_set.rb', line 387 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
124 125 126 127 128 |
# File 'lib/antlr4/runtime/interval_set.rb', line 124 def subtract(a) return IntervalSet.new self if a.nil? || a.is_nil subtract_interval_sets(self, a) end |
#subtract_interval_sets(left, right) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 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 |
# File 'lib/antlr4/runtime/interval_set.rb', line 130 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[result_i] = before_current result.intervals[result_i + 1] = after_current result_i += 1 right_i += 1 next else # replace the current interval result.intervals[result_i] = before_current result_i += 1 next end else if !after_current.nil? result.intervals[result_i] = after_current right_i += 1 next else result.intervals.delete_at(result_i) next end end end result end |
#to_a ⇒ Object
442 443 444 |
# File 'lib/antlr4/runtime/interval_set.rb', line 442 def to_a to_integer_list end |
#to_integer_list ⇒ Object
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/antlr4/runtime/interval_set.rb', line 403 def to_integer_list values = [] n = @intervals.length i = 0 while i < n interval = @intervals[i] a = interval.a b = interval.b v = a while v <= b values.push(v) v += 1 end i += 1 end values end |
#to_list ⇒ Object
421 422 423 |
# File 'lib/antlr4/runtime/interval_set.rb', line 421 def to_list to_integer_list end |
#to_set ⇒ Object
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/antlr4/runtime/interval_set.rb', line 425 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
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 346 347 348 |
# File 'lib/antlr4/runtime/interval_set.rb', line 317 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
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 |
# File 'lib/antlr4/runtime/interval_set.rb', line 350 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 - 1) i += 1 end buf << '}' if size > 1 buf end |