aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
variableNodeMap.cpp
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 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() {
42  GUM_CONSTRUCTOR(VariableNodeMap);
43  ;
44  }
45 
46  // Copy constructor.
47  VariableNodeMap::VariableNodeMap(const VariableNodeMap& source) {
48  GUM_CONS_CPY(VariableNodeMap);
49 
50  _copy_(source);
51  }
52 
53  // Destructor
54  VariableNodeMap::~VariableNodeMap() {
55  GUM_DESTRUCTOR(VariableNodeMap);
56 
57  clear();
58  }
59 
60  // Copy operator.
61  VariableNodeMap& VariableNodeMap::operator=(const VariableNodeMap& source) {
62  clear();
63  _copy_(source);
64 
65  return *this;
66  }
67 
68  void VariableNodeMap::clear() {
69  for (auto iter = _nodes2vars_.begin(); iter != _nodes2vars_.end(); ++iter)
70  delete iter.second();
71 
72  _nodes2vars_.clear();
73  _names2nodes_.clear();
74  }
75 
76  /// friendly displays the content of the VariableNodeMap
77  std::string VariableNodeMap::toString() const {
78  std::stringstream stream;
79 
80  stream << "list of associations:" << std::endl;
81  stream << _nodes2vars_ << std::endl << std::endl;
82  stream << "list of variable names:" << std::endl;
83  stream << _names2nodes_ << std::endl;
84 
85  return stream.str();
86  }
87 
88  /// do the copy
89  void VariableNodeMap::_copy_(const VariableNodeMap& source) {
90  for (auto iter = source._nodes2vars_.begin(); iter != source._nodes2vars_.end(); ++iter)
91  _nodes2vars_.insert(iter.first(), iter.second()->clone());
92 
93  // copy factory is used inside insert
94 
95  _names2nodes_ = source._names2nodes_;
96  }
97 
98  /// for friendly displaying the content of clique graphs
99  std::ostream& operator<<(std::ostream& stream, const VariableNodeMap& v) {
100  stream << v.toString();
101  return stream;
102  }
103 
104 } /* namespace gum */
105 
106 #endif /* DOXYGEN_SHOULD_SKIP_THIS */