aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
gum::IndexedTree< Key, Data > Class Template Reference

The class for storing the nodes of the Arborescence. More...

#include <agrum/base/core/indexedTree.h>

Collaboration diagram for gum::IndexedTree< Key, Data >:

Public Member Functions

Constructors / Destructors
 IndexedTree (Data *data=nullptr)
 Creates a tree with one node with or without data.
 IndexedTree (const Key &theKey, Data *data=nullptr)
 Creates a tree with one node (with or without data).
 IndexedTree (const Key &theKey, const Data &data)
 Creates a tree with one node with data.
 IndexedTree (const IndexedTree< Key, Data > &from)
 Copy constructor.
IndexedTree< Key, Data > & operator= (const IndexedTree< Key, Data > &from)
 Copy operator.
 ~IndexedTree ()
 Class destructor.
Accessors / Modifiers
void insertNode (const std::vector< Key > &index, const Data *data)
 Adds a new node into the tree.
void insertNode (const std::vector< Key > &index, const Data &data)
 Adds a new node into the tree.
void setNode (const std::vector< Key > &index, Data *data)
 Updates the value of a node (or adds it if it does not already exist).
void setNode (const std::vector< Key > &index, const Data &data)
 Updates the value of a node (or adds it if it does not already exist).
Data & getData (const std::vector< Key > &index) const
 Returns the value of a given node of the tree.
IndexedTree< Key, Data > & getNode (const std::vector< Key > &index) const
 Returns a given node of the tree.

Private Attributes

Key key
 The key of the current node.
Data * data
 The data stored into the node.
IndexedTree< Key, Data > * parent
 The parent of the node.
HashTable< Key, IndexedTree< Key, Data > * > children
 The list of children nodes of the current node.

Detailed Description

template<typename Key, typename Data>
class gum::IndexedTree< Key, Data >

The class for storing the nodes of the Arborescence.

Template Parameters
KeyThe tree's keys type.
DataThe tree's values type.

Definition at line 73 of file indexedTree.h.

Constructor & Destructor Documentation

◆ IndexedTree() [1/4]

template<typename Key, typename Data>
gum::IndexedTree< Key, Data >::IndexedTree ( Data * data = nullptr)

Creates a tree with one node with or without data.

If data equals the nullptr, then tree is created with one node without data.

Parameters
dataAdds data as the root of the tree.

Definition at line 72 of file indexedTree_tpl.h.

72 : data(theData), parent(0) {
73 // for debugging purposes
75 }
The class for storing the nodes of the Arborescence.
Definition indexedTree.h:73
Data * data
The data stored into the node.
IndexedTree< Key, Data > * parent
The parent of the node.
IndexedTree(Data *data=nullptr)
Creates a tree with one node with or without data.

References IndexedTree(), data, and parent.

Referenced by IndexedTree(), IndexedTree(), IndexedTree(), IndexedTree(), ~IndexedTree(), getData(), getNode(), insertNode(), insertNode(), operator=(), setNode(), and setNode().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IndexedTree() [2/4]

template<typename Key, typename Data>
gum::IndexedTree< Key, Data >::IndexedTree ( const Key & theKey,
Data * data = nullptr )

Creates a tree with one node (with or without data).

The parameters are inserted directly into the tree, i.e., no copy is performed. For copies of key and data to occur, use the constructor with const& parameters.

If data equals the nullptr, then tree is created with one node without data.

Parameters
theKeyThe data's key.
dataThe data added to the tree.

Definition at line 63 of file indexedTree_tpl.h.

63 :
65 // for debugging purposes
67 }
Key key
The key of the current node.

References IndexedTree(), data, key, and parent.

Here is the call graph for this function:

◆ IndexedTree() [3/4]

template<typename Key, typename Data>
gum::IndexedTree< Key, Data >::IndexedTree ( const Key & theKey,
const Data & data )

Creates a tree with one node with data.

The key and data are copied into the tree. If you do not want any copy, use the constructor with the pointers parameters.

Parameters
theKeyThe data's key.
dataThe data added to the tree.

Definition at line 80 of file indexedTree_tpl.h.

80 :
81 key(theKey), data(new Data(theData)), parent(0) {
82 // for debugging purposes
84 }

References IndexedTree(), data, key, and parent.

Here is the call graph for this function:

◆ IndexedTree() [4/4]

template<typename Key, typename Data>
gum::IndexedTree< Key, Data >::IndexedTree ( const IndexedTree< Key, Data > & from)

Copy constructor.

Parameters
fromThe gum::IndexedTree to copy.

Definition at line 89 of file indexedTree_tpl.h.

89 :
90 key(from.key), data(0), parent(0) {
91 // for debugging purposes
93
94 try {
95 // create the data of the node
96 if (from.data) data = new Data(*from.data);
97
98 // create and link properly the children
100
102 iter != children.end();
103 ++iter)
104 iter->parent = this;
105 } catch (...) {
106 if (data) delete data;
107
108 children.clear();
109
110 throw;
111 }
112 }
HashTable< Key, IndexedTree< Key, Data > * > children
The list of children nodes of the current node.

References IndexedTree(), children, data, key, and parent.

Here is the call graph for this function:

◆ ~IndexedTree()

template<typename Key, typename Data>
gum::IndexedTree< Key, Data >::~IndexedTree ( )

Class destructor.

Definition at line 153 of file indexedTree_tpl.h.

153 {
154 // for debugging purposes
156
157 if (data) delete data;
158 }

References IndexedTree(), and data.

Here is the call graph for this function:

Member Function Documentation

◆ getData()

template<typename Key, typename Data>
INLINE Data & gum::IndexedTree< Key, Data >::getData ( const std::vector< Key > & index) const

Returns the value of a given node of the tree.

Parameters
indexThe node's index.
Returns
Returns the data at index.
Exceptions
NotFoundexception is thrown if the so-called value cannot be found in the tree.

Definition at line 364 of file indexedTree_tpl.h.

364 {
366
367 for (unsigned int i = 0; i < index.size(); ++i)
369
370 if (data == 0) { GUM_ERROR(NotFound, "the datum could not be found") }
371
372 return *(current_node->data);
373 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

References IndexedTree(), children, data, and GUM_ERROR.

Here is the call graph for this function:

◆ getNode()

template<typename Key, typename Data>
INLINE IndexedTree< Key, Data > & gum::IndexedTree< Key, Data >::getNode ( const std::vector< Key > & index) const

Returns a given node of the tree.

Parameters
indexThe node's index.
Returns
Returns a given node of the tree.
Exceptions
NotFoundexception is thrown if the node we look for cannot be found in the tree.

Definition at line 379 of file indexedTree_tpl.h.

379 {
381
382 for (unsigned int i = 0; i < index.size(); ++i)
384
385 return *current_node;
386 }

References IndexedTree(), and children.

Here is the call graph for this function:

◆ insertNode() [1/2]

template<typename Key, typename Data>
void gum::IndexedTree< Key, Data >::insertNode ( const std::vector< Key > & index,
const Data & data )

Adds a new node into the tree.

Parameters
indexThe node's index.
dataThe node's data.
Exceptions
DuplicateElementexception is thrown if a node with an identical index has already been entered into the tree. If, in this case, you would like the value of the to be updated, use function setNode instead.

Definition at line 212 of file indexedTree_tpl.h.

212 {
213 // parse the tree until we are on the proper index. Then, insert the new
214 // node.
215 // current_node is a pointer on the node of the tree corresponding to
216 // position
217 // i in vector index. When i+2 < index.size(), we need go down into the tree
218 // structure before we can insert the new node
220 unsigned int i;
221
222 for (i = 0; i + 1 < index.size(); ++i) {
223 // if the node that should be on the path between the root of the tree and
224 // the node that we wish to insert does not exist, create it
225 if (!children.exists(index[i])) {
228 new_node->parent = this;
231 }
232
233 // here, we can insert the new node. if ind + 1 == index.size(), this means
234 // that
235 // the index vector was not empty, else the vector was empty and we are at
236 // the
237 // root of the tree
238 if (i + 1 == index.size()) {
239 // if the node to be inserted already exist, throw an exception
240 if (current_node->children.exists(index[i])) {
241 GUM_ERROR(DuplicateElement, "the indexed tree already contains the node")
242 }
243
244 // here, the node to be inserted does not exist, so we must create it
246
248
250 } else {
251 // here, the node to be inserted is the root of the tree (so it already
252 // exists)
253 GUM_ERROR(DuplicateElement, "the indexed tree already contains the node")
254 }
255 }

References IndexedTree(), children, GUM_ERROR, and parent.

Here is the call graph for this function:

◆ insertNode() [2/2]

template<typename Key, typename Data>
void gum::IndexedTree< Key, Data >::insertNode ( const std::vector< Key > & index,
const Data * data )

Adds a new node into the tree.

Parameters
indexThe node's index.
dataThe node's data.
Exceptions
DuplicateElementexception is thrown if a node with an identical index has already been entered into the tree. If, in this case, you would like the value of the to be updated, use function setNode instead.

Definition at line 163 of file indexedTree_tpl.h.

163 {
164 // parse the tree until we are on the proper index. Then, insert the new
165 // node.
166 // current_node is a pointer on the node of the tree corresponding to
167 // position
168 // i in vector index. When i+2 < index.size(), we need go down into the tree
169 // structure before we can insert the new node
171 unsigned int i;
172
173 for (i = 0; i + 1 < index.size(); ++i) {
174 // if the node that should be on the path between the root of the tree and
175 // the node that we wish to insert does not exist, create it
176 if (!children.exists(index[i])) {
179 new_node->parent = this;
182 }
183
184 // here, we can insert the new node. if ind + 1 == index.size(), this means
185 // that
186 // the index vector was not empty, else the vector was empty and we are at
187 // the
188 // root of the tree
189 if (i + 1 == index.size()) {
190 // if the node to be inserted already exist, throw an exception
191 if (current_node->children.exists(index[i])) {
192 GUM_ERROR(DuplicateElement, "the indexed tree already contains the node")
193 }
194
195 // here, the node to be inserted does not exist, so we must create it
197 = new IndexedTree< Key, Data >(index[i], const_cast< Data* >(theData));
198
200
202 } else {
203 // here, the node to be inserted is the root of the tree (so it already
204 // exists)
205 GUM_ERROR(DuplicateElement, "the indexed tree already contains the node")
206 }
207 }

References IndexedTree(), children, GUM_ERROR, and parent.

Here is the call graph for this function:

◆ operator=()

template<typename Key, typename Data>
IndexedTree< Key, Data > & gum::IndexedTree< Key, Data >::operator= ( const IndexedTree< Key, Data > & from)

Copy operator.

Parameters
fromThe gum::IndexedTree to copy.
Returns
Returns this gum::IndexedTree.

Definition at line 118 of file indexedTree_tpl.h.

118 {
119 // avoid self assignment
120 if (this != &from) {
121 // for debugging purposes
123
124 try {
125 key = from.key;
126
127 if (data) delete data;
128
129 if (from.data) data = new Data(*from.data);
130 else data = 0;
131
133
135 iter != children.end();
136 ++iter)
137 iter->parent = this;
138 } catch (...) {
139 if (data) delete data;
140
141 children.clear();
142
143 throw;
144 }
145 }
146
147 return *this;
148 }

References IndexedTree(), children, data, and key.

Here is the call graph for this function:

◆ setNode() [1/2]

template<typename Key, typename Data>
void gum::IndexedTree< Key, Data >::setNode ( const std::vector< Key > & index,
const Data & data )

Updates the value of a node (or adds it if it does not already exist).

Parameters
indexThe node's index.
dataThe node's data.

Definition at line 312 of file indexedTree_tpl.h.

312 {
313 // parse the tree until we are on the proper index. Then, insert the new
314 // node.
315 // current_node is a pointer on the node of the tree corresponding to
316 // position
317 // i in vector index. When i+2 < index.size(), we need go down into the tree
318 // structure before we can insert the new node
320 unsigned int i;
321
322 for (i = 0; i + 1 < index.size(); ++i) {
323 // if the node that should be on the path between the root of the tree and
324 // the node that we wish to insert does not exist, create it
325 if (!children.exists(index[i])) {
328 new_node->parent = this;
331 }
332
333 // here, we can set the new node. if ind + 1 == index.size(), this means
334 // that
335 // the index vector was not empty, else the vector was empty and we are at
336 // the
337 // root of the tree
338 if (i + 1 == index.size()) {
339 // if the node to be set does not exist, create it, else modify it
340 if (current_node->children.exists(index[i])) {
342
343 if (node->data) delete node->data;
344
345 node->data = new Data(theData);
346 } else {
347 // here, the node tobe set does not exist, so we must create it
351 }
352 } else {
353 // here, the node to be set is the root of the tree (so it does already
354 // exist
355 if (data) delete data;
356
357 data = new Data(theData);
358 }
359 }

References IndexedTree(), children, data, and parent.

Here is the call graph for this function:

◆ setNode() [2/2]

template<typename Key, typename Data>
void gum::IndexedTree< Key, Data >::setNode ( const std::vector< Key > & index,
Data * data )

Updates the value of a node (or adds it if it does not already exist).

Parameters
indexThe node's index.
dataThe node's data.

Definition at line 260 of file indexedTree_tpl.h.

260 {
261 // parse the tree until we are on the proper index. Then, insert the new
262 // node.
263 // current_node is a pointer on the node of the tree corresponding to
264 // position
265 // i in vector index. When i+2 < index.size(), we need go down into the tree
266 // structure before we can insert the new node
268 unsigned int i;
269
270 for (i = 0; i + 1 < index.size(); ++i) {
271 // if the node that should be on the path between the root of the tree and
272 // the node that we wish to insert does not exist, create it
273 if (!children.exists(index[i])) {
276 new_node->parent = this;
279 }
280
281 // here, we can set the new node. if ind + 1 == index.size(), this means
282 // that
283 // the index vector was not empty, else the vector was empty and we are at
284 // the
285 // root of the tree
286 if (i + 1 == index.size()) {
287 // if the node to be set does not exist, create it, else modify it
288 if (current_node->children.exists(index[i])) {
290
291 if (node->data) delete node->data;
292
293 node->data = theData;
294 } else {
295 // here, the node tobe set does not exist, so we must create it
299 }
300 } else {
301 // here, the node to be set is the root of the tree (so it does already
302 // exist
303 if (data) delete data;
304
305 data = theData;
306 }
307 }

References IndexedTree(), children, data, and parent.

Here is the call graph for this function:

Member Data Documentation

◆ children

template<typename Key, typename Data>
HashTable< Key, IndexedTree< Key, Data >* > gum::IndexedTree< Key, Data >::children
private

The list of children nodes of the current node.

Definition at line 212 of file indexedTree.h.

Referenced by IndexedTree(), getData(), getNode(), insertNode(), insertNode(), operator=(), setNode(), and setNode().

◆ data

template<typename Key, typename Data>
Data* gum::IndexedTree< Key, Data >::data
private

The data stored into the node.

Definition at line 206 of file indexedTree.h.

Referenced by IndexedTree(), IndexedTree(), IndexedTree(), IndexedTree(), ~IndexedTree(), getData(), operator=(), setNode(), and setNode().

◆ key

template<typename Key, typename Data>
Key gum::IndexedTree< Key, Data >::key
private

The key of the current node.

Definition at line 203 of file indexedTree.h.

Referenced by IndexedTree(), IndexedTree(), IndexedTree(), and operator=().

◆ parent

template<typename Key, typename Data>
IndexedTree< Key, Data >* gum::IndexedTree< Key, Data >::parent
private

The parent of the node.

Definition at line 209 of file indexedTree.h.

Referenced by IndexedTree(), IndexedTree(), IndexedTree(), IndexedTree(), insertNode(), insertNode(), setNode(), and setNode().


The documentation for this class was generated from the following files: