aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
nodeGraphPart.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 /** @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:643
std::ostream & operator<<(std::ostream &aStream, const Instantiation &i)
Print information of the instantiation in the stream.