Class: Antlr4::Runtime::Interval

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

Constant Summary collapse

INTERVAL_POOL_MAX_VALUE =
1000
@@cache =
[]
@@creates =
0
@@misses =
0
@@hits =
0
@@outOfRange =
0
@@invalid =
Interval.new(-1, -2)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a, b) ⇒ Interval

Returns a new instance of Interval.



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

def initialize(a, b)
  @a = a
  @b = b
end

Class Attribute Details

.createsObject

Returns the value of attribute creates.



15
16
17
# File 'lib/antlr4/runtime/interval.rb', line 15

def creates
  @creates
end

.hitsObject

Returns the value of attribute hits.



17
18
19
# File 'lib/antlr4/runtime/interval.rb', line 17

def hits
  @hits
end

.invalidObject

Returns the value of attribute invalid.



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

def invalid
  @invalid
end

.missesObject

Returns the value of attribute misses.



16
17
18
# File 'lib/antlr4/runtime/interval.rb', line 16

def misses
  @misses
end

.outOfRangeObject

Returns the value of attribute outOfRange.



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

def outOfRange
  @outOfRange
end

Instance Attribute Details

#aObject

Returns the value of attribute a.



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

def a
  @a
end

#bObject

Returns the value of attribute b.



7
8
9
# File 'lib/antlr4/runtime/interval.rb', line 7

def b
  @b
end

Class Method Details

.of(a, b) ⇒ Object



29
30
31
32
33
34
# File 'lib/antlr4/runtime/interval.rb', line 29

def self.of(a, b)
  return Interval.new(a, b) if a != b || a < 0 || a > INTERVAL_POOL_MAX_VALUE

  @@cache[a] = Interval.new(a, a) if @@cache[a].nil?
  @@cache[a]
end

Instance Method Details

#==(o) ⇒ Object



42
43
44
45
46
# File 'lib/antlr4/runtime/interval.rb', line 42

def ==(o)
  return false if o.nil? || !(o.is_a? Interval)

  @a == o.a && @b == o.b
end

#adjacent(other) ⇒ Object



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

def adjacent(other)
  @a == other.b + 1 || @b == other.a - 1
end

#difference_not_properly_contained(other) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/antlr4/runtime/interval.rb', line 95

def difference_not_properly_contained(other)
  diff = null
  if other.starts_before_non_disjoint(this)

    diff = Interval.of([@a, other.b + 1].max, @b)

  elsif other.startsAfterNonDisjoint(this)

    diff = Interval.of(@a, other.a - 1)
  end
  diff
end

#disjoint(other) ⇒ Object



75
76
77
# File 'lib/antlr4/runtime/interval.rb', line 75

def disjoint(other)
  starts_before_disjoint(other) || startsAfterDisjoint(other)
end

#hashObject



48
49
50
51
52
53
# File 'lib/antlr4/runtime/interval.rb', line 48

def hash
  return @_hash unless @_hash.nil?
  @_hash = 23
  @_hash = @_hash * 31 + @a
  @_hash = @_hash * 31 + @b
end

#intersection(other) ⇒ Object



91
92
93
# File 'lib/antlr4/runtime/interval.rb', line 91

def intersection(other)
  Interval.of([a, other.a].max, [b, other.b].min)
end

#lengthObject



36
37
38
39
40
# File 'lib/antlr4/runtime/interval.rb', line 36

def length
  return 0 if @b < @a

  @b - @a + 1
end

#properlyContains(other) ⇒ Object



83
84
85
# File 'lib/antlr4/runtime/interval.rb', line 83

def properlyContains(other)
  other.a >= @a && other.b <= @b
end

#starts_before_disjoint(other) ⇒ Object



55
56
57
# File 'lib/antlr4/runtime/interval.rb', line 55

def starts_before_disjoint(other)
  @a < other.a && @b < other.a
end

#starts_before_non_disjoint(other) ⇒ Object



59
60
61
# File 'lib/antlr4/runtime/interval.rb', line 59

def starts_before_non_disjoint(other)
  @a <= other.a && @b >= other.a
end

#startsAfter(other) ⇒ Object



63
64
65
# File 'lib/antlr4/runtime/interval.rb', line 63

def startsAfter(other)
  @a > other.a
end

#startsAfterDisjoint(other) ⇒ Object



67
68
69
# File 'lib/antlr4/runtime/interval.rb', line 67

def startsAfterDisjoint(other)
  @a > other.b
end

#startsAfterNonDisjoint(other) ⇒ Object



71
72
73
# File 'lib/antlr4/runtime/interval.rb', line 71

def startsAfterNonDisjoint(other)
  @a > other.a && @a <= other.b
end

#to_sObject



108
109
110
# File 'lib/antlr4/runtime/interval.rb', line 108

def to_s
  @a.to_s + '..' + @b.to_s
end

#union(other) ⇒ Object



87
88
89
# File 'lib/antlr4/runtime/interval.rb', line 87

def union(other)
  Interval.of([a, other.a].min, [b, other.b].max)
end