Google OR-Tools v9.11
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-2024 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 OR_TOOLS_UTIL_VECTOR_OR_FUNCTION_H_
15#define OR_TOOLS_UTIL_VECTOR_OR_FUNCTION_H_
16
17#include <algorithm>
18#include <vector>
19
21
22namespace operations_research {
23
24// Template to abstract the access to STL functions or vector values.
25template <typename ScalarType, typename Evaluator>
27 public:
28 explicit VectorOrFunction(Evaluator evaluator)
29 : evaluator_(std::move(evaluator)) {}
30 void Reset(Evaluator evaluator) { evaluator_ = std::move(evaluator); }
31 ScalarType operator()(int i) const { return evaluator_(i); }
32
33 private:
34 Evaluator evaluator_;
35};
36
37// Specialization for vectors.
38template <typename ScalarType>
39class VectorOrFunction<ScalarType, std::vector<ScalarType>> {
40 public:
41 explicit VectorOrFunction(std::vector<ScalarType> values)
42 : values_(std::move(values)) {}
43 void Reset(std::vector<ScalarType> values) { values_ = std::move(values); }
44 ScalarType operator()(int i) const { return values_[i]; }
45
46 private:
47 std::vector<ScalarType> values_;
48};
49
50// Template to abstract the access to STL functions or vector-base matrix
51// values.
52template <typename ScalarType, typename Evaluator, bool square = false>
54 public:
55 explicit MatrixOrFunction(Evaluator evaluator)
56 : evaluator_(std::move(evaluator)) {}
57 void Reset(Evaluator evaluator) { evaluator_ = std::move(evaluator); }
58 ScalarType operator()(int i, int j) const { return evaluator_(i, j); }
59 bool Check() const { return true; }
60
61 private:
62 Evaluator evaluator_;
63};
64
65// Specialization for vector-based matrices.
66template <typename ScalarType, bool square>
67class MatrixOrFunction<ScalarType, std::vector<std::vector<ScalarType>>,
68 square> {
69 public:
70 explicit MatrixOrFunction(std::vector<std::vector<ScalarType>> matrix)
71 : matrix_(std::move(matrix)) {}
72 void Reset(std::vector<std::vector<ScalarType>> matrix) {
73 matrix_ = std::move(matrix);
74 }
75 ScalarType operator()(int i, int j) const { return matrix_[i][j]; }
76 // Returns true if the matrix is square or rectangular.
77 // Intended to be used in a CHECK.
78 bool Check() const {
79 if (matrix_.empty()) return true;
80 const int size = square ? matrix_.size() : matrix_[0].size();
81 const char* msg =
82 square ? "Matrix must be square." : "Matrix must be rectangular.";
83 for (const std::vector<ScalarType>& row : matrix_) {
84 CHECK_EQ(size, row.size()) << msg;
85 }
86 return true;
87 }
88
89 private:
90 std::vector<std::vector<ScalarType>> matrix_;
91};
92
93} // namespace operations_research
94
95#endif // OR_TOOLS_UTIL_VECTOR_OR_FUNCTION_H_
IntegerValue size
ScalarType operator()(int i, int j) const
Template to abstract the access to STL functions or vector values.
RowIndex row
Definition markowitz.cc:186
In SWIG mode, we don't want anything besides these top-level includes.
STL namespace.