Loading...
Searching...
No Matches
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 Inria
6 *
7 * Modification(s):
8 * - YYYY/MM Author: Description of the modification
9 */
10
17
18#ifndef PM_RU_PAIRING_H
19#define PM_RU_PAIRING_H
20
21#include <unordered_map>
22
23namespace Gudhi {
24namespace persistence_matrix {
25
26template <typename Master_matrix>
27class RU_barcode_swap;
28
37 friend void swap([[maybe_unused]] Dummy_ru_pairing& d1, [[maybe_unused]] Dummy_ru_pairing& d2) noexcept {}
38};
39
48template <class Master_matrix>
50{
51 public:
52 using Barcode = typename Master_matrix::Barcode;
53
57 RU_pairing() = default;
58
64 const Barcode& get_current_barcode() const { return barcode_; }
65
69 friend void swap(RU_pairing& pairing1, RU_pairing& pairing2) noexcept
70 {
71 pairing1.barcode_.swap(pairing2.barcode_);
72 pairing1.indexToBar_.swap(pairing2.indexToBar_);
73 pairing1.idToPosition_.swap(pairing2.idToPosition_);
74 }
75
76 protected:
77 using Pos_index = typename Master_matrix::Pos_index;
78 using ID_index = typename Master_matrix::ID_index;
79 using Dimension = typename Master_matrix::Dimension;
80
81 void _reserve(unsigned int numberOfColumns) { indexToBar_.reserve(numberOfColumns); }
82
83 void _update_barcode(ID_index birthPivot, Pos_index death)
84 {
85 auto it = idToPosition_.find(birthPivot);
86 Pos_index pivotBirth = it == idToPosition_.end() ? birthPivot : it->second;
87 if constexpr (Master_matrix::hasFixedBarcode || !Master_matrix::Option_list::has_removable_columns) {
88 barcode_[indexToBar_[pivotBirth]].death = death;
89 indexToBar_.push_back(indexToBar_[pivotBirth]);
90 } else {
91 auto& barIt = indexToBar_.at(pivotBirth);
92 barIt->death = death;
93 indexToBar_.try_emplace(death, barIt); // list so iterators are stable
94 }
95 }
96
97 void _add_bar(Dimension dim, Pos_index birth)
98 {
99 barcode_.emplace_back(birth, Master_matrix::template get_null_value<Pos_index>(), dim);
100 if constexpr (Master_matrix::hasFixedBarcode || !Master_matrix::Option_list::has_removable_columns) {
101 indexToBar_.push_back(barcode_.size() - 1);
102 } else {
103 indexToBar_.try_emplace(birth, --barcode_.end());
104 }
105 }
106
107 void _remove_last(Pos_index eventIndex)
108 {
109 static_assert(Master_matrix::Option_list::has_removable_columns, "_remove_last not available.");
110 constexpr const Pos_index nullDeath = Master_matrix::template get_null_value<Pos_index>();
111
112 if constexpr (Master_matrix::hasFixedBarcode) {
113 auto& bar = barcode_[indexToBar_[eventIndex]];
114 if (bar.death == nullDeath) { // birth
115 barcode_.pop_back(); // sorted by birth and eventIndex has to be the highest one
116 } else { // death
117 bar.death = nullDeath;
118 };
119 indexToBar_.pop_back();
120 } else { // birth order eventually shuffled by vine updates. No sort possible to keep the matchings.
121 auto it = indexToBar_.find(eventIndex);
122 typename Barcode::iterator bar = it->second;
123
124 if (bar->death == nullDeath)
125 barcode_.erase(bar);
126 else
127 bar->death = nullDeath;
128
129 indexToBar_.erase(it);
130 }
131
132 auto& map = static_cast<typename Master_matrix::Master_RU_matrix*>(this)->positionToID_;
133 auto it = map.find(eventIndex);
134 if (it != map.end()) {
135 idToPosition_.erase(it->second);
136 map.erase(it);
137 }
138 }
139
140 void _insert_id_position(ID_index id, Pos_index pos) { idToPosition_.emplace(id, pos); }
141
142 void _reset()
143 {
144 barcode_.clear();
145 indexToBar_.clear();
146 }
147
148 private:
149 using Dictionary = typename Master_matrix::Bar_dictionary;
150
151 // could also just mark everything as protected as RU_barcode_swap inherits from RU_pairing
152 // but this way, it marks a better difference between "class using this mixin" with "class extending this mixin"
153 friend RU_barcode_swap<Master_matrix>;
154
155 Barcode barcode_;
156 Dictionary indexToBar_;
160 std::unordered_map<ID_index, Pos_index> idToPosition_; // TODO: test other map types
161};
162
163} // namespace persistence_matrix
164} // namespace Gudhi
165
166#endif // PM_RU_PAIRING_H
Class managing the barcode for RU_vine_swap.
Definition ru_vine_swap.h:62
const Barcode & get_current_barcode() const
Returns the current barcode which is maintained at any insertion, removal or vine swap.
Definition ru_pairing.h:64
RU_pairing()=default
Default constructor.
typename Master_matrix::Barcode Barcode
Definition ru_pairing.h:52
friend void swap(RU_pairing &pairing1, RU_pairing &pairing2) noexcept
Swap operator.
Definition ru_pairing.h:69
Persistence matrix namespace.
Definition FieldOperators.h:18
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