base_swap.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-23 Inria
6 *
7 * Modification(s):
8 * - YYYY/MM Author: Description of the modification
9 */
10
18#ifndef PM_BASE_SWAP_H
19#define PM_BASE_SWAP_H
20
21#include <utility> //std::swap, std::move & std::exchange
22#include <algorithm> //std::max
23
24namespace Gudhi {
25namespace persistence_matrix {
26
34 friend void swap([[maybe_unused]] Dummy_base_swap& d1, [[maybe_unused]] Dummy_base_swap& d2) {}
35
36 Dummy_base_swap([[maybe_unused]] unsigned int numberOfColumns = 0) {}
37};
38
48template <class Master_matrix, class Base_matrix>
49class Base_swap {
50 public:
51 using Column_container = typename Master_matrix::Column_container;
52 using Index = typename Master_matrix::Index;
53 using ID_index = typename Master_matrix::ID_index;
58 Base_swap();
64 Base_swap(unsigned int numberOfColumns);
70 Base_swap(const Base_swap& matrixToCopy) = default;
76 Base_swap(Base_swap&& other) noexcept;
77
85 void swap_columns(Index columnIndex1, Index columnIndex2);
93 void swap_rows(ID_index rowIndex1, ID_index rowIndex2);
94
102 friend void swap(Base_swap& base1, Base_swap& base2) {
103 base1.indexToRow_.swap(base2.indexToRow_);
104 base1.rowToIndex_.swap(base2.rowToIndex_);
105 std::swap(base1.rowSwapped_, base2.rowSwapped_);
106 }
107
108 protected:
109 using Index_dictionary = typename Master_matrix::template Dictionary<Index>;
110 using Row_dictionary = typename Master_matrix::template Dictionary<ID_index>;
111
112 Index_dictionary indexToRow_;
113 Row_dictionary rowToIndex_;
114 bool rowSwapped_;
116 void _orderRows();
117
118 //access to inheriting matrix class
119 constexpr Base_matrix* _matrix() { return static_cast<Base_matrix*>(this); }
120 constexpr const Base_matrix* _matrix() const { return static_cast<const Base_matrix*>(this); }
121};
122
123template <class Master_matrix, class Base_matrix>
125{}
126
127template <class Master_matrix, class Base_matrix>
128inline Base_swap<Master_matrix, Base_matrix>::Base_swap(unsigned int numberOfColumns)
129 : indexToRow_(numberOfColumns), rowToIndex_(numberOfColumns), rowSwapped_(false)
130{
131 for (Index i = 0; i < numberOfColumns; i++) {
132 indexToRow_[i] = i;
133 rowToIndex_[i] = i;
134 }
135}
136
137template <class Master_matrix, class Base_matrix>
139 : indexToRow_(std::move(other.indexToRow_)),
140 rowToIndex_(std::move(other.rowToIndex_)),
141 rowSwapped_(std::exchange(other.rowSwapped_, 0))
142{}
143
144template <class Master_matrix, class Base_matrix>
146{
147 swap(_matrix()->matrix_.at(columnIndex1), _matrix()->matrix_.at(columnIndex2));
148 if constexpr (Master_matrix::Option_list::has_row_access) rowSwapped_ = true; //to update column index in entries.
149}
150
151template <class Master_matrix, class Base_matrix>
153{
154 rowSwapped_ = true;
155
156 if constexpr (Master_matrix::Option_list::has_map_column_container) {
157 auto it1 = indexToRow_.find(rowIndex1);
158 auto it2 = indexToRow_.find(rowIndex2);
159
160 if (it1 == indexToRow_.end() && it2 == indexToRow_.end()) return;
161
162 if (it1 == indexToRow_.end()) {
163 indexToRow_.emplace(rowIndex1, it2->second);
164 rowToIndex_.at(it2->second) = rowIndex1;
165 indexToRow_.erase(it2->second);
166 return;
167 }
168
169 if (it2 == indexToRow_.end()) {
170 indexToRow_.emplace(rowIndex2, it1->second);
171 rowToIndex_.at(it1->second) = rowIndex2;
172 indexToRow_.erase(it1);
173 return;
174 }
175
176 std::swap(rowToIndex_.at(it1->second), rowToIndex_.at(it2->second));
177 std::swap(it1->second, it2->second);
178 } else {
179 for (auto i = indexToRow_.size(); i <= std::max(rowIndex1, rowIndex2); ++i) indexToRow_.push_back(i);
180
181 std::swap(rowToIndex_[indexToRow_[rowIndex1]], rowToIndex_[indexToRow_[rowIndex2]]);
182 std::swap(indexToRow_[rowIndex1], indexToRow_[rowIndex2]);
183 }
184}
185
186template <class Master_matrix, class Base_matrix>
188{
189 indexToRow_.swap(other.indexToRow_);
190 rowToIndex_.swap(other.rowToIndex_);
191 std::swap(rowSwapped_, other.rowSwapped_);
192 return *this;
193}
194
195template <class Master_matrix, class Base_matrix>
197{
198 for (unsigned int i = 0; i < _matrix()->get_number_of_columns(); i++) {
199 _matrix()->matrix_.at(i).reorder(rowToIndex_, i);
200 }
201 for (Index i = 0; i < _matrix()->get_number_of_columns(); i++) {
202 indexToRow_[i] = i;
203 rowToIndex_[i] = i;
204 }
205 rowSwapped_ = false;
206}
207
208} // namespace persistence_matrix
209} // namespace Gudhi
210
211#endif // PM_BASE_SWAP_H
A basic matrix structure allowing to easily manipulate and access entire columns and rows,...
Definition: Base_matrix.h:39
Class managing the column and row swaps in Base_matrix and Boundary_matrix.
Definition: base_swap.h:49
Base_swap(const Base_swap &matrixToCopy)=default
Copy constructor.
Base_swap & operator=(Base_swap other)
Assign operator.
Definition: base_swap.h:187
friend void swap(Base_swap &base1, Base_swap &base2)
Swap operator.
Definition: base_swap.h:102
void swap_rows(ID_index rowIndex1, ID_index rowIndex2)
Swaps the two rows at the given indices, but in a lazy manner. That is, the swap is registered but no...
Definition: base_swap.h:152
Base_swap()
Default constructor.
Definition: base_swap.h:124
void swap_columns(Index columnIndex1, Index columnIndex2)
Swaps the two columns at given indices in the column container. Does not updates the column index val...
Definition: base_swap.h:145
typename Master_matrix::Index Index
Definition: base_swap.h:52
typename Master_matrix::Column_container Column_container
Definition: base_swap.h:51
typename Master_matrix::ID_index ID_index
Definition: base_swap.h:53
Data structure for matrices, and in particular thought for matrices representing filtered complexes i...
Definition: Matrix.h:144
Gudhi namespace.
Definition: SimplicialComplexForAlpha.h:14
Empty structure. Inherited instead of Base_swap, when the column and row swaps are not enabled.
Definition: base_swap.h:33