Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
flat_matrix.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_FLAT_MATRIX_H_
15#define OR_TOOLS_UTIL_FLAT_MATRIX_H_
16
17// A very simple flattened 2D array of fixed size. It's movable, copyable.
18// It can also be assigned.
19// This was originally made to replace uses of vector<vector<...>> where each
20// vector had a fixed size: vector<vector<>> has much worse performance in a
21// highly concurrent setting, because it does a lot of memory allocations.
22
23#include <cstddef>
24#include <vector>
25
26#include "absl/types/span.h"
27
28namespace operations_research {
29
30// NOTE(user): T=bool is not yet supported (the [] operator doesn't work).
31template <typename T>
33 public:
34 FlatMatrix() : num_rows_(0), num_cols_(0) {}
36 : num_rows_(num_rows),
37 num_cols_(num_cols),
38 array_(num_rows_ * num_cols_) {}
39 FlatMatrix(size_t num_rows, size_t num_cols, const T& elem)
40 : num_rows_(num_rows),
41 num_cols_(num_cols),
42 array_(num_rows_ * num_cols_, elem) {}
43
44 size_t num_rows() const { return num_rows_; }
45 size_t num_cols() const { return num_cols_; }
46
47 absl::Span<T> operator[](size_t row) {
48 return absl::Span<T>(array_.data() + row * num_cols_, num_cols_);
49 }
50 absl::Span<const T> operator[](size_t row) const {
51 return {array_.data() + row * num_cols_, num_cols_};
52 }
53
54 // All the elements of the FlatMatrix.
55 absl::Span<T> all_elements() { return absl::MakeSpan(array_); }
56 absl::Span<const T> all_elements() const {
57 return absl::MakeConstSpan(array_);
58 }
59
60 // To iterate over the rows of the FlatMatrix. Example:
61 // FlatMatrix<double> matrix(23, 45);
62 // for (absl::Span<const double> row : matrix.rows()) {
63 // LOG(INFO) << DUMP_VARS(row);
64 // }
66 public:
67 typedef T value_type;
68 ConstRowsIterator(const T* ptr, size_t row, size_t row_size);
69 absl::Span<const T> operator*() const { return {ptr_, row_size_}; }
71 bool operator!=(const ConstRowsIterator& rhs) const;
72
73 private:
74 const T* ptr_;
75 size_t row_;
76 const size_t row_size_;
77 };
78 struct ConstRows {
79 typedef absl::Span<const T> value_type;
82 ConstRowsIterator end() const;
83 FlatMatrix* matrix = nullptr;
84 };
85 ConstRows rows() { return {.matrix = this}; }
86
87 private:
88 // Those are non-const only to support the assignment operators.
89 size_t num_rows_;
90 size_t num_cols_;
91 // NOTE(user): We could use a simpler unique_ptr<T[]> or even a self-managed
92 // memory block, but we'd need to define the copy constructor.
93 std::vector<T> array_;
94};
95
96// Implementation of the templates.
97template <typename T>
99 size_t row_size)
100 : ptr_(ptr), row_(row), row_size_(row_size) {}
101
102template <typename T>
105 ptr_ += row_size_;
106 ++row_;
107 return *this;
108}
109
110template <typename T>
112 const ConstRowsIterator& rhs) const {
113 return std::tie(ptr_, row_, row_size_) !=
114 std::tie(rhs.ptr_, rhs.row_, rhs.row_size_);
115}
116
117template <typename T>
119 const {
120 return ConstRowsIterator(matrix->array_.data(), /*row=*/0,
121 /*row_size=*/matrix->num_cols_);
122}
123
124template <typename T>
126 const {
127 return ConstRowsIterator(matrix->array_.data() + matrix->array_.size(),
128 /*row=*/matrix->num_rows_,
129 /*row_size=*/matrix->num_cols_);
130}
131
132} // namespace operations_research
133
134#endif // OR_TOOLS_UTIL_FLAT_MATRIX_H_
bool operator!=(const ConstRowsIterator &rhs) const
ConstRowsIterator(const T *ptr, size_t row, size_t row_size)
Implementation of the templates.
Definition flat_matrix.h:98
NOTE(user): T=bool is not yet supported (the [] operator doesn't work).
Definition flat_matrix.h:32
FlatMatrix(size_t num_rows, size_t num_cols)
Definition flat_matrix.h:35
FlatMatrix(size_t num_rows, size_t num_cols, const T &elem)
Definition flat_matrix.h:39
absl::Span< const T > all_elements() const
Definition flat_matrix.h:56
absl::Span< T > operator[](size_t row)
Definition flat_matrix.h:47
absl::Span< T > all_elements()
All the elements of the FlatMatrix.
Definition flat_matrix.h:55
absl::Span< const T > operator[](size_t row) const
Definition flat_matrix.h:50
RowIndex row
Definition markowitz.cc:186
In SWIG mode, we don't want anything besides these top-level includes.