aGrUM  0.20.3
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 = children.begin();
81  iter != children.end();
82  ++iter)
83  iter->parent = this;
84  } catch (...) {
85  if (data) delete data;
86 
87  children.clear();
88 
89  throw;
90  }
91  }
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 134 of file indexedTree_tpl.h.

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

134  {
135  // for debugging purposes
136  GUM_DESTRUCTOR(IndexedTree);
137 
138  if (data) delete data;
139  }
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 349 of file indexedTree_tpl.h.

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

349  {
350  IndexedTree< Key, Data >* current_node = const_cast< IndexedTree< Key, Data >* >(this);
351 
352  for (unsigned int i = 0; i < index.size(); ++i)
353  current_node = current_node->children[index[i]];
354 
355  if (data == 0) { GUM_ERROR(NotFound, "the datum could not be found") }
356 
357  return *(current_node->data);
358  }
Data * data
The data stored into the node.
Definition: indexedTree.h:185
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ 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 364 of file indexedTree_tpl.h.

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

364  {
365  IndexedTree< Key, Data >* current_node = 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  return *current_node;
371  }
+ 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 144 of file indexedTree_tpl.h.

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

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

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

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

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

97  {
98  // avoid self assignment
99  if (this != &from) {
100  // for debugging purposes
101  GUM_OP_CPY(IndexedTree);
102 
103  try {
104  key = from.key;
105 
106  if (data) delete data;
107 
108  if (from.data)
109  data = new Data(*from.data);
110  else
111  data = 0;
112 
113  children = from.children;
114 
115  for (HashTableIteratorSafe< Key, IndexedTree< Key, Data > > iter = children.begin();
116  iter != children.end();
117  ++iter)
118  iter->parent = this;
119  } catch (...) {
120  if (data) delete data;
121 
122  children.clear();
123 
124  throw;
125  }
126  }
127 
128  return *this;
129  }
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 243 of file indexedTree_tpl.h.

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

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

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

296  {
297  // parse the tree until we are on the proper index. Then, insert the new
298  // node.
299  // current_node is a pointer on the node of the tree corresponding to
300  // position
301  // i in vector index. When i+2 < index.size(), we need go down into the tree
302  // structure before we can insert the new node
303  IndexedTree< Key, Data >* current_node = this;
304  unsigned int i;
305 
306  for (i = 0; i + 1 < index.size(); ++i) {
307  // if the node that should be on the path between the root of the tree and
308  // the node that we wish to insert does not exist, create it
309  if (!children.exists(index[i])) {
310  IndexedTree< Key, Data >* new_node = new IndexedTree< Key, Data >(index[i], (Data*)0);
311  current_node->children.insert(index[i], new_node);
312  new_node->parent = this;
313  current_node = new_node;
314  } else
315  current_node = current_node->children[index[i]];
316  }
317 
318  // here, we can set the new node. if ind + 1 == index.size(), this means
319  // that
320  // the index vector was not empty, else the vector was empty and we are at
321  // the
322  // root of the tree
323  if (i + 1 == index.size()) {
324  // if the node to be set does not exist, create it, else modify it
325  if (current_node->children.exists(index[i])) {
326  IndexedTree< Key, Data >* node = current_node->children[index[i]];
327 
328  if (node->data) delete node->data;
329 
330  node->data = new Data(theData);
331  } else {
332  // here, the node tobe set does not exist, so we must create it
333  IndexedTree< Key, Data >* new_node = new IndexedTree< Key, Data >(index[i], theData);
334  current_node->children.insert(index[i], new_node);
335  new_node->parent = current_node;
336  }
337  } else {
338  // here, the node to be set is the root of the tree (so it does already
339  // exist
340  if (data) delete data;
341 
342  data = new Data(theData);
343  }
344  }
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: