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


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

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

#difference_not_properly_contained(other) ⇒ Object


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

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


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

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

#hashObject


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

def hash
  hash = 23
  hash = hash * 31 + @a
  hash * 31 + @b
end

#intersection(other) ⇒ Object


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

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


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

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

#starts_before_disjoint(other) ⇒ Object


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

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

#starts_before_non_disjoint(other) ⇒ Object


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

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

#startsAfter(other) ⇒ Object


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

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

#startsAfterDisjoint(other) ⇒ Object


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

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

#startsAfterNonDisjoint(other) ⇒ Object


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

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

#to_sObject


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

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

#union(other) ⇒ Object


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

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