aGrUM 2.3.2
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-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
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::stringstream str;
92
93 str << '{';
94 bool deja = false;
95
96 for (std::size_t i = std::size_t(0); i < _nb_lhs_ids_; ++i) {
97 if (deja) str << " , ";
98 else deja = true;
99 str << _ids_[i];
100 }
101
102 deja = false;
103 for (auto iter = _ids_.begin() + _nb_lhs_ids_; iter != _ids_.end(); ++iter) {
104 if (deja) str << " , ";
105 else {
106 deja = true;
107 str << " | ";
108 }
109 str << *iter;
110 }
111
112 str << '}';
113
114 return str.str();
115 }
116
117 std::pair< NodeSet, NodeSet > IdCondSet::toNodeSets() const {
118 gum::NodeSet left;
119 gum::NodeSet right;
120
121 for (auto i = std::size_t(0); i < _ids_.size(); ++i)
122 if (i < _nb_lhs_ids_) left.insert(_ids_[i]);
123 else right.insert(_ids_[i]);
124 return {left, right};
125 }
126
128 bool IdCondSet::contains(const IdCondSet& set) const {
129 if (set._ids_.size() > _ids_.size()) return false;
130 for (const auto node: set._ids_) {
131 if (!_ids_.exists(node)) return false;
132 }
133 return true;
134 }
135
136 // the display operator
137 std::ostream& operator<<(std::ostream& stream, const IdCondSet& idset) {
138 return stream << idset.toString();
139 }
140
141 } /* namespace learning */
142
143 // Returns the value of a key as a Size.
145 Size h = Size(key.nbLHSIds());
146 const Sequence< NodeId >& vect = key.ids();
147 const std::size_t size = vect.size();
148
149 std::size_t i = std::size_t(0);
150 while (i < size) {
151 const Size id = Size(vect[i]);
152 ++i;
153 h += Size(i) * id;
154 }
155
156 return h;
157 }
158
159
160} /* namespace gum */
161
162#endif /* DOXYGEN_SHOULD_SKIP_THIS */
static Size castToSize(const learning::IdCondSet &key)
Returns the value of a key as a Size.
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:539
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
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 ...
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:54
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