5 #ifndef BPP_NUMERIC_RANGE_H 6 #define BPP_NUMERIC_RANGE_H 9 #include "../Clonable.h" 10 #include "../Text/TextTools.h" 46 Range(
const T& a = 0,
const T& b = 0) :
47 begin_(
std::min(a, b)),
51 Range(
const Range<T>& range) : begin_(range.begin_), end_(range.end_) {}
73 bool operator<(const Range<T>& r)
const 75 return begin_ < r.begin_ || end_ < r.end_;
163 if (r.
end_ < end_ && r.
end_ >= begin_)
194 virtual void addRange(
const Range<T>& r) = 0;
203 virtual void restrictTo(
const Range<T>& r) = 0;
210 virtual void filterWithin(
const Range<T>& r) = 0;
215 virtual std::string
toString()
const = 0;
220 virtual bool isEmpty()
const = 0;
225 virtual size_t size()
const = 0;
230 virtual size_t totalLength()
const = 0;
235 virtual const Range<T>& getRange(
size_t i)
const = 0;
240 virtual void clear() = 0;
273 for (
const auto& it :
set.ranges_)
275 ranges_.push_back(it->clone());
282 for (
const auto& it :
set.ranges_)
284 ranges_.push_back(it->clone());
298 ranges_.push_back(r.
clone());
303 auto it = ranges_.begin();
304 while (it != ranges_.end())
307 if ((**it).isEmpty())
310 it = ranges_.erase(it);
321 auto it = ranges_.begin();
322 while (it != ranges_.end())
331 it = ranges_.erase(it);
338 std::string s =
"{ ";
339 for (
const auto& it : ranges_)
341 s += it->toString() +
" ";
347 bool isEmpty()
const {
return ranges_.size() == 0; }
349 size_t size()
const {
return ranges_.size(); }
357 for (
const auto& it : ranges_)
369 const std::vector< Range<T>* >&
getSet()
const {
return ranges_; }
371 std::vector< Range<T>* >&
getSet() {
return ranges_; }
381 for (
auto it = ranges_.begin(); it != ranges_.end(); ++it)
403 for (
size_t i = 0; i < mr.
ranges_.size(); ++i)
405 ranges_.push_back(mr.
ranges_[i]->clone());
412 for (
size_t i = 0; i < mr.
ranges_.size(); ++i)
414 ranges_.push_back(mr.
ranges_[i]->clone());
428 std::vector<size_t> overlappingPositions;
429 for (
size_t i = 0; i < ranges_.size(); ++i)
432 overlappingPositions.push_back(i);
435 if (overlappingPositions.size() == 0)
438 ranges_.push_back(r.
clone());
443 ranges_[overlappingPositions[0]]->expandWith(r);
445 for (
size_t i = overlappingPositions.size() - 1; i > 0; --i)
448 ranges_[overlappingPositions[0]]->expandWith(*ranges_[overlappingPositions[i]]);
450 delete ranges_[overlappingPositions[i]];
451 ranges_.erase(ranges_.begin() +
static_cast<ptrdiff_t
>(overlappingPositions[i]));
459 for (
auto& it : ranges_)
468 auto it = ranges_.begin();
469 while (it != ranges_.end())
478 it = ranges_.erase(it);
488 std::string s =
"{ ";
489 for (
const auto& it : ranges_)
491 s += it->toString() +
" ";
502 std::vector<T> bounds;
503 for (
const auto& it : ranges_)
505 bounds.push_back(it->begin());
506 bounds.push_back(it->end());
514 bool isEmpty()
const {
return ranges_.size() == 0; }
516 size_t size()
const {
return ranges_.size(); }
521 for (
const auto& it : ranges_)
540 std::sort(ranges_.begin(), ranges_.end(),
comp);
542 auto it = ranges_.begin();
543 while (it != ranges_.end())
545 if ((**it).isEmpty())
548 it = ranges_.erase(it);
560 for (
size_t i = 0; i < ranges_.size(); ++i)
568 #endif // BPP_NUMERIC_RANGE_H void expandWith(const Range &r)
Expand the current interval with the given one.
RangeSet & operator=(const RangeSet< T > &set)
Interface discribing a collection of Range objects.
Range(const T &a=0, const T &b=0)
Creates a new interval.
MultiRange(const MultiRange< T > &mr)
std::vector< T > getBounds() const
virtual Range & operator-=(const T &val)
The Range class, defining an interval.
Range< T > * clone() const
Create a copy of this object and send a pointer to it.
const Range< T > & getRange(size_t i) const
const std::vector< Range< T > *> & getSet() const
This class implements a data structure describing a set of non-overlapping intervals.
std::vector< Range< T > *> & getSet()
void filterWithin(const Range< T > &r)
Only keep the ranges that fall within the given range.
std::vector< Range< T > *> ranges_
void clear()
Clear the collection.
MultiRange & operator=(const MultiRange< T > &mr)
void filterWithin(const Range< T > &r)
Only keep the ranges that fall within the given range.
Range(const Range< T > &range)
virtual Range operator+(const T &val)
void sliceWith(const Range &r)
Restrict the current interval to the intersection with the given one.
std::vector< Range< T > * > ranges_
std::string toString() const
A special class used inside RangeCollection.
std::string toString() const
std::string toString() const
size_t totalLength() const
Range< T > & operator=(const Range< T > &range)
void clear()
Clear the collection.
bool operator==(const Range< T > &r) const
bool isContiguous(const Range &r) const
bool contains(const Range &r) const
The Clonable interface (allow an object to be cloned).
virtual ~RangeCollection()
bool operator()(const Range< T > *a, const Range< T > *b) const
void addRange(const Range< T > &r)
Add a new range to the collection.
This class implements a data structure describing a set of intervals.
virtual Range operator-(const T &val)
void restrictTo(const Range< T > &r)
Get the intersection with a given range.
std::string toString(T t)
General template method to convert to a string.
const Range< T > & getRange(size_t i) const
size_t totalLength() const
bool operator!=(const Range< T > &r) const
RangeSet(const RangeSet< T > &set)
void restrictTo(const Range< T > &r)
Get the intersection with a given range.
virtual Range & operator+=(const T &val)
void addRange(const Range< T > &r)
Add a new range to the collection.
bool overlap(const Range &r) const