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.



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

def initialize
    @intervals = []
end

Instance Attribute Details

#intervalsObject (readonly)

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



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

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



157
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
# File 'lib/roby/event_structure/temporal_constraints.rb', line 157

def add(min, max)
    if intervals.empty?
        intervals << [min, max]
        return
    end

    new_list = []
    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



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

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)


142
143
144
145
146
# File 'lib/roby/event_structure/temporal_constraints.rb', line 142

def include?(value)
    candidate = intervals
        .find { |min, max| max >= value }
    candidate && (candidate[0] <= value)
end