14 #ifndef OR_TOOLS_UTIL_SORTED_INTERVAL_LIST_H_
15 #define OR_TOOLS_UTIL_SORTED_INTERVAL_LIST_H_
24 #include "absl/container/inlined_vector.h"
25 #include "absl/types/span.h"
37 DLOG_IF(DFATAL, s > e) <<
"Invalid ClosedInterval(" << s <<
", " << e
59 const std::vector<ClosedInterval>& intervals);
70 absl::Span<const ClosedInterval> intervals);
93 intervals_ = other.intervals_;
98 Domain(
Domain&& other) : intervals_(std::move(other.intervals_)) {}
102 intervals_ = std::move(other.intervals_);
114 Domain(int64_t left, int64_t right);
137 absl::Span<const int64_t> flat_intervals);
145 const std::vector<std::vector<int64_t> >&
intervals);
174 const absl::InlinedVector<ClosedInterval, 1>&
intervals)
182 if (value_ == it_->end) {
184 if (it_ != end_) value_ = it_->start;
191 absl::InlinedVector<ClosedInterval, 1>::const_iterator
end)
const {
197 absl::InlinedVector<ClosedInterval, 1>::const_iterator it_;
198 absl::InlinedVector<ClosedInterval, 1>::const_iterator end_;
202 absl::InlinedVector<ClosedInterval, 1>::const_iterator
end()
const {
205 const absl::InlinedVector<ClosedInterval, 1>&
intervals;
209 absl::InlinedVector<ClosedInterval, 1>::const_iterator
end()
const {
216 return {std::move(this->intervals_)};
228 int64_t
Size()
const;
421 return intervals_ == other.intervals_;
425 return intervals_ != other.intervals_;
437 absl::InlinedVector<ClosedInterval, 1>::const_iterator
begin()
const {
438 return intervals_.begin();
440 absl::InlinedVector<ClosedInterval, 1>::const_iterator
end()
const {
441 return intervals_.end();
449 return {intervals_.begin(), intervals_.end()};
454 void NegateInPlace();
458 static constexpr
int kDomainComplexityLimit = 100;
465 absl::InlinedVector<ClosedInterval, 1> intervals_;
468 std::ostream&
operator<<(std::ostream& out,
const Domain& domain);
488 return a.start !=
b.start ?
a.start <
b.start :
a.end <
b.end;
497 const std::vector<ClosedInterval>& intervals);
507 const std::vector<int64_t>& ends);
509 const std::vector<int>& ends);
546 const std::vector<int64_t>& ends);
548 const std::vector<int>& ends);
586 void clear() { intervals_.clear(); }
588 intervals_.swap(other.intervals_);
593 void InsertAll(
const std::vector<T>& starts,
const std::vector<T>& ends);
Allows to iterate over all values of a domain in order with for (const int64_t v : domain....
bool operator!=(absl::InlinedVector< ClosedInterval, 1 >::const_iterator end) const
int64_t operator*() const
DomainIterator(const absl::InlinedVector< ClosedInterval, 1 > &intervals)
We call domain any subset of Int64 = [kint64min, kint64max].
static Domain AllValues()
Returns the full domain Int64.
Domain(Domain &&other)
Move constructor.
Domain InverseMultiplicationBy(const int64_t coeff) const
Returns {x ∈ Int64, ∃ e ∈ D, x * coeff = e}.
std::string ToString() const
Returns a compact string of a vector of intervals like "[1,4][6][10,20]".
Domain Negation() const
Returns {x ∈ Int64, ∃ e ∈ D, x = -e}.
Domain Complement() const
Returns the set Int64 ∖ D.
bool IsIncludedIn(const Domain &domain) const
Returns true iff D is included in the given domain.
bool Contains(int64_t value) const
Returns true iff value is in Domain.
Domain ContinuousMultiplicationBy(int64_t coeff) const
Returns a superset of MultiplicationBy() to avoid the explosion in the representation size.
Domain & operator=(Domain &&other)
Move operator.
static Domain FromFlatSpanOfIntervals(absl::Span< const int64_t > flat_intervals)
Same as FromIntervals() for a flattened representation (start, end, start, end, .....
absl::InlinedVector< ClosedInterval, 1 >::const_iterator end() const
DomainIteratorBeginEndWithOwnership Values() const &&
int NumIntervals() const
Basic read-only std::vector<> wrapping to view a Domain as a sorted list of non-adjacent intervals.
ClosedInterval back() const
int64_t FixedValue() const
Returns the value of a fixed domain.
bool operator<(const Domain &other) const
Lexicographic order on the intervals() representation.
Domain AdditionWith(const Domain &domain) const
Returns {x ∈ Int64, ∃ a ∈ D, ∃ b ∈ domain, x = a + b}.
ClosedInterval front() const
int64_t Size() const
Returns the number of elements in the domain.
bool operator!=(const Domain &other) const
Domain UnionWith(const Domain &domain) const
Returns the union of D and domain.
ClosedInterval operator[](int i) const
Domain MultiplicationBy(int64_t coeff, bool *exact=nullptr) const
Returns {x ∈ Int64, ∃ e ∈ D, x = e * coeff}.
static Domain FromFlatIntervals(const std::vector< int64_t > &flat_intervals)
This method is available in Python, Java and .NET.
std::vector< int64_t > FlattenedIntervals() const
This method returns the flattened list of interval bounds of the domain.
bool IsFixed() const
Returns true iff the domain is reduced to a single value.
Domain IntersectionWith(const Domain &domain) const
Returns the intersection of D and domain.
static Domain FromVectorIntervals(const std::vector< std::vector< int64_t > > &intervals)
This method is available in Python, Java and .NET.
int64_t Min() const
Returns the min value of the domain.
bool IsEmpty() const
Returns true if this is the empty set.
Domain PositiveDivisionBySuperset(const Domain &divisor) const
Returns a superset of {x ∈ Int64, ∃ e ∈ D, ∃ d ∈ divisor, x = e / d }.
std::vector< ClosedInterval > intervals() const
static Domain FromIntervals(absl::Span< const ClosedInterval > intervals)
Creates a domain from the union of an unsorted list of intervals.
bool operator==(const Domain &other) const
Domain()
By default, Domain will be empty.
int64_t SmallestValue() const
Returns the value closest to zero.
Domain(const Domain &other)
Copy constructor (mandatory as we define the move constructor).
int64_t Max() const
Returns the max value of the domain.
Domain RelaxIfTooComplex() const
If NumIntervals() is too large, this return a superset of the domain.
static Domain FromValues(std::vector< int64_t > values)
Creates a domain from the union of an unsorted list of integer values.
Domain SquareSuperset() const
Returns a superset of {x ∈ Int64, ∃ y ∈ D, x = y * y }.
absl::InlinedVector< ClosedInterval, 1 >::const_iterator begin() const
Domain & operator=(const Domain &other)
Copy operator (mandatory as we define the move operator).
Domain DivisionBy(int64_t coeff) const
Returns {x ∈ Int64, ∃ e ∈ D, x = e / coeff}.
DomainIteratorBeginEnd Values() const &
int64_t ValueAtOrAfter(int64_t input) const
Domain PositiveModuloBySuperset(const Domain &modulo) const
Returns a superset of {x ∈ Int64, ∃ e ∈ D, ∃ m ∈ modulo, x = e % m }.
Domain SimplifyUsingImpliedDomain(const Domain &implied_domain) const
Advanced usage.
int64_t ValueAtOrBefore(int64_t input) const
Returns the closest value in the domain that is <= (resp.
This class represents a sorted list of disjoint, closed intervals.
Iterator GrowRightByOne(int64_t value, int64_t *newly_covered)
If value is in an interval, increase its end by one, otherwise insert the interval [value,...
const ClosedInterval & last() const
Returns a const& to the last interval.
void InsertIntervals(const std::vector< int64_t > &starts, const std::vector< int64_t > &ends)
Adds all intervals [starts[i]..ends[i]].
int NumIntervals() const
Returns the number of disjoint intervals in the list.
IntervalSet::const_iterator ConstIterator
Iterator InsertInterval(int64_t start, int64_t end)
Adds the interval [start..end] to the list, and merges overlapping or immediately adjacent intervals ...
Iterator LastIntervalLessOrEqual(int64_t value) const
ConstIterator end() const
void swap(SortedDisjointIntervalList &other)
std::string DebugString() const
Iterator FirstIntervalGreaterOrEqual(int64_t value) const
Returns an iterator to either:
IntervalSet::iterator Iterator
std::set< ClosedInterval, IntervalComparator > IntervalSet
ConstIterator begin() const
Const iterators for SortedDisjoinIntervalList.
SortedDisjointIntervalList BuildComplementOnInterval(int64_t start, int64_t end)
Builds the complement of the interval list on the interval [start, end].
SortedDisjointIntervalList()
Collection of objects used to extend the Constraint Solver library.
int64_t SumOfKMinValueInDomain(const Domain &domain, int k)
std::ostream & operator<<(std::ostream &out, const Assignment &assignment)
int64_t SumOfKMaxValueInDomain(const Domain &domain, int k)
bool IntervalsAreSortedAndNonAdjacent(absl::Span< const ClosedInterval > intervals)
Returns true iff we have:
static int input(yyscan_t yyscanner)
Represents a closed interval [start, end].
bool operator==(const ClosedInterval &other) const
std::string DebugString() const
bool operator<(const ClosedInterval &other) const
ClosedInterval(int64_t s, int64_t e)
absl::InlinedVector< ClosedInterval, 1 >::const_iterator end() const
const absl::InlinedVector< ClosedInterval, 1 > & intervals
DomainIterator begin() const
absl::InlinedVector< ClosedInterval, 1 >::const_iterator end() const
absl::InlinedVector< ClosedInterval, 1 > intervals
DomainIterator begin() const
bool operator()(const ClosedInterval &a, const ClosedInterval &b) const