wrap flat matrices

This commit is contained in:
Laurent Perron
2025-07-28 10:34:48 -07:00
parent 87a66e6239
commit 13e2a14ab6
2 changed files with 17 additions and 0 deletions

View File

@@ -358,6 +358,7 @@ cc_library(
name = "vector_or_function",
hdrs = ["vector_or_function.h"],
deps = [
":flat_matrix",
"//ortools/base",
],
)
@@ -405,6 +406,7 @@ cc_library(
cc_library(
name = "random_engine",
hdrs = ["random_engine.h"],
deps = [],
)
cc_library(

View File

@@ -18,6 +18,7 @@
#include <vector>
#include "ortools/base/logging.h"
#include "ortools/util/flat_matrix.h"
namespace operations_research {
@@ -90,6 +91,20 @@ class MatrixOrFunction<ScalarType, std::vector<std::vector<ScalarType>>,
std::vector<std::vector<ScalarType>> matrix_;
};
// 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_;
};
} // namespace operations_research
#endif // OR_TOOLS_UTIL_VECTOR_OR_FUNCTION_H_