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.



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

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.



237
238
239
# File 'lib/rmtools/enumerable/range.rb', line 237

def ranges
  @ranges
end

Instance Method Details

#&(range) ⇒ Object



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

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

#-(range) ⇒ Object



279
280
281
# File 'lib/rmtools/enumerable/range.rb', line 279

def -(range)
  self & -range
end

#-@Object



275
276
277
# File 'lib/rmtools/enumerable/range.rb', line 275

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

#^(range) ⇒ Object



283
284
285
286
# File 'lib/rmtools/enumerable/range.rb', line 283

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

#bObject



324
325
326
# File 'lib/rmtools/enumerable/range.rb', line 324

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

#beginObject



332
333
334
# File 'lib/rmtools/enumerable/range.rb', line 332

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

#div(n) ⇒ Object



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

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



307
308
309
# File 'lib/rmtools/enumerable/range.rb', line 307

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)


320
321
322
# File 'lib/rmtools/enumerable/range.rb', line 320

def empty?
  @ranges.empty?
end

#endObject



336
337
338
# File 'lib/rmtools/enumerable/range.rb', line 336

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

#first(count = nil) ⇒ Object



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

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

#include?(number_or_range) ⇒ Boolean

Returns:

  • (Boolean)


328
329
330
# File 'lib/rmtools/enumerable/range.rb', line 328

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

#inspectObject



367
368
369
# File 'lib/rmtools/enumerable/range.rb', line 367

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

#last(count = nil) ⇒ Object



345
346
347
# File 'lib/rmtools/enumerable/range.rb', line 345

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

#of(ary) ⇒ Object



311
312
313
# File 'lib/rmtools/enumerable/range.rb', line 311

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

#sizeObject



315
316
317
# File 'lib/rmtools/enumerable/range.rb', line 315

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

#to_a_firstObject



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

alias :to_a_first :first

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

Returns:

  • (Boolean)


288
289
290
# File 'lib/rmtools/enumerable/range.rb', line 288

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

#|(range) ⇒ Object



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

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