aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
indexedTree.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Class for storing trees (as data structures, not graphs).
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 #ifndef GUM_INDEXED_TREE_H
29 #define GUM_INDEXED_TREE_H
30 
31 #include <iostream>
32 
33 #include <agrum/agrum.h>
34 #include <agrum/tools/core/hashTable.h>
35 
36 namespace gum {
37 
38  // =========================================================================
39  // === NODES FOR GENERIC TREE (DATA) STRUCTURE === */
40  // =========================================================================
41 
42  /**
43  * @class IndexedTree
44  * @headerfile indexedTree.h <agrum/tools/core/indexedTree.h>
45  * @brief The class for storing the nodes of the Arborescence
46  * @ingroup basicstruct_group
47  *
48  * @tparam Key The tree's keys type.
49  * @tparam Data The tree's values type.
50  */
51  template < typename Key, typename Data >
52  class IndexedTree {
53  public:
54  // ============================================================================
55  /// @name Constructors / Destructors
56  // ============================================================================
57  /// @{
58 
59  /**
60  * @brief Creates a tree with one node with or without data.
61  *
62  * If data equals the nullptr, then tree is created with one node without
63  * data.
64  *
65  * @param data Adds data as the root of the tree.
66  */
67  IndexedTree(Data* data = nullptr);
68 
69  /**
70  * @brief Creates a tree with one node (with or without data).
71  *
72  * The parameters are inserted directly into the tree, i.e., no copy is
73  * performed. For copies of key and data to occur, use the constructor
74  * with const& parameters.
75  *
76  * If data equals the nullptr, then tree is created with one node without
77  * data.
78  *
79  * @param theKey The data's key.
80  * @param data The data added to the tree.
81  */
82  IndexedTree(const Key& theKey, Data* data = nullptr);
83 
84  /**
85  * @brief Creates a tree with one node with data.
86  *
87  * The key and data are copied into the tree. If you do not want any copy,
88  * use the constructor with the pointers parameters.
89  *
90  * @param theKey The data's key.
91  * @param data The data added to the tree.
92  */
93  IndexedTree(const Key& theKey, const Data& data);
94 
95  /**
96  * @brief Copy constructor.
97  * @param from The gum::IndexedTree to copy.
98  */
99  IndexedTree(const IndexedTree< Key, Data >& from);
100 
101  /**
102  * @brief Copy operator.
103  * @param from The gum::IndexedTree to copy.
104  * @return Returns this gum::IndexedTree.
105  */
106  IndexedTree< Key, Data >& operator=(const IndexedTree< Key, Data >& from);
107 
108  /**
109  * @brief Class destructor.
110  */
111  ~IndexedTree();
112 
113  /// @}
114  // ============================================================================
115  /// @name Accessors / Modifiers
116  // ============================================================================
117  /// @{
118 
119  /**
120  * @brief Adds a new node into the tree.
121  *
122  * @param index The node's index.
123  * @param data The node's data.
124  * @throw DuplicateElement exception is thrown if a node with an identical
125  * index has already been entered into the tree. If, in this case, you
126  * would like the value of the to be updated, use function setNode instead.
127  */
128  void insertNode(const std::vector< Key >& index, const Data* data);
129 
130  /**
131  * @brief Adds a new node into the tree.
132  *
133  * @param index The node's index.
134  * @param data The node's data.
135  * @throw DuplicateElement exception is thrown if a node with an identical
136  * index has already been entered into the tree. If, in this case, you
137  * would like the value of the to be updated, use function setNode instead.
138  */
139  void insertNode(const std::vector< Key >& index, const Data& data);
140 
141  /**
142  * @brief Updates the value of a node (or adds it if it does not already
143  * exist).
144  *
145  * @param index The node's index.
146  * @param data The node's data.
147  */
148  void setNode(const std::vector< Key >& index, Data* data);
149 
150  /**
151  * @brief Updates the value of a node (or adds it if it does not already
152  * exist).
153  *
154  * @param index The node's index.
155  * @param data The node's data.
156  */
157  void setNode(const std::vector< Key >& index, const Data& data);
158 
159  /**
160  * @brief Returns the value of a given node of the tree.
161  *
162  * @param index The node's index.
163  * @return Returns the data at index.
164  * @throw NotFound exception is thrown if the so-called value
165  * cannot be found in the tree.
166  */
167  Data& getData(const std::vector< Key >& index) const;
168 
169  /**
170  * @brief Returns a given node of the tree.
171  * @param index The node's index.
172  * @return Returns a given node of the tree.
173  * @throw NotFound exception is thrown if the node we look for cannot
174  * be found in the tree.
175  */
176  IndexedTree< Key, Data >& getNode(const std::vector< Key >& index) const;
177 
178  /// @}
179 
180  private:
181  /// The key of the current node.
182  Key key;
183 
184  /// The data stored into the node.
185  Data* data;
186 
187  /// The parent of the node.
188  IndexedTree< Key, Data >* parent;
189 
190  /// The list of children nodes of the current node.
191  HashTable< Key, IndexedTree< Key, Data >* > children;
192  };
193 
194  /// Necessary for the hashtable operator <<
195  template < typename Key, typename Data >
196  std::ostream& operator<<(std::ostream&, const IndexedTree< Key, Data >&);
197 
198 } /* namespace gum */
199 
200 // always include the templated implementations
201 #include <agrum/tools/core/indexedTree_tpl.h>
202 
203 #endif /* GUM_INDEXED_TREE_H */