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-24 Inria
6  *
7  * Modification(s):
8  * - YYYY/MM Author: Description of the modification
9  */
10 
17 #ifndef PM_BASE_MATRIX_RA_H
18 #define PM_BASE_MATRIX_RA_H
19 
20 #include <utility> //std::move
21 
22 namespace Gudhi {
23 namespace persistence_matrix {
24 
32  Dummy_matrix_row_access([[maybe_unused]] unsigned int numberOfRows = 0){};
33 
34  friend void swap([[maybe_unused]] Dummy_matrix_row_access& d1, [[maybe_unused]] Dummy_matrix_row_access& d2) {}
35 };
36 
50 template <typename Row_type, typename Row_container_type, bool has_removable_rows, typename id_index>
52 {
53  public:
57  Matrix_row_access() : rows_(new Row_container_type()){};
65  Matrix_row_access(unsigned int numberOfRows) : rows_(new Row_container_type()) {
66  if constexpr (!has_removable_rows) {
67  rows_->resize(numberOfRows);
68  }
69  }
76  : rows_(new Row_container_type()) // as the matrix is rebuild, the rows should not be copied.
77  {
78  if constexpr (!has_removable_rows) {
79  rows_->resize(toCopy.rows_->size());
80  }
81  }
87  Matrix_row_access(Matrix_row_access&& other) noexcept : rows_(std::exchange(other.rows_, nullptr)) {}
91  ~Matrix_row_access() { delete rows_; }
92 
101  Row_type& get_row(id_index rowIndex) {
102  if constexpr (has_removable_rows) {
103  return rows_->at(rowIndex);
104  } else {
105  return rows_->operator[](rowIndex);
106  }
107  }
116  const Row_type& get_row(id_index rowIndex) const {
117  if constexpr (has_removable_rows) {
118  return rows_->at(rowIndex);
119  } else {
120  return rows_->operator[](rowIndex);
121  }
122  }
129  void erase_empty_row(id_index rowIndex) {
130  static_assert(has_removable_rows, "'erase_empty_row' is not implemented for the chosen options.");
131 
132  auto it = rows_->find(rowIndex);
133  if (it != rows_->end() && it->second.empty()) rows_->erase(it);
134  }
135 
140  if constexpr (has_removable_rows)
141  rows_->reserve(other.rows_->size());
142  else
143  rows_->resize(other.rows_->size());
144  return *this;
145  }
149  friend void swap(Matrix_row_access& matrix1, Matrix_row_access& matrix2) { std::swap(matrix1.rows_, matrix2.rows_); }
150 
151  protected:
156  Row_container_type* rows_;
157 };
158 
159 } // namespace persistence_matrix
160 } // namespace Gudhi
161 
162 #endif // PM_BASE_MATRIX_RA_H
Class managing the row access for the inheritating matrix.
Definition: matrix_row_access.h:52
Matrix_row_access & operator=(const Matrix_row_access &other)
Assign operator.
Definition: matrix_row_access.h:139
Matrix_row_access(unsigned int numberOfRows)
Constructor reserving space for the given number of rows.
Definition: matrix_row_access.h:65
friend void swap(Matrix_row_access &matrix1, Matrix_row_access &matrix2)
Swap operator.
Definition: matrix_row_access.h:149
const Row_type & get_row(id_index rowIndex) const
Returns the row at the given row index. The type of the row depends on the choosen options,...
Definition: matrix_row_access.h:116
Row_type & get_row(id_index rowIndex)
Returns the row at the given row index. The type of the row depends on the choosen options,...
Definition: matrix_row_access.h:101
Matrix_row_access(const Matrix_row_access &toCopy)
Copy constructor.
Definition: matrix_row_access.h:75
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:129
Matrix_row_access()
Default constructor.
Definition: matrix_row_access.h:57
Matrix_row_access(Matrix_row_access &&other) noexcept
Move constructor.
Definition: matrix_row_access.h:87
~Matrix_row_access()
Destructor.
Definition: matrix_row_access.h:91
Gudhi namespace.
Definition: SimplicialComplexForAlpha.h:14
Empty structure. Inheritated instead of Matrix_row_access, when the the row access is not enabled.
Definition: matrix_row_access.h:31