ru_pairing.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
18#ifndef PM_RU_PAIRING_H
19#define PM_RU_PAIRING_H
20
21#include <unordered_map>
22
24
25namespace Gudhi {
26namespace persistence_matrix {
27
36{
37 friend void swap([[maybe_unused]] Dummy_ru_pairing& d1, [[maybe_unused]] Dummy_ru_pairing& d2) {}
38};
39
48template <class Master_matrix>
49class RU_pairing : public std::conditional<
50 Master_matrix::Option_list::has_removable_columns,
51 Cell_position_to_ID_mapper<typename Master_matrix::ID_index, typename Master_matrix::Pos_index>,
52 Dummy_pos_mapper
53 >::type
54{
55 protected:
56 using Pos_index = typename Master_matrix::Pos_index;
57 using ID_index = typename Master_matrix::ID_index;
58 //PIDM = Position to ID Map
59 using PIDM = typename std::conditional<Master_matrix::Option_list::has_removable_columns,
60 Cell_position_to_ID_mapper<ID_index, Pos_index>,
61 Dummy_pos_mapper
62 >::type;
63
64 public:
65 using Barcode = typename Master_matrix::Barcode;
70 RU_pairing() : PIDM() {}
71
77 const Barcode& get_current_barcode() const { return barcode_; }
78
82 friend void swap(RU_pairing& pairing1, RU_pairing& pairing2) {
83 swap(static_cast<PIDM&>(pairing1), static_cast<PIDM&>(pairing2));
84 pairing1.barcode_.swap(pairing2.barcode_);
85 pairing1.indexToBar_.swap(pairing2.indexToBar_);
86 pairing1.idToPosition_.swap(pairing2.idToPosition_);
87 }
88
89 protected:
90 using Dimension = typename Master_matrix::Dimension;
91 using Dictionary = typename Master_matrix::Bar_dictionary;
92
93 Barcode barcode_;
94 Dictionary indexToBar_;
98 std::unordered_map<ID_index, Pos_index> idToPosition_; //TODO: test other map types
99
100 void _update_barcode(ID_index birthPivot, Pos_index death) {
101 auto it = idToPosition_.find(birthPivot);
102 Pos_index pivotBirth = it == idToPosition_.end() ? birthPivot : it->second;
103 if constexpr (Master_matrix::hasFixedBarcode || !Master_matrix::Option_list::has_removable_columns) {
104 barcode_[indexToBar_[pivotBirth]].death = death;
105 indexToBar_.push_back(indexToBar_[pivotBirth]);
106 } else {
107 auto& barIt = indexToBar_.at(pivotBirth);
108 barIt->death = death;
109 indexToBar_.try_emplace(death, barIt); // list so iterators are stable
110 }
111 }
112
113 void _add_bar(Dimension dim, Pos_index birth) {
114 barcode_.emplace_back(birth, Master_matrix::template get_null_value<Pos_index>(), dim);
115 if constexpr (Master_matrix::hasFixedBarcode || !Master_matrix::Option_list::has_removable_columns) {
116 indexToBar_.push_back(barcode_.size() - 1);
117 } else {
118 indexToBar_.try_emplace(birth, --barcode_.end());
119 }
120 }
121
122 void _remove_last(Pos_index eventIndex) {
123 static_assert(Master_matrix::Option_list::has_removable_columns, "_remove_last not available.");
124 constexpr const Pos_index nullDeath = Master_matrix::template get_null_value<Pos_index>();
125
126 if constexpr (Master_matrix::hasFixedBarcode) {
127 auto& bar = barcode_[indexToBar_[eventIndex]];
128 if (bar.death == nullDeath) { // birth
129 barcode_.pop_back(); // sorted by birth and eventIndex has to be the highest one
130 } else { // death
131 bar.death = nullDeath;
132 };
133 indexToBar_.pop_back();
134 } else { // birth order eventually shuffled by vine updates. No sort possible to keep the matchings.
135 auto it = indexToBar_.find(eventIndex);
136 typename Barcode::iterator bar = it->second;
137
138 if (bar->death == nullDeath)
139 barcode_.erase(bar);
140 else
141 bar->death = nullDeath;
142
143 indexToBar_.erase(it);
144 }
145
146 auto it = PIDM::map_.find(eventIndex);
147 if (it != PIDM::map_.end()){
148 idToPosition_.erase(it->second);
149 PIDM::map_.erase(it);
150 }
151 }
152};
153
154} // namespace persistence_matrix
155} // namespace Gudhi
156
157#endif // PM_RU_PAIRING_H
Contains the Gudhi::persistence_matrix::Cell_position_to_ID_mapper class and Gudhi::persistence_matri...
Class managing the barcode for RU_matrix if the option was enabled.
Definition: ru_pairing.h:54
friend void swap(RU_pairing &pairing1, RU_pairing &pairing2)
Swap operator.
Definition: ru_pairing.h:82
const Barcode & get_current_barcode() const
Returns the current barcode which is maintained at any insertion, removal or vine swap.
Definition: ru_pairing.h:77
typename Master_matrix::Barcode Barcode
Definition: ru_pairing.h:65
RU_pairing()
Default constructor.
Definition: ru_pairing.h:70
Gudhi namespace.
Definition: SimplicialComplexForAlpha.h:14
Empty structure. Inherited instead of RU_pairing, when the computation of the barcode was not enabled...
Definition: ru_pairing.h:36