8 #ifndef META_OCEAN_BASE_SEGMENT_UNION_H
9 #define META_OCEAN_BASE_SEGMENT_UNION_H
54 void addSegment(
const T& startPosition,
const T& stopPosition);
92 explicit inline operator bool()
const;
108 template <
typename T>
111 ocean_assert(isCorrect());
113 ocean_assert(startPosition < stopPosition);
118 if (segmentMap_.empty())
120 segmentMap_.insert(std::make_pair(startPosition, stopPosition));
121 ocean_assert(isCorrect());
126 typename SegmentMap::iterator i = segmentMap_.lower_bound(startPosition);
128 if (i == segmentMap_.end())
130 ocean_assert(segmentMap_.rbegin() != segmentMap_.rend());
132 typename SegmentMap::reverse_iterator lastSegment = segmentMap_.rbegin();
134 if (startPosition > lastSegment->second)
139 segmentMap_.insert(std::make_pair(startPosition, stopPosition));
140 ocean_assert(isCorrect());
145 if (startPosition == lastSegment->second)
150 lastSegment->second = stopPosition;
151 ocean_assert(isCorrect());
156 ocean_assert(startPosition > lastSegment->first);
157 ocean_assert(startPosition < lastSegment->second);
159 if (stopPosition <= lastSegment->second)
168 ocean_assert(stopPosition > lastSegment->second);
173 lastSegment->second = stopPosition;
174 ocean_assert(isCorrect());
179 ocean_assert(startPosition <= i->first);
181 if (i == segmentMap_.begin())
183 if (stopPosition < i->first)
188 segmentMap_.insert(std::make_pair(startPosition, stopPosition));
189 ocean_assert(isCorrect());
194 if (stopPosition == i->first)
199 const T newStopPosition = i->second;
201 segmentMap_.erase(i);
203 segmentMap_.insert(std::make_pair(startPosition, newStopPosition));
205 ocean_assert(isCorrect());
210 ocean_assert(stopPosition > i->first);
212 typename SegmentMap::iterator iEnd = i;
214 while (iEnd != segmentMap_.end())
216 if (stopPosition < iEnd->first)
222 segmentMap_.erase(i, iEnd);
224 segmentMap_.insert(std::make_pair(startPosition, stopPosition));
226 ocean_assert(isCorrect());
230 else if (stopPosition < iEnd->second)
236 const T newStopPostion = iEnd->second;
238 segmentMap_.erase(i, ++iEnd);
240 segmentMap_.insert(std::make_pair(startPosition, newStopPostion));
242 ocean_assert(isCorrect());
253 segmentMap_.erase(i, segmentMap_.end());
255 segmentMap_.insert(std::make_pair(startPosition, stopPosition));
257 ocean_assert(isCorrect());
273 if (stopPosition <= i->second)
281 if (startPosition <= i->second)
283 typename SegmentMap::iterator iEnd = i;
285 while (++iEnd != segmentMap_.end())
287 if (stopPosition < iEnd->first)
293 i->second = stopPosition;
295 segmentMap_.erase(++i, iEnd);
297 ocean_assert(isCorrect());
301 else if (stopPosition < iEnd->second)
307 i->second = iEnd->second;
309 segmentMap_.erase(++i, ++iEnd);
311 ocean_assert(isCorrect());
320 i->second = stopPosition;
322 segmentMap_.erase(++i, segmentMap_.end());
324 ocean_assert(isCorrect());
329 ocean_assert(startPosition > i->second);
331 typename SegmentMap::iterator iEnd = i;
333 while (++iEnd != segmentMap_.end())
335 if (stopPosition < iEnd->first)
342 segmentMap_.erase(++i, iEnd);
344 segmentMap_.insert(std::make_pair(startPosition, stopPosition));
346 ocean_assert(isCorrect());
350 else if (stopPosition < iEnd->second)
356 const T newStopPosition = iEnd->second;
358 segmentMap_.erase(++i, ++iEnd);
360 segmentMap_.insert(std::make_pair(startPosition, newStopPosition));
362 ocean_assert(isCorrect());
371 segmentMap_.erase(++i, segmentMap_.end());
373 segmentMap_.insert(std::make_pair(startPosition, stopPosition));
375 ocean_assert(isCorrect());
378 template <
typename T>
381 ocean_assert(isCorrect());
382 ocean_assert(startPosition < stopPosition);
388 if (segmentMap_.empty())
393 typename SegmentMap::const_iterator i = segmentMap_.lower_bound(startPosition);
395 if (i == segmentMap_.end())
397 ocean_assert(segmentMap_.rbegin() != segmentMap_.rend());
399 typename SegmentMap::const_reverse_iterator lastSegment = segmentMap_.crbegin();
401 if (startPosition >= lastSegment->second)
410 ocean_assert(startPosition > lastSegment->first);
411 ocean_assert(startPosition < lastSegment->second);
418 const T newStartPosition = std::max(lastSegment->first, startPosition);
419 const T newStopPosition = std::min(stopPosition, lastSegment->second);
422 result.
segmentMap_.insert(std::make_pair(newStartPosition, newStopPosition));
429 ocean_assert(startPosition <= i->first);
431 if (i == segmentMap_.begin())
433 if (stopPosition <= i->first)
442 ocean_assert(stopPosition > i->first);
460 if (stopPosition <= i->second)
467 const T newStartPosition = std::max(i->first, startPosition);
468 const T newStopPosition = std::min(stopPosition, i->second);
471 result.
segmentMap_.insert(std::make_pair(newStartPosition, newStopPosition));
478 if (startPosition >= i->second)
483 ocean_assert(startPosition < i->second);
485 if (stopPosition <= i->first)
493 typename SegmentMap::const_iterator iEnd = i;
495 while (++iEnd != segmentMap_.end())
497 if (stopPosition <= iEnd->first)
506 else if (stopPosition <= iEnd->second)
519 const T newStartPosition = std::max(startPosition, i->first);
520 result.
segmentMap_.insert(std::make_pair(newStartPosition, i->second));
524 for (
typename SegmentMap::const_iterator iN = i; iN != iEnd; ++iN)
526 result.
segmentMap_.insert(std::make_pair(iN->first, iN->second));
536 template <
typename T>
539 ocean_assert(isCorrect());
541 if (segmentMap_.size() <= 1)
546 typename SegmentMap::const_iterator left = segmentMap_.cbegin();
547 typename SegmentMap::const_iterator right = left;
550 T maximalValue = right->first - left->second;
551 ocean_assert(maximalValue > T(0));
553 while (++right != segmentMap_.cend())
557 const T value = right->first - left->second;
558 ocean_assert(value > T(0));
560 if (value > maximalValue)
562 maximalValue = value;
569 template <
typename T>
572 ocean_assert(isCorrect());
577 template <
typename T>
580 ocean_assert(isCorrect());
584 for (
typename SegmentMap::const_iterator i = segmentMap_.cbegin(); i != segmentMap_.cend(); ++i)
586 ocean_assert(i->first < i->second);
588 sum += i->second - i->first;
594 template <
typename T>
597 ocean_assert(isCorrect());
599 return !segmentMap_.empty();
602 template <
typename T>
605 T previousEnd = std::numeric_limits<T>::lowest();
607 for (
typename SegmentMap::const_iterator i = segmentMap_.cbegin(); i != segmentMap_.cend(); ++i)
609 if (i->first <= previousEnd)
614 previousEnd = i->second;
This class implements a functionality to determine the union of individual segments.
Definition: SegmentUnion.h:39
const SegmentMap & segments() const
Returns the segments of this object.
Definition: SegmentUnion.h:570
std::map< T, T > SegmentMap
Definition of a map mapping a start point to the corresponding end point of a segment.
Definition: SegmentUnion.h:45
T maximalGap() const
Returns the maximal gap between all successive segments.
Definition: SegmentUnion.h:537
bool isCorrect() const
Returns whether the data structure of this object is correct.
Definition: SegmentUnion.h:603
SegmentMap segmentMap_
The map holding all segments.
Definition: SegmentUnion.h:105
SegmentUnion< T > intersection(const T &startPosition, const T &stopPosition) const
Returns the intersection of this union with a given range (an additional segment).
Definition: SegmentUnion.h:379
void addSegment(const T &startPosition, const T &stopPosition)
Adds a new segment to this union object.
Definition: SegmentUnion.h:109
T unionSize() const
Returns the sum of all segment sizes.
Definition: SegmentUnion.h:578
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15