aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
idCondSet_inl.h
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#pragma once
41
42
48
49#ifndef DOXYGEN_SHOULD_SKIP_THIS
50
52
53namespace gum {
54
55 namespace learning {
56
57
59 INLINE IdCondSetIterator::IdCondSetIterator() { GUM_CONSTRUCTOR(IdCondSetIterator); }
60
62 INLINE IdCondSetIterator::IdCondSetIterator(const IdCondSet& idset) : _seq_(&(idset.ids())) {
63 GUM_CONSTRUCTOR(IdCondSetIterator);
64 }
65
67 INLINE IdCondSetIterator::IdCondSetIterator(const IdCondSetIterator& from) :
68 _seq_(from._seq_), _index_(from._index_) {
69 GUM_CONS_CPY(IdCondSetIterator);
70 }
71
73 INLINE IdCondSetIterator::IdCondSetIterator(IdCondSetIterator&& from) :
74 _seq_(from._seq_), _index_(from._index_) {
75 GUM_CONS_MOV(IdCondSetIterator);
76 }
77
79 INLINE IdCondSetIterator::~IdCondSetIterator() { GUM_DESTRUCTOR(IdCondSetIterator); }
80
82 INLINE void IdCondSetIterator::_gotoEnd_() {
83 if (_seq_ != nullptr) _index_ = _seq_->size();
84 else _index_ = std::size_t(0);
85 }
86
88 INLINE IdCondSetIterator& IdCondSetIterator::operator=(const IdCondSetIterator& from) {
89 _seq_ = from._seq_;
90 _index_ = from._index_;
91 return *this;
92 }
93
95 INLINE IdCondSetIterator& IdCondSetIterator::operator=(IdCondSetIterator&& from) {
96 _seq_ = from._seq_;
97 _index_ = from._index_;
98 return *this;
99 }
100
102 INLINE NodeId IdCondSetIterator::operator*() const { return _seq_->operator[](_index_); }
103
105 INLINE bool IdCondSetIterator::operator!=(const IdCondSetIterator& from) const {
106 return (_index_ != from._index_) || (_seq_ != from._seq_);
107 }
108
110 INLINE bool IdCondSetIterator::operator==(const IdCondSetIterator& from) const {
111 return !operator!=(from);
112 }
113
115 INLINE IdCondSetIterator& IdCondSetIterator::operator++() {
116 ++_index_;
117 return *this;
118 }
119
121 INLINE IdCondSetIterator& IdCondSetIterator::operator+=(const std::size_t i) {
122 _index_ += i;
123 return *this;
124 }
125
127 INLINE IdCondSetIterator IdCondSetIterator::operator+(const std::size_t i) {
128 IdCondSetIterator res(*this);
129 res += i;
130 return res;
131 }
132
134 INLINE std::size_t IdCondSetIterator::pos() const {
135 if (_seq_ == nullptr)
137 "The IdCondSet is empty, so its iterators have no position")
138 if (_index_ >= _seq_->size())
140 "the IdCondSet iterator has no position because it reached "
141 "the set's end.");
142 return _index_;
143 }
144
147
148
150 INLINE IdCondSet::IdCondSet() : _end_safe_(*this) { GUM_CONSTRUCTOR(IdCondSet); }
151
153 INLINE IdCondSet::IdCondSet(const std::vector< NodeId >& ids,
154 const bool rhs_ids,
155 const bool ordered_ids) : _end_safe_(*this) {
156 _ids_.resize(ids.size());
157
158 // if the rhs_ids should be considered as unordered, we sort them by
159 // increasing order so that we can compare easily two different rhs_ids
160 if (!ordered_ids) {
161 std::vector< NodeId > vect(ids);
162 std::sort(vect.begin(), vect.end());
163 for (const auto id: vect)
164 _ids_ << id;
165 } else {
166 for (const auto id: ids)
167 _ids_ << id;
168 }
169
170 if (!rhs_ids) _nb_lhs_ids_ = _ids_.size();
171
172 // update the end iterator
173 _end_safe_._gotoEnd_();
174
175 GUM_CONSTRUCTOR(IdCondSet);
176 }
177
179 INLINE IdCondSet::IdCondSet(NodeId var1,
180 const std::vector< NodeId >& rhs_ids,
181 const bool ordered_rhs_ids) :
182 _nb_lhs_ids_(std::size_t(1)), _end_safe_(*this) {
183 _ids_.resize(rhs_ids.size() + std::size_t(1));
184 _ids_ << var1;
185
186 // if the rhs_ids should be considered as unordered, we sort them by
187 // increasing order so that we can compare easily two different rhs_ids
188 if (!ordered_rhs_ids) {
189 std::vector< NodeId > vect(rhs_ids);
190 std::sort(vect.begin(), vect.end());
191 for (const auto id: vect)
192 _ids_ << id;
193 } else {
194 for (const auto id: rhs_ids)
195 _ids_ << id;
196 }
197
198 // update the end iterator
199 _end_safe_._gotoEnd_();
200
201 GUM_CONSTRUCTOR(IdCondSet);
202 }
203
205 INLINE IdCondSet::IdCondSet(NodeId var1,
206 NodeId var2,
207 const std::vector< NodeId >& rhs_ids,
208 const bool ordered_lhs_vars,
209 const bool ordered_rhs_ids) :
210 _nb_lhs_ids_(std::size_t(2)), _end_safe_(*this) {
211 _ids_.resize(rhs_ids.size() + std::size_t(2));
212
213 // if the variables on the left side are unordered, sort them by
214 // increasing order
215 if (!ordered_lhs_vars && (var1 > var2)) std::swap(var1, var2);
216 _ids_ << var1;
217 _ids_ << var2;
218
219 // if the rhs_ids should be considered as unordered, we sort them by
220 // increasing order so that we can compare easily two different rhs_ids
221 if (!ordered_rhs_ids) {
222 std::vector< NodeId > vect(rhs_ids);
223 std::sort(vect.begin(), vect.end());
224 for (const auto id: vect)
225 _ids_ << id;
226 } else {
227 for (const auto id: rhs_ids)
228 _ids_ << id;
229 }
230
231 // update the end iterator
232 _end_safe_._gotoEnd_();
233
234 GUM_CONSTRUCTOR(IdCondSet);
235 }
236
238 INLINE IdCondSet::IdCondSet(NodeId var1,
239 NodeId var2,
240 NodeId var3,
241 const std::vector< NodeId >& rhs_ids,
242 const bool ordered_lhs_vars,
243 const bool ordered_rhs_ids) :
244 _nb_lhs_ids_(std::size_t(3)), _end_safe_(*this) {
245 _ids_.resize(rhs_ids.size() + std::size_t(3));
246
247 // if the variables on the left side are unordered, sort them by
248 // increasing order
249 if (!ordered_lhs_vars) {
250 if (var1 > var2) std::swap(var1, var2);
251 if (var1 > var3) std::swap(var1, var3);
252 if (var2 > var3) std::swap(var2, var3);
253 }
254 _ids_ << var1;
255 _ids_ << var2;
256 _ids_ << var3;
257
258 // if the rhs_ids should be considered as unordered, we sort them by
259 // increasing order so that we can compare easily two different rhs_ids
260 if (!ordered_rhs_ids) {
261 std::vector< NodeId > vect(rhs_ids);
262 std::sort(vect.begin(), vect.end());
263 for (const auto id: vect)
264 _ids_ << id;
265 } else {
266 for (const auto id: rhs_ids)
267 _ids_ << id;
268 }
269
270 // update the end iterator
271 _end_safe_._gotoEnd_();
272
273 GUM_CONSTRUCTOR(IdCondSet);
274 }
275
277 INLINE IdCondSet::IdCondSet(const IdCondSet& from) :
278 _ids_(from._ids_), _nb_lhs_ids_(from._nb_lhs_ids_), _end_safe_(*this) {
279 _end_safe_._gotoEnd_();
280 GUM_CONS_CPY(IdCondSet);
281 }
282
284 INLINE IdCondSet::IdCondSet(IdCondSet&& from) :
285 _ids_(std::move(from._ids_)), _nb_lhs_ids_(from._nb_lhs_ids_), _end_safe_(*this) {
286 _end_safe_._gotoEnd_();
287 GUM_CONS_MOV(IdCondSet);
288 }
289
291 INLINE IdCondSet* IdCondSet::clone() const { return new IdCondSet(*this); }
292
294 INLINE IdCondSet::~IdCondSet() { GUM_DESTRUCTOR(IdCondSet); }
295
297 INLINE IdCondSet& IdCondSet::operator=(const IdCondSet& from) {
298 if (this != &from) {
299 _ids_ = from._ids_;
300 _nb_lhs_ids_ = from._nb_lhs_ids_;
301 _end_safe_._gotoEnd_();
302 }
303 return *this;
304 }
305
307 INLINE IdCondSet& IdCondSet::operator=(IdCondSet&& from) {
308 if (this != &from) {
309 _ids_ = std::move(from._ids_);
310 _nb_lhs_ids_ = from._nb_lhs_ids_;
311 _end_safe_._gotoEnd_();
312 }
313 return *this;
314 }
315
317 INLINE NodeId IdCondSet::operator[](const std::size_t index) const {
318 return _ids_.atPos(index);
319 }
320
322 INLINE bool IdCondSet::operator==(const IdCondSet& from) const {
323 if (_nb_lhs_ids_ != from._nb_lhs_ids_) return false;
324
325 const std::size_t size = _ids_.size();
326
327 if (size != from._ids_.size()) return false;
328
329 for (std::size_t i = std::size_t(0); i < size; ++i) {
330 if (_ids_[i] != from._ids_[i]) return false;
331 }
332
333 return true;
334 }
335
337 INLINE bool IdCondSet::operator!=(const IdCondSet& from) const { return !operator==(from); }
338
340 INLINE typename IdCondSet::iterator_safe IdCondSet::beginSafe() const {
341 return IdCondSetIterator(*this);
342 }
343
345 INLINE const typename IdCondSet::iterator_safe& IdCondSet::endSafe() const {
346 return _end_safe_;
347 }
348
350 INLINE typename IdCondSet::iterator IdCondSet::begin() const {
351 return IdCondSetIterator(*this);
352 }
353
355 INLINE const typename IdCondSet::iterator& IdCondSet::end() const { return _end_safe_; }
356
358 INLINE const Sequence< NodeId >& IdCondSet::ids() const { return _ids_; }
359
361 INLINE std::size_t IdCondSet::nbLHSIds() const { return _nb_lhs_ids_; }
362
364 INLINE std::size_t IdCondSet::nbRHSIds() const { return _ids_.size() - _nb_lhs_ids_; }
365
367 INLINE void IdCondSet::clear() {
368 _ids_.clear();
369 _nb_lhs_ids_ = std::size_t(0);
370 _end_safe_._gotoEnd_();
371 }
372
374 INLINE std::size_t IdCondSet::size() const { return _ids_.size(); }
375
377 INLINE std::size_t IdCondSet::pos(const NodeId id) const { return _ids_.pos(id); }
378
380 INLINE bool IdCondSet::exists(const NodeId id) const { return _ids_.exists(id); }
381
383 INLINE bool IdCondSet::hasConditioningSet() const { return _nb_lhs_ids_ != _ids_.size(); }
384
386 INLINE bool IdCondSet::empty() const { return _ids_.empty(); }
387
388 } /* namespace learning */
389
390 // the hash function for idSets
391 INLINE Size HashFunc< learning::IdCondSet >::operator()(const learning::IdCondSet& key) const {
392 return (castToSize(key) * HashFuncConst::gold) & this->hash_mask_;
393 }
394
395} /* namespace gum */
396
397#endif /* DOXYGEN_SHOULD_SKIP_THIS */
Exception : generic error on iterator.
IdCondSetIterator()
default constructor
A class for storing a pair of sets of NodeIds, the second one corresponding to a conditional set.
Definition idCondSet.h:214
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
A class used by learning caches to represent uniquely sets of variables.
include the inlined functions if necessary
Definition CSVParser.h:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.
bool operator==(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.h:243
bool operator!=(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.h:251