aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
nodeGraphPart.cpp
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 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-2025 *
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
49
50#ifdef GUM_NO_INLINE
52#endif // GU%_NO_INLINE
53
54namespace gum {
55
57 NodeGraphPart::NodeGraphPart(Size holes_size, bool holes_resize_policy) :
58 _holes_size_(holes_size), _holes_resize_policy_(holes_resize_policy),
59 _endIteratorSafe_(*this), _boundVal_(0) {
60 _holes_ = nullptr;
61 GUM_CONSTRUCTOR(NodeGraphPart);
63 }
64
76
78 if (_holes_) delete _holes_;
79
80 GUM_DESTRUCTOR(NodeGraphPart);
81 }
82
84 clear(); // "virtual" flush of the nodes set
87
88 if (s._holes_) _holes_ = new NodeSet(*s._holes_);
89
91
93 }
94
95 // id is assumed to belong to NodeGraphPart
97 // we assume that the node exists
98 if (node + 1 == _boundVal_) {
99 // we remove the max : no new hole and maybe a bunch of holes to remove
100 --_boundVal_;
101
102 if (_holes_) {
103 while (_holes_->contains(_boundVal_ - 1)) {
104 // a bunch of holes to remove. We do not use inHoles for optimisation
105 // :
106 // not to repeat the test if ( _holes_) each time
107 _holes_->erase(--_boundVal_);
108 }
109
110 if (_holes_->empty()) {
111 delete _holes_;
112 _holes_ = nullptr;
113 }
114 }
115
117 } else {
119
120 _holes_->insert(node);
121 }
122 }
123
124 std::string NodeGraphPart::toString() const {
125 std::stringstream s;
126 bool first = true;
127 s << "{";
128
129 for (NodeId id = 0; id < _boundVal_; ++id) {
130 if (_inHoles_(id)) continue;
131
132 if (first) {
133 first = false;
134 } else {
135 s << ",";
136 }
137
138 s << id;
139 }
140
141 s << "}";
142
143 return s.str();
144 }
145
146 std::ostream& operator<<(std::ostream& stream, const NodeGraphPart& set) {
147 stream << set.toString();
148 return stream;
149 }
150
152 if (id >= _boundVal_) {
153 if (id > _boundVal_) { // we have to add holes
155
156 for (NodeId i = _boundVal_; i < id; ++i)
157 _holes_->insert(i);
158 }
159
160 _boundVal_ = id + 1;
161
163 } else {
164 if (_inHoles_(id)) { // we fill a hole
165 _eraseHole_(id);
166 } else {
167 GUM_ERROR(DuplicateElement, "Id " << id << " is already used")
168 }
169 }
170
172 }
173
176 _boundVal_ = 0;
177
178 if (onNodeDeleted.hasListener()) {
179 for (NodeId n = 0; n < bound; ++n) {
180 if (!_inHoles_(n)) GUM_EMIT1(onNodeDeleted, n);
181 }
182 }
183
185
186 delete (_holes_);
187 _holes_ = nullptr;
188 }
189
191 if (id == pos_) { // we just deleted the pos_ in NodeGraphPart
192 valid_ = false;
193 }
194
195 if (pos_ >= nodes_->bound()) { // moreover, it was the last position
196 pos_ = nodes_->bound();
197 valid_ = false;
198 }
199 }
200
201} /* namespace gum */
Exception : a similar element already exists.
void whenNodeDeleted(const void *src, NodeId id)
called when a node is deleted in the iterated NodeGraphPart
const NodeGraphPart * nodes_
the nodegraphpart on which points the iterator
NodeId pos_
the nodeid on which the iterator points currently
Class for node sets in graph.
NodeGraphPartIteratorSafe _endIteratorSafe_
the end iterator (used to speed-up parsings of the NodeGraphPart)
Signaler1< NodeId > onNodeDeleted
void populateNodes(const NodeGraphPart &s)
populateNodes clears *this and fills it with the same nodes as "s"
Signaler1< NodeId > onNodeAdded
void _clearNodes_()
code for clearing nodes (called twice)
virtual void clear()
alias for clearNodes
NodeId bound() const
returns a number n such that all node ids are strictly lower than n
void _eraseHole_(NodeId id)
to delete hole.
void _updateEndIteratorSafe_()
updating endIterator (always at max+1)
Size _holes_size_
value for holes configuration
virtual std::string toString() const
a function to display the set of nodes
bool _holes_resize_policy_
value for holes configuration
virtual ~NodeGraphPart()
destructor
NodeSet * _holes_
the set of nodes not contained in the NodeGraphPart in the interval 1.
NodeId _boundVal_
the id below which NodeIds may belong to the NodeGraphPart
NodeGraphPart(Size holes_size=HashTableConst::default_size, bool holes_resize_policy=true)
default constructor
void _addHole_(NodeId id)
to add a hole.
bool _inHoles_(NodeId id) const
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
Definition AVLTree.h:913
Base node set class for graphs.
Inline implementation of the base node set class for graphs.
#define GUM_EMIT1(signal, arg1)
Definition signaler1.h:61