HashSet.C
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 \*---------------------------------------------------------------------------*/
25 
26 #ifndef HashSet_C
27 #define HashSet_C
28 
29 #include "HashSet.H"
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
33 template<class Key, class Hash>
35 :
36  HashTable<nil, Key, Hash>(2*lst.size())
37 {
38  forAll(lst, elemI)
39  {
40  this->insert(lst[elemI]);
41  }
42 }
43 
44 
45 template<class Key, class Hash>
46 template<unsigned Size>
48 :
49  HashTable<nil, Key, Hash>(2*lst.size())
50 {
51  forAll(lst, elemI)
52  {
53  this->insert(lst[elemI]);
54  }
55 }
56 
57 
58 template<class Key, class Hash>
59 template<class AnyType, class AnyHash>
61 (
63 )
64 :
65  HashTable<nil, Key, Hash>(h.size())
66 {
67  for
68  (
70  iter = h.cbegin();
71  iter != h.cend();
72  ++iter
73  )
74  {
75  this->insert(iter.key());
76  }
77 }
78 
79 
80 template<class Key, class Hash>
82 (
83  std::initializer_list<Key> lst
84 )
85 :
86  HashTable<nil, Key, Hash>(lst.size())
87 {
88  for (const Key& key : lst)
89  {
90  this->insert(key);
91  }
92 }
93 
94 
95 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
96 
97 template<class Key, class Hash>
99 {
101  forAll(lst, elemI)
102  {
103  if (this->insert(lst[elemI]))
104  {
105  ++count;
106  }
107  }
108 
109  return count;
110 }
111 
112 
113 template<class Key, class Hash>
115 {
117  for (const_iterator iter = set.cbegin(); iter != set.cend(); ++iter)
118  {
119  if (this->insert(iter.key()))
120  {
121  ++count;
122  }
123  }
124 
125  return count;
126 }
127 
128 
129 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
130 
131 template<class Key, class Hash>
132 inline bool Foam::HashSet<Key, Hash>::operator[](const Key& key) const
133 {
134  return this->found(key);
135 }
136 
137 
138 template<class Key, class Hash>
140 {
141  // Check for assignment to self
142  if (this == &rhs)
143  {
145  << "attempted assignment to self"
146  << abort(FatalError);
147  }
148 
150 }
151 
152 
153 template<class Key, class Hash>
155 {
156  // Check for assignment to self
157  if (this == &rhs)
158  {
160  << "attempted assignment to self"
162  }
163 
165 }
166 
167 
168 template<class Key, class Hash>
170 {
171  // Are all lhs elements in rhs?
172  for (const_iterator iter = this->cbegin(); iter != this->cend(); ++iter)
173  {
174  if (!rhs.found(iter.key()))
175  {
176  return false;
177  }
178  }
179 
180  // Are all rhs elements in lhs?
181  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
182  {
183  if (!this->found(iter.key()))
184  {
185  return false;
186  }
187  }
188 
189  return true;
190 }
191 
192 
193 template<class Key, class Hash>
195 {
196  return !(operator==(rhs));
197 }
198 
199 
200 template<class Key, class Hash>
202 {
203  // Add rhs elements into lhs
204  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
205  {
206  this->insert(iter.key());
207  }
208 }
209 
210 
211 template<class Key, class Hash>
213 {
214  // Remove elements not also found in rhs
215  for (iterator iter = this->begin(); iter != this->end(); ++iter)
216  {
217  if (!rhs.found(iter.key()))
218  {
219  this->erase(iter);
220  }
221  }
222 }
223 
224 
225 template<class Key, class Hash>
227 {
228  // Add missed rhs elements, remove duplicate elements
229  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
230  {
231  if (this->found(iter.key()))
232  {
233  this->erase(iter.key());
234  }
235  else
236  {
237  this->insert(iter.key());
238  }
239  }
240 }
241 
242 
243 template<class Key, class Hash>
245 {
246  // Remove rhs elements from lhs
247  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
248  {
249  this->erase(iter.key());
250  }
251 }
252 
253 
254 /* * * * * * * * * * * * * * * * Global operators * * * * * * * * * * * * * */
255 
256 template<class Key, class Hash>
258 Foam::operator|
259 (
260  const HashSet<Key, Hash>& hash1,
261  const HashSet<Key, Hash>& hash2
262 )
263 {
264  HashSet<Key, Hash> out(hash1);
265  out |= hash2;
266  return out;
267 }
268 
269 
270 template<class Key, class Hash>
272 Foam::operator&
273 (
274  const HashSet<Key, Hash>& hash1,
275  const HashSet<Key, Hash>& hash2
276 )
277 {
278  HashSet<Key, Hash> out(hash1);
279  out &= hash2;
280  return out;
281 }
282 
283 
284 template<class Key, class Hash>
286 Foam::operator^
287 (
288  const HashSet<Key, Hash>& hash1,
289  const HashSet<Key, Hash>& hash2
290 )
291 {
292  HashSet<Key, Hash> out(hash1);
293  out ^= hash2;
294  return out;
295 }
296 
297 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
298 
299 #endif
300 
301 // ************************************************************************* //
bool found
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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
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
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
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
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
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
Definition: HashTableI.H:506
bool found(const Key &) const
Return true if hashedEntry is found in table.
Definition: HashTable.C:138
void operator=(const HashTable< T, Key, Hash > &)
Assignment operator.
Definition: HashTable.C:611
Hash function class for primitives. All non-primitives used to hash entries on hash tables likely nee...
Definition: Hash.H:53
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
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
srcOptions erase("case")
const dimensionedScalar h
Planck constant.
void insert(const scalar, DynamicList< floatScalar > &)
Append scalar to given DynamicList.
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
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
errorManip< error > abort(error &err)
Definition: errorManip.H:131
error FatalError
label count(const ListType &l, typename ListType::const_reference x)
Count the number of occurrences of a value in a list.
static iteratorEnd cend()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:106