Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
range_query_function.h
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14// The header defines an interface for functions taking and returning an int64_t
15// and supporting range queries over their domain and codomain.
16
17#ifndef OR_TOOLS_UTIL_RANGE_QUERY_FUNCTION_H_
18#define OR_TOOLS_UTIL_RANGE_QUERY_FUNCTION_H_
19
20#include <cstdint>
21#include <functional>
22
23namespace operations_research {
24// RangeIntToIntFunction is an interface to int64_t->int64_t functions
25// supporting fast answer to range queries about their domain/codomain.
27 public:
28 virtual ~RangeIntToIntFunction() = default;
29
30 // Suppose f is the abstract underlying function.
31 // Returns f(argument).
32 // TODO(user): Rename to Run
33 virtual int64_t Query(int64_t argument) const = 0;
34 // Returns min_x f(x), where x is in [from, to).
35 virtual int64_t RangeMin(int64_t from, int64_t to) const = 0;
36 // Returns max_x f(x), where x is in [from, to).
37 virtual int64_t RangeMax(int64_t from, int64_t to) const = 0;
38 // Returns the first x from [range_begin, range_end) for which f(x) is in
39 // [interval_begin, interval_end), or range_end if there is no such x.
40 virtual int64_t RangeFirstInsideInterval(int64_t range_begin,
41 int64_t range_end,
42 int64_t interval_begin,
43 int64_t interval_end) const = 0;
44 // Returns the last x from [range_begin, range_end) for which f(x) is in
45 // [interval_begin, interval_end), or range_begin-1 if there is no such x.
46 virtual int64_t RangeLastInsideInterval(int64_t range_begin,
47 int64_t range_end,
48 int64_t interval_begin,
49 int64_t interval_end) const = 0;
50};
51
52// RangeMinMaxIndexFunction is different from RangeIntToIntFunction in two ways:
53//
54// 1. It does not support codomain or value queries.
55//
56// 2. For domain queries it returns an argument where the minimum/maximum is
57// attained, rather than the minimum/maximum value.
59 public:
60 virtual ~RangeMinMaxIndexFunction() = default;
61 // Suppose f is the abstract underlying function.
62 // Returns an x from [from, to), such that f(x) => f(y) for every y from
63 // [from, to).
64 virtual int64_t RangeMaxArgument(int64_t from, int64_t to) const = 0;
65 // Returns an x from [from, to), such that f(x) <= f(y) for every y from
66 // [from, to).
67 virtual int64_t RangeMinArgument(int64_t from, int64_t to) const = 0;
68};
69
70// A copy of f is going to be stored in the returned object, so its closure
71// should remain intact as long as the returned object is being used.
73 std::function<int64_t(int64_t)> f);
74// It is assumed that f is defined over the interval [domain_start, domain_end).
75// The function scans f once and it is safe to destroy f and its closure after
76// MakeCachedIntToIntFunction returns.
78 const std::function<int64_t(int64_t)>& f, int64_t domain_start,
79 int64_t domain_end);
80// It is safe to destroy the first argument and its closure after
81// MakeCachedRangeMinMaxIndexFunction returns.
83 const std::function<int64_t(int64_t)>& f, int64_t domain_start,
84 int64_t domain_end);
85} // namespace operations_research
86
87#endif // OR_TOOLS_UTIL_RANGE_QUERY_FUNCTION_H_
virtual int64_t RangeMin(int64_t from, int64_t to) const =0
Returns min_x f(x), where x is in [from, to).
virtual int64_t RangeLastInsideInterval(int64_t range_begin, int64_t range_end, int64_t interval_begin, int64_t interval_end) const =0
virtual int64_t RangeFirstInsideInterval(int64_t range_begin, int64_t range_end, int64_t interval_begin, int64_t interval_end) const =0
virtual int64_t Query(int64_t argument) const =0
virtual int64_t RangeMax(int64_t from, int64_t to) const =0
Returns max_x f(x), where x is in [from, to).
virtual int64_t RangeMinArgument(int64_t from, int64_t to) const =0
virtual int64_t RangeMaxArgument(int64_t from, int64_t to) const =0
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