Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
|
#include <sorted_interval_list.h>
Classes | |
class | DomainIterator |
struct | DomainIteratorBeginEnd |
struct | DomainIteratorBeginEndWithOwnership |
Public Member Functions | |
Domain () | |
By default, Domain will be empty. | |
Domain (const Domain &other) | |
Copy constructor (mandatory as we define the move constructor). | |
Domain & | operator= (const Domain &other) |
Copy operator (mandatory as we define the move operator). | |
Domain (Domain &&other) noexcept | |
Move constructor. | |
Domain & | operator= (Domain &&other) noexcept |
Move operator. | |
Domain (int64_t value) | |
Constructor for the common case of a singleton domain. | |
Domain (int64_t left, int64_t right) | |
std::vector< int64_t > | FlattenedIntervals () const |
DomainIteratorBeginEnd | Values () const & |
DomainIteratorBeginEndWithOwnership | Values () const && |
bool | IsEmpty () const |
int64_t | Size () const |
bool | HasTwoValues () const |
int64_t | Min () const |
int64_t | Max () const |
int64_t | SmallestValue () const |
int64_t | ClosestValue (int64_t wanted) const |
int64_t | ValueAtOrBefore (int64_t input) const |
int64_t | ValueAtOrAfter (int64_t input) const |
Domain | PartAroundZero () const |
bool | IsFixed () const |
int64_t | FixedValue () const |
bool | Contains (int64_t value) const |
int64_t | Distance (int64_t value) const |
bool | IsIncludedIn (const Domain &domain) const |
Domain | Complement () const |
Domain | Negation () const |
Domain | IntersectionWith (const Domain &domain) const |
Domain | UnionWith (const Domain &domain) const |
Domain | AdditionWith (const Domain &domain) const |
Domain | MultiplicationBy (int64_t coeff, bool *exact=nullptr) const |
Domain | RelaxIfTooComplex () const |
Domain | ContinuousMultiplicationBy (int64_t coeff) const |
Domain | ContinuousMultiplicationBy (const Domain &domain) const |
Domain | DivisionBy (int64_t coeff) const |
Domain | InverseMultiplicationBy (int64_t coeff) const |
Domain | PositiveModuloBySuperset (const Domain &modulo) const |
Domain | PositiveDivisionBySuperset (const Domain &divisor) const |
Domain | SquareSuperset () const |
Domain | SimplifyUsingImpliedDomain (const Domain &implied_domain) const |
std::string | ToString () const |
bool | operator< (const Domain &other) const |
bool | operator== (const Domain &other) const |
bool | operator!= (const Domain &other) const |
int | NumIntervals () const |
ClosedInterval | front () const |
ClosedInterval | back () const |
ClosedInterval | operator[] (int i) const |
absl::InlinedVector< ClosedInterval, 1 >::const_iterator | begin () const |
absl::InlinedVector< ClosedInterval, 1 >::const_iterator | end () const |
Static Public Member Functions | |
static Domain | AllValues () |
static Domain | FromValues (std::vector< int64_t > values) |
static Domain | FromIntervals (absl::Span< const ClosedInterval > intervals) |
static Domain | FromFlatSpanOfIntervals (absl::Span< const int64_t > flat_intervals) |
static Domain | FromVectorIntervals (const std::vector< std::vector< int64_t > > &intervals) |
static Domain | FromFlatIntervals (const std::vector< int64_t > &flat_intervals) |
Friends | |
template<typename H > | |
H | AbslHashValue (H h, const Domain &domain) |
We call domain any subset of Int64 = [kint64min, kint64max].
This class can be used to represent such set efficiently as a sorted and non-adjacent list of intervals. This is efficient as long as the size of such list stays reasonable.
In the comments below, the domain of *this will always be written 'D'. Note that all the functions are safe with respect to integer overflow.
Definition at line 87 of file sorted_interval_list.h.
|
inline |
By default, Domain will be empty.
Definition at line 90 of file sorted_interval_list.h.
|
inline |
Copy constructor (mandatory as we define the move constructor).
Definition at line 94 of file sorted_interval_list.h.
|
inlinenoexcept |
Move constructor.
Definition at line 103 of file sorted_interval_list.h.
|
explicit |
Constructor for the common case of a singleton domain.
Definition at line 122 of file sorted_interval_list.cc.
operations_research::Domain::Domain | ( | int64_t | left, |
int64_t | right ) |
Constructor for the common case of a single interval [left, right]. If left > right, this will result in the empty domain.
Definition at line 139 of file sorted_interval_list.cc.
Returns {x ∈ Int64, ∃ a ∈ D, ∃ b ∈ domain, x = a + b}.
The sort is not needed if one of the list is of size 1.
Definition at line 428 of file sorted_interval_list.cc.
|
static |
Returns the full domain Int64.
Definition at line 144 of file sorted_interval_list.cc.
|
inline |
Definition at line 476 of file sorted_interval_list.h.
|
inline |
Definition at line 478 of file sorted_interval_list.h.
int64_t operations_research::Domain::ClosestValue | ( | int64_t | wanted | ) | const |
Returns the value closest to the given point. If there is a tie, pick larger one.
Definition at line 252 of file sorted_interval_list.cc.
Domain operations_research::Domain::Complement | ( | ) | const |
Returns the set Int64 ∖ D.
Definition at line 341 of file sorted_interval_list.cc.
bool operations_research::Domain::Contains | ( | int64_t | value | ) | const |
Returns true iff value is in Domain.
Because we only compare by start and there is no duplicate starts, this should be the next interval after the one that has a chance to contains value.
Definition at line 302 of file sorted_interval_list.cc.
Returns a superset of MultiplicationBy() to avoid the explosion in the representation size. This behaves as if we replace the set D of non-adjacent integer intervals by the set of floating-point elements in the same intervals.
For instance, [1, 100] * 2 will be transformed in [2, 200] and not in [2][4][6]...[200] like in MultiplicationBy(). Note that this would be similar to a InverseDivisionBy(), but not quite the same because if we look for {x ∈ Int64, ∃ e ∈ D, x / coeff = e}, then we will get [2, 201] in the case above.
Definition at line 514 of file sorted_interval_list.cc.
Domain operations_research::Domain::ContinuousMultiplicationBy | ( | int64_t | coeff | ) | const |
Returns a superset of MultiplicationBy() to avoid the explosion in the representation size. This behaves as if we replace the set D of non-adjacent integer intervals by the set of floating-point elements in the same intervals.
For instance, [1, 100] * 2 will be transformed in [2, 200] and not in [2][4][6]...[200] like in MultiplicationBy(). Note that this would be similar to a InverseDivisionBy(), but not quite the same because if we look for {x ∈ Int64, ∃ e ∈ D, x / coeff = e}, then we will get [2, 201] in the case above.
Definition at line 502 of file sorted_interval_list.cc.
int64_t operations_research::Domain::Distance | ( | int64_t | value | ) | const |
Returns the distance from the value to the domain.
Definition at line 314 of file sorted_interval_list.cc.
Domain operations_research::Domain::DivisionBy | ( | int64_t | coeff | ) | const |
Returns {x ∈ Int64, ∃ e ∈ D, x = e / coeff}.
For instance Domain(1, 7).DivisionBy(2) == Domain(0, 3).
Definition at line 533 of file sorted_interval_list.cc.
|
inline |
Definition at line 481 of file sorted_interval_list.h.
int64_t operations_research::Domain::FixedValue | ( | ) | const |
Returns the value of a fixed domain. IsFixed() must be true. This is the same as Min() or Max() but allows for a more readable code and also crash in debug mode if called on a non fixed domain.
Definition at line 297 of file sorted_interval_list.cc.
std::vector< int64_t > operations_research::Domain::FlattenedIntervals | ( | ) | const |
This method returns the flattened list of interval bounds of the domain.
Thus the domain {0, 1, 2, 5, 8, 9, 10} will return [0, 2, 5, 5, 8, 10] (as a C++ std::vector<int64_t>, as a java or C# long[], as a python list of integers).
Definition at line 689 of file sorted_interval_list.cc.
|
static |
This method is available in Python, Java and .NET. It allows building a Domain object from a flattened list of intervals (long[] in Java and .NET, [0, 2, 5, 5, 8, 10] in python).
Definition at line 183 of file sorted_interval_list.cc.
|
static |
Same as FromIntervals() for a flattened representation (start, end, start, end, ...).
Definition at line 170 of file sorted_interval_list.cc.
|
static |
Creates a domain from the union of an unsorted list of intervals.
Definition at line 162 of file sorted_interval_list.cc.
|
static |
Creates a domain from the union of an unsorted list of integer values. Input values may be repeated, with no consequence on the output
Definition at line 146 of file sorted_interval_list.cc.
|
static |
This method is available in Python, Java and .NET. It allows building a Domain object from a list of intervals (long[][] in Java and .NET, [[0, 2], [5, 5], [8, 10]] in python).
Definition at line 187 of file sorted_interval_list.cc.
|
inline |
Definition at line 475 of file sorted_interval_list.h.
|
inline |
Returns true if the domain has just two values. This often mean a non-fixed Boolean variable.
Definition at line 239 of file sorted_interval_list.h.
Returns the intersection of D and domain.
Empty intersection. We advance past the first interval.
Non-empty intersection: push back the intersection of these two, and advance past the first interval to finish.
We do the exact same thing as above, but swapping a and b.
Definition at line 378 of file sorted_interval_list.cc.
Domain operations_research::Domain::InverseMultiplicationBy | ( | int64_t | coeff | ) | const |
Returns {x ∈ Int64, ∃ e ∈ D, x * coeff = e}.
For instance Domain(1, 7).InverseMultiplicationBy(2) == Domain(1, 3).
Definition at line 546 of file sorted_interval_list.cc.
bool operations_research::Domain::IsEmpty | ( | ) | const |
Returns true if this is the empty set.
Definition at line 203 of file sorted_interval_list.cc.
bool operations_research::Domain::IsFixed | ( | ) | const |
Returns true iff the domain is reduced to a single value. The domain must not be empty.
Definition at line 205 of file sorted_interval_list.cc.
bool operations_research::Domain::IsIncludedIn | ( | const Domain & | domain | ) | const |
Returns true iff D is included in the given domain.
Find the unique interval in others that contains interval if any.
Definition at line 328 of file sorted_interval_list.cc.
int64_t operations_research::Domain::Max | ( | ) | const |
Returns the max value of the domain. The domain must not be empty.
Definition at line 224 of file sorted_interval_list.cc.
int64_t operations_research::Domain::Min | ( | ) | const |
Returns the min value of the domain. The domain must not be empty.
Definition at line 219 of file sorted_interval_list.cc.
Domain operations_research::Domain::MultiplicationBy | ( | int64_t | coeff, |
bool * | exact = nullptr ) const |
Returns {x ∈ Int64, ∃ e ∈ D, x = e * coeff}.
Note that because the resulting domain will only contains multiple of coeff, the size of intervals.size() can become really large. If it is larger than a fixed constant, exact will be set to false and the result will be set to ContinuousMultiplicationBy(coeff).
Note that if you multiply by a negative coeff, kint64min will be dropped from the result even if it was here due to how this is implemented.
We ignore anything that overflow.
Because abs_coeff > 1, all new values are disjoint.
This is to avoid doing ++v when v is kint64max!
Definition at line 465 of file sorted_interval_list.cc.
Domain operations_research::Domain::Negation | ( | ) | const |
Returns {x ∈ Int64, ∃ e ∈ D, x = -e}.
Note in particular that if the negation of Int64 is not Int64 but Int64 \ {kint64min} !!
Definition at line 357 of file sorted_interval_list.cc.
|
inline |
Basic read-only std::vector<> wrapping to view a Domain as a sorted list of non-adjacent intervals. Note that we don't expose size() which might be confused with the number of values in the domain.
Definition at line 474 of file sorted_interval_list.h.
|
inline |
Definition at line 460 of file sorted_interval_list.h.
bool operations_research::Domain::operator< | ( | const Domain & | other | ) | const |
Lexicographic order on the intervals() representation.
Definition at line 698 of file sorted_interval_list.cc.
Copy operator (mandatory as we define the move operator).
Definition at line 97 of file sorted_interval_list.h.
Move operator.
Definition at line 106 of file sorted_interval_list.h.
|
inline |
Definition at line 456 of file sorted_interval_list.h.
|
inline |
Definition at line 477 of file sorted_interval_list.h.
Domain operations_research::Domain::PartAroundZero | ( | ) | const |
If the domain contains zero, this return the simple interval around it. Otherwise, this returns an empty domain.
Definition at line 242 of file sorted_interval_list.cc.
Returns a superset of {x ∈ Int64, ∃ e ∈ D, ∃ d ∈ divisor, x = e / d }.
We check that divisor is strictly positive. For now we just intersect with the min/max possible value.
Definition at line 604 of file sorted_interval_list.cc.
Returns a superset of {x ∈ Int64, ∃ e ∈ D, ∃ m ∈ modulo, x = e % m }.
We check that modulo is strictly positive. The sign of the modulo depends on the sign of e. We compute the exact min/max if the modulo is fixed, otherwise we will just return a superset.
Definition at line 590 of file sorted_interval_list.cc.
Domain operations_research::Domain::RelaxIfTooComplex | ( | ) | const |
If NumIntervals() is too large, this return a superset of the domain.
Definition at line 457 of file sorted_interval_list.cc.
Domain operations_research::Domain::SimplifyUsingImpliedDomain | ( | const Domain & | implied_domain | ) | const |
Advanced usage. Given some implied information on this domain that is assumed to be always true (i.e. only values in the intersection with implied domain matter), this function will simplify the current domain without changing the set of "possible values".
More precisely, this will:
Note that domain.SimplifyUsingImpliedDomain(domain) will just return [domain.Min(), domain.Max()]. This is meant to be applied to the right-hand side of a constraint to make its propagation more efficient.
It is a bit difficult to see, but this code is doing the same thing as for all interval in this.UnionWith(implied_domain.Complement())):
We only "close" the new result interval if it cannot be extended by implied_domain.Complement(). The only extension possible look like: interval_: ...] [.... implied : ...] [... i ...]
Find the two extreme points in interval \inter implied_domain. Always stop the loop at the first interval with and end strictly greater that interval.end.
Current and interval have a non-empty intersection.
No need to update the min_point here, and the new inter_max must necessarily be > old one.
Definition at line 641 of file sorted_interval_list.cc.
int64_t operations_research::Domain::Size | ( | ) | const |
Returns the number of elements in the domain. It is capped at kint64max
Because the intervals are closed on both side above, with miss 1 per interval.
Definition at line 207 of file sorted_interval_list.cc.
int64_t operations_research::Domain::SmallestValue | ( | ) | const |
Returns the value closest to zero. If there is a tie, pick positive one.
Definition at line 229 of file sorted_interval_list.cc.
Domain operations_research::Domain::SquareSuperset | ( | ) | const |
Returns a superset of {x ∈ Int64, ∃ y ∈ D, x = y * y }.
Definition at line 611 of file sorted_interval_list.cc.
std::string operations_research::Domain::ToString | ( | ) | const |
Returns a compact string of a vector of intervals like "[1,4][6][10,20]".
Definition at line 713 of file sorted_interval_list.cc.
Returns the union of D and domain.
Definition at line 417 of file sorted_interval_list.cc.
int64_t operations_research::Domain::ValueAtOrAfter | ( | int64_t | input | ) | const |
Because we only compare by start and there is no duplicate starts, this should be the next interval after the one that has a chance to contains value.
Definition at line 284 of file sorted_interval_list.cc.
int64_t operations_research::Domain::ValueAtOrBefore | ( | int64_t | input | ) | const |
Returns the closest value in the domain that is <= (resp. >=) to the input. Do not change the input if there is no such value.
Because we only compare by start and there is no duplicate starts, this should be the next interval after the one that has a chance to contains value.
Definition at line 273 of file sorted_interval_list.cc.
|
inline |
Definition at line 219 of file sorted_interval_list.h.
|
inline |
Definition at line 220 of file sorted_interval_list.h.
|
friend |
Definition at line 465 of file sorted_interval_list.h.