aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
nodeGraphPart.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 /** @file
23  * @brief Source implementation of nodes sets
24  *
25  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
26  *
27  */
28 #include <agrum/tools/graphs/parts/nodeGraphPart.h>
29 
30 #ifdef GUM_NO_INLINE
31 # include <agrum/tools/graphs/parts/nodeGraphPart_inl.h>
32 #endif // GUM_NOINLINE
33 
34 namespace gum {
35 
36  ///////////////////// NodeGraphPart
39  endIteratorSafe__(*this), boundVal__(0) {
40  holes__ = nullptr;
43  }
44 
48  holes__ = nullptr;
49 
50  if (s.holes__) holes__ = new NodeSet(*s.holes__);
51 
53 
55  }
56 
58  if (holes__) delete holes__;
59 
61  }
62 
64  clear(); // "virtual" flush of the nodes set
67 
68  if (s.holes__) holes__ = new NodeSet(*s.holes__);
69 
71 
73  }
74 
75  // id is assumed to belong to NodeGraphPart
77  // we assume that the node exists
78  if (node + 1 == boundVal__) {
79  // we remove the max : no new hole and maybe a bunch of holes to remove
80  --boundVal__;
81 
82  if (holes__) {
83  while (holes__->contains(boundVal__ - 1)) {
84  // a bunch of holes to remove. We do not use inHoles for optimisation
85  // :
86  // not to repeat the test if (holes__) each time
88  }
89 
90  if (holes__->empty()) {
91  delete holes__;
92  holes__ = nullptr;
93  }
94  }
95 
97  } else {
99 
100  holes__->insert(node);
101  }
102  }
103 
105  std::stringstream s;
106  bool first = true;
107  s << "{";
108 
109  for (NodeId id = 0; id < boundVal__; ++id) {
110  if (inHoles__(id)) continue;
111 
112  if (first) {
113  first = false;
114  } else {
115  s << ",";
116  }
117 
118  s << id;
119  }
120 
121  s << "}";
122 
123  return s.str();
124  }
125 
127  stream << set.toString();
128  return stream;
129  }
130 
132  if (id >= boundVal__) {
133  if (id > boundVal__) { // we have to add holes
135 
136  for (NodeId i = boundVal__; i < id; ++i)
137  holes__->insert(i);
138  }
139 
140  boundVal__ = id + 1;
141 
143  } else {
144  if (inHoles__(id)) { // we fill a hole
145  eraseHole__(id);
146  } else {
147  GUM_ERROR(DuplicateElement, "Id " << id << " is already used");
148  }
149  }
150 
152  }
153 
156  boundVal__ = 0;
157 
158  if (onNodeDeleted.hasListener()) {
159  for (NodeId n = 0; n < bound; ++n) {
161  }
162  }
163 
165 
166  delete (holes__);
167  holes__ = nullptr;
168  }
169 
171  if (id == pos_) { // we just deleted the pos_ in NodeGraphPart
172  valid_ = false;
173  }
174 
175  if (pos_ >= nodes_->bound()) { // moreover, it was the last position
176  pos_ = nodes_->bound();
177  valid_ = false;
178  }
179  }
180 
181 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
std::ostream & operator<<(std::ostream &aStream, const Instantiation &i)
Print information of the instantiation in the stream.