2025-01-10 11:35:44 +01:00
|
|
|
// Copyright 2010-2025 Google LLC
|
2016-01-26 13:50:39 +01:00
|
|
|
// 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.
|
|
|
|
|
|
2025-11-05 11:34:49 +01:00
|
|
|
#ifndef ORTOOLS_UTIL_VECTOR_OR_FUNCTION_H_
|
|
|
|
|
#define ORTOOLS_UTIL_VECTOR_OR_FUNCTION_H_
|
2016-01-26 13:50:39 +01:00
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2017-04-26 17:30:25 +02:00
|
|
|
#include "ortools/base/logging.h"
|
2025-08-06 10:53:59 +02:00
|
|
|
#include "ortools/util/flat_matrix.h"
|
2016-01-26 13:50:39 +01:00
|
|
|
|
|
|
|
|
namespace operations_research {
|
|
|
|
|
|
|
|
|
|
// Template to abstract the access to STL functions or vector values.
|
2020-10-22 23:36:58 +02:00
|
|
|
template <typename ScalarType, typename Evaluator>
|
|
|
|
|
class VectorOrFunction {
|
|
|
|
|
public:
|
2016-01-26 13:50:39 +01:00
|
|
|
explicit VectorOrFunction(Evaluator evaluator)
|
|
|
|
|
: evaluator_(std::move(evaluator)) {}
|
|
|
|
|
void Reset(Evaluator evaluator) { evaluator_ = std::move(evaluator); }
|
|
|
|
|
ScalarType operator()(int i) const { return evaluator_(i); }
|
|
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
private:
|
2016-01-26 13:50:39 +01:00
|
|
|
Evaluator evaluator_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Specialization for vectors.
|
|
|
|
|
template <typename ScalarType>
|
2020-11-16 17:50:35 +01:00
|
|
|
class VectorOrFunction<ScalarType, std::vector<ScalarType>> {
|
2020-10-22 23:36:58 +02:00
|
|
|
public:
|
2016-01-26 13:50:39 +01:00
|
|
|
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]; }
|
|
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
private:
|
2016-01-26 13:50:39 +01:00
|
|
|
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 {
|
2020-10-22 23:36:58 +02:00
|
|
|
public:
|
2016-01-26 13:50:39 +01:00
|
|
|
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; }
|
|
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
private:
|
2016-01-26 13:50:39 +01:00
|
|
|
Evaluator evaluator_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Specialization for vector-based matrices.
|
|
|
|
|
template <typename ScalarType, bool square>
|
2020-11-16 17:50:35 +01:00
|
|
|
class MatrixOrFunction<ScalarType, std::vector<std::vector<ScalarType>>,
|
2017-01-18 10:50:37 +01:00
|
|
|
square> {
|
2020-10-22 23:36:58 +02:00
|
|
|
public:
|
2020-11-16 17:50:35 +01:00
|
|
|
explicit MatrixOrFunction(std::vector<std::vector<ScalarType>> matrix)
|
2016-01-26 13:50:39 +01:00
|
|
|
: matrix_(std::move(matrix)) {}
|
2020-11-16 17:50:35 +01:00
|
|
|
void Reset(std::vector<std::vector<ScalarType>> matrix) {
|
2016-11-09 13:23:01 +01:00
|
|
|
matrix_ = std::move(matrix);
|
|
|
|
|
}
|
2016-01-26 13:50:39 +01:00
|
|
|
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 {
|
2020-10-22 23:36:58 +02:00
|
|
|
if (matrix_.empty()) return true;
|
2016-01-26 13:50:39 +01:00
|
|
|
const int size = square ? matrix_.size() : matrix_[0].size();
|
2020-10-29 14:25:39 +01:00
|
|
|
const char* msg =
|
2016-01-26 13:50:39 +01:00
|
|
|
square ? "Matrix must be square." : "Matrix must be rectangular.";
|
2020-10-29 14:25:39 +01:00
|
|
|
for (const std::vector<ScalarType>& row : matrix_) {
|
2016-01-26 13:50:39 +01:00
|
|
|
CHECK_EQ(size, row.size()) << msg;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
private:
|
2020-11-16 17:50:35 +01:00
|
|
|
std::vector<std::vector<ScalarType>> matrix_;
|
2016-01-26 13:50:39 +01:00
|
|
|
};
|
|
|
|
|
|
2025-08-06 10:53:59 +02:00
|
|
|
// Specialization for FlatMatrix<>, which is faster than vector<vector<>>.
|
|
|
|
|
template <typename ScalarType, bool square>
|
|
|
|
|
class MatrixOrFunction<ScalarType, FlatMatrix<ScalarType>, square> {
|
|
|
|
|
public:
|
|
|
|
|
explicit MatrixOrFunction(FlatMatrix<ScalarType> matrix)
|
|
|
|
|
: matrix_(std::move(matrix)) {}
|
|
|
|
|
void Reset(FlatMatrix<ScalarType> matrix) { matrix_ = std::move(matrix); }
|
|
|
|
|
ScalarType operator()(int i, int j) const { return matrix_[i][j]; }
|
|
|
|
|
bool Check() const { return true; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
FlatMatrix<ScalarType> matrix_;
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
} // namespace operations_research
|
2016-01-26 13:50:39 +01:00
|
|
|
|
2025-11-05 11:34:49 +01:00
|
|
|
#endif // ORTOOLS_UTIL_VECTOR_OR_FUNCTION_H_
|