23#include "absl/container/btree_set.h"
24#include "absl/strings/str_format.h"
36int FindSegmentIndex(
const std::vector<PiecewiseSegment>& segments, int64_t
x) {
37 if (segments.empty() || segments.front().start_x() >
x) {
43 std::vector<PiecewiseSegment>::const_iterator position = std::upper_bound(
45 if (position == segments.end()) {
46 return segments.size() - 1;
48 position -= position->start_x() >
x ? 1 : 0;
50 return position - segments.begin();
53inline bool IsAtBounds(int64_t
value) {
57inline bool PointInsideRange(int64_t point, int64_t range_start,
59 return range_start <= point && range_end >= point;
64inline bool FormConvexPair(
const PiecewiseSegment& left,
65 const PiecewiseSegment& right) {
66 return right.slope() >= left.slope() && right.start_x() == left.end_x() &&
67 right.start_y() == left.end_y();
70uint64_t UnsignedCapAdd(uint64_t left, uint64_t right) {
74uint64_t UnsignedCapProd(uint64_t left, uint64_t right) {
75 if (right == 0)
return 0;
82 int64_t slope, int64_t other_point_x)
83 : slope_(slope), reference_x_(point_x), reference_y_(point_y) {
84 start_x_ = std::min(point_x, other_point_x);
85 end_x_ = std::max(point_x, other_point_x);
87 reference_x_ < 0 ? SafeValuePostReference(0) : SafeValuePreReference(0);
91 CHECK_GE(
x, start_x_);
94 const int64_t span_x =
CapSub(
x, reference_x_);
97 return SafeValuePostReference(
x);
100 return SafeValuePreReference(
x);
103 const int64_t span_y =
CapProd(slope_, span_x);
104 if (IsAtBounds(span_y)) {
106 return SafeValuePostReference(
x);
108 return SafeValuePreReference(
x);
112 const int64_t
value =
CapAdd(reference_y_, span_y);
113 if (IsAtBounds(
value)) {
115 return SafeValuePostReference(
x);
117 return SafeValuePreReference(
x);
124int64_t PiecewiseSegment::SafeValuePostReference(int64_t
x)
const {
125 DCHECK_GE(
x, reference_x_);
126 const uint64_t span_x =
static_cast<uint64_t
>(
x) - reference_x_;
133 }
else if (slope_ > 0) {
135 const uint64_t span_y = UnsignedCapProd(span_x, slope_);
136 if (reference_y_ == 0) {
138 }
else if (reference_y_ > 0) {
139 const uint64_t unsigned_sum = UnsignedCapAdd(reference_y_, span_y);
141 :
static_cast<int64_t
>(unsigned_sum);
143 const uint64_t opp_reference_y = -
static_cast<uint64_t
>(reference_y_);
144 if (span_y >= opp_reference_y) {
145 return span_y - opp_reference_y >
kint64max
147 :
static_cast<int64_t
>(span_y - opp_reference_y);
149 return opp_reference_y - span_y >
static_cast<uint64_t
>(
kint64max) + 1
151 : -static_cast<int64_t>(opp_reference_y - span_y);
156 const uint64_t span_y = UnsignedCapProd(span_x, -slope_);
157 if (reference_y_ == 0) {
159 }
else if (reference_y_ < 0) {
160 const uint64_t opp_reference_y = -
static_cast<uint64_t
>(reference_y_);
161 const uint64_t opp_unsigned_sum = UnsignedCapAdd(opp_reference_y, span_y);
164 : -
static_cast<int64_t
>(opp_unsigned_sum);
166 if (reference_y_ >= span_y) {
169 :
static_cast<int64_t
>(reference_y_ - span_y);
171 return span_y - reference_y_ >
static_cast<uint64_t
>(
kint64max) + 1
173 : -static_cast<int64_t>(span_y - reference_y_);
179int64_t PiecewiseSegment::SafeValuePreReference(int64_t
x)
const {
180 DCHECK_LE(
x, reference_x_);
181 const uint64_t span_x =
static_cast<uint64_t
>(reference_x_) -
x;
185 }
else if (slope_ > 0) {
187 const uint64_t span_y = UnsignedCapProd(span_x, slope_);
188 if (reference_y_ == 0) {
190 }
else if (reference_y_ > 0) {
191 if (reference_y_ >= span_y) {
194 :
static_cast<int64_t
>(reference_y_ - span_y);
196 return span_y - reference_y_ >
static_cast<uint64_t
>(
kint64max) + 1
198 : -static_cast<uint64_t>(span_y - reference_y_);
201 const uint64_t opp_reference_y = -
static_cast<uint64_t
>(reference_y_);
202 const uint64_t opp_unsigned_sum = UnsignedCapAdd(opp_reference_y, span_y);
205 : -
static_cast<uint64_t
>(opp_unsigned_sum);
209 const uint64_t span_y = UnsignedCapProd(span_x, -slope_);
210 if (reference_y_ == 0) {
212 }
else if (reference_y_ < 0) {
213 const uint64_t opp_reference_y = -
static_cast<uint64_t
>(reference_y_);
214 if (span_y >= opp_reference_y) {
215 return span_y - opp_reference_y >
kint64max
217 :
static_cast<int64_t
>(span_y - opp_reference_y);
219 return opp_reference_y - span_y >
static_cast<uint64_t
>(
kint64max) + 1
221 : -static_cast<uint64_t>(opp_reference_y - span_y);
224 const uint64_t unsigned_sum = UnsignedCapAdd(reference_y_, span_y);
226 :
static_cast<int64_t
>(unsigned_sum);
233 return segment1.start_x_ < segment2.start_x_;
242 end_x_ = std::max(end_x_,
end_x);
246 if (IsAtBounds(
CapAdd(reference_x_, constant))) {
247 LOG(ERROR) <<
"Segment Overflow: " <<
DebugString();
250 start_x_ =
CapAdd(start_x_, constant);
251 end_x_ =
CapAdd(end_x_, constant);
252 reference_x_ =
CapAdd(reference_x_, constant);
256 if (IsAtBounds(
CapAdd(reference_y_, constant))) {
257 LOG(ERROR) <<
"Segment Overflow: " <<
DebugString();
260 reference_y_ =
CapAdd(reference_y_, constant);
264 std::string result = absl::StrFormat(
265 "PiecewiseSegment(<start: (%d, %d), end: (%d, %d), "
266 "reference: (%d, %d), slope = %d>)",
267 start_x_,
Value(start_x_), end_x_,
Value(end_x_), reference_x_,
268 reference_y_, slope_);
274PiecewiseLinearFunction::PiecewiseLinearFunction(
275 std::vector<PiecewiseSegment> segments)
276 : is_modified_(true),
278 is_non_decreasing_(false),
279 is_non_increasing_(false) {
283 for (
int i = 0; i < segments.size() - 1; ++i) {
285 LOG(FATAL) <<
"Overlapping segments: " << segments[i].DebugString()
286 <<
" & " << segments[i + 1].DebugString();
290 for (
const auto& segment : segments) {
291 InsertSegment(segment);
296 std::vector<int64_t> points_x, std::vector<int64_t> points_y,
297 std::vector<int64_t> slopes, std::vector<int64_t> other_points_x) {
298 CHECK_EQ(points_x.size(), points_y.size());
299 CHECK_EQ(points_x.size(), other_points_x.size());
300 CHECK_EQ(points_x.size(), slopes.size());
301 CHECK_GT(points_x.size(), 0);
303 std::vector<PiecewiseSegment>
segments;
304 for (
int i = 0; i < points_x.size(); ++i) {
313 std::vector<int64_t> points_x, std::vector<int64_t> points_y,
314 std::vector<int64_t> other_points_x) {
315 CHECK_EQ(points_x.size(), points_y.size());
316 CHECK_EQ(points_x.size(), other_points_x.size());
317 CHECK_GT(points_x.size(), 0);
319 std::vector<PiecewiseSegment>
segments;
320 for (
int i = 0; i < points_x.size(); ++i) {
329 int64_t initial_level, std::vector<int64_t> points_x,
330 std::vector<int64_t> slopes) {
331 CHECK_EQ(points_x.size(), slopes.size() - 1);
332 CHECK_GT(points_x.size(), 0);
334 int64_t level = initial_level;
335 std::vector<PiecewiseSegment>
segments;
339 level = segment.
Value(points_x[0]);
340 for (
int i = 1; i < points_x.size(); ++i) {
344 level = segment.
Value(points_x[i]);
353 int64_t point_x, int64_t point_y, int64_t slope, int64_t other_point_x) {
356 std::vector<PiecewiseSegment>
segments = {
362 int64_t point_x, int64_t point_y, int64_t slope) {
363 std::vector<PiecewiseSegment>
segments = {
369 int64_t point_x, int64_t point_y, int64_t slope) {
370 std::vector<PiecewiseSegment>
segments = {
376 int64_t slope, int64_t
value) {
377 std::vector<PiecewiseSegment>
segments = {
386 int64_t reference, int64_t earliness_slope, int64_t tardiness_slope) {
387 std::vector<PiecewiseSegment>
segments = {
390 CHECK_GE(earliness_slope, 0);
391 CHECK_GE(tardiness_slope, 0);
397 int64_t early_slack, int64_t late_slack, int64_t earliness_slope,
398 int64_t tardiness_slope) {
399 std::vector<PiecewiseSegment>
segments = {
404 CHECK_GE(earliness_slope, 0);
405 CHECK_GE(tardiness_slope, 0);
410 int index = FindSegmentIndex(segments_,
x);
414 if (segments_[
index].end_x() <
x) {
427 return is_non_decreasing_;
432 return is_non_increasing_;
441 const int index = FindSegmentIndex(segments_,
x);
442 return segments_[
index].Value(
x);
446 int64_t range_end)
const {
448 return Value(range_end);
450 return Value(range_start);
452 int start_segment = -1;
453 int end_segment = -1;
454 if (!FindSegmentIndicesFromRange(range_start, range_end, &start_segment,
458 CHECK_GE(end_segment, start_segment);
462 range_maximum = std::max(
Value(range_start), range_maximum);
465 range_maximum = std::max(
Value(range_end), range_maximum);
468 for (
int i = std::max(0, start_segment); i <= end_segment; ++i) {
469 if (PointInsideRange(segments_[i].start_x(), range_start, range_end)) {
470 range_maximum = std::max(range_maximum, segments_[i].start_y());
472 if (PointInsideRange(segments_[i].end_x(), range_start, range_end)) {
473 range_maximum = std::max(range_maximum, segments_[i].end_y());
476 return range_maximum;
480 int64_t range_end)
const {
482 return Value(range_start);
484 return Value(range_end);
486 int start_segment = -1;
487 int end_segment = -1;
488 if (!FindSegmentIndicesFromRange(range_start, range_end, &start_segment,
492 CHECK_GE(end_segment, start_segment);
496 range_minimum = std::min(
Value(range_start), range_minimum);
499 range_minimum = std::min(
Value(range_end), range_minimum);
502 for (
int i = std::max(0, start_segment); i <= end_segment; ++i) {
503 if (PointInsideRange(segments_[i].start_x(), range_start, range_end)) {
504 range_minimum = std::min(range_minimum, segments_[i].start_y());
506 if (PointInsideRange(segments_[i].end_x(), range_start, range_end)) {
507 range_minimum = std::min(range_minimum, segments_[i].end_y());
510 return range_minimum;
514 return GetMaximum(segments_.front().start_x(), segments_.back().end_x());
518 return GetMinimum(segments_.front().start_x(), segments_.back().end_x());
521std::pair<int64_t, int64_t>
524 int64_t
value)
const {
528std::pair<int64_t, int64_t>
531 int64_t
value)
const {
536std::pair<int64_t, int64_t> ComputeXFromY(int64_t start_x, int64_t start_y,
537 int64_t slope, int64_t
y) {
539 const int64_t delta_y =
CapSub(
y, start_y);
540 const int64_t delta_x = delta_y / slope;
541 if ((delta_y >= 0 && slope >= 0) || (delta_y <= 0 && slope <= 0)) {
542 const int64_t delta_x_down = delta_x;
543 const int64_t delta_x_up = delta_y % slope == 0 ? delta_x : delta_x + 1;
544 return {delta_x_down + start_x, delta_x_up + start_x};
546 const int64_t delta_x_down = delta_y % slope == 0 ? delta_x : delta_x - 1;
547 const int64_t delta_x_up = -(-delta_y / slope);
548 return {delta_x_down + start_x, delta_x_up + start_x};
552std::pair<int64_t, int64_t> GetRangeInValueRange(int64_t start_x, int64_t end_x,
553 int64_t start_y, int64_t end_y,
557 if ((start_y > value_max && end_y > value_max) ||
558 (start_y < value_min && end_y < value_min)) {
562 if (start_y <= value_max && end_y <= value_max) {
563 x_range_max = {start_x, end_x};
564 }
else if (start_y <= value_max || end_y <= value_max) {
566 ? ComputeXFromY(end_x, end_y, slope, value_max)
567 : ComputeXFromY(start_x, start_y, slope, value_max);
568 if (end_y <= value_max) {
569 x_range_max = {
x.second, end_x};
571 x_range_max = {start_x,
x.first};
575 if (start_y >= value_min && end_y >= value_min) {
576 x_range_min = {start_x, end_x};
577 }
else if (start_y >= value_min || end_y >= value_min) {
579 ? ComputeXFromY(end_x, end_y, slope, value_min)
580 : ComputeXFromY(start_x, start_y, slope, value_min);
581 if (end_y >= value_min) {
582 x_range_min = {
x.second, end_x};
584 x_range_min = {start_x,
x.first};
587 if (x_range_min.first > x_range_max.second ||
588 x_range_max.first > x_range_min.second) {
591 return {std::max(x_range_min.first, x_range_max.first),
592 std::min(x_range_min.second, x_range_max.second)};
596std::pair<int64_t, int64_t>
600 int64_t value_max)
const {
603 int start_segment = -1;
604 int end_segment = -1;
605 if (!FindSegmentIndicesFromRange(range_start, range_end, &start_segment,
607 return {reduced_range_start, reduced_range_end};
609 for (
int i = std::max(0, start_segment); i <= end_segment; ++i) {
610 const auto& segment = segments_[i];
611 const int64_t start_x = std::max(range_start, segment.start_x());
612 const int64_t end_x = std::min(range_end, segment.end_x());
613 const int64_t start_y = segment.Value(start_x);
614 const int64_t end_y = segment.Value(end_x);
615 const std::pair<int64_t, int64_t>
range = GetRangeInValueRange(
616 start_x, end_x, start_y, end_y, segment.slope(), value_min, value_max);
617 reduced_range_start = std::min(reduced_range_start,
range.first);
618 reduced_range_end = std::max(reduced_range_end,
range.second);
620 return {reduced_range_start, reduced_range_end};
625 for (
int i = 0; i < segments_.size(); ++i) {
626 segments_[i].AddConstantToX(constant);
632 for (
int i = 0; i < segments_.size(); ++i) {
633 segments_[i].AddConstantToY(constant);
638 Operation(other, [](int64_t
a, int64_t
b) {
return CapAdd(
a,
b); });
642 Operation(other, [](int64_t
a, int64_t
b) {
return CapSub(
a,
b); });
645std::vector<PiecewiseLinearFunction*>
647 CHECK_GE(segments_.size(), 1);
652 std::vector<PiecewiseLinearFunction*> convex_functions;
653 std::vector<PiecewiseSegment> convex_segments;
656 if (convex_segments.empty()) {
657 convex_segments.push_back(segment);
662 if (FormConvexPair(last, segment)) {
664 convex_segments.push_back(segment);
667 convex_segments.clear();
668 convex_segments.push_back(segment);
672 if (!convex_segments.empty()) {
673 convex_functions.push_back(
676 return convex_functions;
680 std::string result =
"PiecewiseLinearFunction(";
681 for (
int i = 0; i < segments_.size(); ++i) {
688void PiecewiseLinearFunction::InsertSegment(
const PiecewiseSegment& segment) {
691 if (segments_.empty() || segments_.back().end_x() < segment.
start_x()) {
692 segments_.push_back(segment);
697 if (segments_.back().end_x() == segment.
start_x()) {
698 if (segments_.back().end_y() == segment.
start_y() &&
699 segments_.back().slope() == segment.
slope()) {
700 segments_.back().ExpandEnd(segment.
end_x());
703 segments_.push_back(segment);
707void PiecewiseLinearFunction::Operation(
708 const PiecewiseLinearFunction& other,
709 const std::function<int64_t(int64_t, int64_t)>& operation) {
711 std::vector<PiecewiseSegment> own_segments;
712 const std::vector<PiecewiseSegment>& other_segments = other.segments();
713 own_segments.swap(segments_);
715 absl::btree_set<int64_t> start_x_points;
716 for (
int i = 0;
i < own_segments.size(); ++
i) {
717 start_x_points.insert(own_segments[i].start_x());
719 for (
int i = 0;
i < other_segments.size(); ++
i) {
720 start_x_points.insert(other_segments[i].start_x());
723 for (int64_t start_x : start_x_points) {
724 const int own_index = FindSegmentIndex(own_segments, start_x);
725 const int other_index = FindSegmentIndex(other_segments, start_x);
726 if (own_index >= 0 && other_index >= 0) {
727 const PiecewiseSegment& own_segment = own_segments[own_index];
728 const PiecewiseSegment& other_segment = other_segments[other_index];
730 const int64_t end_x =
731 std::min(own_segment.end_x(), other_segment.end_x());
732 const int64_t start_y =
733 operation(own_segment.Value(start_x), other_segment.Value(start_x));
734 const int64_t end_y =
735 operation(own_segment.Value(end_x), other_segment.Value(end_x));
736 const int64_t slope =
737 operation(own_segment.slope(), other_segment.slope());
739 int64_t point_x, point_y, other_point_x;
740 if (IsAtBounds(start_y)) {
743 other_point_x = start_x;
747 other_point_x = end_x;
749 InsertSegment(PiecewiseSegment(point_x, point_y, slope, other_point_x));
754bool PiecewiseLinearFunction::FindSegmentIndicesFromRange(
755 int64_t range_start, int64_t range_end,
int* start_segment,
756 int* end_segment)
const {
757 *start_segment = FindSegmentIndex(segments_, range_start);
758 *end_segment = FindSegmentIndex(segments_, range_end);
759 if (*start_segment == *end_segment) {
760 if (*start_segment < 0) {
764 if (segments_[*start_segment].end_x() < range_start) {
772bool PiecewiseLinearFunction::IsConvexInternal()
const {
773 for (
int i = 1;
i < segments_.size(); ++
i) {
774 if (!FormConvexPair(segments_[i - 1], segments_[i])) {
781bool PiecewiseLinearFunction::IsNonDecreasingInternal()
const {
783 for (
const auto& segment : segments_) {
784 const int64_t start_y = segment.
start_y();
785 const int64_t end_y = segment.
end_y();
786 if (end_y < start_y || start_y <
value)
return false;
792bool PiecewiseLinearFunction::IsNonIncreasingInternal()
const {
794 for (
const auto& segment : segments_) {
795 const int64_t start_y = segment.
start_y();
796 const int64_t end_y = segment.
end_y();
797 if (end_y > start_y || start_y >
value)
return false;
int64_t GetMaximum() const
Returns the maximum value of all the segments in the function.
std::vector< PiecewiseLinearFunction * > DecomposeToConvexFunctions() const
static PiecewiseLinearFunction * CreateFullDomainFunction(int64_t initial_level, std::vector< int64_t > points_x, std::vector< int64_t > slopes)
static PiecewiseLinearFunction * CreateRightRayFunction(int64_t point_x, int64_t point_y, int64_t slope)
bool IsNonIncreasing() const
Returns true if the piecewise linear function is non-increasing.
std::pair< int64_t, int64_t > GetSmallestRangeLessThanValue(int64_t range_start, int64_t range_end, int64_t value) const
static const int kNotFound
static PiecewiseLinearFunction * CreateFixedChargeFunction(int64_t slope, int64_t value)
std::string DebugString() const
void Add(const PiecewiseLinearFunction &other)
static PiecewiseLinearFunction * CreateStepFunction(std::vector< int64_t > points_x, std::vector< int64_t > points_y, std::vector< int64_t > other_points_x)
int64_t GetMinimum() const
Returns the minimum value of all the segments in the function.
void AddConstantToY(int64_t constant)
const std::vector< PiecewiseSegment > & segments() const
static PiecewiseLinearFunction * CreateOneSegmentFunction(int64_t point_x, int64_t point_y, int64_t slope, int64_t other_point_x)
Builds a function consisting of one segment.
std::pair< int64_t, int64_t > GetSmallestRangeInValueRange(int64_t range_start, int64_t range_end, int64_t value_min, int64_t value_max) const
static PiecewiseLinearFunction * CreateLeftRayFunction(int64_t point_x, int64_t point_y, int64_t slope)
bool IsNonDecreasing() const
Returns true if the piecewise linear function is non-decreasing.
static PiecewiseLinearFunction * CreateEarlyTardyFunction(int64_t reference, int64_t earliness_slope, int64_t tardiness_slope)
void AddConstantToX(int64_t constant)
static PiecewiseLinearFunction * CreatePiecewiseLinearFunction(std::vector< int64_t > points_x, std::vector< int64_t > points_y, std::vector< int64_t > slopes, std::vector< int64_t > other_points_x)
static PiecewiseLinearFunction * CreateEarlyTardyFunctionWithSlack(int64_t early_slack, int64_t late_slack, int64_t earliness_slope, int64_t tardiness_slope)
int64_t Value(int64_t x) const
Returns the value of the piecewise linear function for x.
std::pair< int64_t, int64_t > GetSmallestRangeGreaterThanValue(int64_t range_start, int64_t range_end, int64_t value) const
bool InDomain(int64_t x) const
Returns if x is in the domain of the function.
void Subtract(const PiecewiseLinearFunction &other)
int64_t end_x() const
Returns the end of the segment's domain.
int64_t start_y() const
Returns the value at the start of the segment's domain.
static bool FindComparator(int64_t point, const PiecewiseSegment &segment)
Comparison method useful for finding in which segment a point belongs.
static bool SortComparator(const PiecewiseSegment &segment1, const PiecewiseSegment &segment2)
Comparison method useful for sorting a sequence of segments.
std::string DebugString() const
void ExpandEnd(int64_t end_x)
void AddConstantToY(int64_t constant)
Adds 'constant' to the 'y' the segments.
int64_t slope() const
Returns the segment's slope.
int64_t start_x() const
Returns the start of the segment's domain.
void AddConstantToX(int64_t constant)
Adds 'constant' to the 'x' the segments.
PiecewiseSegment(int64_t point_x, int64_t point_y, int64_t slope, int64_t other_point_x)
int64_t Value(int64_t x) const
Returns the value of the segment at point x.
int64_t end_y() const
Returns the value at the end of the segment's domain.
In SWIG mode, we don't want anything besides these top-level includes.
int64_t CapAdd(int64_t x, int64_t y)
int64_t CapSub(int64_t x, int64_t y)
int64_t CapProd(int64_t x, int64_t y)
const std::optional< Range > & range
static const uint64_t kuint64max
static const int64_t kint64max
static const int64_t kint64min