HashSet.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::HashSet
26 
27 Description
28  A HashTable with keys but without contents.
29 
30 Typedef
31  Foam::wordHashSet
32 
33 Description
34  A HashSet with (the default) word keys.
35 
36 Typedef
37  Foam::labelHashSet
38 
39 Description
40  A HashSet with label keys.
41 
42 \*---------------------------------------------------------------------------*/
43 
44 #ifndef HashSet_H
45 #define HashSet_H
46 
47 #include "HashTable.H"
48 #include "nil.H"
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 
55 /*---------------------------------------------------------------------------*\
56  Class HashSet Declaration
57 \*---------------------------------------------------------------------------*/
58 
59 template<class Key=word, class Hash=string::hash>
60 class HashSet
61 :
62  public HashTable<nil, Key, Hash>
63 {
64 
65 public:
66 
69 
70 
71  // Constructors
72 
73  //- Construct given initial size
74  HashSet(const label size = 128)
75  :
76  HashTable<nil, Key, Hash>(size)
77  {}
78 
79  //- Construct from Istream
80  HashSet(Istream& is)
81  :
82  HashTable<nil, Key, Hash>(is)
83  {}
84 
85  //- Construct from UList of Key
86  HashSet(const UList<Key>&);
87 
88  //- Construct from FixedList of Key
89  template<unsigned Size>
91 
92  //- Copy constructor
93  HashSet(const HashSet<Key, Hash>& hs) = default;
94 
95  //- Move constructor
96  HashSet(HashSet<Key, Hash>&& hs) = default;
97 
98  //- Construct from the keys of another HashTable,
99  // the type of values held is arbitrary.
100  template<class AnyType, class AnyHash>
102 
103  //- Construct from an initialiser list
104  HashSet(std::initializer_list<Key>);
105 
106 
107  // Member Functions
108 
109  //- Insert a new entry
110  bool insert(const Key& key)
111  {
112  return HashTable<nil, Key, Hash>::insert(key, nil());
113  }
114 
115  //- Insert keys from a UList of Key
116  // Return the number of new elements inserted
117  label insert(const UList<Key>&);
118 
119  //- Insert keys from a HashSet of Key
120  // Return the number of new elements inserted
122 
123  //- Same as insert (cannot overwrite nil content)
124  bool set(const Key& key)
125  {
126  return insert(key);
127  }
128 
129  //- Same as insert (cannot overwrite nil content)
130  label set(const UList<Key>& lst)
131  {
132  return insert(lst);
133  }
134 
135  //- Unset the specified key - same as erase
136  bool unset(const Key& key)
137  {
139  }
140 
141 
142  // Member Operators
143 
144  //- Return true if the entry exists, same as found()
145  inline bool operator[](const Key&) const;
146 
147  //- Assignment operator
148  void operator=(const HashSet<Key, Hash>&);
149 
150  //- Move assignment operator
152 
153  //- Equality. Two hashtables are equal when their contents are equal.
154  // Independent of table size or order.
155  bool operator==(const HashSet<Key, Hash>&) const;
156 
157  //- The opposite of the equality operation.
158  bool operator!=(const HashSet<Key, Hash>&) const;
159 
160 
161  //- Combine entries from HashSets
162  void operator|=(const HashSet<Key, Hash>&);
163 
164  //- Only retain entries found in both HashSets
165  void operator&=(const HashSet<Key, Hash>&);
166 
167  //- Only retain unique entries (xor)
168  void operator^=(const HashSet<Key, Hash>&);
169 
170  //- Add entries listed in the given HashSet to this HashSet
171  inline void operator+=(const HashSet<Key, Hash>& rhs)
172  {
173  this->operator|=(rhs);
174  }
175 
176  //- Remove entries listed in the given HashSet from this HashSet
177  void operator-=(const HashSet<Key, Hash>&);
178 };
179 
180 
181 // Global Operators
182 
183 //- Combine entries from HashSets
184 template<class Key, class Hash>
185 HashSet<Key,Hash> operator|
186 (
187  const HashSet<Key,Hash>& hash1,
188  const HashSet<Key,Hash>& hash2
189 );
190 
191 
192 //- Create a HashSet that only contains entries found in both HashSets
193 template<class Key, class Hash>
194 HashSet<Key,Hash> operator&
195 (
196  const HashSet<Key,Hash>& hash1,
197  const HashSet<Key,Hash>& hash2
198 );
199 
200 
201 //- Create a HashSet that only contains unique entries (xor)
202 template<class Key, class Hash>
203 HashSet<Key,Hash> operator^
204 (
205  const HashSet<Key,Hash>& hash1,
206  const HashSet<Key,Hash>& hash2
207 );
208 
209 
210 //- A HashSet with word keys.
211 typedef HashSet<> wordHashSet;
212 
213 //- A HashSet with label keys.
215 
216 
217 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
218 
219 } // End namespace Foam
220 
221 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
222 
223 #ifdef NoRepository
224  #include "HashSet.C"
225 #endif
226 
227 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
228 
229 #endif
230 
231 // ************************************************************************* //
scalar hs(const scalar p, const scalar T) const
Definition: EtoHthermo.H:11
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:78
A HashTable with keys but without contents.
Definition: HashSet.H:62
void operator-=(const HashSet< Key, Hash > &)
Remove entries listed in the given HashSet from this HashSet.
Definition: HashSet.C:244
bool operator[](const Key &) const
Return true if the entry exists, same as found()
Definition: HashSet.C:132
void operator=(const HashSet< Key, Hash > &)
Assignment operator.
Definition: HashSet.C:139
HashTable< nil, Key, Hash >::iterator iterator
Definition: HashSet.H:66
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:109
bool operator!=(const HashSet< Key, Hash > &) const
The opposite of the equality operation.
Definition: HashSet.C:194
void operator^=(const HashSet< Key, Hash > &)
Only retain unique entries (xor)
Definition: HashSet.C:226
HashTable< nil, Key, Hash >::const_iterator const_iterator
Definition: HashSet.H:67
bool unset(const Key &key)
Unset the specified key - same as erase.
Definition: HashSet.H:135
bool operator==(const HashSet< Key, Hash > &) const
Equality. Two hashtables are equal when their contents are equal.
Definition: HashSet.C:169
void operator|=(const HashSet< Key, Hash > &)
Combine entries from HashSets.
Definition: HashSet.C:201
void operator+=(const HashSet< Key, Hash > &rhs)
Add entries listed in the given HashSet to this HashSet.
Definition: HashSet.H:170
HashSet(const label size=128)
Construct given initial size.
Definition: HashSet.H:73
void operator&=(const HashSet< Key, Hash > &)
Only retain entries found in both HashSets.
Definition: HashSet.C:212
bool set(const Key &key)
Same as insert (cannot overwrite nil content)
Definition: HashSet.H:123
An STL-conforming const_iterator.
Definition: HashTable.H:498
An STL-conforming iterator.
Definition: HashTable.H:443
An STL-conforming hash table.
Definition: HashTable.H:127
bool erase(const iterator &)
Erase a hashedEntry specified by given iterator.
Definition: HashTable.C:445
label size() const
Return number of elements in table.
Definition: HashTableI.H:65
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
Hash function class for primitives. All non-primitives used to hash entries on hash tables likely nee...
Definition: Hash.H:53
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:74
A zero-sized class without any storage. Used, for example, in HashSet.
Definition: nil.H:59
Namespace for OpenFOAM.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
HashSet wordHashSet
A HashSet with word keys.
Definition: HashSet.H:210
HashSet< label, Hash< label > > labelHashSet
A HashSet with label keys.
Definition: HashSet.H:213