Loading...
Searching...
No Matches
base_swap.h
Go to the documentation of this file.
/* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
* See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
* Author(s): Hannah Schreiber
*
* Copyright (C) 2022-23 Inria
*
* Modification(s):
* - YYYY/MM Author: Description of the modification
*/
#ifndef PM_BASE_SWAP_H
#define PM_BASE_SWAP_H
#include <utility> //std::swap, std::move & std::exchange
#include <algorithm> //std::max
namespace Gudhi {
namespace persistence_matrix {
struct Dummy_base_swap {
friend void swap([[maybe_unused]] Dummy_base_swap& d1, [[maybe_unused]] Dummy_base_swap& d2) {}
Dummy_base_swap([[maybe_unused]] unsigned int numberOfColumns = 0) {}
};
template <class Master_matrix, class Base_matrix>
class Base_swap {
public:
using Column_container = typename Master_matrix::Column_container;
using Index = typename Master_matrix::Index;
using ID_index = typename Master_matrix::ID_index;
Base_swap();
Base_swap(unsigned int numberOfColumns);
Base_swap(const Base_swap& matrixToCopy) = default;
Base_swap(Base_swap&& other) noexcept;
void swap_columns(Index columnIndex1, Index columnIndex2);
void swap_rows(ID_index rowIndex1, ID_index rowIndex2);
Base_swap& operator=(Base_swap other);
friend void swap(Base_swap& base1, Base_swap& base2) {
base1.indexToRow_.swap(base2.indexToRow_);
base1.rowToIndex_.swap(base2.rowToIndex_);
std::swap(base1.rowSwapped_, base2.rowSwapped_);
}
protected:
using Index_dictionary = typename Master_matrix::template Dictionary<Index>;
using Row_dictionary = typename Master_matrix::template Dictionary<ID_index>;
Index_dictionary indexToRow_;
Row_dictionary rowToIndex_;
bool rowSwapped_;
void _orderRows();
//access to inheriting matrix class
constexpr Base_matrix* _matrix() { return static_cast<Base_matrix*>(this); }
constexpr const Base_matrix* _matrix() const { return static_cast<const Base_matrix*>(this); }
};
template <class Master_matrix, class Base_matrix>
inline Base_swap<Master_matrix, Base_matrix>::Base_swap() : rowSwapped_(false)
{}
template <class Master_matrix, class Base_matrix>
inline Base_swap<Master_matrix, Base_matrix>::Base_swap(unsigned int numberOfColumns)
: indexToRow_(numberOfColumns), rowToIndex_(numberOfColumns), rowSwapped_(false)
{
for (Index i = 0; i < numberOfColumns; i++) {
indexToRow_[i] = i;
rowToIndex_[i] = i;
}
}
template <class Master_matrix, class Base_matrix>
inline Base_swap<Master_matrix, Base_matrix>::Base_swap(Base_swap&& other) noexcept
: indexToRow_(std::move(other.indexToRow_)),
rowToIndex_(std::move(other.rowToIndex_)),
rowSwapped_(std::exchange(other.rowSwapped_, 0))
{}
template <class Master_matrix, class Base_matrix>
inline void Base_swap<Master_matrix, Base_matrix>::swap_columns(Index columnIndex1, Index columnIndex2)
{
swap(_matrix()->matrix_.at(columnIndex1), _matrix()->matrix_.at(columnIndex2));
if constexpr (Master_matrix::Option_list::has_row_access) rowSwapped_ = true; //to update column index in entries.
}
template <class Master_matrix, class Base_matrix>
inline void Base_swap<Master_matrix, Base_matrix>::swap_rows(ID_index rowIndex1, ID_index rowIndex2)
{
rowSwapped_ = true;
if constexpr (Master_matrix::Option_list::has_map_column_container) {
auto it1 = indexToRow_.find(rowIndex1);
auto it2 = indexToRow_.find(rowIndex2);
if (it1 == indexToRow_.end() && it2 == indexToRow_.end()) return;
if (it1 == indexToRow_.end()) {
indexToRow_.emplace(rowIndex1, it2->second);
rowToIndex_.at(it2->second) = rowIndex1;
indexToRow_.erase(it2->second);
return;
}
if (it2 == indexToRow_.end()) {
indexToRow_.emplace(rowIndex2, it1->second);
rowToIndex_.at(it1->second) = rowIndex2;
indexToRow_.erase(it1);
return;
}
std::swap(rowToIndex_.at(it1->second), rowToIndex_.at(it2->second));
std::swap(it1->second, it2->second);
} else {
for (auto i = indexToRow_.size(); i <= std::max(rowIndex1, rowIndex2); ++i) indexToRow_.push_back(i);
std::swap(rowToIndex_[indexToRow_[rowIndex1]], rowToIndex_[indexToRow_[rowIndex2]]);
std::swap(indexToRow_[rowIndex1], indexToRow_[rowIndex2]);
}
}
template <class Master_matrix, class Base_matrix>
inline Base_swap<Master_matrix, Base_matrix>& Base_swap<Master_matrix, Base_matrix>::operator=(Base_swap other)
{
indexToRow_.swap(other.indexToRow_);
rowToIndex_.swap(other.rowToIndex_);
std::swap(rowSwapped_, other.rowSwapped_);
return *this;
}
template <class Master_matrix, class Base_matrix>
inline void Base_swap<Master_matrix, Base_matrix>::_orderRows()
{
for (unsigned int i = 0; i < _matrix()->get_number_of_columns(); i++) {
_matrix()->matrix_.at(i).reorder(rowToIndex_, i);
}
for (Index i = 0; i < _matrix()->get_number_of_columns(); i++) {
indexToRow_[i] = i;
rowToIndex_[i] = i;
}
rowSwapped_ = false;
}
} // namespace persistence_matrix
} // namespace Gudhi
#endif // PM_BASE_SWAP_H
Gudhi::persistence_matrix::Base_matrix
A basic matrix structure allowing to easily manipulate and access entire columns and rows,...
Definition Base_matrix.h:39
Gudhi::persistence_matrix::Base_swap::Base_swap
Base_swap(const Base_swap &matrixToCopy)=default
Copy constructor.
Gudhi::persistence_matrix::Base_swap::operator=
Base_swap & operator=(Base_swap other)
Assign operator.
Definition base_swap.h:187
Gudhi::persistence_matrix::Base_swap::swap
friend void swap(Base_swap &base1, Base_swap &base2)
Swap operator.
Definition base_swap.h:102
Gudhi::persistence_matrix::Base_swap::swap_rows
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
Gudhi::persistence_matrix::Base_swap::Base_swap
Base_swap()
Default constructor.
Definition base_swap.h:124
Gudhi::persistence_matrix::Base_swap::swap_columns
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
Gudhi::persistence_matrix::Base_swap::Index
typename Master_matrix::Index Index
Definition base_swap.h:52
Gudhi::persistence_matrix::Base_swap::Column_container
typename Master_matrix::Column_container Column_container
Definition base_swap.h:51
Gudhi::persistence_matrix::Base_swap::ID_index
typename Master_matrix::ID_index ID_index
Definition base_swap.h:53
Gudhi::persistence_matrix
Persistence matrix namespace.
Definition FieldOperators.h:18
Gudhi
Gudhi namespace.
Definition SimplicialComplexForAlpha.h:14