From 98ff660e6650a049f501f1ca3cff2570dd57e009 Mon Sep 17 00:00:00 2001 From: hschreiber Date: Thu, 7 Nov 2024 16:37:09 +0100 Subject: [PATCH] replace static_cast(-1) by get_null_value in persistence matrix --- src/Persistence_matrix/include/gudhi/Matrix.h | 27 +++++--- .../Persistence_matrix/Boundary_matrix.h | 4 +- .../gudhi/Persistence_matrix/Chain_matrix.h | 13 ++-- .../Persistence_matrix/Id_to_index_overlay.h | 10 +-- .../gudhi/Persistence_matrix/RU_matrix.h | 11 ++-- .../gudhi/Persistence_matrix/base_pairing.h | 7 ++- .../columns/chain_column_extra_properties.h | 2 +- .../Persistence_matrix/columns/heap_column.h | 2 +- .../columns/intrusive_list_column.h | 4 +- .../columns/intrusive_set_column.h | 5 +- .../Persistence_matrix/columns/list_column.h | 4 +- .../columns/naive_vector_column.h | 4 +- .../Persistence_matrix/columns/set_column.h | 4 +- .../columns/unordered_set_column.h | 4 +- .../columns/vector_column.h | 6 +- .../gudhi/Persistence_matrix/ru_pairing.h | 4 +- .../gudhi/Persistence_matrix/ru_rep_cycles.h | 2 +- .../gudhi/Persistence_matrix/ru_vine_swap.h | 11 ++-- src/Persistence_matrix/test/pm_column_tests.h | 6 +- .../test/pm_column_tests_mastermatrix.h | 5 ++ src/Persistence_matrix/test/pm_matrix_tests.h | 62 +++++++++---------- 21 files changed, 111 insertions(+), 86 deletions(-) diff --git a/src/Persistence_matrix/include/gudhi/Matrix.h b/src/Persistence_matrix/include/gudhi/Matrix.h index d917bddc8..69cbe7e32 100644 --- a/src/Persistence_matrix/include/gudhi/Matrix.h +++ b/src/Persistence_matrix/include/gudhi/Matrix.h @@ -162,7 +162,16 @@ class Matrix { */ using Element = typename Field_operators::Element; using Characteristic = typename Field_operators::Characteristic; - + + /** + * @private + * @brief Returns value from a type when not set. + */ + template + static constexpr const T get_null_value() { + return -1; + } + /** * @brief Type for a bar in the computed barcode. Stores the birth, death and dimension of the bar. */ @@ -367,7 +376,7 @@ class Matrix { //purposely triggers operators() instead of operators(characteristic) as the "dummy" values for the different //operators can be different from -1. Column_zp_settings(Characteristic characteristic) : operators(), entryConstructor() { - if (characteristic != static_cast(-1)) operators.set_characteristic(characteristic); + if (characteristic != get_null_value()) operators.set_characteristic(characteristic); } Column_zp_settings(const Column_zp_settings& toCopy) : operators(toCopy.operators.get_characteristic()), entryConstructor() {} @@ -592,7 +601,7 @@ class Matrix { * @ref set_characteristic before calling for the first time a method needing it. Ignored if * @ref PersistenceMatrixOptions::is_z2 is true. */ - Matrix(unsigned int numberOfColumns, Characteristic characteristic = static_cast(-1)); + Matrix(unsigned int numberOfColumns, Characteristic characteristic = get_null_value()); /** * @brief Constructs a new empty matrix with the given comparator functions. Only available when those comparators * are necessary. @@ -671,7 +680,7 @@ class Matrix { Matrix(unsigned int numberOfColumns, const std::function& birthComparator, const std::function& deathComparator, - Characteristic characteristic = static_cast(-1)); + Characteristic characteristic = get_null_value()); /** * @brief Copy constructor. * @@ -1510,7 +1519,7 @@ template inline void Matrix::set_characteristic(Characteristic characteristic) { if constexpr (!PersistenceMatrixOptions::is_z2) { - if (colSettings_->operators.get_characteristic() != static_cast(-1)) { + if (colSettings_->operators.get_characteristic() != get_null_value()) { std::cerr << "Warning: Characteristic already initialised. Changing it could lead to incoherences in the matrix " "as the modulo was already applied to values in existing columns."; } @@ -1524,7 +1533,7 @@ template inline void Matrix::insert_column(const Container& column) { if constexpr (!PersistenceMatrixOptions::is_z2){ - GUDHI_CHECK(colSettings_->operators.get_characteristic() != static_cast(-1), + GUDHI_CHECK(colSettings_->operators.get_characteristic() != get_null_value(), std::logic_error("Matrix::insert_column - Columns cannot be initialized if the coefficient field " "characteristic is not specified.")); } @@ -1540,7 +1549,7 @@ template inline void Matrix::insert_column(const Container& column, Index columnIndex) { if constexpr (!PersistenceMatrixOptions::is_z2){ - GUDHI_CHECK(colSettings_->operators.get_characteristic() != static_cast(-1), + GUDHI_CHECK(colSettings_->operators.get_characteristic() != get_null_value(), std::logic_error("Matrix::insert_column - Columns cannot be initialized if the coefficient field " "characteristic is not specified.")); } @@ -1558,7 +1567,7 @@ inline typename Matrix::Insertion_return Matrix::insert_boundary(const Boundary_range& boundary, Dimension dim) { if constexpr (!PersistenceMatrixOptions::is_z2){ - GUDHI_CHECK(colSettings_->operators.get_characteristic() != static_cast(-1), + GUDHI_CHECK(colSettings_->operators.get_characteristic() != get_null_value(), std::logic_error("Matrix::insert_boundary - Columns cannot be initialized if the coefficient field " "characteristic is not specified.")); } @@ -1578,7 +1587,7 @@ Matrix::insert_boundary(ID_index cellIndex, Dimension dim) { if constexpr (!PersistenceMatrixOptions::is_z2){ - GUDHI_CHECK(colSettings_->operators.get_characteristic() != static_cast(-1), + GUDHI_CHECK(colSettings_->operators.get_characteristic() != get_null_value(), std::logic_error("Matrix::insert_boundary - Columns cannot be initialized if the coefficient field " "characteristic is not specified.")); } diff --git a/src/Persistence_matrix/include/gudhi/Persistence_matrix/Boundary_matrix.h b/src/Persistence_matrix/include/gudhi/Persistence_matrix/Boundary_matrix.h index 4ffb3fbe6..30c2c6dfa 100644 --- a/src/Persistence_matrix/include/gudhi/Persistence_matrix/Boundary_matrix.h +++ b/src/Persistence_matrix/include/gudhi/Persistence_matrix/Boundary_matrix.h @@ -558,7 +558,7 @@ inline typename Boundary_matrix::Index Boundary_matrixsecond.get_pivot(); if constexpr (activeSwapOption) { // if the removed column is positive, the pivot won't change value - if (Swap_opt::rowSwapped_ && pivot != static_cast(-1)) { + if (Swap_opt::rowSwapped_ && pivot != Master_matrix::template get_null_value()) { Swap_opt::_orderRows(); pivot = it->second.get_pivot(); } @@ -568,7 +568,7 @@ inline typename Boundary_matrix::Index Boundary_matrix(-1)) { + if (Swap_opt::rowSwapped_ && pivot != Master_matrix::template get_null_value()) { Swap_opt::_orderRows(); pivot = matrix_[nextInsertIndex_].get_pivot(); } diff --git a/src/Persistence_matrix/include/gudhi/Persistence_matrix/Chain_matrix.h b/src/Persistence_matrix/include/gudhi/Persistence_matrix/Chain_matrix.h index dce0a1d50..4a3f79640 100644 --- a/src/Persistence_matrix/include/gudhi/Persistence_matrix/Chain_matrix.h +++ b/src/Persistence_matrix/include/gudhi/Persistence_matrix/Chain_matrix.h @@ -700,7 +700,9 @@ inline std::vector Chain_matrix(-1) ? (boundary.size() == 0 ? 0 : boundary.size() - 1) : dim); + Dim_opt::update_up(dim == Master_matrix::template get_null_value() + ? (boundary.size() == 0 ? 0 : boundary.size() - 1) + : dim); } return _reduce_boundary(cellID, boundary, dim); @@ -915,7 +917,7 @@ inline void Chain_matrix::print() const if constexpr (!Master_matrix::Option_list::has_map_column_container) { for (ID_index i = 0; i < pivotToColumnIndex_.size(); ++i) { Index pos = pivotToColumnIndex_[i]; - if (pos != static_cast(-1)){ + if (pos != Master_matrix::template get_null_value()){ const Column& col = matrix_[pos]; for (const auto& entry : col) { std::cout << entry.get_row_index() << " "; @@ -928,7 +930,7 @@ inline void Chain_matrix::print() const std::cout << "Row Matrix:\n"; for (ID_index i = 0; i < pivotToColumnIndex_.size(); ++i) { Index pos = pivotToColumnIndex_[i]; - if (pos != static_cast(-1)){ + if (pos != Master_matrix::template get_null_value()){ const Row& row = RA_opt::get_row(pos); for (const auto& entry : row) { std::cout << entry.get_column_index() << " "; @@ -966,7 +968,8 @@ inline std::vector Chain_matrix(-1)) dim = boundary.begin() == boundary.end() ? 0 : boundary.size() - 1; + if (dim == Master_matrix::template get_null_value()) + dim = boundary.begin() == boundary.end() ? 0 : boundary.size() - 1; std::vector chainsInH; // for corresponding indices in H (paired columns) std::vector chainsInF; // for corresponding indices in F (unpaired, essential columns) @@ -1225,7 +1228,7 @@ inline void Chain_matrix::_remove_last(Index lastIndex) auto it = _indexToBar().find(--_nextPosition()); typename Barcode::iterator bar = it->second; - if (bar->death == static_cast(-1)) + if (bar->death == Master_matrix::template get_null_value()) _barcode().erase(bar); else bar->death = -1; diff --git a/src/Persistence_matrix/include/gudhi/Persistence_matrix/Id_to_index_overlay.h b/src/Persistence_matrix/include/gudhi/Persistence_matrix/Id_to_index_overlay.h index be885d182..66f20ecff 100644 --- a/src/Persistence_matrix/include/gudhi/Persistence_matrix/Id_to_index_overlay.h +++ b/src/Persistence_matrix/include/gudhi/Persistence_matrix/Id_to_index_overlay.h @@ -760,8 +760,9 @@ inline void Id_to_index_overlay::insert_bounda GUDHI_CHECK(idToIndex_->find(cellIndex) == idToIndex_->end(), std::invalid_argument("Id_to_index_overlay::insert_boundary - Index for simplex already chosen!")); } else { - GUDHI_CHECK((idToIndex_->size() <= cellIndex || _id_to_index(cellIndex) == static_cast(-1)), - std::invalid_argument("Id_to_index_overlay::insert_boundary - Index for simplex already chosen!")); + GUDHI_CHECK( + (idToIndex_->size() <= cellIndex || _id_to_index(cellIndex) == Master_matrix::template get_null_value()), + std::invalid_argument("Id_to_index_overlay::insert_boundary - Index for simplex already chosen!")); } matrix_.insert_boundary(cellIndex, boundary, dim); if constexpr (Master_matrix::Option_list::is_of_boundary_type) { @@ -808,7 +809,7 @@ inline void Id_to_index_overlay::remove_maxima } } else { for (ID_index i = 0; i < idToIndex_->size(); ++i) { - if (_id_to_index(i) != static_cast(-1)) indexToID[_id_to_index(i)] = i; + if (_id_to_index(i) != Master_matrix::template get_null_value()) indexToID[_id_to_index(i)] = i; } } --nextIndex_; @@ -857,7 +858,8 @@ inline void Id_to_index_overlay::remove_last() idToIndex_->erase(it); } else { Index id = idToIndex_->size() - 1; - while (_id_to_index(id) == static_cast(-1)) --id; // should always stop before reaching -1 + // should always stop before reaching -1 + while (_id_to_index(id) == Master_matrix::template get_null_value()) --id; GUDHI_CHECK(_id_to_index(id) == nextIndex_, std::logic_error("Id_to_index_overlay::remove_last - Indexation problem.")); _id_to_index(id) = -1; diff --git a/src/Persistence_matrix/include/gudhi/Persistence_matrix/RU_matrix.h b/src/Persistence_matrix/include/gudhi/Persistence_matrix/RU_matrix.h index 2020e0d11..b26f87da5 100644 --- a/src/Persistence_matrix/include/gudhi/Persistence_matrix/RU_matrix.h +++ b/src/Persistence_matrix/include/gudhi/Persistence_matrix/RU_matrix.h @@ -604,7 +604,7 @@ inline void RU_matrix::remove_last() pivotToColumnIndex_.erase(reducedMatrixR_.remove_last()); } else { ID_index lastPivot = reducedMatrixR_.remove_last(); - if (lastPivot != static_cast(-1)) pivotToColumnIndex_[lastPivot] = -1; + if (lastPivot != Master_matrix::template get_null_value()) pivotToColumnIndex_[lastPivot] = -1; } // if has_vine_update and has_column_pairings are both true, @@ -752,7 +752,7 @@ inline void RU_matrix::_insert_boundary(Index currentIndex) if constexpr (!Master_matrix::Option_list::has_map_column_container) { ID_index pivot = reducedMatrixR_.get_column(currentIndex).get_pivot(); - if (pivot != static_cast(-1) && pivotToColumnIndex_.size() <= pivot) + if (pivot != Master_matrix::template get_null_value() && pivotToColumnIndex_.size() <= pivot) pivotToColumnIndex_.resize((pivot + 1) * 2, -1); } @@ -806,7 +806,7 @@ template inline void RU_matrix::_reduce_column(Index target, Index eventIndex) { auto get_column_with_pivot_ = [&](ID_index pivot) -> Index { - if (pivot == static_cast(-1)) return -1; + if (pivot == Master_matrix::template get_null_value()) return -1; if constexpr (Master_matrix::Option_list::has_map_column_container) { auto it = pivotToColumnIndex_.find(pivot); if (it == pivotToColumnIndex_.end()) @@ -822,13 +822,14 @@ inline void RU_matrix::_reduce_column(Index target, Index eventIn ID_index pivot = curr.get_pivot(); Index currIndex = get_column_with_pivot_(pivot); - while (pivot != static_cast(-1) && currIndex != static_cast(-1)) { + while (pivot != Master_matrix::template get_null_value() && + currIndex != Master_matrix::template get_null_value()) { _reduce_column_by(target, currIndex); pivot = curr.get_pivot(); currIndex = get_column_with_pivot_(pivot); } - if (pivot != static_cast(-1)) { + if (pivot != Master_matrix::template get_null_value()) { if constexpr (Master_matrix::Option_list::has_map_column_container) { pivotToColumnIndex_.try_emplace(pivot, target); } else { diff --git a/src/Persistence_matrix/include/gudhi/Persistence_matrix/base_pairing.h b/src/Persistence_matrix/include/gudhi/Persistence_matrix/base_pairing.h index 780040f01..94d268188 100644 --- a/src/Persistence_matrix/include/gudhi/Persistence_matrix/base_pairing.h +++ b/src/Persistence_matrix/include/gudhi/Persistence_matrix/base_pairing.h @@ -155,7 +155,8 @@ inline void Base_pairing::_reduce() auto itNeg = negativeColumns.find(pivotColumnNumber); Index pivotKiller = itNeg == negativeColumns.end() ? -1 : itNeg->second; - while (pivot != static_cast(-1) && pivotKiller != static_cast(-1)) { + while (pivot != Master_matrix::template get_null_value() && + pivotKiller != Master_matrix::template get_null_value()) { if constexpr (Master_matrix::Option_list::is_z2) { curr += _matrix()->get_column(pivotKiller); } else { @@ -174,7 +175,7 @@ inline void Base_pairing::_reduce() pivotKiller = itNeg == negativeColumns.end() ? -1 : itNeg->second; } - if (pivot != static_cast(-1)) { + if (pivot != Master_matrix::template get_null_value()) { negativeColumns.emplace(pivotColumnNumber, i); _matrix()->get_column(pivotColumnNumber).clear(); barcode_.emplace_back(pivotColumnNumber, i, dim - 1); @@ -193,7 +194,7 @@ inline void Base_pairing::_reduce() // map can only be constructed once barcode is sorted for (Index i = 0; i < barcode_.size(); ++i) { auto d = barcode_[i].death; - if (d != static_cast(-1)) { + if (d != Master_matrix::template get_null_value()) { deathToBar_.emplace(d, i); } } diff --git a/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/chain_column_extra_properties.h b/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/chain_column_extra_properties.h index 5fb6d6acd..b2b6fa167 100644 --- a/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/chain_column_extra_properties.h +++ b/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/chain_column_extra_properties.h @@ -105,7 +105,7 @@ class Chain_column_extra_properties * @return true If the column is paired. * @return false Otherwise. */ - bool is_paired() const { return pairedColumn_ != static_cast(-1); } + bool is_paired() const { return pairedColumn_ != Master_matrix::template get_null_value(); } /** * @brief Sets the value of the pair. * diff --git a/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/heap_column.h b/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/heap_column.h index e195dfea4..7e49874c4 100644 --- a/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/heap_column.h +++ b/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/heap_column.h @@ -634,7 +634,7 @@ inline typename Heap_column::Field_element Heap_column(-1)) return sum; + if (Chain_opt::get_pivot() == Master_matrix::template get_null_value()) return sum; for (const Entry* entry : column_) { if (entry->get_row_index() == Chain_opt::get_pivot()) operators_->add_inplace(sum, entry->get_element()); } diff --git a/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/intrusive_list_column.h b/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/intrusive_list_column.h index 0f5aaa832..9bf8e6e0b 100644 --- a/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/intrusive_list_column.h +++ b/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/intrusive_list_column.h @@ -510,7 +510,7 @@ inline void Intrusive_list_column::reorder(const Row_index_map& v Entry* entry = &(*it); if constexpr (Master_matrix::Option_list::has_row_access) { RA_opt::unlink(entry); - if (columnIndex != static_cast(-1)) entry->set_column_index(columnIndex); + if (columnIndex != Master_matrix::template get_null_value()) entry->set_column_index(columnIndex); } entry->set_row_index(valueMap.at(entry->get_row_index())); if constexpr (Master_matrix::Option_list::has_intrusive_rows && Master_matrix::Option_list::has_row_access) @@ -576,7 +576,7 @@ Intrusive_list_column::get_pivot_value() const if (column_.empty()) return 0; return column_.back().get_element(); } else { - if (Chain_opt::get_pivot() == static_cast(-1)) return Field_element(); + if (Chain_opt::get_pivot() == Master_matrix::template get_null_value()) return Field_element(); for (const Entry& entry : column_) { if (entry.get_row_index() == Chain_opt::get_pivot()) return entry.get_element(); } diff --git a/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/intrusive_set_column.h b/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/intrusive_set_column.h index 994a5014d..e64ae4cd9 100644 --- a/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/intrusive_set_column.h +++ b/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/intrusive_set_column.h @@ -499,7 +499,8 @@ inline void Intrusive_set_column::reorder(const Row_index_map& va if constexpr (Master_matrix::Option_list::has_row_access) { for (auto it = column_.begin(); it != column_.end();) { Entry* newEntry = entryPool_->construct( - columnIndex == static_cast(-1) ? RA_opt::columnIndex_ : columnIndex, valueMap.at(it->get_row_index())); + columnIndex == Master_matrix::template get_null_value() ? RA_opt::columnIndex_ : columnIndex, + valueMap.at(it->get_row_index())); if constexpr (!Master_matrix::Option_list::is_z2) { newEntry->set_element(it->get_element()); } @@ -578,7 +579,7 @@ Intrusive_set_column::get_pivot_value() const if (column_.empty()) return 0; return column_.rbegin()->get_element(); } else { - if (Chain_opt::get_pivot() == static_cast(-1)) return 0; + if (Chain_opt::get_pivot() == Master_matrix::template get_null_value()) return 0; auto it = column_.find(Entry(Chain_opt::get_pivot())); GUDHI_CHECK(it != column_.end(), "Intrusive_set_column::get_pivot_value - Pivot not found only if the column was misused."); diff --git a/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/list_column.h b/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/list_column.h index 658541f97..c4cce22c3 100644 --- a/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/list_column.h +++ b/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/list_column.h @@ -499,7 +499,7 @@ inline void List_column::reorder(const Row_index_map& valueMap, [ Entry* entry = *it; if constexpr (Master_matrix::Option_list::has_row_access) { RA_opt::unlink(entry); - if (columnIndex != static_cast(-1)) entry->set_column_index(columnIndex); + if (columnIndex != Master_matrix::template get_null_value()) entry->set_column_index(columnIndex); } entry->set_row_index(valueMap.at(entry->get_row_index())); if constexpr (Master_matrix::Option_list::has_intrusive_rows && Master_matrix::Option_list::has_row_access) @@ -569,7 +569,7 @@ inline typename List_column::Field_element List_columnget_element(); } else { - if (Chain_opt::get_pivot() == static_cast(-1)) return Field_element(); + if (Chain_opt::get_pivot() == Master_matrix::template get_null_value()) return Field_element(); for (const Entry* entry : column_) { if (entry->get_row_index() == Chain_opt::get_pivot()) return entry->get_element(); } diff --git a/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/naive_vector_column.h b/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/naive_vector_column.h index e99d37b7e..aafa8ce25 100644 --- a/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/naive_vector_column.h +++ b/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/naive_vector_column.h @@ -488,7 +488,7 @@ inline void Naive_vector_column::reorder(const Row_index_map& val for (Entry* entry : column_) { if constexpr (Master_matrix::Option_list::has_row_access) { RA_opt::unlink(entry); - if (columnIndex != static_cast(-1)) entry->set_column_index(columnIndex); + if (columnIndex != Master_matrix::template get_null_value()) entry->set_column_index(columnIndex); } entry->set_row_index(valueMap.at(entry->get_row_index())); if constexpr (Master_matrix::Option_list::has_intrusive_rows && Master_matrix::Option_list::has_row_access) @@ -559,7 +559,7 @@ inline typename Naive_vector_column::Field_element Naive_vector_c if constexpr (Master_matrix::Option_list::is_of_boundary_type) { return column_.empty() ? Field_element() : column_.back()->get_element(); } else { - if (Chain_opt::get_pivot() == static_cast(-1)) return Field_element(); + if (Chain_opt::get_pivot() == Master_matrix::template get_null_value()) return Field_element(); for (const Entry* entry : column_) { if (entry->get_row_index() == Chain_opt::get_pivot()) return entry->get_element(); } diff --git a/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/set_column.h b/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/set_column.h index 6f69c53c9..7d78f957c 100644 --- a/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/set_column.h +++ b/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/set_column.h @@ -487,7 +487,7 @@ inline void Set_column::reorder(const Row_index_map& valueMap, [[ for (Entry* entry : column_) { if constexpr (Master_matrix::Option_list::has_row_access) { RA_opt::unlink(entry); - if (columnIndex != static_cast(-1)) entry->set_column_index(columnIndex); + if (columnIndex != Master_matrix::template get_null_value()) entry->set_column_index(columnIndex); } entry->set_row_index(valueMap.at(entry->get_row_index())); newSet.insert(entry); @@ -561,7 +561,7 @@ inline typename Set_column::Field_element Set_columnget_element(); } else { - if (Chain_opt::get_pivot() == static_cast(-1)) return Field_element(); + if (Chain_opt::get_pivot() == Master_matrix::template get_null_value()) return Field_element(); for (const Entry* entry : column_) { if (entry->get_row_index() == Chain_opt::get_pivot()) return entry->get_element(); } diff --git a/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/unordered_set_column.h b/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/unordered_set_column.h index b63adcbac..f23bd3825 100644 --- a/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/unordered_set_column.h +++ b/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/unordered_set_column.h @@ -508,7 +508,7 @@ inline void Unordered_set_column::reorder(const Row_index_map& va for (Entry* entry : column_) { if constexpr (Master_matrix::Option_list::has_row_access) { RA_opt::unlink(entry); - if (columnIndex != static_cast(-1)) entry->set_column_index(columnIndex); + if (columnIndex != Master_matrix::template get_null_value()) entry->set_column_index(columnIndex); } entry->set_row_index(valueMap.at(entry->get_row_index())); newSet.insert(entry); @@ -585,7 +585,7 @@ Unordered_set_column::get_pivot_value() const if (column_.empty()) return 0; return (*std::max_element(column_.begin(), column_.end(), EntryPointerComp()))->get_element(); } else { - if (Chain_opt::get_pivot() == static_cast(-1)) return Field_element(); + if (Chain_opt::get_pivot() == Master_matrix::template get_null_value()) return Field_element(); for (const Entry* entry : column_) { if (entry->get_row_index() == Chain_opt::get_pivot()) return entry->get_element(); } diff --git a/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/vector_column.h b/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/vector_column.h index f6a7da8e8..eeba511de 100644 --- a/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/vector_column.h +++ b/src/Persistence_matrix/include/gudhi/Persistence_matrix/columns/vector_column.h @@ -549,7 +549,7 @@ inline void Vector_column::reorder(const Row_index_map& valueMap, for (Entry* entry : column_) { if constexpr (Master_matrix::Option_list::has_row_access) { RA_opt::unlink(entry); - if (columnIndex != static_cast(-1)) entry->set_column_index(columnIndex); + if (columnIndex != Master_matrix::template get_null_value()) entry->set_column_index(columnIndex); } entry->set_row_index(valueMap.at(entry->get_row_index())); if constexpr (Master_matrix::Option_list::has_intrusive_rows && Master_matrix::Option_list::has_row_access) @@ -570,7 +570,7 @@ inline void Vector_column::reorder(const Row_index_map& valueMap, if (erasedValues_.find(entry->get_row_index()) == erasedValues_.end()) { if constexpr (Master_matrix::Option_list::has_row_access) { RA_opt::unlink(entry); - if (columnIndex != static_cast(-1)) entry->set_column_index(columnIndex); + if (columnIndex != Master_matrix::template get_null_value()) entry->set_column_index(columnIndex); } entry->set_row_index(valueMap.at(entry->get_row_index())); newColumn.push_back(entry); @@ -665,7 +665,7 @@ inline typename Vector_column::Field_element Vector_columnget_element(); } else { - if (Chain_opt::get_pivot() == static_cast(-1)) return Field_element(); + if (Chain_opt::get_pivot() == Master_matrix::template get_null_value()) return Field_element(); for (const Entry* entry : column_) { if (entry->get_row_index() == Chain_opt::get_pivot()) return entry->get_element(); } diff --git a/src/Persistence_matrix/include/gudhi/Persistence_matrix/ru_pairing.h b/src/Persistence_matrix/include/gudhi/Persistence_matrix/ru_pairing.h index e6afe9fc8..ccda5b20a 100644 --- a/src/Persistence_matrix/include/gudhi/Persistence_matrix/ru_pairing.h +++ b/src/Persistence_matrix/include/gudhi/Persistence_matrix/ru_pairing.h @@ -124,7 +124,7 @@ class RU_pairing : public std::conditional< if constexpr (Master_matrix::hasFixedBarcode) { auto& bar = barcode_[indexToBar_[eventIndex]]; - if (bar.death == static_cast(-1)) { // birth + if (bar.death == Master_matrix::template get_null_value()) { // birth barcode_.pop_back(); // sorted by birth and eventIndex has to be the highest one } else { // death bar.death = -1; @@ -134,7 +134,7 @@ class RU_pairing : public std::conditional< auto it = indexToBar_.find(eventIndex); typename Barcode::iterator bar = it->second; - if (bar->death == static_cast(-1)) + if (bar->death == Master_matrix::template get_null_value()) barcode_.erase(bar); else bar->death = -1; diff --git a/src/Persistence_matrix/include/gudhi/Persistence_matrix/ru_rep_cycles.h b/src/Persistence_matrix/include/gudhi/Persistence_matrix/ru_rep_cycles.h index fb2e0c088..8e0b79f3c 100644 --- a/src/Persistence_matrix/include/gudhi/Persistence_matrix/ru_rep_cycles.h +++ b/src/Persistence_matrix/include/gudhi/Persistence_matrix/ru_rep_cycles.h @@ -150,7 +150,7 @@ inline void RU_representative_cycles::update_representative_cycle for (Index i = 0; i < _matrix()->mirrorMatrixU_.get_number_of_columns(); i++) { for (const auto& entry : _matrix()->mirrorMatrixU_.get_column(i)) { auto idx = birthToCycle_[entry.get_row_index()]; - if (idx != static_cast(-1)) { + if (idx != Master_matrix::template get_null_value()) { representativeCycles_[idx].push_back(i); } } diff --git a/src/Persistence_matrix/include/gudhi/Persistence_matrix/ru_vine_swap.h b/src/Persistence_matrix/include/gudhi/Persistence_matrix/ru_vine_swap.h index 00c76749f..996fbe6ff 100644 --- a/src/Persistence_matrix/include/gudhi/Persistence_matrix/ru_vine_swap.h +++ b/src/Persistence_matrix/include/gudhi/Persistence_matrix/ru_vine_swap.h @@ -274,14 +274,15 @@ template inline bool RU_vine_swap::_is_paired(Index columnIndex) { if constexpr (Master_matrix::Option_list::has_column_pairings) { - return _get_death(columnIndex) != static_cast(-1); + return _get_death(columnIndex) != Master_matrix::template get_null_value(); } else { if (!_matrix()->reducedMatrixR_.is_zero_column(columnIndex)) return true; if constexpr (Master_matrix::Option_list::has_map_column_container) { if (_matrix()->pivotToColumnIndex_.find(columnIndex) == _matrix()->pivotToColumnIndex_.end()) return false; } else { - if (_matrix()->pivotToColumnIndex_.operator[](columnIndex) == static_cast(-1)) return false; + if (_matrix()->pivotToColumnIndex_.operator[](columnIndex) == Master_matrix::template get_null_value()) + return false; } return true; @@ -391,7 +392,8 @@ inline bool RU_vine_swap::_positive_vine_swap(Index columnIndex) const Pos_index iDeath = _get_death(columnIndex); const Pos_index iiDeath = _get_death(columnIndex + 1); - if (iDeath != static_cast(-1) && iiDeath != static_cast(-1) && + if (iDeath != Master_matrix::template get_null_value() && + iiDeath != Master_matrix::template get_null_value() && !(_matrix()->reducedMatrixR_.is_zero_entry(iiDeath, _get_row_id_from_position(columnIndex)))) { if (iDeath < iiDeath) { _swap_at_index(columnIndex); @@ -407,7 +409,8 @@ inline bool RU_vine_swap::_positive_vine_swap(Index columnIndex) _swap_at_index(columnIndex); - if (iDeath != static_cast(-1) || iiDeath == static_cast(-1) || + if (iDeath != Master_matrix::template get_null_value() || + iiDeath == Master_matrix::template get_null_value() || _matrix()->reducedMatrixR_.is_zero_entry(iiDeath, _get_row_id_from_position(columnIndex + 1))) { _positive_transpose(columnIndex); return true; diff --git a/src/Persistence_matrix/test/pm_column_tests.h b/src/Persistence_matrix/test/pm_column_tests.h index 01d875974..15c5a74bd 100644 --- a/src/Persistence_matrix/test/pm_column_tests.h +++ b/src/Persistence_matrix/test/pm_column_tests.h @@ -1010,11 +1010,11 @@ void column_test_chain_methods() { Column col(&settings); BOOST_CHECK(!col.is_paired()); - BOOST_CHECK(col.get_paired_chain_index() == static_cast(-1)); + BOOST_CHECK(col.get_paired_chain_index() == Column::Master::template get_null_value()); col.unassign_paired_chain(); BOOST_CHECK(!col.is_paired()); - BOOST_CHECK(col.get_paired_chain_index() == static_cast(-1)); + BOOST_CHECK(col.get_paired_chain_index() == Column::Master::template get_null_value()); col.assign_paired_chain(2); BOOST_CHECK(col.is_paired()); @@ -1022,7 +1022,7 @@ void column_test_chain_methods() { col.unassign_paired_chain(); BOOST_CHECK(!col.is_paired()); - BOOST_CHECK(col.get_paired_chain_index() == static_cast(-1)); + BOOST_CHECK(col.get_paired_chain_index() == Column::Master::template get_null_value()); } #endif // PM_COLUMN_TESTS_H diff --git a/src/Persistence_matrix/test/pm_column_tests_mastermatrix.h b/src/Persistence_matrix/test/pm_column_tests_mastermatrix.h index 7a6c711a5..d88d0e082 100644 --- a/src/Persistence_matrix/test/pm_column_tests_mastermatrix.h +++ b/src/Persistence_matrix/test/pm_column_tests_mastermatrix.h @@ -57,6 +57,11 @@ struct Column_mini_matrix { using Characteristic = typename std::conditional::type; + template + static constexpr const T get_null_value() { + return -1; + } + struct Matrix_row_tag; struct Matrix_column_tag; diff --git a/src/Persistence_matrix/test/pm_matrix_tests.h b/src/Persistence_matrix/test/pm_matrix_tests.h index 0028079c5..ceabea502 100644 --- a/src/Persistence_matrix/test/pm_matrix_tests.h +++ b/src/Persistence_matrix/test/pm_matrix_tests.h @@ -474,13 +474,13 @@ void test_boundary_insertion() { BOOST_CHECK_EQUAL(m.get_column_dimension(5), 1); BOOST_CHECK_EQUAL(m.get_column_dimension(6), 2); - BOOST_CHECK_EQUAL(m.get_pivot(0), static_cast(-1)); - BOOST_CHECK_EQUAL(m.get_pivot(1), static_cast(-1)); - BOOST_CHECK_EQUAL(m.get_pivot(2), static_cast(-1)); + BOOST_CHECK_EQUAL(m.get_pivot(0), Matrix::template get_null_value()); + BOOST_CHECK_EQUAL(m.get_pivot(1), Matrix::template get_null_value()); + BOOST_CHECK_EQUAL(m.get_pivot(2), Matrix::template get_null_value()); BOOST_CHECK_EQUAL(m.get_pivot(3), 1); BOOST_CHECK_EQUAL(m.get_pivot(4), 2); if constexpr (is_RU()) { - BOOST_CHECK_EQUAL(m.get_pivot(5), static_cast(-1)); // was reduced + BOOST_CHECK_EQUAL(m.get_pivot(5), Matrix::template get_null_value()); // was reduced } else { BOOST_CHECK_EQUAL(m.get_pivot(5), 2); // not reduced } @@ -865,7 +865,7 @@ void test_boundary_maximal_simplex_removal() { test_content_equality(columns, m); BOOST_CHECK_EQUAL(m.get_number_of_columns(), 6); - BOOST_CHECK_EQUAL(m.get_current_barcode().back().death, static_cast(-1)); + BOOST_CHECK_EQUAL(m.get_current_barcode().back().death, Matrix::template get_null_value()); } template @@ -891,7 +891,8 @@ void test_ru_maximal_simplex_removal() { test_content_equality(columns, m); BOOST_CHECK_EQUAL(m.get_number_of_columns(), 6); if constexpr (Matrix::Option_list::has_column_pairings) { - BOOST_CHECK_EQUAL(m.get_current_barcode().back().death, static_cast(-1)); + BOOST_CHECK_EQUAL(m.get_current_barcode().back().death, + Matrix::template get_null_value()); } } @@ -916,7 +917,8 @@ void test_chain_maximal_simplex_removal(Matrix& m) { test_content_equality(columns, m); BOOST_CHECK_EQUAL(m.get_number_of_columns(), 6); if constexpr (Matrix::Option_list::has_column_pairings) { - BOOST_CHECK_EQUAL(m.get_current_barcode().back().death, static_cast(-1)); + BOOST_CHECK_EQUAL(m.get_current_barcode().back().death, + Matrix::template get_null_value()); } } @@ -1109,8 +1111,7 @@ void test_ru_operation() { if constexpr (is_indexed_by_position()) { i = 0; for (auto& b : uColumns) { - test_column_equality(b, - get_column_content_via_iterators(m.get_column(i++, false))); + test_column_equality(b, get_column_content_via_iterators(m.get_column(i++, false))); } } @@ -1126,8 +1127,7 @@ void test_ru_operation() { if constexpr (is_indexed_by_position()) { i = 0; for (auto& b : uColumns) { - test_column_equality(b, - get_column_content_via_iterators(m.get_column(i++, false))); + test_column_equality(b, get_column_content_via_iterators(m.get_column(i++, false))); } } } @@ -1386,10 +1386,10 @@ void test_barcode() { std::set, BarComp> bars3; // bars are not ordered the same for all matrices for (auto it = barcode.begin(); it != barcode.end(); ++it) { - //three access possibilities + // three access possibilities bars1.emplace(it->dim, it->birth, it->death); bars2.emplace(std::get<2>(*it), std::get<0>(*it), std::get<1>(*it)); - auto [ x, y, z ] = *it; + auto [x, y, z] = *it; bars3.emplace(z, x, y); } auto it = bars1.begin(); @@ -1551,7 +1551,7 @@ void test_shifted_barcode1() { } } - if constexpr (Matrix::Option_list::column_indexation_type == Column_indexation_types::IDENTIFIER){ + if constexpr (Matrix::Option_list::column_indexation_type == Column_indexation_types::IDENTIFIER) { test_column_equality(reducedMatrix[0], get_column_content_via_iterators(m.get_column(0))); test_column_equality(reducedMatrix[1], get_column_content_via_iterators(m.get_column(1))); test_column_equality(reducedMatrix[2], get_column_content_via_iterators(m.get_column(2))); @@ -1578,10 +1578,10 @@ void test_shifted_barcode1() { std::set, BarComp> bars3; // bars are not ordered the same for all matrices for (auto it = barcode.begin(); it != barcode.end(); ++it) { - //three access possibilities + // three access possibilities bars1.emplace(it->dim, it->birth, it->death); bars2.emplace(std::get<2>(*it), std::get<0>(*it), std::get<1>(*it)); - auto [ x, y, z ] = *it; + auto [x, y, z] = *it; bars3.emplace(z, x, y); } auto it = bars1.begin(); @@ -1709,7 +1709,7 @@ void test_shifted_barcode2() { reducedMatrix.push_back({{7, 1}, {13, 1}}); } } - if constexpr (Matrix::Option_list::column_indexation_type == Column_indexation_types::IDENTIFIER){ + if constexpr (Matrix::Option_list::column_indexation_type == Column_indexation_types::IDENTIFIER) { test_column_equality(reducedMatrix[0], get_column_content_via_iterators(m.get_column(2))); test_column_equality(reducedMatrix[1], get_column_content_via_iterators(m.get_column(3))); test_column_equality(reducedMatrix[2], get_column_content_via_iterators(m.get_column(5))); @@ -1728,10 +1728,10 @@ void test_shifted_barcode2() { std::set, BarComp> bars3; // bars are not ordered the same for all matrices for (auto it = barcode.begin(); it != barcode.end(); ++it) { - //three access possibilities + // three access possibilities bars1.emplace(it->dim, it->birth, it->death); bars2.emplace(std::get<2>(*it), std::get<0>(*it), std::get<1>(*it)); - auto [ x, y, z ] = *it; + auto [x, y, z] = *it; bars3.emplace(z, x, y); } auto it = bars1.begin(); @@ -1850,7 +1850,7 @@ void test_vine_swap_with_position_index(Matrix& m) { auto it = barcode.begin(); BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 0); - BOOST_CHECK_EQUAL(it->death, static_cast(-1)); + BOOST_CHECK_EQUAL(it->death, Matrix::template get_null_value()); ++it; BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 1); @@ -1909,7 +1909,7 @@ void test_vine_swap_with_position_index(Matrix& m) { auto it = barcode.begin(); BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 0); - BOOST_CHECK_EQUAL(it->death, static_cast(-1)); + BOOST_CHECK_EQUAL(it->death, Matrix::template get_null_value()); ++it; BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 1); @@ -1960,7 +1960,7 @@ void test_vine_swap_with_position_index(Matrix& m) { auto it = barcode.begin(); BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 0); - BOOST_CHECK_EQUAL(it->death, static_cast(-1)); + BOOST_CHECK_EQUAL(it->death, Matrix::template get_null_value()); ++it; BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 1); @@ -2011,7 +2011,7 @@ void test_vine_swap_with_position_index(Matrix& m) { auto it = barcode.begin(); BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 0); - BOOST_CHECK_EQUAL(it->death, static_cast(-1)); + BOOST_CHECK_EQUAL(it->death, Matrix::template get_null_value()); ++it; BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 1); @@ -2062,7 +2062,7 @@ void test_vine_swap_with_position_index(Matrix& m) { auto it = barcode.begin(); BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 0); - BOOST_CHECK_EQUAL(it->death, static_cast(-1)); + BOOST_CHECK_EQUAL(it->death, Matrix::template get_null_value()); ++it; BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 1); @@ -2129,7 +2129,7 @@ void test_vine_swap_with_position_index(Matrix& m) { auto it = barcode.begin(); BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 0); - BOOST_CHECK_EQUAL(it->death, static_cast(-1)); + BOOST_CHECK_EQUAL(it->death, Matrix::template get_null_value()); ++it; BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 1); @@ -2189,7 +2189,7 @@ void test_vine_swap_with_id_index(Matrix& m) { auto it = barcode.begin(); BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 0); - BOOST_CHECK_EQUAL(it->death, static_cast(-1)); + BOOST_CHECK_EQUAL(it->death, Matrix::template get_null_value()); ++it; BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 1); @@ -2248,7 +2248,7 @@ void test_vine_swap_with_id_index(Matrix& m) { auto it = barcode.begin(); BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 0); - BOOST_CHECK_EQUAL(it->death, static_cast(-1)); + BOOST_CHECK_EQUAL(it->death, Matrix::template get_null_value()); ++it; BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 1); @@ -2299,7 +2299,7 @@ void test_vine_swap_with_id_index(Matrix& m) { auto it = barcode.begin(); BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 0); - BOOST_CHECK_EQUAL(it->death, static_cast(-1)); + BOOST_CHECK_EQUAL(it->death, Matrix::template get_null_value()); ++it; BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 1); @@ -2350,7 +2350,7 @@ void test_vine_swap_with_id_index(Matrix& m) { auto it = barcode.begin(); BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 0); - BOOST_CHECK_EQUAL(it->death, static_cast(-1)); + BOOST_CHECK_EQUAL(it->death, Matrix::template get_null_value()); ++it; BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 1); @@ -2401,7 +2401,7 @@ void test_vine_swap_with_id_index(Matrix& m) { auto it = barcode.begin(); BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 0); - BOOST_CHECK_EQUAL(it->death, static_cast(-1)); + BOOST_CHECK_EQUAL(it->death, Matrix::template get_null_value()); ++it; BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 1); @@ -2473,7 +2473,7 @@ void test_vine_swap_with_id_index(Matrix& m) { auto it = barcode.begin(); BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 0); - BOOST_CHECK_EQUAL(it->death, static_cast(-1)); + BOOST_CHECK_EQUAL(it->death, Matrix::template get_null_value()); ++it; BOOST_CHECK_EQUAL(it->dim, 0); BOOST_CHECK_EQUAL(it->birth, 1);