Class: Antlr4ruby::RangeSet

Inherits:
Object
  • Object
show all
Defined in:
lib/antlr4ruby/misc/range_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = []) ⇒ RangeSet



8
9
10
# File 'lib/antlr4ruby/misc/range_set.rb', line 8

def initialize(data = [])
  @data = data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/antlr4ruby/misc/range_set.rb', line 6

def data
  @data
end

Instance Method Details

#&(other) ⇒ Object



82
83
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
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/antlr4ruby/misc/range_set.rb', line 82

def & (other)
  result = []
  i, j = 0, 0

  while i < data.length && j < other.data.length
    while j < other.data.length && data[i].min > other.data[j].max
      j += 1
    end
    break if j >= other.data.length
    while i < data.length && other.data[j].min > data[i].max
      i += 1
    end
    break if i >= data.length


    a, b = data[i], other.data[j]
    if a.cover?(b)
      result << b; j += 1
    elsif b.cover?(a)
      result << a; i += 1
    elsif a.include?(b.min)
      result << (b.min..a.max); i += 1
    elsif b.include?(a.min)
      result << (a.min..b.max); j += 1

    end
  end

  RangeSet.new(result)
  # other.data.each do |a|
  #   data.each do |b|
  #     next if a.min > b.max || a.max < b.min
  #     if a.cover?(b)
  #       result.push(b)
  #     elsif b.cover?(a)
  #       result.push(a)
  #     else
  #       start = a.min > b.min ? a.min : b.min
  #       stop = a.max < b.max ? a.max : b.max
  #       result.push(start..stop)
  #     end
  #   end
  # end
  # RangeSet.new(result)
end

#+(other) ⇒ 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
# File 'lib/antlr4ruby/misc/range_set.rb', line 42

def + (other)
  # other_data = other.data.reverse
  # range = other_data.pop
  # i = 0
  # result = []
  # while range && i < data.length
  #   a = data[i]
  #   if a.min > range.max + 1
  #     new_range = other_data.pop
  #     result << range; range = other_data.pop
  #   elsif a.max < range.min - 1
  #     result << a; i += 1
  #   else
  #     # 直接将 a 合并到 range 里面
  #     start = a.min < range.min ? a.min : range.min
  #     stop = a.max > range.max ? a.max : range.max
  #     range = start..stop; i += 1
  #   end
  #
  # end
  #
  # result << range if range
  # while i < data.length
  #   result << data[i]; i += 1
  # end
  #
  # while other_data.length > 0
  #   result << other_data.pop
  # end
  # RangeSet.new(result)
  #
  result = self.clone
  other.data.each { |item| result.add(item) }
  result
end

#-(other) ⇒ Object



154
155
156
157
158
# File 'lib/antlr4ruby/misc/range_set.rb', line 154

def - (other)
  result = self.clone
  other.data.each { |item| result.delete(item) }
  result
end

#==(other) ⇒ Object



217
218
219
# File 'lib/antlr4ruby/misc/range_set.rb', line 217

def == (other)
  self.eql?(other)
end

#add(range) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/antlr4ruby/misc/range_set.rb', line 16

def add(range)
  i = 0
  result = []
  while i < data.length
    a = data[i]
    if a.min > range.max + 1
      break
    elsif a.max < range.min - 1
      result << a; i += 1; next
    else
      # 直接将 a 合并到 range 里面
      start = a.min < range.min ? a.min : range.min
      stop = a.max > range.max ? a.max : range.max
      range = start..stop; i += 1
    end
  end

  result << range
  while i < data.length
    result << data[i]; i += 1
  end


  @data = result
end

#add_all(ranges) ⇒ Object



128
129
130
131
132
133
# File 'lib/antlr4ruby/misc/range_set.rb', line 128

def add_all(ranges)
  other_data = ranges.instance_of?(RangeSet) ? ranges.data : ranges
  other_data.each do |item|
    add(item)
  end
end

#clearObject



12
13
14
# File 'lib/antlr4ruby/misc/range_set.rb', line 12

def clear
  data.clear
end

#complement(total) ⇒ Object



160
161
162
# File 'lib/antlr4ruby/misc/range_set.rb', line 160

def complement(total)
  total - self
end

#cover?(range) ⇒ Boolean



177
178
179
180
181
182
# File 'lib/antlr4ruby/misc/range_set.rb', line 177

def cover?(range)
  data.each do |item|
    return true if item.cover?(range)
  end
  false
end

#delete(range) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/antlr4ruby/misc/range_set.rb', line 135

def delete(range)
  result = []
  data.each do |item|
    next if range.cover?(item)
    result.push(item.min...range.min) if item.include?(range.min) && range.min > item.min
    result.push(range.max + 1..item.max) if item.include?(range.max) && range.max < item.max
    result.push(item) if item.max < range.min || item.min > range.max
  end

  @data = result
end

#delete_all(ranges) ⇒ Object



147
148
149
150
151
152
# File 'lib/antlr4ruby/misc/range_set.rb', line 147

def delete_all(ranges)
  other_data = ranges.instance_of?(RangeSet) ? ranges.data : ranges
  other_data.each do |item|
    delete(item)
  end
end

#empty?Boolean



168
169
170
# File 'lib/antlr4ruby/misc/range_set.rb', line 168

def empty?
  data.empty?
end

#eql?(other) ⇒ Boolean



209
210
211
212
213
214
215
# File 'lib/antlr4ruby/misc/range_set.rb', line 209

def eql?(other)
  return false if !other || data.length != other.data.length
  data.length.times do |i|
    return false unless data[i].eql?(other.data[i])
  end
  true
end

#include?(n) ⇒ Boolean



172
173
174
175
# File 'lib/antlr4ruby/misc/range_set.rb', line 172

def include?(n)
  data.each { |item| return true if item.include?(n) }
  false
end

#nil?Boolean



164
165
166
# File 'lib/antlr4ruby/misc/range_set.rb', line 164

def nil?
  data.empty?
end

#startObject



184
185
186
187
188
189
190
191
# File 'lib/antlr4ruby/misc/range_set.rb', line 184

def start
  nil if empty?
  data.sort! do |a, b|
    a.min == b.min ? a.max <=> b.max : a.min <=> b.min
  end

  data[0].min
end

#stopObject



193
194
195
196
197
198
199
# File 'lib/antlr4ruby/misc/range_set.rb', line 193

def stop
  nil if empty?
  data.sort! do |a, b|
    a.min == b.min ? a.max <=> b.max : a.min <=> b.min
  end
  data[-1].max
end

#to_aObject



201
202
203
# File 'lib/antlr4ruby/misc/range_set.rb', line 201

def to_a
  @data
end

#to_setObject



205
206
207
# File 'lib/antlr4ruby/misc/range_set.rb', line 205

def to_set
  data.to_set
end

#|(other) ⇒ Object



78
79
80
# File 'lib/antlr4ruby/misc/range_set.rb', line 78

def | (other)
  self + other
end