Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
vector_or_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#ifndef ORTOOLS_UTIL_VECTOR_OR_FUNCTION_H_
15#define ORTOOLS_UTIL_VECTOR_OR_FUNCTION_H_
16
17#include <algorithm>
18#include <vector>
19
22
23namespace operations_research {
24
25// Template to abstract the access to STL functions or vector values.
26template <typename ScalarType, typename Evaluator>
28 public:
29 explicit VectorOrFunction(Evaluator evaluator)
30 : evaluator_(std::move(evaluator)) {}
31 void Reset(Evaluator evaluator) { evaluator_ = std::move(evaluator); }
32 ScalarType operator()(int i) const { return evaluator_(i); }
33
34 private:
35 Evaluator evaluator_;
36};
37
38// Specialization for vectors.
39template <typename ScalarType>
40class VectorOrFunction<ScalarType, std::vector<ScalarType>> {
41 public:
42 explicit VectorOrFunction(std::vector<ScalarType> values)
43 : values_(std::move(values)) {}
44 void Reset(std::vector<ScalarType> values) { values_ = std::move(values); }
45 ScalarType operator()(int i) const { return values_[i]; }
46
47 private:
48 std::vector<ScalarType> values_;
49};
50
51// Template to abstract the access to STL functions or vector-base matrix
52// values.
53template <typename ScalarType, typename Evaluator, bool square = false>
55 public:
56 explicit MatrixOrFunction(Evaluator evaluator)
57 : evaluator_(std::move(evaluator)) {}
58 void Reset(Evaluator evaluator) { evaluator_ = std::move(evaluator); }
59 ScalarType operator()(int i, int j) const { return evaluator_(i, j); }
60 bool Check() const { return true; }
61
62 private:
63 Evaluator evaluator_;
64};
65
66// Specialization for vector-based matrices.
67template <typename ScalarType, bool square>
68class MatrixOrFunction<ScalarType, std::vector<std::vector<ScalarType>>,
69 square> {
70 public:
71 explicit MatrixOrFunction(std::vector<std::vector<ScalarType>> matrix)
72 : matrix_(std::move(matrix)) {}
73 void Reset(std::vector<std::vector<ScalarType>> matrix) {
74 matrix_ = std::move(matrix);
75 }
76 ScalarType operator()(int i, int j) const { return matrix_[i][j]; }
77 // Returns true if the matrix is square or rectangular.
78 // Intended to be used in a CHECK.
79 bool Check() const {
80 if (matrix_.empty()) return true;
81 const int size = square ? matrix_.size() : matrix_[0].size();
82 const char* msg =
83 square ? "Matrix must be square." : "Matrix must be rectangular.";
84 for (const std::vector<ScalarType>& row : matrix_) {
85 CHECK_EQ(size, row.size()) << msg;
86 }
87 return true;
88 }
89
90 private:
91 std::vector<std::vector<ScalarType>> matrix_;
92};
93
94// Specialization for FlatMatrix<>, which is faster than vector<vector<>>.
95template <typename ScalarType, bool square>
96class MatrixOrFunction<ScalarType, FlatMatrix<ScalarType>, square> {
97 public:
99 : matrix_(std::move(matrix)) {}
100 void Reset(FlatMatrix<ScalarType> matrix) { matrix_ = std::move(matrix); }
101 ScalarType operator()(int i, int j) const { return matrix_[i][j]; }
102 bool Check() const { return true; }
103
104 private:
106};
107
108} // namespace operations_research
109
110#endif // ORTOOLS_UTIL_VECTOR_OR_FUNCTION_H_
ScalarType operator()(int i, int j) const
OR-Tools root namespace.
STL namespace.