OR-Tools  8.2
range_query_function.h
Go to the documentation of this file.
1 // Copyright 2010-2018 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
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 <functional>
21 #include <memory>
22 
24 
25 namespace operations_research {
26 // RangeIntToIntFunction is an interface to int64->int64 functions supporting
27 // fast answer to range queries about their domain/codomain.
29  public:
30  virtual ~RangeIntToIntFunction() = default;
31 
32  // Suppose f is the abstract underlying function.
33  // Returns f(argument).
34  // TODO(user): Rename to Run
35  virtual int64 Query(int64 argument) const = 0;
36  // Returns min_x f(x), where x is in [from, to).
37  virtual int64 RangeMin(int64 from, int64 to) const = 0;
38  // Returns max_x f(x), where x is in [from, to).
39  virtual int64 RangeMax(int64 from, int64 to) const = 0;
40  // Returns the first x from [range_begin, range_end) for which f(x) is in
41  // [interval_begin, interval_end), or range_end if there is no such x.
42  virtual int64 RangeFirstInsideInterval(int64 range_begin, int64 range_end,
43  int64 interval_begin,
44  int64 interval_end) const = 0;
45  // Returns the last x from [range_begin, range_end) for which f(x) is in
46  // [interval_begin, interval_end), or range_begin-1 if there is no such x.
47  virtual int64 RangeLastInsideInterval(int64 range_begin, int64 range_end,
48  int64 interval_begin,
49  int64 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 RangeMaxArgument(int64 from, int64 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 RangeMinArgument(int64 from, int64 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 // It is assumed that f is defined over the interval [domain_start, domain_end).
74 // The function scans f once and it is safe to destroy f and its closure after
75 // MakeCachedIntToIntFunction returns.
77  const std::function<int64(int64)>& f, int64 domain_start, int64 domain_end);
78 // It is safe to destroy the first argument and its closure after
79 // MakeCachedRangeMinMaxIndexFunction returns.
81  const std::function<int64(int64)>& f, int64 domain_start, int64 domain_end);
82 } // namespace operations_research
83 
84 #endif // OR_TOOLS_UTIL_RANGE_QUERY_FUNCTION_H_
virtual int64 Query(int64 argument) const =0
virtual int64 RangeFirstInsideInterval(int64 range_begin, int64 range_end, int64 interval_begin, int64 interval_end) const =0
virtual int64 RangeLastInsideInterval(int64 range_begin, int64 range_end, int64 interval_begin, int64 interval_end) const =0
virtual int64 RangeMax(int64 from, int64 to) const =0
virtual int64 RangeMin(int64 from, int64 to) const =0
virtual int64 RangeMaxArgument(int64 from, int64 to) const =0
virtual int64 RangeMinArgument(int64 from, int64 to) const =0
int64_t int64
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
RangeMinMaxIndexFunction * MakeCachedRangeMinMaxIndexFunction(const std::function< int64(int64)> &f, int64 domain_start, int64 domain_end)
RangeIntToIntFunction * MakeBareIntToIntFunction(std::function< int64(int64)> f)
RangeIntToIntFunction * MakeCachedIntToIntFunction(const std::function< int64(int64)> &f, int64 domain_start, int64 domain_end)