Class: Roby::EventStructure::DisjointIntervalSet

Inherits:
Object
  • Object
show all
Defined in:
lib/roby/event_structure/temporal_constraints.rb

Overview

A representation of a set of disjoint intervals, sorted in increasing order

Direct Known Subclasses

TemporalConstraintSet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDisjointIntervalSet

Returns a new instance of DisjointIntervalSet.



138
139
140
# File 'lib/roby/event_structure/temporal_constraints.rb', line 138

def initialize
    @intervals = Array.new
end

Instance Attribute Details

#intervalsObject (readonly)

A list of intervals as [min, max]. The list is sorted in increasing order



136
137
138
# File 'lib/roby/event_structure/temporal_constraints.rb', line 136

def intervals
  @intervals
end

Instance Method Details

#add(min, max) ⇒ Object

Adds a new interval to the set, merging it with existing intervals if needed

Returns self



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# 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

#boundariesObject

Returns the lower and upper bound of the union of all intervals



150
151
152
# File 'lib/roby/event_structure/temporal_constraints.rb', line 150

def boundaries
    [intervals.first[0], intervals.last[1]]
end

#include?(value) ⇒ Boolean

Returns true if value is included in one of the intervals

Returns:

  • (Boolean)


143
144
145
146
147
# 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