class Roby::EventStructure::DisjointIntervalSet
A representation of a set of disjoint intervals, sorted in increasing order
Attributes
intervals[R]
A list of intervals as [min, max]. The list is sorted in increasing order
Public Class Methods
new()
click to toggle source
# File lib/roby/event_structure/temporal_constraints.rb, line 138 def initialize @intervals = Array.new end
Public Instance Methods
add(min, max)
click to toggle source
Adds a new interval to the set, merging it with existing intervals if needed
Returns self
# File lib/roby/event_structure/temporal_constraints.rb, line 158 def add(min, max) if intervals.empty? intervals << [min, max] return end new_list = Array.new while interval = intervals.shift if interval[1] < min new_list << interval elsif interval[0] > min if interval[0] > max new_list << [min, max] << interval break else new_list << [min, [max, interval[1]].max] end break else new_list << [interval[0], [max, interval[1]].max] break end end if intervals.empty? && new_list.last[1] < min new_list << [min, max] elsif new_list.last[1] <= max while interval = intervals.shift last_interval = new_list.last # It is guaranteed that interval[0] > last_interval[0]. # We therefore only need to check if interval[0] is # included in last_interval if interval[0] <= last_interval[1] if last_interval[1] < interval[1] last_interval[1] = interval[1] break end else new_list << interval break end end end # We now know that the last interval in new_list has an upper # bound that comes from an already existing interval. We are # therefore sure that there are no overlaps. new_list.concat(intervals) @intervals = new_list self end
boundaries()
click to toggle source
Returns the lower and upper bound of the union of all intervals
# File lib/roby/event_structure/temporal_constraints.rb, line 150 def boundaries [intervals.first[0], intervals.last[1]] end
include?(value)
click to toggle source
Returns true if value
is included in one of the intervals
# File lib/roby/event_structure/temporal_constraints.rb, line 143 def include?(value) candidate = intervals. find { |min, max| max >= value } candidate && (candidate[0] <= value) end