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-2019 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 :
66 {
67  for
68  (
70  cit = h.cbegin();
71  cit != h.cend();
72  ++cit
73  )
74  {
75  this->insert(cit.key());
76  }
77 }
78 
79 
80 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
81 
82 template<class Key, class Hash>
84 {
85  label count = 0;
86  forAll(lst, elemI)
87  {
88  if (this->insert(lst[elemI]))
89  {
90  ++count;
91  }
92  }
93 
94  return count;
95 }
96 
97 
98 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
99 
100 template<class Key, class Hash>
101 inline bool Foam::HashSet<Key, Hash>::operator[](const Key& key) const
102 {
103  return this->found(key);
104 }
105 
106 
107 template<class Key, class Hash>
109 {
110  // Check for assignment to self
111  if (this == &rhs)
112  {
114  << "attempted assignment to self"
115  << abort(FatalError);
116  }
117 
119 }
120 
121 
122 template<class Key, class Hash>
124 {
125  // Check for assignment to self
126  if (this == &rhs)
127  {
129  << "attempted assignment to self"
130  << abort(FatalError);
131  }
132 
134 }
135 
136 
137 template<class Key, class Hash>
139 {
140  // Are all lhs elements in rhs?
141  for (const_iterator iter = this->cbegin(); iter != this->cend(); ++iter)
142  {
143  if (!rhs.found(iter.key()))
144  {
145  return false;
146  }
147  }
148 
149  // Are all rhs elements in lhs?
150  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
151  {
152  if (!this->found(iter.key()))
153  {
154  return false;
155  }
156  }
157 
158  return true;
159 }
160 
161 
162 template<class Key, class Hash>
164 {
165  return !(operator==(rhs));
166 }
167 
168 
169 template<class Key, class Hash>
171 {
172  // Add rhs elements into lhs
173  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
174  {
175  this->insert(iter.key());
176  }
177 }
178 
179 
180 template<class Key, class Hash>
182 {
183  // Remove elements not also found in rhs
184  for (iterator iter = this->begin(); iter != this->end(); ++iter)
185  {
186  if (!rhs.found(iter.key()))
187  {
188  this->erase(iter);
189  }
190  }
191 }
192 
193 
194 template<class Key, class Hash>
196 {
197  // Add missed rhs elements, remove duplicate elements
198  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
199  {
200  if (this->found(iter.key()))
201  {
202  this->erase(iter.key());
203  }
204  else
205  {
206  this->insert(iter.key());
207  }
208  }
209 }
210 
211 
212 template<class Key, class Hash>
214 {
215  // Remove rhs elements from lhs
216  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
217  {
218  this->erase(iter.key());
219  }
220 }
221 
222 
223 /* * * * * * * * * * * * * * * * Global operators * * * * * * * * * * * * * */
224 
225 template<class Key, class Hash>
227 Foam::operator|
228 (
229  const HashSet<Key, Hash>& hash1,
230  const HashSet<Key, Hash>& hash2
231 )
232 {
233  HashSet<Key, Hash> out(hash1);
234  out |= hash2;
235  return out;
236 }
237 
238 
239 template<class Key, class Hash>
241 Foam::operator&
242 (
243  const HashSet<Key, Hash>& hash1,
244  const HashSet<Key, Hash>& hash2
245 )
246 {
247  HashSet<Key, Hash> out(hash1);
248  out &= hash2;
249  return out;
250 }
251 
252 
253 template<class Key, class Hash>
255 Foam::operator^
256 (
257  const HashSet<Key, Hash>& hash1,
258  const HashSet<Key, Hash>& hash2
259 )
260 {
261  HashSet<Key, Hash> out(hash1);
262  out ^= hash2;
263  return out;
264 }
265 
266 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
267 
268 #endif
269 
270 // ************************************************************************* //
A HashTable with keys but without contents.
Definition: HashSet.H:59
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
HashTable< nil, label, Hash< label > >::const_iterator const_iterator
Definition: HashSet.H:67
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
HashTable< nil, label, Hash< label > >::iterator iterator
Definition: HashSet.H:66
An STL-conforming const_iterator.
Definition: HashTable.H:481
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:54
srcOptions erase("case")
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:108
label size() const
Return number of elements in table.
Definition: HashTableI.H:65
void operator-=(const HashSet< Key, Hash > &)
Remove entries listed in the given HashSet from this HashSet.
Definition: HashSet.C:213
void operator &=(const HashSet< Key, Hash > &)
Only retain entries found in both HashSets.
HashSet(const label size=128)
Construct given initial size.
Definition: HashSet.H:73
bool found(const Key &) const
Return true if hashedEntry is found in table.
Definition: HashTable.C:113
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
timeIndices insert(timeIndex, timeDirs[timeI].value())
bool operator!=(const HashSet< Key, Hash > &) const
The opposite of the equality operation.
Definition: HashSet.C:163
An STL-conforming hash table.
Definition: HashTable.H:61
errorManip< error > abort(error &err)
Definition: errorManip.H:131
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:60
void operator|=(const HashSet< Key, Hash > &)
Combine entries from HashSets.
Definition: HashSet.C:170
bool operator[](const Key &) const
Return true if the entry exists, same as found()
Definition: HashSet.C:101
void operator=(const HashSet< Key, Hash > &)
Assignment operator.
Definition: HashSet.C:108
bool operator==(const HashSet< Key, Hash > &) const
Equality. Two hashtables are equal when their contents are equal.
Definition: HashSet.C:138
Hash function class for primitives. All non-primitives used to hash entries on hash tables likely nee...
Definition: Hash.H:54
void operator^=(const HashSet< Key, Hash > &)
Only retain unique entries (xor)
Definition: HashSet.C:195
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
Definition: HashTableI.H:506
bool found
static iteratorEnd cend()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:106
A zero-sized class without any storage. Used, for example, in HashSet.
Definition: nil.H:58