Skip to content

Commit

Permalink
removal of child pointer + factorization of intrusive list column
Browse files Browse the repository at this point in the history
  • Loading branch information
hschreiber committed Sep 28, 2023
1 parent cc168c2 commit bd9234d
Show file tree
Hide file tree
Showing 26 changed files with 2,438 additions and 1,484 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ namespace persistence_matrix {

template<class Master_matrix>
class Base_matrix
: public Master_matrix::Base_swap_option, protected Master_matrix::Matrix_row_access_option
: public Master_matrix::template Base_swap_option<Base_matrix<Master_matrix> >,
protected Master_matrix::Matrix_row_access_option
{
public:
using index = typename Master_matrix::index;
Expand Down Expand Up @@ -73,8 +74,8 @@ class Base_matrix

Base_matrix& operator=(const Base_matrix& other);
friend void swap(Base_matrix& matrix1, Base_matrix& matrix2){
swap(static_cast<typename Master_matrix::Base_swap_option&>(matrix1),
static_cast<typename Master_matrix::Base_swap_option&>(matrix2));
swap(static_cast<typename Master_matrix::template Base_swap_option<Base_matrix<Master_matrix> >&>(matrix1),
static_cast<typename Master_matrix::template Base_swap_option<Base_matrix<Master_matrix> >&>(matrix2));
matrix1.matrix_.swap(matrix2.matrix_);
std::swap(matrix1.nextInsertIndex_, matrix2.nextInsertIndex_);

Expand Down Expand Up @@ -102,7 +103,7 @@ class Base_matrix
void print(); //for debug

private:
using swap_opt = typename Master_matrix::Base_swap_option;
using swap_opt = typename Master_matrix::template Base_swap_option<Base_matrix<Master_matrix> >;
using ra_opt = typename Master_matrix::Matrix_row_access_option;
using matrix_type = typename Master_matrix::column_container_type;
using cell_rep_type = typename std::conditional<
Expand All @@ -111,6 +112,8 @@ class Base_matrix
std::pair<index,Field_element_type>
>::type;

friend swap_opt; //direct access to matrix_ to avoid row reorder.

matrix_type matrix_;
index nextInsertIndex_;

Expand All @@ -119,15 +122,15 @@ class Base_matrix

template<class Master_matrix>
inline Base_matrix<Master_matrix>::Base_matrix()
: swap_opt(matrix_),
: swap_opt(),
ra_opt(),
nextInsertIndex_(0)
{}

template<class Master_matrix>
template<class Container_type>
inline Base_matrix<Master_matrix>::Base_matrix(const std::vector<Container_type> &columns)
: swap_opt(matrix_, columns.size()),
: swap_opt(columns.size()),
ra_opt(columns.size()), //not ideal if max row index is much smaller than max column index, does that happen often?
matrix_(!Master_matrix::Option_list::has_removable_columns && Master_matrix::Option_list::has_row_access ? 0 : columns.size()),
nextInsertIndex_(columns.size())
Expand All @@ -154,7 +157,7 @@ inline Base_matrix<Master_matrix>::Base_matrix(const std::vector<Container_type>

template<class Master_matrix>
inline Base_matrix<Master_matrix>::Base_matrix(unsigned int numberOfColumns)
: swap_opt(matrix_, numberOfColumns),
: swap_opt(numberOfColumns),
ra_opt(numberOfColumns),
matrix_(!Master_matrix::Option_list::has_removable_columns && Master_matrix::Option_list::has_row_access ? 0 : numberOfColumns),
nextInsertIndex_(0)
Expand All @@ -165,13 +168,10 @@ inline Base_matrix<Master_matrix>::Base_matrix(unsigned int numberOfColumns)

template<class Master_matrix>
inline Base_matrix<Master_matrix>::Base_matrix(const Base_matrix &matrixToCopy)
: swap_opt(matrixToCopy),
ra_opt(matrixToCopy),
: swap_opt(static_cast<const swap_opt&>(matrixToCopy)),
ra_opt(static_cast<const ra_opt&>(matrixToCopy)),
nextInsertIndex_(matrixToCopy.nextInsertIndex_)
{
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps)
swap_opt::matrix_ = &matrix_;

if constexpr (Master_matrix::Option_list::has_row_access){
matrix_.reserve(matrixToCopy.matrix_.size());
if constexpr (Master_matrix::Option_list::has_removable_columns){
Expand All @@ -191,14 +191,11 @@ inline Base_matrix<Master_matrix>::Base_matrix(const Base_matrix &matrixToCopy)

template<class Master_matrix>
inline Base_matrix<Master_matrix>::Base_matrix(Base_matrix &&other) noexcept
: swap_opt(std::move(other)),
ra_opt(std::move(other)),
: swap_opt(std::move(static_cast<swap_opt&>(other))),
ra_opt(std::move(static_cast<ra_opt&>(other))),
matrix_(std::move(other.matrix_)),
nextInsertIndex_(std::exchange(other.nextInsertIndex_, 0))
{
if constexpr (Master_matrix::Option_list::has_column_and_row_swaps)
swap_opt::matrix_ = &matrix_;

if constexpr (Master_matrix::Option_list::has_row_access){
if constexpr (Master_matrix::Option_list::has_removable_columns){
for (auto& p : matrix_){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
namespace Gudhi {
namespace persistence_matrix {

struct Dummy_matrix_row_access{
Dummy_matrix_row_access(){};
Dummy_matrix_row_access([[maybe_unused]] unsigned int numberOfColumns){};
};

template<typename Row_type, typename Row_container_type, bool has_removable_rows, typename index>
class Base_matrix_row_access
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,27 @@ class Base_matrix_with_column_compression : protected Master_matrix::Matrix_row_
Column_type(const Container_type& nonZeroRowIndices)
: Base(nonZeroRowIndices)
{}
template<class Container_type>
Column_type(const Container_type& nonZeroRowIndices, dimension_type dimension)
: Base(nonZeroRowIndices, dimension)
{}
template<class Row_container_type>
Column_type(index columnIndex, Row_container_type &rowContainer)
: Base(columnIndex, rowContainer)
{}
template<class Container_type, class Row_container_type>
Column_type(index columnIndex, const Container_type& nonZeroRowIndices, Row_container_type &rowContainer)
: Base(columnIndex, nonZeroRowIndices, rowContainer)
{}
template<class Container_type>
Column_type(const Container_type& nonZeroRowIndices, dimension_type dimension)
: Base(nonZeroRowIndices, dimension)
{}
template<class Container_type, class Row_container_type>
Column_type(index columnIndex, const Container_type& nonZeroRowIndices, dimension_type dimension, Row_container_type &rowContainer)
: Base(columnIndex, nonZeroRowIndices, dimension, rowContainer)
{}
Column_type(const Column_type& column)
: Base(static_cast<const Base&>(column))
{}
Column_type(const Column_type& column, index columnIndex)
: Base(static_cast<const Base&>(column), columnIndex)
{}
template<class Row_container_type>
Column_type(const Column_type& column, index columnIndex, Row_container_type &rowContainer)
: Base(static_cast<const Base&>(column), columnIndex, rowContainer)
{}
Column_type(Column_type&& column) noexcept
: Base(std::move(static_cast<Base&&>(column)))
: Base(std::move(static_cast<Base&>(column)))
{}

index get_rep() const{
Expand All @@ -88,7 +81,7 @@ class Base_matrix_with_column_compression : protected Master_matrix::Matrix_row_
struct Hasher {
size_t operator()(const Column_type& c) const
{
return std::hash<Base>()(c);
return std::hash<Base>()(static_cast<Base>(c));
}
};

Expand Down Expand Up @@ -216,7 +209,7 @@ inline Base_matrix_with_column_compression<Master_matrix>::Base_matrix_with_colu

template<class Master_matrix>
inline Base_matrix_with_column_compression<Master_matrix>::Base_matrix_with_column_compression(const Base_matrix_with_column_compression &matrixToCopy)
: ra_opt(matrixToCopy),
: ra_opt(static_cast<const ra_opt&>(matrixToCopy)),
columnClasses_(matrixToCopy.columnClasses_),
repToColumn_(matrixToCopy.repToColumn_.size(), nullptr),
nextColumnIndex_(0)
Expand All @@ -237,7 +230,7 @@ inline Base_matrix_with_column_compression<Master_matrix>::Base_matrix_with_colu

template<class Master_matrix>
inline Base_matrix_with_column_compression<Master_matrix>::Base_matrix_with_column_compression(Base_matrix_with_column_compression &&other) noexcept
: ra_opt(std::move(other)),
: ra_opt(std::move(static_cast<ra_opt&>(other))),
columnToRep_(std::move(other.columnToRep_)),
columnClasses_(std::move(other.columnClasses_)),
repToColumn_(std::move(other.repToColumn_)),
Expand Down Expand Up @@ -512,7 +505,7 @@ inline void Base_matrix_with_column_compression<Master_matrix>::_insert_column(i
return;
}

repToColumn_[columnIndex]->set_rep(columnIndex);
col.set_rep(columnIndex);
auto res = columnToRep_.insert(col);
if (res.first->get_rep() != columnIndex){
_insert_double_column(columnIndex, res.first);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
namespace Gudhi {
namespace persistence_matrix {

struct Dummy_base_pairing{
Dummy_base_pairing& operator=([[maybe_unused]] Dummy_base_pairing other){return *this;}
friend void swap([[maybe_unused]] Dummy_base_pairing& d1, [[maybe_unused]] Dummy_base_pairing& d2){}

Dummy_base_pairing(){}
Dummy_base_pairing([[maybe_unused]] const Dummy_base_pairing& matrixToCopy){}
Dummy_base_pairing([[maybe_unused]] Dummy_base_pairing&& other) noexcept{}
};

template<class Master_matrix>
class Base_pairing
{
Expand All @@ -27,7 +36,7 @@ class Base_pairing
using dimension_type = typename Master_matrix::dimension_type;
using Bar = typename Master_matrix::Bar;

Base_pairing(matrix_type& matrix, dimension_type& maxDim);
Base_pairing();
Base_pairing(const Base_pairing& matrixToCopy);
Base_pairing(Base_pairing&& other) noexcept;

Expand All @@ -43,36 +52,34 @@ class Base_pairing
protected:
using column_type = typename Master_matrix::Column_type;
using dictionnary_type = typename Master_matrix::bar_dictionnary_type;
using base_matrix = typename Master_matrix::Boundary_matrix_type;

matrix_type* matrix_;
dimension_type* maxDim_;
barcode_type barcode_;
dictionnary_type indexToBar_;
bool isReduced_;

void _reduce();
void _remove_maximal(index columnIndex);

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>
inline Base_pairing<Master_matrix>::Base_pairing(matrix_type &matrix, dimension_type &maxDim)
: matrix_(&matrix), maxDim_(&maxDim), isReduced_(false)
inline Base_pairing<Master_matrix>::Base_pairing()
: isReduced_(false)
{}

template<class Master_matrix>
inline Base_pairing<Master_matrix>::Base_pairing(const Base_pairing &matrixToCopy)
: matrix_(matrixToCopy.matrix_),
maxDim_(matrixToCopy.maxDim_),
barcode_(matrixToCopy.barcode_),
: barcode_(matrixToCopy.barcode_),
indexToBar_(matrixToCopy.indexToBar_),
isReduced_(matrixToCopy.isReduced_)
{}

template<class Master_matrix>
inline Base_pairing<Master_matrix>::Base_pairing(Base_pairing<Master_matrix> &&other) noexcept
: matrix_(other.matrix_),
maxDim_(other.maxDim_),
barcode_(std::move(other.barcode_)),
: barcode_(std::move(other.barcode_)),
indexToBar_(std::move(other.indexToBar_)),
isReduced_(std::move(other.isReduced_))
{}
Expand All @@ -89,20 +96,19 @@ template<class Master_matrix>
inline void Base_pairing<Master_matrix>::_reduce()
{
std::unordered_map<index, index> pivotsToColumn;
auto& matrix = *matrix_;

for (int d = *maxDim_; d > 0; d--){
for (unsigned int i = 0; i < matrix_->size(); i++){
if (!(matrix.at(i).is_empty()) && matrix.at(i).get_dimension() == d)
for (int d = _matrix()->get_max_dimension(); d > 0; d--){
for (unsigned int i = 0; i < _matrix()->get_number_of_columns(); i++){
auto& curr = _matrix()->get_column(i);
if (!(curr.is_empty()) && curr.get_dimension() == d)
{
column_type &curr = matrix.at(i);
int pivot = curr.get_pivot();

while (pivot != -1 && pivotsToColumn.find(pivot) != pivotsToColumn.end()){
if constexpr (Master_matrix::Option_list::is_z2){
curr += matrix.at(pivotsToColumn.at(pivot));
curr += _matrix()->get_column(pivotsToColumn.at(pivot));
} else {
column_type &toadd = matrix.at(pivotsToColumn.at(pivot));
column_type &toadd = _matrix()->get_column(pivotsToColumn.at(pivot));
typename Master_matrix::Field_type coef = curr.get_pivot_value();
coef = coef.get_inverse();
coef *= (Master_matrix::Field_type::get_characteristic() - static_cast<unsigned int>(toadd.get_pivot_value()));
Expand All @@ -114,17 +120,17 @@ inline void Base_pairing<Master_matrix>::_reduce()

if (pivot != -1){
pivotsToColumn.emplace(pivot, i);
matrix.at(pivot).clear();
_matrix()->get_column(pivot).clear();
barcode_.push_back(Bar(d - 1, pivot, i));
} else {
matrix.at(i).clear();
curr.clear();
barcode_.push_back(Bar(d, i, -1));
}
}
}
}
for (unsigned int i = 0; i < matrix_->size(); i++){
if (matrix.at(i).get_dimension() == 0 && pivotsToColumn.find(i) == pivotsToColumn.end()){
for (unsigned int i = 0; i < _matrix()->get_number_of_columns(); i++){
if (_matrix()->get_column(i).get_dimension() == 0 && pivotsToColumn.find(i) == pivotsToColumn.end()){
barcode_.push_back(Bar(0, i, -1));
}
}
Expand Down
Loading

0 comments on commit bd9234d

Please sign in to comment.