Class: RubyTorrent::AwesomeRange

Inherits:
Range
  • Object
show all
Defined in:
lib/rubytorrent/package.rb

Overview

Range plus a lot of utility methods

Instance Method Summary collapse

Constructor Details

#initialize(start, endd = nil, exclude_end = false) ⇒ AwesomeRange

Returns a new instance of AwesomeRange.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rubytorrent/package.rb', line 27

def initialize(start, endd=nil, exclude_end=false)
  case start
  when Integer
    raise ArgumentError, "both start and endd must be specified" if endd.nil?
    super(start, endd, exclude_end)
  when Range
    super(start.first, start.last, start.exclude_end?)
  else
    raise ArgumentError, "start should be an Integer or a Range, is a #{start.class}"
  end
end

Instance Method Details

#rcont?(o) ⇒ Boolean

range continuity

Returns:

  • (Boolean)


70
71
72
# File 'lib/rubytorrent/package.rb', line 70

def rcont?(o)
  (first == o.last) || (last == o.first) || (rint(o) != nil)
end

#rdiff(o) ⇒ Object

range difference. returns an array of 0, 1 or 2 ranges.

Raises:

  • (RangeError)


86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rubytorrent/package.rb', line 86

def rdiff(o)
  return [] if o == self
  ret = []
  int = rint o
  return [] if int == self
  return [self] if int == nil
  raise RangeError, "can't subtract a range that doesn't have an exclusive end" unless int.exclude_end?
  if int.first > first
    ret << AwesomeRange.new(first, int.first, true)
  end
  ret + [AwesomeRange.new(int.last, last, exclude_end?)]
end

#rint(o) ⇒ Object

range intersection



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rubytorrent/package.rb', line 46

def rint(o)
  ## three cases. either:
  ## a) our left endpoint is within o
  if ((first >= o.first) &&
    ((first < o.last) || (!o.exclude_end? && (first == o.last))))
    if last < o.last
      AwesomeRange.new(first, last, exclude_end?)
    elsif last > o.last
      AwesomeRange.new(first, o.last, o.exclude_end?)
    else # ==
      AwesomeRange.new(first, last, exclude_end? || o.exclude_end?)
    end
  ## b) our right endpoint is within o
  elsif (((last > o.first) || (!exclude_end? && (last == o.first))) && ((last < o.last) || (!o.exclude_end? && (last == o.last))))
    AwesomeRange.new([first, o.first].max, last, exclude_end?)
  ## c) we encompass o
  elsif rss?(o)
    o
  else
    nil
  end
end

#rss?(o) ⇒ Boolean

range super-set: does this range encompass ‘o’?

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/rubytorrent/package.rb', line 40

def rss?(o)
  (first <= o.first) &&
    ((last > o.last) || (o.exclude_end? && (last == o.last)))
end

#runion(o) ⇒ Object

range union: only valid for continuous ranges



75
76
77
78
79
80
81
82
83
# File 'lib/rubytorrent/package.rb', line 75

def runion(o)
  if last > o.last
    AwesomeRange.new([first, o.first].min, last, exclude_end?)
  elsif o.last > last
    AwesomeRange.new([first, o.first].min, o.last, o.exclude_end?)
  else # equal
    AwesomeRange.new([first, o.first].min, last, (exclude_end? && o.exclude_end?))
  end
end