Loading...
Searching...
No Matches
matrix_row_access.h
Go to the documentation of this file.
1/* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2 * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3 * Author(s): Hannah Schreiber
4 *
5 * Copyright (C) 2022 Inria
6 *
7 * Modification(s):
8 * - YYYY/MM Author: Description of the modification
9 */
10
17
18#ifndef PM_BASE_MATRIX_RA_H
19#define PM_BASE_MATRIX_RA_H
20
21#include <utility> //std::move
22
23namespace Gudhi {
24namespace persistence_matrix {
25
32struct Dummy_matrix_row_access {
33 Dummy_matrix_row_access([[maybe_unused]] unsigned int numberOfRows = 0) {};
34
35 friend void swap([[maybe_unused]] Dummy_matrix_row_access& d1, [[maybe_unused]] Dummy_matrix_row_access& d2) noexcept
36 {}
37};
38
52template <typename Row, typename Row_container, bool has_removable_rows, typename ID_index>
54{
55 public:
59 Matrix_row_access() : rows_(new Row_container()) {};
60
68 Matrix_row_access(unsigned int numberOfRows) : rows_(new Row_container())
69 {
70 if constexpr (!has_removable_rows) {
71 rows_->resize(numberOfRows);
72 }
73 }
74
81 : rows_(new Row_container()) // as the matrix is rebuild, the rows should not be copied.
82 {
83 if constexpr (!has_removable_rows) {
84 rows_->resize(toCopy.rows_->size());
85 }
86 }
87
93 Matrix_row_access(Matrix_row_access&& other) noexcept : rows_(std::exchange(other.rows_, nullptr)) {}
94
98 ~Matrix_row_access() { delete rows_; }
99
108 Row& get_row(ID_index rowIndex)
109 {
110 if constexpr (has_removable_rows) {
111 return rows_->at(rowIndex);
112 } else {
113 return rows_->operator[](rowIndex);
114 }
115 }
116
125 const Row& get_row(ID_index rowIndex) const
126 {
127 if constexpr (has_removable_rows) {
128 return rows_->at(rowIndex);
129 } else {
130 return rows_->operator[](rowIndex);
131 }
132 }
133
140 void erase_empty_row(ID_index rowIndex)
141 {
142 static_assert(has_removable_rows, "'erase_empty_row' is not implemented for the chosen options.");
143
144 auto it = rows_->find(rowIndex);
145 if (it != rows_->end() && it->second.empty()) rows_->erase(it);
146 }
147
152 {
153 if constexpr (has_removable_rows)
154 rows_->reserve(other.rows_->size());
155 else
156 rows_->resize(other.rows_->size());
157 return *this;
158 }
159
164 {
165 if (this == &other) return *this;
166
167 rows_ = std::exchange(other.rows_, nullptr);
168 return *this;
169 }
170
174 friend void swap(Matrix_row_access& matrix1, Matrix_row_access& matrix2) noexcept
175 {
176 std::swap(matrix1.rows_, matrix2.rows_);
177 }
178
179 protected:
180 Row_container* _get_rows_ptr() const { return rows_; }
181
182 void _resize(ID_index size)
183 {
184 static_assert(!has_removable_rows, "'_resize' is not implemented for the chosen options.");
185
186 if (rows_->size() <= size) rows_->resize(size + 1);
187 }
188
189 private:
194 Row_container* rows_;
195};
196
197} // namespace persistence_matrix
198} // namespace Gudhi
199
200#endif // PM_BASE_MATRIX_RA_H
friend void swap(Matrix_row_access &matrix1, Matrix_row_access &matrix2) noexcept
Swap operator.
Definition matrix_row_access.h:174
const Row & get_row(ID_index rowIndex) const
Returns the row at the given row index. The type of the row depends on the chosen options,...
Definition matrix_row_access.h:125
Matrix_row_access()
Default constructor.
Definition matrix_row_access.h:59
Matrix_row_access & operator=(const Matrix_row_access &other)
Assign operator.
Definition matrix_row_access.h:151
~Matrix_row_access()
Destructor.
Definition matrix_row_access.h:98
Matrix_row_access & operator=(Matrix_row_access &&other) noexcept
Move assign operator.
Definition matrix_row_access.h:163
void erase_empty_row(ID_index rowIndex)
Only available if PersistenceMatrixOptions::has_removable_rows is true. Removes the given row from th...
Definition matrix_row_access.h:140
Row & get_row(ID_index rowIndex)
Returns the row at the given row index. The type of the row depends on the chosen options,...
Definition matrix_row_access.h:108
Matrix_row_access(const Matrix_row_access &toCopy)
Copy constructor.
Definition matrix_row_access.h:80
Matrix_row_access(unsigned int numberOfRows)
Constructor reserving space for the given number of rows.
Definition matrix_row_access.h:68
Matrix_row_access(Matrix_row_access &&other) noexcept
Move constructor.
Definition matrix_row_access.h:93
Persistence matrix namespace.
Definition FieldOperators.h:18
Gudhi namespace.
Definition SimplicialComplexForAlpha.h:14