30class LinearRangeIntToIntFunction :
public RangeIntToIntFunction {
32 explicit LinearRangeIntToIntFunction(
33 std::function<int64_t(int64_t)> base_function)
34 : base_function_(
std::move(base_function)) {}
37 LinearRangeIntToIntFunction(
const LinearRangeIntToIntFunction&) =
delete;
38 LinearRangeIntToIntFunction& operator=(
const LinearRangeIntToIntFunction&) =
41 int64_t Query(int64_t argument)
const override {
42 return base_function_(argument);
45 int64_t RangeMin(int64_t range_begin, int64_t range_end)
const override {
46 DCHECK_LT(range_begin, range_end);
48 for (int64_t i = range_begin;
i < range_end; ++
i) {
49 min_val = std::min(min_val, base_function_(i));
54 int64_t RangeMax(int64_t range_begin, int64_t range_end)
const override {
55 DCHECK_LT(range_begin, range_end);
57 for (int64_t i = range_begin;
i < range_end; ++
i) {
58 max_val = std::max(max_val, base_function_(i));
63 int64_t RangeFirstInsideInterval(int64_t range_begin, int64_t range_end,
64 int64_t interval_begin,
65 int64_t interval_end)
const override {
67 DCHECK_LT(range_begin, range_end);
68 DCHECK_LT(interval_begin, interval_end);
69 int64_t
i = range_begin;
70 for (;
i < range_end; ++
i) {
71 const int64_t
value = base_function_(i);
72 if (interval_begin <=
value &&
value < interval_end)
break;
77 int64_t RangeLastInsideInterval(int64_t range_begin, int64_t range_end,
78 int64_t interval_begin,
79 int64_t interval_end)
const override {
82 DCHECK_LT(range_begin, range_end);
83 DCHECK_LT(interval_begin, interval_end);
84 int64_t
i = range_end - 1;
85 for (;
i >= range_begin; --
i) {
86 const int64_t
value = base_function_(i);
87 if (interval_begin <=
value &&
value < interval_end)
break;
93 std::function<int64_t(int64_t)> base_function_;
96std::vector<int64_t> FunctionToVector(
const std::function<int64_t(int64_t)>& f,
99 CHECK_LT(domain_start, domain_end);
100 std::vector<int64_t> output(domain_end - domain_start, 0);
101 for (int64_t i = 0;
i < domain_end - domain_start; ++
i) {
102 output[
i] = f(i + domain_start);
111class CachedRangeIntToIntFunction :
public RangeIntToIntFunction {
113 CachedRangeIntToIntFunction(
114 const std::function<int64_t(int64_t)>& base_function,
115 int64_t domain_start, int64_t domain_end)
116 : domain_start_(domain_start),
117 rmq_min_(FunctionToVector(base_function, domain_start, domain_end)),
118 rmq_max_(rmq_min_.array()) {
119 CHECK_LT(domain_start, domain_end);
123 CachedRangeIntToIntFunction(
const CachedRangeIntToIntFunction&) =
delete;
124 CachedRangeIntToIntFunction& operator=(
const CachedRangeIntToIntFunction&) =
127 int64_t Query(int64_t argument)
const override {
128 DCHECK_LE(domain_start_, argument);
129 DCHECK_LE(argument, domain_start_ +
static_cast<int64_t
>(array().
size()));
130 return array()[argument - domain_start_];
132 int64_t RangeMin(int64_t from, int64_t
to)
const override {
133 DCHECK_LE(domain_start_, from);
135 DCHECK_LE(
to, domain_start_ +
static_cast<int64_t
>(array().
size()));
136 return rmq_min_.GetMinimumFromRange(from - domain_start_,
139 int64_t RangeMax(int64_t from, int64_t
to)
const override {
140 DCHECK_LE(domain_start_, from);
142 DCHECK_LE(
to, domain_start_ +
static_cast<int64_t
>(array().
size()));
143 return rmq_max_.GetMinimumFromRange(from - domain_start_,
146 int64_t RangeFirstInsideInterval(int64_t range_begin, int64_t range_end,
147 int64_t interval_begin,
148 int64_t interval_end)
const override {
150 DCHECK_LE(domain_start_, range_begin);
151 DCHECK_LT(range_begin, range_end);
152 DCHECK_LE(range_end, domain_start_ + array().
size());
153 DCHECK_LT(interval_begin, interval_end);
154 int64_t
i = range_begin;
155 for (;
i < range_end; ++
i) {
156 const int64_t
value = array()[
i - domain_start_];
157 if (interval_begin <=
value &&
value < interval_end)
break;
161 int64_t RangeLastInsideInterval(int64_t range_begin, int64_t range_end,
162 int64_t interval_begin,
163 int64_t interval_end)
const override {
165 DCHECK_LE(domain_start_, range_begin);
166 DCHECK_LT(range_begin, range_end);
167 DCHECK_LE(range_end, domain_start_ + array().
size());
168 DCHECK_LT(interval_begin, interval_end);
169 int64_t
i = range_end - 1;
170 for (;
i >= range_begin; --
i) {
171 const int64_t
value = array()[
i - domain_start_];
172 if (interval_begin <=
value &&
value < interval_end)
break;
178 const std::vector<int64_t>& array()
const {
return rmq_min_.array(); }
180 const int64_t domain_start_;
181 const RangeMinimumQuery<int64_t, std::less<int64_t>> rmq_min_;
182 const RangeMinimumQuery<int64_t, std::greater<int64_t>> rmq_max_;
185class CachedRangeMinMaxIndexFunction :
public RangeMinMaxIndexFunction {
187 CachedRangeMinMaxIndexFunction(
const std::function<int64_t(int64_t)>& f,
188 int64_t domain_start, int64_t domain_end)
189 : domain_start_(domain_start),
190 domain_end_(domain_end),
191 index_rmq_min_(FunctionToVector(f, domain_start, domain_end)),
192 index_rmq_max_(index_rmq_min_.array()) {
193 CHECK_LT(domain_start, domain_end);
197 CachedRangeMinMaxIndexFunction(
const CachedRangeMinMaxIndexFunction&) =
199 CachedRangeMinMaxIndexFunction& operator=(
200 const CachedRangeMinMaxIndexFunction&) =
delete;
202 inline int64_t RangeMinArgument(int64_t from, int64_t
to)
const override {
203 DCHECK_LE(domain_start_, from);
205 DCHECK_LE(
to, domain_end_);
206 return index_rmq_min_.GetMinimumIndexFromRange(from - domain_start_,
207 to - domain_start_) +
210 inline int64_t RangeMaxArgument(int64_t from, int64_t
to)
const override {
211 DCHECK_LE(domain_start_, from);
213 DCHECK_LE(
to, domain_end_);
214 return index_rmq_max_.GetMinimumIndexFromRange(from - domain_start_,
215 to - domain_start_) +
220 const int64_t domain_start_;
221 const int64_t domain_end_;
222 const RangeMinimumIndexQuery<int64_t, std::less<int64_t>> index_rmq_min_;
223 const RangeMinimumIndexQuery<int64_t, std::greater<int64_t>> index_rmq_max_;
228 std::function<int64_t(int64_t)> f) {
229 return new LinearRangeIntToIntFunction(std::move(f));
233 const std::function<int64_t(int64_t)>& f, int64_t domain_start,
234 int64_t domain_end) {
235 return new CachedRangeIntToIntFunction(f, domain_start, domain_end);
239 const std::function<int64_t(int64_t)>& f, int64_t domain_start,
240 int64_t domain_end) {
241 return new CachedRangeMinMaxIndexFunction(f, domain_start, domain_end);
In SWIG mode, we don't want anything besides these top-level includes.
RangeIntToIntFunction * MakeCachedIntToIntFunction(const std::function< int64_t(int64_t)> &f, int64_t domain_start, int64_t domain_end)
RangeIntToIntFunction * MakeBareIntToIntFunction(std::function< int64_t(int64_t)> f)
RangeMinMaxIndexFunction * MakeCachedRangeMinMaxIndexFunction(const std::function< int64_t(int64_t)> &f, int64_t domain_start, int64_t domain_end)
trees with all degrees equal to
static const int64_t kint64max
static const int64_t kint64min