Class: RangeSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rangeset/version.rb,
lib/rangeset/range_set.rb

Constant Summary collapse

VERSION =
'0.1.0-RC3-SNAPSHOT'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(range_map = TreeMap.new) ⇒ RangeSet

Returns a new instance of RangeSet.


13
14
15
16
17
18
19
20
21
# File 'lib/rangeset/range_set.rb', line 13

def initialize(range_map = TreeMap.new)
  unless range_map.instance_of?(TreeMap) || range_map.instance_of?(TreeMap::BoundedMap)
    raise ArgumentError.new("invalid range_map #{range_map}")
  end

  @range_map = range_map

  update_bounds
end

Class Method Details

.[](*ranges) ⇒ Object


7
8
9
10
11
# File 'lib/rangeset/range_set.rb', line 7

def self.[](*ranges)
  RangeSet.new.tap do |range_set|
    ranges.each {|range| range_set << range}
  end
end

Instance Method Details

#add(object) ⇒ Object Also known as: <<, union!


114
115
116
117
118
119
120
121
122
123
# File 'lib/rangeset/range_set.rb', line 114

def add(object)
  case object
    when Range
      add_range(object)
    when RangeSet
      add_range_set(object)
    else
      raise ArgumentError.new("unexpected object #{object}")
  end
end

#boundsObject


35
36
37
# File 'lib/rangeset/range_set.rb', line 35

def bounds
  min..max
end

#buffer(range) ⇒ Object


224
225
226
# File 'lib/rangeset/range_set.rb', line 224

def buffer(range)
  clone.buffer!(range)
end

#buffer!(range) ⇒ Object


220
221
222
# File 'lib/rangeset/range_set.rb', line 220

def buffer!(range)
  convolve_range!(range)
end

#clearObject


228
229
230
231
232
# File 'lib/rangeset/range_set.rb', line 228

def clear
  @range_map.clear
  @min = nil
  @max = nil
end

#cloneObject


238
239
240
# File 'lib/rangeset/range_set.rb', line 238

def clone
  RangeSet.new.copy(self)
end

#convolve(object) ⇒ Object Also known as: *


206
207
208
# File 'lib/rangeset/range_set.rb', line 206

def convolve(object)
  clone.convolve!(object)
end

#convolve!(object) ⇒ Object


195
196
197
198
199
200
201
202
203
204
# File 'lib/rangeset/range_set.rb', line 195

def convolve!(object)
  case object
    when Range
      convolve_range!(object)
    when RangeSet
      convolve_range_set!(object)
    else
      convolve_element!(object)
  end
end

#copy(range_set) ⇒ Object


242
243
244
245
246
247
248
249
# File 'lib/rangeset/range_set.rb', line 242

def copy(range_set)
  clear
  range_set.each {|range| put(range)}
  @min = range_set.min
  @max = range_set.max

  self
end

#countObject


110
111
112
# File 'lib/rangeset/range_set.rb', line 110

def count
  @range_map.count
end

#difference(object) ⇒ Object Also known as: -


182
183
184
185
186
187
188
189
190
191
# File 'lib/rangeset/range_set.rb', line 182

def difference(object)
  case object
    when Range
      difference_range(object)
    when RangeSet
      difference_range_set(object)
    else
      raise ArgumentError.new("unexpected object #{object}")
  end
end

#eachObject


234
235
236
# File 'lib/rangeset/range_set.rb', line 234

def each
  @range_map.each_node {|node| yield node.value}
end

#empty?Boolean

Returns:

  • (Boolean)

23
24
25
# File 'lib/rangeset/range_set.rb', line 23

def empty?
  @range_map.empty?
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)

39
40
41
42
43
44
45
46
47
# File 'lib/rangeset/range_set.rb', line 39

def eql?(other)
  return false if count != other.count
  return false if bounds != other.bounds

  lhs_iter = enum_for
  rhs_iter = other.enum_for

  count.times.all? {lhs_iter.next == rhs_iter.next}
end

#include?(object) ⇒ Boolean

whether all elements are present

Returns:

  • (Boolean)

88
89
90
91
92
93
94
95
96
97
# File 'lib/rangeset/range_set.rb', line 88

def include?(object)
  case object
    when Range
      include_range?(object)
    when RangeSet
      include_range_set?(object)
    else
      include_element?(object)
  end
end

#intersect(object) ⇒ Object Also known as: intersection!


142
143
144
145
146
147
148
149
150
151
# File 'lib/rangeset/range_set.rb', line 142

def intersect(object)
  case object
    when Range
      intersect_range(object)
    when RangeSet
      intersect_range_set(object)
    else
      raise ArgumentError.new("unexpected object #{object}")
  end
end

#intersect?(object) ⇒ Boolean

Returns:

  • (Boolean)

99
100
101
102
103
104
105
106
107
108
# File 'lib/rangeset/range_set.rb', line 99

def intersect?(object)
  case object
    when Range
      intersect_range?(object)
    when RangeSet
      intersect_range_set?(object)
    else
      include_element?(object)
  end
end

#intersection(object) ⇒ Object Also known as: &


155
156
157
158
159
160
161
162
163
164
# File 'lib/rangeset/range_set.rb', line 155

def intersection(object)
  case object
    when Range
      intersection_range(object)
    when RangeSet
      intersection_range_set(object)
    else
      raise ArgumentError.new("unexpected object #{object}")
  end
end

#maxObject


31
32
33
# File 'lib/rangeset/range_set.rb', line 31

def max
  @max
end

#minObject


27
28
29
# File 'lib/rangeset/range_set.rb', line 27

def min
  @min
end

#overlapped_by?(range) ⇒ Boolean

Returns:

  • (Boolean)

75
76
77
# File 'lib/rangeset/range_set.rb', line 75

def overlapped_by?(range)
  empty? || (range.min <= min && range.max >= max)
end

#proper_subset?(other) ⇒ Boolean Also known as: <

Returns:

  • (Boolean)

69
70
71
# File 'lib/rangeset/range_set.rb', line 69

def proper_subset?(other)
  !eql?(other) && subset?(other)
end

#proper_superset?(other) ⇒ Boolean Also known as: >

Returns:

  • (Boolean)

57
58
59
# File 'lib/rangeset/range_set.rb', line 57

def proper_superset?(other)
  !eql?(other) && superset?(other)
end

#remove(object) ⇒ Object Also known as: >>, difference!


128
129
130
131
132
133
134
135
136
137
# File 'lib/rangeset/range_set.rb', line 128

def remove(object)
  case object
    when Range
      remove_range(object)
    when RangeSet
      remove_range_set(object)
    else
      raise ArgumentError.new("unexpected object #{object}")
  end
end

#shift(object) ⇒ Object


216
217
218
# File 'lib/rangeset/range_set.rb', line 216

def shift(object)
  clone.shift!(object)
end

#shift!(object) ⇒ Object


212
213
214
# File 'lib/rangeset/range_set.rb', line 212

def shift!(object)
  convolve_element!(object)
end

#subset?(other) ⇒ Boolean Also known as: <=

Returns:

  • (Boolean)

63
64
65
# File 'lib/rangeset/range_set.rb', line 63

def subset?(other)
  other.include_range_set?(self)
end

#superset?(other) ⇒ Boolean Also known as: >=

Returns:

  • (Boolean)

51
52
53
# File 'lib/rangeset/range_set.rb', line 51

def superset?(other)
  include_range_set?(other)
end

#to_aObject


251
252
253
# File 'lib/rangeset/range_set.rb', line 251

def to_a
  @range_map.values
end

#to_sObject Also known as: inspect


255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/rangeset/range_set.rb', line 255

def to_s
  string_io = StringIO.new

  last_index = count - 1

  string_io << '['
  each_with_index do |range, i|
    string_io << range
    string_io << ', ' if i < last_index
  end
  string_io << ']'

  string_io.string
end

#union(object) ⇒ Object Also known as: |, +


168
169
170
171
172
173
174
175
176
177
# File 'lib/rangeset/range_set.rb', line 168

def union(object)
  case object
    when Range
      union_range(object)
    when RangeSet
      union_range_set(object)
    else
      raise ArgumentError.new("unexpected object #{object}")
  end
end

#within_bounds?(range) ⇒ Boolean

Returns:

  • (Boolean)

79
80
81
# File 'lib/rangeset/range_set.rb', line 79

def within_bounds?(range)
  !empty? && range.min < max && range.max > min
end

#within_or_touching_bounds?(range) ⇒ Boolean

Returns:

  • (Boolean)

83
84
85
# File 'lib/rangeset/range_set.rb', line 83

def within_or_touching_bounds?(range)
  !empty? && range.min <= max && range.max >= min
end