aGrUM  0.13.2
idSet.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
25 #ifndef GUM_LEARNING_ID_SET_H
26 #define GUM_LEARNING_ID_SET_H
27 
28 #include <cstring>
29 #include <initializer_list>
30 #include <iostream>
31 #include <sstream>
32 #include <string>
33 #include <type_traits>
34 #include <utility>
35 #include <vector>
36 
37 #include <agrum/agrum.h>
38 #include <agrum/core/hashFunc.h>
39 
40 namespace gum {
41 
42  namespace learning {
43 
56  template < typename Alloc = std::allocator< Idx > >
57  class IdSet {
58  public:
59  // ##########################################################################
61  // ##########################################################################
63 
65  IdSet();
66 
68  IdSet(const std::initializer_list< Idx > list);
69 
71  template < typename OtherAlloc >
72  IdSet(const std::vector< Idx, OtherAlloc >& ids, Size sz);
73 
75  IdSet(const IdSet< Alloc >& from);
76 
78  template < typename OtherAlloc >
79  IdSet(const IdSet< OtherAlloc >& from);
80 
82  IdSet(IdSet< Alloc >&& from);
83 
85  ~IdSet();
86 
88 
89  // ##########################################################################
91  // ##########################################################################
93 
96 
98  template < typename OtherAlloc >
100 
103 
105  Idx operator[](Idx index) const noexcept;
106 
109 
111  template < typename OtherAlloc >
112  bool operator==(const IdSet< OtherAlloc >& from) const noexcept;
113 
115  template < typename OtherAlloc >
116  bool operator!=(const IdSet< OtherAlloc >& from) const noexcept;
117 
119 
120  // ##########################################################################
122  // ##########################################################################
124 
126  const std::vector< Idx, Alloc >& ids() const noexcept;
127 
129  Size size() const noexcept;
130 
132  void setSize(Idx) noexcept;
133 
135  std::string toString() const noexcept;
136 
138  template < typename OtherAlloc >
139  bool isSubset(const IdSet< OtherAlloc >& otherset) const noexcept;
140 
142 
143  private:
145  std::vector< Idx, Alloc > __ids;
146 
149  };
150 
152  template < typename Alloc >
153  std::ostream& operator<<(std::ostream& stream, const IdSet< Alloc >& idset);
154 
155  } /* namespace learning */
156 
158  template < typename Alloc >
159  class HashFunc< learning::IdSet< Alloc > >
160  : public HashFuncBase< learning::IdSet< Alloc > > {
161  public:
163  Size operator()(const learning::IdSet< Alloc >& key) const;
164  };
165 
167  template < typename Alloc >
168  class HashFunc< std::pair< learning::IdSet< Alloc >, Idx > >
169  : public HashFuncBase< std::pair< learning::IdSet< Alloc >, Idx > > {
170  public:
172  Size operator()(const std::pair< learning::IdSet< Alloc >, Idx >& key) const;
173  };
174 
176  template < typename Alloc >
177  class HashFunc< std::tuple< learning::IdSet< Alloc >, Idx, Idx > >
178  : public HashFuncBase< std::tuple< learning::IdSet< Alloc >, Idx, Idx > > {
179  public:
181  Size operator()(
182  const std::tuple< learning::IdSet< Alloc >, Idx, Idx >& key) const;
183  };
184 
186  template < typename Alloc >
187  class HashFunc< std::tuple< learning::IdSet< Alloc >, Idx, Idx, Idx > >
188  : public HashFuncBase<
189  std::tuple< learning::IdSet< Alloc >, Idx, Idx, Idx > > {
190  public:
192  Size operator()(
193  const std::tuple< learning::IdSet< Alloc >, Idx, Idx, Idx >& key) const;
194  };
195 
196 } /* namespace gum */
197 
198 
199 extern template class gum::HashFunc<
200  std::tuple< gum::learning::IdSet< std::allocator< gum::Idx > > > >;
201 extern template class gum::HashFunc<
202  std::tuple< gum::learning::IdSet< std::allocator< gum::Idx > >, gum::Idx > >;
203 extern template class gum::HashFunc<
204  std::tuple< gum::learning::IdSet< std::allocator< gum::Idx > >,
205  gum::Idx,
206  gum::Idx > >;
207 extern template class gum::HashFunc<
208  std::tuple< gum::learning::IdSet< std::allocator< gum::Idx > >,
209  gum::Idx,
210  gum::Idx,
211  gum::Idx > >;
212 
213 
214 // always include the template implementation
216 
217 #endif /* GUM_LEARNING_ID_SET_H */
bool operator!=(const IdSet< OtherAlloc > &from) const noexcept
returns true if the sets differ
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
~IdSet()
destructor
STL namespace.
transforms an unordered set of ids into an (increasingly) ordered one
Definition: idSet.h:57
Classes providing basic hash functions for hash tables.
Class template representing hashing function of LpCol.
Definition: hashFunc.h:612
Size size() const noexcept
returns the domain size of the id set
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
IdSet< Alloc > & operator<<(Idx id)
inserts a new element into the set (assuming it is a Boolean)
Template implementation of idSets.
const std::vector< Idx, Alloc > & ids() const noexcept
returns the set of ids contained in the object
IdSet< Alloc > & operator=(const IdSet< Alloc > &from)
copy operator
void setSize(Idx) noexcept
sets the domain size of the set
Idx operator[](Idx index) const noexcept
returns the id stored at a given index
All hash functions should inherit from this class.
Definition: hashFunc.h:150
bool isSubset(const IdSet< OtherAlloc > &otherset) const noexcept
indicates wether the current object is a subset of &#39;otherset&#39;
IdSet()
default constructor
std::vector< Idx, Alloc > __ids
the ordered set of ids
Definition: idSet.h:145
bool operator==(const IdSet< OtherAlloc > &from) const noexcept
returns true if both sets are equal
Size __size
the domain size of the set
Definition: idSet.h:148
unsigned long Idx
Type for indexes.
Definition: types.h:43
std::string toString() const noexcept
returns the content of the set as a string