Class: XRange

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/rmtools/enumerable/range.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#+, #dump, #export, #foldl, #foldr, #import, #map_hash, #present, #rand, #randsample, #recursive_find, #recursive_select, #threadify, #to_traversable, #truth_map, #urlencode, #xprod

Constructor Details

#initialize(*args) ⇒ XRange

Returns a new instance of XRange.



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/rmtools/enumerable/range.rb', line 242

def initialize *args
  if (str = args[0]).is String
    str.scan(/([&|v\^])?((-?\d+)\.\.(\.)?(-?\d+))/).each {|s|
      s[2], s[4] = s[2].to_i, s[4].to_i
      r = s[3] ? s[2]..s[4]-1 : s[2]..s[4]
      @ranges = case s[0]
                        when '&', '^'; intersect r
                        when '|', 'v'; union r
                        else [r]
                      end
    }
    @ranges.sort!
  else
    0.upto(args.sort!.size-2) {|i| args[i,2] = [nil, args[i]|args[i+1]] if args[i].x? args[i+1]}
    @ranges = args.compact.include_ends
  end
end

Instance Attribute Details

#rangesObject

Returns the value of attribute ranges.



239
240
241
# File 'lib/rmtools/enumerable/range.rb', line 239

def ranges
  @ranges
end

Instance Method Details

#&(range) ⇒ Object



260
261
262
263
264
265
266
# File 'lib/rmtools/enumerable/range.rb', line 260

def &(range)
  if range.is Range
    XRange.new *intersect(range)
  else
    @ranges.map {|r| range & r}.foldl(:|) || XRange.new
  end
end

#-(range) ⇒ Object



281
282
283
# File 'lib/rmtools/enumerable/range.rb', line 281

def -(range)
  self & -range
end

#-@Object



277
278
279
# File 'lib/rmtools/enumerable/range.rb', line 277

def -@
  @ranges.map {|r| -r}.foldl(:&) || XRange.new(-Inf..+Inf)
end

#^(range) ⇒ Object



285
286
287
288
# File 'lib/rmtools/enumerable/range.rb', line 285

def ^(range)
  common = self & range
  self - common | range - common
end

#bObject



326
327
328
# File 'lib/rmtools/enumerable/range.rb', line 326

def b
  !@ranges.empty? && self
end

#beginObject



334
335
336
# File 'lib/rmtools/enumerable/range.rb', line 334

def begin
  @begin ||= @ranges.first && @ranges.first.begin
end

#div(n) ⇒ Object



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/rmtools/enumerable/range.rb', line 351

def div(n)
  unless n < 1
    i = 0
    rarray = []
    j = @ranges[0].begin
    while range = @ranges[i]
      if j+n > range.end
        rarray << (j..range.end)
        i += 1
        j = @ranges[i].begin if @ranges[i]
      else
        rarray << (j..(j+=n)-1)
      end
    end
    rarray
  end
end

#each(&b) ⇒ Object



309
310
311
# File 'lib/rmtools/enumerable/range.rb', line 309

def each(&b) 
  @ranges.each {|r| r.each &b} 
end

#empty?Boolean

XRange doesn’t support ranges with end excluded, so it’s empty only if contains nothing

Returns:

  • (Boolean)


322
323
324
# File 'lib/rmtools/enumerable/range.rb', line 322

def empty?
  @ranges.empty?
end

#endObject



338
339
340
# File 'lib/rmtools/enumerable/range.rb', line 338

def end
  @end ||= @ranges.last && @ranges.last.end
end

#first(count = nil) ⇒ Object



343
344
345
# File 'lib/rmtools/enumerable/range.rb', line 343

def first(count=nil)
  count ? to_a_first(count) : self.begin
end

#include?(number_or_range) ⇒ Boolean

Returns:

  • (Boolean)


330
331
332
# File 'lib/rmtools/enumerable/range.rb', line 330

def include?(number_or_range)
  @ranges.find_include?(number_or_range)
end

#inspectObject



369
370
371
# File 'lib/rmtools/enumerable/range.rb', line 369

def inspect
  "XRange(#{@ranges.join(', ').gsub('-Infinity', '-∞').gsub('Infinity', '+∞')})"
end

#last(count = nil) ⇒ Object



347
348
349
# File 'lib/rmtools/enumerable/range.rb', line 347

def last(count=nil)
  count ? to_a.last(count) : self.end
end

#of(ary) ⇒ Object



313
314
315
# File 'lib/rmtools/enumerable/range.rb', line 313

def of(ary) 
  @ranges.sum_of(ary)
end

#sizeObject



317
318
319
# File 'lib/rmtools/enumerable/range.rb', line 317

def size
  @size ||= @ranges.sum_size
end

#to_a_firstObject



342
# File 'lib/rmtools/enumerable/range.rb', line 342

alias :to_a_first :first

#x?(range) ⇒ Boolean Also known as: intersects?

Returns:

  • (Boolean)


290
291
292
# File 'lib/rmtools/enumerable/range.rb', line 290

def x?(range)
  @ranges.any? {|r| range.x? r}
end

#|(range) ⇒ Object



268
269
270
271
272
273
274
275
# File 'lib/rmtools/enumerable/range.rb', line 268

def |(range)
  if range.is Range
    XRange.new *union(range)
  else
    @ranges.each {|r| range |= r}
    range
  end
end