TTK
Loading...
Searching...
No Matches
DynamicTree.h
Go to the documentation of this file.
1
13
14#pragma once
15
16#include <algorithm>
17#include <iostream>
18#include <string>
19#include <vector>
20
21namespace ttk {
22
25 struct DynTreeNode {
27
28 // Accessor functions
29 // ------------------
30
31 inline bool hasParent() const {
32 return parent_ != nullptr;
33 }
34
35 // Graph functions
36 // ---------------
37
39 // Various way to do that, test perfs ?
40 void evert();
41
43 DynTreeNode *findRoot() const;
44
48 bool insertEdge(DynTreeNode *const n);
49
52 inline void removeEdge() {
53#ifndef TTK_ENABLE_KAMIKAZE
54 if(!parent_) {
55 std::cerr << "[DynamicTree]: DynTree remove edge in root node"
56 << std::endl;
57 return;
58 }
59#endif
60 parent_ = nullptr;
61 }
62 };
63
64 // WARNING : this class allows to maintain the number of components
65 // as it evolves, IT IS NOT PARALLEL SAFE
67 std::vector<DynTreeNode> nodes_{};
68
69 public:
70 // Initialize functions
71 // --------------------
72
73 inline void alloc(const std::size_t nbNodes) {
74 nodes_.resize(nbNodes);
75 }
76
78 inline DynTreeNode *getNode(const std::size_t nid) {
79 return &nodes_[nid];
80 }
81
82 inline const DynTreeNode *getNode(const std::size_t nid) const {
83 return &nodes_[nid];
84 }
85
87 inline std::size_t getNodeId(DynTreeNode *node) {
88 return node - &nodes_[0];
89 }
90
93 bool insertEdge(DynTreeNode *const n1, DynTreeNode *const n2) {
94 return n1->insertEdge(n2);
95 }
96
98 inline bool insertEdge(const std::size_t n1, const std::size_t n2) {
99 return getNode(n1)->insertEdge(getNode(n2));
100 }
101
103 inline void removeEdge(DynTreeNode *const n) {
104 n->removeEdge();
105 }
106
108 inline void removeEdge(const std::size_t nid) {
109 getNode(nid)->removeEdge();
110 }
111
114 int removeEdge(DynTreeNode *const n1, DynTreeNode *const n2) {
115 if(n1->parent_ == n2) {
116 n1->removeEdge();
117 return 1;
118 }
119 if(n2->parent_ == n1) {
120 n2->removeEdge();
121 return 2;
122 }
123 return 0;
124 }
125
128 inline int removeEdge(const std::size_t nid1, const std::size_t nid2) {
129 return removeEdge(getNode(nid1), getNode(nid2));
130 }
131
132 inline std::size_t getCCFromNode(const std::size_t n0) const {
133 return getNode(n0)->findRoot() - &nodes_[0];
134 }
135 void retrieveNbCC(std::vector<size_t> &nbccIds) const {
136 for(size_t nId = 0; nId < nodes_.size(); nId++) {
137 if(nodes_[nId].parent_ == nullptr)
138 nbccIds.emplace_back(nId);
139 }
140 }
141 inline size_t getNbCC() const {
142 return std::count_if(
143 nodes_.begin(), nodes_.end(),
144 [](const DynTreeNode &dtn) { return dtn.parent_ == nullptr; });
145 }
146
147 // Debug
148
149 std::string print() const;
150 std::string printNbCC() const;
151 };
152
153} // namespace ttk
Implements the Dynamic Tree data-structure (or ST-Tree)
Definition DynamicTree.h:66
void alloc(const std::size_t nbNodes)
Definition DynamicTree.h:73
bool insertEdge(DynTreeNode *const n1, DynTreeNode *const n2)
Definition DynamicTree.h:93
DynTreeNode * getNode(const std::size_t nid)
get the node with the id: nid
Definition DynamicTree.h:78
void removeEdge(const std::size_t nid)
remove the link btwn n and its parent
const DynTreeNode * getNode(const std::size_t nid) const
Definition DynamicTree.h:82
size_t getNbCC() const
void removeEdge(DynTreeNode *const n)
remove the link btwn n and its parent
bool insertEdge(const std::size_t n1, const std::size_t n2)
inert or replace existing edge between n1 and n2
Definition DynamicTree.h:98
std::size_t getNodeId(DynTreeNode *node)
get the id of the node: node
Definition DynamicTree.h:87
std::size_t getCCFromNode(const std::size_t n0) const
std::string print() const
int removeEdge(const std::size_t nid1, const std::size_t nid2)
void retrieveNbCC(std::vector< size_t > &nbccIds) const
std::string printNbCC() const
int removeEdge(DynTreeNode *const n1, DynTreeNode *const n2)
The Topology ToolKit.
class representing a node of a tree and the link to its parent if not the root
Definition DynamicTree.h:25
bool insertEdge(DynTreeNode *const n)
DynTreeNode * findRoot() const
Get representative node.
DynTreeNode * parent_
Definition DynamicTree.h:26
bool hasParent() const
Definition DynamicTree.h:31
void evert()
Make this node the root of its tree.