aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
variableNodeMap.cpp
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
48#ifndef DOXYGEN_SHOULD_SKIP_THIS
49
50# include <iostream>
51# include <sstream>
52
54
55# ifdef GUM_NO_INLINE
57# endif /* GUM_NO_INLINE */
58
59namespace gum {
60
61 // Default constructor.
63
64 // Copy constructor.
66 GUM_CONS_CPY(VariableNodeMap);
67
68 _copy_(source);
69 }
70
71 // Move constructor.
73 _nodes2vars_(std::move(source._nodes2vars_)), _names2nodes_(std::move(source._names2nodes_)) {
74 GUM_CONS_MOV(VariableNodeMap);
75 }
76
77 // Destructor
78 VariableNodeMap::~VariableNodeMap() {
79 GUM_DESTRUCTOR(VariableNodeMap);
80
81 clear();
82 }
83
84 // Copy operator.
85 VariableNodeMap& VariableNodeMap::operator=(const VariableNodeMap& source) {
86 clear();
87 _copy_(source);
88 GUM_OP_CPY(VariableNodeMap);
89
90 return *this;
91 }
92
93 // Move assignment operator.
94 VariableNodeMap& VariableNodeMap::operator=(VariableNodeMap&& source) {
95 if (this != &source) {
96 clear();
97 _nodes2vars_ = std::move(source._nodes2vars_);
98 _names2nodes_ = std::move(source._names2nodes_);
99 GUM_OP_MOV(VariableNodeMap);
100 }
101 return *this;
102 }
103
104 void VariableNodeMap::clear() {
105 for (auto iter = _nodes2vars_.begin(); iter != _nodes2vars_.end(); ++iter)
106 delete iter.second();
107
108 _nodes2vars_.clear();
109 _names2nodes_.clear();
110 }
111
113 std::string VariableNodeMap::toString() const {
114 std::stringstream stream;
115
116 stream << "list of associations:\n";
117 stream << _nodes2vars_ << "\n\n";
118 stream << "list of variable names:\n";
119 stream << _names2nodes_ << '\n';
120
121 return stream.str();
122 }
123
125 void VariableNodeMap::_copy_(const VariableNodeMap& source) {
126 for (auto iter = source._nodes2vars_.begin(); iter != source._nodes2vars_.end(); ++iter)
127 _nodes2vars_.insert(iter.first(), iter.second()->clone());
128
129 // copy factory is used inside insert
130
131 _names2nodes_ = source._names2nodes_;
132 }
133
135 std::ostream& operator<<(std::ostream& stream, const VariableNodeMap& v) {
136 stream << v.toString();
137 return stream;
138 }
139
140} /* namespace gum */
141
142#endif /* DOXYGEN_SHOULD_SKIP_THIS */
Container used to map discrete variables with nodes.
void _copy_(const VariableNodeMap &source)
effectively do the copy (for copy constructor or operator=)
VariableNodeMap()
Default constructor.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
std::ostream & operator<<(std::ostream &out, const TiXmlNode &base)
Definition tinyxml.cpp:1516
Header of class VariableNodeMap.
Inlined implementation of VariableNodeMap.