-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
vector_or_function.h
95 lines (81 loc) · 3.13 KB
/
vector_or_function.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2010-2024 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef OR_TOOLS_UTIL_VECTOR_OR_FUNCTION_H_
#define OR_TOOLS_UTIL_VECTOR_OR_FUNCTION_H_
#include <algorithm>
#include <vector>
#include "ortools/base/logging.h"
namespace operations_research {
// Template to abstract the access to STL functions or vector values.
template <typename ScalarType, typename Evaluator>
class VectorOrFunction {
public:
explicit VectorOrFunction(Evaluator evaluator)
: evaluator_(std::move(evaluator)) {}
void Reset(Evaluator evaluator) { evaluator_ = std::move(evaluator); }
ScalarType operator()(int i) const { return evaluator_(i); }
private:
Evaluator evaluator_;
};
// Specialization for vectors.
template <typename ScalarType>
class VectorOrFunction<ScalarType, std::vector<ScalarType>> {
public:
explicit VectorOrFunction(std::vector<ScalarType> values)
: values_(std::move(values)) {}
void Reset(std::vector<ScalarType> values) { values_ = std::move(values); }
ScalarType operator()(int i) const { return values_[i]; }
private:
std::vector<ScalarType> values_;
};
// Template to abstract the access to STL functions or vector-base matrix
// values.
template <typename ScalarType, typename Evaluator, bool square = false>
class MatrixOrFunction {
public:
explicit MatrixOrFunction(Evaluator evaluator)
: evaluator_(std::move(evaluator)) {}
void Reset(Evaluator evaluator) { evaluator_ = std::move(evaluator); }
ScalarType operator()(int i, int j) const { return evaluator_(i, j); }
bool Check() const { return true; }
private:
Evaluator evaluator_;
};
// Specialization for vector-based matrices.
template <typename ScalarType, bool square>
class MatrixOrFunction<ScalarType, std::vector<std::vector<ScalarType>>,
square> {
public:
explicit MatrixOrFunction(std::vector<std::vector<ScalarType>> matrix)
: matrix_(std::move(matrix)) {}
void Reset(std::vector<std::vector<ScalarType>> matrix) {
matrix_ = std::move(matrix);
}
ScalarType operator()(int i, int j) const { return matrix_[i][j]; }
// Returns true if the matrix is square or rectangular.
// Intended to be used in a CHECK.
bool Check() const {
if (matrix_.empty()) return true;
const int size = square ? matrix_.size() : matrix_[0].size();
const char* msg =
square ? "Matrix must be square." : "Matrix must be rectangular.";
for (const std::vector<ScalarType>& row : matrix_) {
CHECK_EQ(size, row.size()) << msg;
}
return true;
}
private:
std::vector<std::vector<ScalarType>> matrix_;
};
} // namespace operations_research
#endif // OR_TOOLS_UTIL_VECTOR_OR_FUNCTION_H_