OR-Tools  8.2
vector_or_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#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
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} // namespace operations_research
95
96#endif // OR_TOOLS_UTIL_VECTOR_OR_FUNCTION_H_
#define CHECK_EQ(val1, val2)
Definition: base/logging.h:697
ScalarType operator()(int i, int j) const
RowIndex row
Definition: markowitz.cc:175
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
STL namespace.