aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
variableNodeMap.cpp
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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 Outlined implementation VariableNodeMap
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 
30 # include <agrum/tools/graphicalModels/variableNodeMap.h>
31 # include <iostream>
32 # include <sstream>
33 
34 # ifdef GUM_NO_INLINE
35 # include <agrum/tools/graphicalModels/variableNodeMap_inl.h>
36 # endif /* GUM_NO_INLINE */
37 
38 namespace gum {
39 
40  // Default constructor.
41  VariableNodeMap::VariableNodeMap() { GUM_CONSTRUCTOR(VariableNodeMap); }
42 
43  // Copy constructor.
44  VariableNodeMap::VariableNodeMap(const VariableNodeMap& source) {
45  GUM_CONS_CPY(VariableNodeMap);
46 
47  copy__(source);
48  }
49 
50  // Destructor
51  VariableNodeMap::~VariableNodeMap() {
52  GUM_DESTRUCTOR(VariableNodeMap);
53 
54  clear();
55  }
56 
57  // Copy operator.
58  VariableNodeMap& VariableNodeMap::operator=(const VariableNodeMap& source) {
59  clear();
60  copy__(source);
61 
62  return *this;
63  }
64 
65  void VariableNodeMap::clear() {
66  for (auto iter = nodes2vars__.begin(); iter != nodes2vars__.end(); ++iter)
67  delete iter.second();
68 
69  nodes2vars__.clear();
70  names2nodes__.clear();
71  }
72 
73  /// friendly displays the content of the VariableNodeMap
74  std::string VariableNodeMap::toString() const {
75  std::stringstream stream;
76 
77  stream << "list of associations:" << std::endl;
78  stream << nodes2vars__ << std::endl << std::endl;
79  stream << "list of variable names:" << std::endl;
80  stream << names2nodes__ << std::endl;
81 
82  return stream.str();
83  }
84 
85  /// do the copy
86  void VariableNodeMap::copy__(const VariableNodeMap& source) {
87  for (auto iter = source.nodes2vars__.begin();
88  iter != source.nodes2vars__.end();
89  ++iter)
90  nodes2vars__.insert(iter.first(), iter.second()->clone());
91 
92  // copy factory is used inside insert
93 
94  names2nodes__ = source.names2nodes__;
95  }
96 
97  /// for friendly displaying the content of clique graphs
98  std::ostream& operator<<(std::ostream& stream, const VariableNodeMap& v) {
99  stream << v.toString();
100  return stream;
101  }
102 
103 } /* namespace gum */
104 
105 #endif /* DOXYGEN_SHOULD_SKIP_THIS */