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

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

References gum::Set< Key, Alloc >::emplace().

51  : data(theData), parent(0) {
52  // for debugging purposes
53  GUM_CONSTRUCTOR(IndexedTree);
54  }
IndexedTree< Key, Data > * parent
The parent of the node.
Definition: indexedTree.h:188
IndexedTree(Data *data=nullptr)
Creates a tree with one node with or without data.
Data * data
The data stored into the node.
Definition: indexedTree.h:185
+ Here is the call 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 42 of file indexedTree_tpl.h.

42  :
43  key(theKey), data(theData), parent(0) {
44  // for debugging purposes
45  GUM_CONSTRUCTOR(IndexedTree);
46  }
IndexedTree< Key, Data > * parent
The parent of the node.
Definition: indexedTree.h:188
IndexedTree(Data *data=nullptr)
Creates a tree with one node with or without data.
Data * data
The data stored into the node.
Definition: indexedTree.h:185
Key key
The key of the current node.
Definition: indexedTree.h:182

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

References gum::Set< Key, Alloc >::emplace().

59  :
60  key(theKey), data(new Data(theData)), parent(0) {
61  // for debugging purposes
62  GUM_CONSTRUCTOR(IndexedTree);
63  }
IndexedTree< Key, Data > * parent
The parent of the node.
Definition: indexedTree.h:188
IndexedTree(Data *data=nullptr)
Creates a tree with one node with or without data.
Data * data
The data stored into the node.
Definition: indexedTree.h:185
Key key
The key of the current node.
Definition: indexedTree.h:182
+ 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 68 of file indexedTree_tpl.h.

References gum::Set< Key, Alloc >::emplace().

68  :
69  key(from.key), data(0), parent(0) {
70  // for debugging purposes
71  GUM_CONS_CPY(IndexedTree);
72 
73  try {
74  // create the data of the node
75  if (from.data) data = new Data(*from.data);
76 
77  // create and link properly the children
78  children = from.children;
79 
80  for (HashTableIteratorSafe< Key, IndexedTree< Key, Data > > iter
81  = children.begin();
82  iter != children.end();
83  ++iter)
84  iter->parent = this;
85  } catch (...) {
86  if (data) delete data;
87 
88  children.clear();
89 
90  throw;
91  }
92  }
IndexedTree< Key, Data > * parent
The parent of the node.
Definition: indexedTree.h:188
IndexedTree(Data *data=nullptr)
Creates a tree with one node with or without data.
HashTable< Key, IndexedTree< Key, Data > *> children
The list of children nodes of the current node.
Definition: indexedTree.h:191
Data * data
The data stored into the node.
Definition: indexedTree.h:185
Key key
The key of the current node.
Definition: indexedTree.h:182
+ Here is the call graph for this function:

◆ ~IndexedTree()

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

Class destructor.

Definition at line 136 of file indexedTree_tpl.h.

References gum::Set< Key, Alloc >::emplace().

136  {
137  // for debugging purposes
138  GUM_DESTRUCTOR(IndexedTree);
139 
140  if (data) delete data;
141  }
IndexedTree(Data *data=nullptr)
Creates a tree with one node with or without data.
Data * data
The data stored into the node.
Definition: indexedTree.h:185
+ 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 363 of file indexedTree_tpl.h.

References gum::Set< Key, Alloc >::emplace().

363  {
364  IndexedTree< Key, Data >* current_node
365  = const_cast< IndexedTree< Key, Data >* >(this);
366 
367  for (unsigned int i = 0; i < index.size(); ++i)
368  current_node = current_node->children[index[i]];
369 
370  if (data == 0) { GUM_ERROR(NotFound, "the datum could not be found"); }
371 
372  return *(current_node->data);
373  }
Data * data
The data stored into the node.
Definition: indexedTree.h:185
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ 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.

References gum::Set< Key, Alloc >::emplace().

379  {
380  IndexedTree< Key, Data >* current_node
381  = const_cast< IndexedTree< Key, Data >* >(this);
382 
383  for (unsigned int i = 0; i < index.size(); ++i)
384  current_node = current_node->children[index[i]];
385 
386  return *current_node;
387  }
+ 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 146 of file indexedTree_tpl.h.

References gum::Set< Key, Alloc >::emplace().

147  {
148  // parse the tree until we are on the proper index. Then, insert the new
149  // node.
150  // current_node is a pointer on the node of the tree corresponding to
151  // position
152  // i in vector index. When i+2 < index.size(), we need go down into the tree
153  // structure before we can insert the new node
154  IndexedTree< Key, Data >* current_node = this;
155  unsigned int i;
156 
157  for (i = 0; i + 1 < index.size(); ++i) {
158  // if the node that should be on the path between the root of the tree and
159  // the node that we wish to insert does not exist, create it
160  if (!children.exists(index[i])) {
161  IndexedTree< Key, Data >* new_node
162  = new IndexedTree< Key, Data >(index[i], (Data*)0);
163  current_node->children.insert(index[i], new_node);
164  new_node->parent = this;
165  current_node = new_node;
166  } else
167  current_node = current_node->children[index[i]];
168  }
169 
170  // here, we can insert the new node. if ind + 1 == index.size(), this means
171  // that
172  // the index vector was not empty, else the vector was empty and we are at
173  // the
174  // root of the tree
175  if (i + 1 == index.size()) {
176  // if the node to be inserted already exist, throw an exception
177  if (current_node->children.exists(index[i])) {
178  GUM_ERROR(DuplicateElement, "the indexed tree already contains the node");
179  }
180 
181  // here, the node to be inserted does not exist, so we must create it
182  IndexedTree< Key, Data >* new_node
183  = new IndexedTree< Key, Data >(index[i], const_cast< Data* >(theData));
184 
185  current_node->children.insert(index[i], new_node);
186 
187  new_node->parent = current_node;
188  } else {
189  // here, the node to be inserted is the root of the tree (so it already
190  // exists)
191  GUM_ERROR(DuplicateElement, "the indexed tree already contains the node");
192  }
193  }
HashTable< Key, IndexedTree< Key, Data > *> children
The list of children nodes of the current node.
Definition: indexedTree.h:191
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ 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 198 of file indexedTree_tpl.h.

References gum::Set< Key, Alloc >::emplace().

199  {
200  // parse the tree until we are on the proper index. Then, insert the new
201  // node.
202  // current_node is a pointer on the node of the tree corresponding to
203  // position
204  // i in vector index. When i+2 < index.size(), we need go down into the tree
205  // structure before we can insert the new node
206  IndexedTree< Key, Data >* current_node = this;
207  unsigned int i;
208 
209  for (i = 0; i + 1 < index.size(); ++i) {
210  // if the node that should be on the path between the root of the tree and
211  // the node that we wish to insert does not exist, create it
212  if (!children.exists(index[i])) {
213  IndexedTree< Key, Data >* new_node
214  = new IndexedTree< Key, Data >(index[i], (Data*)0);
215  current_node->children.insert(index[i], new_node);
216  new_node->parent = this;
217  current_node = new_node;
218  } else
219  current_node = current_node->children[index[i]];
220  }
221 
222  // here, we can insert the new node. if ind + 1 == index.size(), this means
223  // that
224  // the index vector was not empty, else the vector was empty and we are at
225  // the
226  // root of the tree
227  if (i + 1 == index.size()) {
228  // if the node to be inserted already exist, throw an exception
229  if (current_node->children.exists(index[i])) {
230  GUM_ERROR(DuplicateElement, "the indexed tree already contains the node");
231  }
232 
233  // here, the node to be inserted does not exist, so we must create it
234  IndexedTree< Key, Data >* new_node
235  = new IndexedTree< Key, Data >(index[i], theData);
236 
237  current_node->children.insert(index[i], new_node);
238 
239  new_node->parent = current_node;
240  } else {
241  // here, the node to be inserted is the root of the tree (so it already
242  // exists)
243  GUM_ERROR(DuplicateElement, "the indexed tree already contains the node");
244  }
245  }
HashTable< Key, IndexedTree< Key, Data > *> children
The list of children nodes of the current node.
Definition: indexedTree.h:191
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ 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 98 of file indexedTree_tpl.h.

References gum::Set< Key, Alloc >::emplace().

98  {
99  // avoid self assignment
100  if (this != &from) {
101  // for debugging purposes
102  GUM_OP_CPY(IndexedTree);
103 
104  try {
105  key = from.key;
106 
107  if (data) delete data;
108 
109  if (from.data)
110  data = new Data(*from.data);
111  else
112  data = 0;
113 
114  children = from.children;
115 
116  for (HashTableIteratorSafe< Key, IndexedTree< Key, Data > > iter
117  = children.begin();
118  iter != children.end();
119  ++iter)
120  iter->parent = this;
121  } catch (...) {
122  if (data) delete data;
123 
124  children.clear();
125 
126  throw;
127  }
128  }
129 
130  return *this;
131  }
IndexedTree(Data *data=nullptr)
Creates a tree with one node with or without data.
HashTable< Key, IndexedTree< Key, Data > *> children
The list of children nodes of the current node.
Definition: indexedTree.h:191
Data * data
The data stored into the node.
Definition: indexedTree.h:185
Key key
The key of the current node.
Definition: indexedTree.h:182
+ 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,
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 250 of file indexedTree_tpl.h.

References gum::Set< Key, Alloc >::emplace().

251  {
252  // parse the tree until we are on the proper index. Then, insert the new
253  // node.
254  // current_node is a pointer on the node of the tree corresponding to
255  // position
256  // i in vector index. When i+2 < index.size(), we need go down into the tree
257  // structure before we can insert the new node
258  IndexedTree< Key, Data >* current_node = this;
259  unsigned int i;
260 
261  for (i = 0; i + 1 < index.size(); ++i) {
262  // if the node that should be on the path between the root of the tree and
263  // the node that we wish to insert does not exist, create it
264  if (!children.exists(index[i])) {
265  IndexedTree< Key, Data >* new_node
266  = new IndexedTree< Key, Data >(index[i], (Data*)0);
267  current_node->children.insert(index[i], new_node);
268  new_node->parent = this;
269  current_node = new_node;
270  } else
271  current_node = current_node->children[index[i]];
272  }
273 
274  // here, we can set the new node. if ind + 1 == index.size(), this means
275  // that
276  // the index vector was not empty, else the vector was empty and we are at
277  // the
278  // root of the tree
279  if (i + 1 == index.size()) {
280  // if the node to be set does not exist, create it, else modify it
281  if (current_node->children.exists(index[i])) {
282  IndexedTree< Key, Data >* node = current_node->children[index[i]];
283 
284  if (node->data) delete node->data;
285 
286  node->data = theData;
287  } else {
288  // here, the node tobe set does not exist, so we must create it
289  IndexedTree< Key, Data >* new_node
290  = new IndexedTree< Key, Data >(index[i], theData);
291  current_node->children.insert(index[i], new_node);
292  new_node->parent = current_node;
293  }
294  } else {
295  // here, the node to be set is the root of the tree (so it does already
296  // exist
297  if (data) delete data;
298 
299  data = theData;
300  }
301  }
HashTable< Key, IndexedTree< Key, Data > *> children
The list of children nodes of the current node.
Definition: indexedTree.h:191
Data * data
The data stored into the node.
Definition: indexedTree.h:185
+ 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,
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 306 of file indexedTree_tpl.h.

References gum::Set< Key, Alloc >::emplace().

307  {
308  // parse the tree until we are on the proper index. Then, insert the new
309  // node.
310  // current_node is a pointer on the node of the tree corresponding to
311  // position
312  // i in vector index. When i+2 < index.size(), we need go down into the tree
313  // structure before we can insert the new node
314  IndexedTree< Key, Data >* current_node = this;
315  unsigned int i;
316 
317  for (i = 0; i + 1 < index.size(); ++i) {
318  // if the node that should be on the path between the root of the tree and
319  // the node that we wish to insert does not exist, create it
320  if (!children.exists(index[i])) {
321  IndexedTree< Key, Data >* new_node
322  = new IndexedTree< Key, Data >(index[i], (Data*)0);
323  current_node->children.insert(index[i], new_node);
324  new_node->parent = this;
325  current_node = new_node;
326  } else
327  current_node = current_node->children[index[i]];
328  }
329 
330  // here, we can set the new node. if ind + 1 == index.size(), this means
331  // that
332  // the index vector was not empty, else the vector was empty and we are at
333  // the
334  // root of the tree
335  if (i + 1 == index.size()) {
336  // if the node to be set does not exist, create it, else modify it
337  if (current_node->children.exists(index[i])) {
338  IndexedTree< Key, Data >* node = current_node->children[index[i]];
339 
340  if (node->data) delete node->data;
341 
342  node->data = new Data(theData);
343  } else {
344  // here, the node tobe set does not exist, so we must create it
345  IndexedTree< Key, Data >* new_node
346  = new IndexedTree< Key, Data >(index[i], theData);
347  current_node->children.insert(index[i], new_node);
348  new_node->parent = current_node;
349  }
350  } else {
351  // here, the node to be set is the root of the tree (so it does already
352  // exist
353  if (data) delete data;
354 
355  data = new Data(theData);
356  }
357  }
HashTable< Key, IndexedTree< Key, Data > *> children
The list of children nodes of the current node.
Definition: indexedTree.h:191
Data * data
The data stored into the node.
Definition: indexedTree.h:185
+ 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 191 of file indexedTree.h.

◆ data

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

The data stored into the node.

Definition at line 185 of file indexedTree.h.

◆ key

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

The key of the current node.

Definition at line 182 of file indexedTree.h.

◆ parent

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

The parent of the node.

Definition at line 188 of file indexedTree.h.


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