aGrUM 3.1.1
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 73 of file indexedTree_tpl.h.

73 : data(theData), parent(0) {
74 // for debugging purposes
76 }
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 64 of file indexedTree_tpl.h.

64 :
66 // for debugging purposes
68 }
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 81 of file indexedTree_tpl.h.

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

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 90 of file indexedTree_tpl.h.

90 :
91 key(from.key), data(0), parent(0) {
92 // for debugging purposes
94
95 try {
96 // create the data of the node
97 if (from.data) data = new Data(*from.data);
98
99 // create and link properly the children
101
103 iter != children.end();
104 ++iter)
105 iter->parent = this;
106 } catch (...) {
107 if (data) delete data;
108
109 children.clear();
110
111 throw;
112 }
113 }
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 154 of file indexedTree_tpl.h.

154 {
155 // for debugging purposes
157
158 if (data) delete data;
159 }

References IndexedTree(), and data.

Here is the call graph for this function:

Member Function Documentation

◆ getData()

template<typename Key, typename Data>
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 365 of file indexedTree_tpl.h.

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

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

Here is the call graph for this function:

◆ getNode()

template<typename Key, typename Data>
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 380 of file indexedTree_tpl.h.

380 {
382
383 for (unsigned int i = 0; i < index.size(); ++i)
385
386 return *current_node;
387 }

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 213 of file indexedTree_tpl.h.

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

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 164 of file indexedTree_tpl.h.

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

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 119 of file indexedTree_tpl.h.

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

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 313 of file indexedTree_tpl.h.

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

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 261 of file indexedTree_tpl.h.

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

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: