Class: TaskJuggler::IntervalList

Inherits:
Array
  • Object
show all
Defined in:
lib/taskjuggler/IntervalList.rb

Overview

A list of Intervals. The intervals in the list must not overlap and must be in ascending order.

Instance Method Summary collapse

Instance Method Details

#&(list) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
77
78
79
# File 'lib/taskjuggler/IntervalList.rb', line 24

def &(list)
  res = IntervalList.new
  si = li = 0
  while si < length && li < list.length do
    if self[si].start < list[li].start
      # The current Interval of self starts earlier than the current
      # Interval of list.
      if self[si].end <= list[li].start
        # self[si] does not overlap with list[li]. Ignore it.
        si += 1
      elsif self[si].end < list[li].end
        # self[si] does overlap with list[li] but list[li] goes further
        res << self[si].class.new(list[li].start, self[si].end)
        si += 1
      else
        # self[si] does overlap with list[li] but self[si] goes further
        res << self[si].class.new(list[li].start, list[li].end)
        li += 1
      end
    elsif list[li].start < self[si].start
      # The current Interval of list starts earlier than the current
      # Interval of self.
      if list[li].end <= self[si].start
        # list[li] does not overlap with self[si]. Ignore it.
        li += 1
      elsif list[li].end < self[si].end
        # list[li] does overlap with self[si] but self[si] goes further
        res << self[si].class.new(self[si].start, list[li].end)
        li += 1
      else
        # list[li] does overlap with self[si] but list[li] goes further
        res << self[si].class.new(self[si].start, self[si].end)
        si += 1
      end
    else
      # self[si].start and list[li].start are identical
      if self[si].end == list[li].end
        # self[si] and list[li] are identical. Add the Interval and
        # increase both pointers.
        res << self[si]
        li += 1
        si += 1
      elsif self[si].end < list[li].end
        # self[si] ends earlier.
        res << self[si]
        si += 1
      else
        # list[li] ends earlier.
        res << list[li]
        li += 1
      end
    end
  end

  res
end

#<<(iv) ⇒ Object

Append the Interval iv. If the start of iv matches the end of the list list item, iv is merged with the last item.



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/taskjuggler/IntervalList.rb', line 83

def <<(iv)
  if last
    if last.end > iv.start
      raise "Intervals may not overlap and must be added in " +
            "ascending order."
    elsif last.end == iv.start
      self[-1] = last.class.new(last.start, iv.end)
      return self
    end
  end

  append(iv)
end

#appendObject



22
# File 'lib/taskjuggler/IntervalList.rb', line 22

alias append <<