aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
idCondSet.cpp
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
48
50
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
54# ifdef GUM_NO_INLINE
56# endif /* GUM_NO_INLINE */
57
58namespace gum {
59
60 namespace learning {
61
64 IdCondSet set;
65 const std::size_t size = _ids_.size();
66 for (std::size_t i = _nb_lhs_ids_; i < size; ++i)
67 set._ids_ << _ids_[i];
68 set._end_safe_._gotoEnd_();
69 return set;
70 }
71
73 void IdCondSet::erase(const NodeId id) {
74 // search for id in Sequence _ids_
75 const std::size_t size = _ids_.size();
76 std::size_t pos = std::size_t(0);
77 for (; pos < size; ++pos) {
78 if (_ids_[pos] == id) break;
79 }
80
81 // if we found the id, remove it
82 if (pos < size) {
83 _ids_.erase(SequenceIteratorSafe< NodeId >(_ids_, pos));
84 if (pos < _nb_lhs_ids_) --_nb_lhs_ids_;
85 _end_safe_._gotoEnd_();
86 }
87 }
88
90 std::string IdCondSet::toString() const {
91 std::string str = "{";
92 bool deja = false;
93
94 for (std::size_t i = std::size_t(0); i < _nb_lhs_ids_; ++i) {
95 if (deja) str += " , ";
96 else deja = true;
97 str += std::format("{}", _ids_[i]);
98 }
99
100 deja = false;
101 for (auto iter = _ids_.begin() + _nb_lhs_ids_; iter != _ids_.end(); ++iter) {
102 if (deja) str += " , ";
103 else {
104 deja = true;
105 str += " | ";
106 }
107 str += std::format("{}", *iter);
108 }
109
110 str += '}';
111
112 return str;
113 }
114
115 std::pair< NodeSet, NodeSet > IdCondSet::toNodeSets() const {
116 gum::NodeSet left;
117 gum::NodeSet right;
118
119 for (auto i = std::size_t(0); i < _ids_.size(); ++i)
120 if (i < _nb_lhs_ids_) left.insert(_ids_[i]);
121 else right.insert(_ids_[i]);
122 return {left, right};
123 }
124
126 bool IdCondSet::contains(const IdCondSet& set) const {
127 if (set._ids_.size() > _ids_.size()) return false;
128 for (const auto node: set._ids_) {
129 if (!_ids_.exists(node)) return false;
130 }
131 return true;
132 }
133
134 // the display operator
135 std::ostream& operator<<(std::ostream& stream, const IdCondSet& idset) {
136 return stream << idset.toString();
137 }
138
139 IdCondSet::IdCondSet(const std::vector< NodeId >& ids,
140 const bool rhs_ids,
141 const bool ordered_ids) : _end_safe_(*this) {
142 _ids_.resize(ids.size());
143
144 // if the rhs_ids should be considered as unordered, we sort them by
145 // increasing order so that we can compare easily two different rhs_ids
146 if (!ordered_ids) {
147 std::vector< NodeId > vect(ids);
148 std::sort(vect.begin(), vect.end());
149 for (const auto id: vect)
150 _ids_ << id;
151 } else {
152 for (const auto id: ids)
153 _ids_ << id;
154 }
155
156 if (!rhs_ids) _nb_lhs_ids_ = _ids_.size();
157
158 // update the end iterator
159 _end_safe_._gotoEnd_();
160
161 GUM_CONSTRUCTOR(IdCondSet);
162 }
163
164 IdCondSet::IdCondSet(NodeId var1,
165 NodeId var2,
166 const std::vector< NodeId >& rhs_ids,
167 const bool ordered_lhs_vars,
168 const bool ordered_rhs_ids) :
169 _nb_lhs_ids_(std::size_t(2)), _end_safe_(*this) {
170 _ids_.resize(rhs_ids.size() + std::size_t(2));
171
172 // if the variables on the left side are unordered, sort them by
173 // increasing order
174 if (!ordered_lhs_vars && (var1 > var2)) std::swap(var1, var2);
175 _ids_ << var1;
176 _ids_ << var2;
177
178 // if the rhs_ids should be considered as unordered, we sort them by
179 // increasing order so that we can compare easily two different rhs_ids
180 if (!ordered_rhs_ids) {
181 std::vector< NodeId > vect(rhs_ids);
182 std::sort(vect.begin(), vect.end());
183 for (const auto id: vect)
184 _ids_ << id;
185 } else {
186 for (const auto id: rhs_ids)
187 _ids_ << id;
188 }
189
190 // update the end iterator
191 _end_safe_._gotoEnd_();
192
193 GUM_CONSTRUCTOR(IdCondSet);
194 }
195
196 IdCondSet::IdCondSet(NodeId var1,
197 NodeId var2,
198 NodeId var3,
199 const std::vector< NodeId >& rhs_ids,
200 const bool ordered_lhs_vars,
201 const bool ordered_rhs_ids) :
202 _nb_lhs_ids_(std::size_t(3)), _end_safe_(*this) {
203 _ids_.resize(rhs_ids.size() + std::size_t(3));
204
205 // if the variables on the left side are unordered, sort them by
206 // increasing order
207 if (!ordered_lhs_vars) {
208 if (var1 > var2) std::swap(var1, var2);
209 if (var1 > var3) std::swap(var1, var3);
210 if (var2 > var3) std::swap(var2, var3);
211 }
212 _ids_ << var1;
213 _ids_ << var2;
214 _ids_ << var3;
215
216 // if the rhs_ids should be considered as unordered, we sort them by
217 // increasing order so that we can compare easily two different rhs_ids
218 if (!ordered_rhs_ids) {
219 std::vector< NodeId > vect(rhs_ids);
220 std::sort(vect.begin(), vect.end());
221 for (const auto id: vect)
222 _ids_ << id;
223 } else {
224 for (const auto id: rhs_ids)
225 _ids_ << id;
226 }
227
228 // update the end iterator
229 _end_safe_._gotoEnd_();
230
231 GUM_CONSTRUCTOR(IdCondSet);
232 }
233 } /* namespace learning */
234
235 // Returns the value of a key as a Size.
236 Size HashFunc< learning::IdCondSet >::castToSize(const learning::IdCondSet& key) {
237 Size h = Size(key.nbLHSIds());
238 const Sequence< NodeId >& vect = key.ids();
239 const std::size_t size = vect.size();
240
241 for (std::size_t i = std::size_t(0); i < size; ++i) {
242 h ^= Size(vect[i]) + Size(0x9e3779b9u) + (h << 6) + (h >> 2);
243 }
244
245 return h;
246 }
247
248
249} /* namespace gum */
250
251#endif /* DOXYGEN_SHOULD_SKIP_THIS */
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:510
A class for storing a pair of sets of NodeIds, the second one corresponding to a conditional set.
Definition idCondSet.h:214
bool contains(const IdCondSet &set) const
indicates whether the IdCondSet contains the IdCondSet passed in argument
IdCondSet conditionalIdCondSet() const
returns the idSet at the right hand side of the conditioning bar
std::size_t pos(const NodeId id) const
returns the position of a given node in the IdCondSet
std::string toString() const
returns the content of the set as a string
std::pair< NodeSet, NodeSet > toNodeSets() const
returns the pair of conditioned gum::NodeSet and conditioning gum::NodeSet
void erase(const NodeId id)
erase a node in the idset
std::size_t size() const
returns the number of variables (both left and right hand side)
IdCondSet()
default constructor
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
A class used by learning caches to represent uniquely sets of variables.
Template implementation of idSets.
include the inlined functions if necessary
Definition CSVParser.h:55
std::ostream & operator<<(std::ostream &stream, const IdCondSet &idset)
the display operator
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.