aGrUM 3.1.1
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-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#pragma once
42
43
49
50#include <agrum/base/stattests/idCondSet.h> // to ease IDE parser
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
54
55namespace gum {
56
57 namespace learning {
58
59
61 INLINE IdCondSetIterator::IdCondSetIterator() { GUM_CONSTRUCTOR(IdCondSetIterator); }
62
64 INLINE IdCondSetIterator::IdCondSetIterator(const IdCondSet& idset) : _seq_(&(idset.ids())) {
65 GUM_CONSTRUCTOR(IdCondSetIterator);
66 }
67
69 INLINE IdCondSetIterator::IdCondSetIterator(const IdCondSetIterator& from) :
70 _seq_(from._seq_), _index_(from._index_) {
71 GUM_CONS_CPY(IdCondSetIterator);
72 }
73
75 INLINE IdCondSetIterator::IdCondSetIterator(IdCondSetIterator&& from) :
76 _seq_(from._seq_), _index_(from._index_) {
77 GUM_CONS_MOV(IdCondSetIterator);
78 }
79
81 INLINE IdCondSetIterator::~IdCondSetIterator() { GUM_DESTRUCTOR(IdCondSetIterator); }
82
84 INLINE void IdCondSetIterator::_gotoEnd_() {
85 if (_seq_ != nullptr) _index_ = _seq_->size();
86 else _index_ = std::size_t(0);
87 }
88
90 INLINE IdCondSetIterator& IdCondSetIterator::operator=(const IdCondSetIterator& from) = default;
91
93 INLINE IdCondSetIterator& IdCondSetIterator::operator=(IdCondSetIterator&& from) {
94 _seq_ = from._seq_;
95 _index_ = from._index_;
96 return *this;
97 }
98
100 INLINE NodeId IdCondSetIterator::operator*() const { return _seq_->operator[](_index_); }
101
103 INLINE bool IdCondSetIterator::operator!=(const IdCondSetIterator& from) const {
104 return (_index_ != from._index_) || (_seq_ != from._seq_);
105 }
106
108 INLINE bool IdCondSetIterator::operator==(const IdCondSetIterator& from) const {
109 return !operator!=(from);
110 }
111
113 INLINE IdCondSetIterator& IdCondSetIterator::operator++() {
114 ++_index_;
115 return *this;
116 }
117
119 INLINE IdCondSetIterator& IdCondSetIterator::operator+=(const std::size_t i) {
120 _index_ += i;
121 return *this;
122 }
123
125 INLINE IdCondSetIterator IdCondSetIterator::operator+(const std::size_t i) {
126 IdCondSetIterator res(*this);
127 res += i;
128 return res;
129 }
130
132 INLINE std::size_t IdCondSetIterator::pos() const {
133 if (_seq_ == nullptr)
135 "The IdCondSet is empty, so its iterators have no position")
136 if (_index_ >= _seq_->size())
138 "the IdCondSet iterator has no position because it reached "
139 "the set's end.");
140 return _index_;
141 }
142
145
146
148 INLINE IdCondSet::IdCondSet() : _end_safe_(*this) { GUM_CONSTRUCTOR(IdCondSet); }
149
151
153 INLINE IdCondSet::IdCondSet(NodeId var1,
154 const std::vector< NodeId >& rhs_ids,
155 const bool ordered_rhs_ids) :
156 _nb_lhs_ids_(std::size_t(1)), _end_safe_(*this) {
157 _ids_.resize(rhs_ids.size() + std::size_t(1));
158 _ids_ << var1;
159
160 // if the rhs_ids should be considered as unordered, we sort them by
161 // increasing order so that we can compare easily two different rhs_ids
162 if (!ordered_rhs_ids) {
163 std::vector< NodeId > vect(rhs_ids);
164 std::sort(vect.begin(), vect.end());
165 for (const auto id: vect)
166 _ids_ << id;
167 } else {
168 for (const auto id: rhs_ids)
169 _ids_ << id;
170 }
171
172 // update the end iterator
173 _end_safe_._gotoEnd_();
174
175 GUM_CONSTRUCTOR(IdCondSet);
176 }
177
179
181
183 INLINE IdCondSet::IdCondSet(const IdCondSet& from) :
184 _ids_(from._ids_), _nb_lhs_ids_(from._nb_lhs_ids_), _end_safe_(*this) {
185 _end_safe_._gotoEnd_();
186 GUM_CONS_CPY(IdCondSet);
187 }
188
190 INLINE IdCondSet::IdCondSet(IdCondSet&& from) :
191 _ids_(std::move(from._ids_)), _nb_lhs_ids_(from._nb_lhs_ids_), _end_safe_(*this) {
192 _end_safe_._gotoEnd_();
193 GUM_CONS_MOV(IdCondSet);
194 }
195
197 INLINE IdCondSet* IdCondSet::clone() const { return new IdCondSet(*this); }
198
200 INLINE IdCondSet::~IdCondSet() { GUM_DESTRUCTOR(IdCondSet); }
201
203 INLINE IdCondSet& IdCondSet::operator=(const IdCondSet& from) {
204 if (this != &from) {
205 _ids_ = from._ids_;
206 _nb_lhs_ids_ = from._nb_lhs_ids_;
207 _end_safe_._gotoEnd_();
208 }
209 return *this;
210 }
211
213 INLINE IdCondSet& IdCondSet::operator=(IdCondSet&& from) {
214 if (this != &from) {
215 _ids_ = std::move(from._ids_);
216 _nb_lhs_ids_ = from._nb_lhs_ids_;
217 _end_safe_._gotoEnd_();
218 }
219 return *this;
220 }
221
223 INLINE NodeId IdCondSet::operator[](const std::size_t index) const {
224 return _ids_.atPos(index);
225 }
226
228 INLINE bool IdCondSet::operator==(const IdCondSet& from) const {
229 if (_nb_lhs_ids_ != from._nb_lhs_ids_) return false;
230
231 const std::size_t size = _ids_.size();
232
233 if (size != from._ids_.size()) return false;
234
235 for (std::size_t i = std::size_t(0); i < size; ++i) {
236 if (_ids_[i] != from._ids_[i]) return false;
237 }
238
239 return true;
240 }
241
243 INLINE typename IdCondSet::iterator_safe IdCondSet::beginSafe() const {
244 return IdCondSetIterator(*this);
245 }
246
248 INLINE const typename IdCondSet::iterator_safe& IdCondSet::endSafe() const {
249 return _end_safe_;
250 }
251
253 INLINE typename IdCondSet::iterator IdCondSet::begin() const {
254 return IdCondSetIterator(*this);
255 }
256
258 INLINE const typename IdCondSet::iterator& IdCondSet::end() const { return _end_safe_; }
259
261 INLINE const Sequence< NodeId >& IdCondSet::ids() const { return _ids_; }
262
264 INLINE std::size_t IdCondSet::nbLHSIds() const { return _nb_lhs_ids_; }
265
267 INLINE std::size_t IdCondSet::nbRHSIds() const { return _ids_.size() - _nb_lhs_ids_; }
268
270 INLINE void IdCondSet::clear() {
271 _ids_.clear();
272 _nb_lhs_ids_ = std::size_t(0);
273 _end_safe_._gotoEnd_();
274 }
275
277 INLINE std::size_t IdCondSet::size() const { return _ids_.size(); }
278
280 INLINE std::size_t IdCondSet::pos(const NodeId id) const { return _ids_.pos(id); }
281
283 INLINE bool IdCondSet::exists(const NodeId id) const { return _ids_.exists(id); }
284
286 INLINE bool IdCondSet::hasConditioningSet() const { return _nb_lhs_ids_ != _ids_.size(); }
287
289 INLINE bool IdCondSet::empty() const { return _ids_.empty(); }
290
291 } /* namespace learning */
292
293 // the hash function for idSets
294 INLINE Size HashFunc< learning::IdCondSet >::operator()(const learning::IdCondSet& key) const {
295 return (castToSize(key) * HashFuncConst::gold) & this->hash_mask_;
296 }
297
298} /* namespace gum */
299
300#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:76
A class used by learning caches to represent uniquely sets of variables.
include the inlined functions if necessary
Definition CSVParser.h:55
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:251