All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2020 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 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 {
100  label count = 0;
101  forAll(lst, elemI)
102  {
103  if (this->insert(lst[elemI]))
104  {
105  ++count;
106  }
107  }
108 
109  return count;
110 }
111 
112 
113 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
114 
115 template<class Key, class Hash>
116 inline bool Foam::HashSet<Key, Hash>::operator[](const Key& key) const
117 {
118  return this->found(key);
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  // Check for assignment to self
141  if (this == &rhs)
142  {
144  << "attempted assignment to self"
145  << abort(FatalError);
146  }
147 
149 }
150 
151 
152 template<class Key, class Hash>
154 {
155  // Are all lhs elements in rhs?
156  for (const_iterator iter = this->cbegin(); iter != this->cend(); ++iter)
157  {
158  if (!rhs.found(iter.key()))
159  {
160  return false;
161  }
162  }
163 
164  // Are all rhs elements in lhs?
165  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
166  {
167  if (!this->found(iter.key()))
168  {
169  return false;
170  }
171  }
172 
173  return true;
174 }
175 
176 
177 template<class Key, class Hash>
179 {
180  return !(operator==(rhs));
181 }
182 
183 
184 template<class Key, class Hash>
186 {
187  // Add rhs elements into lhs
188  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
189  {
190  this->insert(iter.key());
191  }
192 }
193 
194 
195 template<class Key, class Hash>
197 {
198  // Remove elements not also found in rhs
199  for (iterator iter = this->begin(); iter != this->end(); ++iter)
200  {
201  if (!rhs.found(iter.key()))
202  {
203  this->erase(iter);
204  }
205  }
206 }
207 
208 
209 template<class Key, class Hash>
211 {
212  // Add missed rhs elements, remove duplicate elements
213  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
214  {
215  if (this->found(iter.key()))
216  {
217  this->erase(iter.key());
218  }
219  else
220  {
221  this->insert(iter.key());
222  }
223  }
224 }
225 
226 
227 template<class Key, class Hash>
229 {
230  // Remove rhs elements from lhs
231  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
232  {
233  this->erase(iter.key());
234  }
235 }
236 
237 
238 /* * * * * * * * * * * * * * * * Global operators * * * * * * * * * * * * * */
239 
240 template<class Key, class Hash>
242 Foam::operator|
243 (
244  const HashSet<Key, Hash>& hash1,
245  const HashSet<Key, Hash>& hash2
246 )
247 {
248  HashSet<Key, Hash> out(hash1);
249  out |= hash2;
250  return out;
251 }
252 
253 
254 template<class Key, class Hash>
256 Foam::operator&
257 (
258  const HashSet<Key, Hash>& hash1,
259  const HashSet<Key, Hash>& hash2
260 )
261 {
262  HashSet<Key, Hash> out(hash1);
263  out &= hash2;
264  return out;
265 }
266 
267 
268 template<class Key, class Hash>
270 Foam::operator^
271 (
272  const HashSet<Key, Hash>& hash1,
273  const HashSet<Key, Hash>& hash2
274 )
275 {
276  HashSet<Key, Hash> out(hash1);
277  out ^= hash2;
278  return out;
279 }
280 
281 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
282 
283 #endif
284 
285 // ************************************************************************* //
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, Foam::triFace, Foam::Hash< Foam::triFace > >::const_iterator const_iterator
Definition: HashSet.H:67
HashTable< nil, Foam::triFace, Foam::Hash< Foam::triFace > >::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:306
label count(const ListType &l, typename ListType::const_reference x)
Count the number of occurrences of a value in a list.
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:111
label size() const
Return number of elements in table.
Definition: HashTableI.H:65
void insert(const scalar, DynamicList< floatScalar > &)
Append scalar to given DynamicList.
void operator-=(const HashSet< Key, Hash > &)
Remove entries listed in the given HashSet from this HashSet.
Definition: HashSet.C:228
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 > &)
bool operator!=(const HashSet< Key, Hash > &) const
The opposite of the equality operation.
Definition: HashSet.C:178
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:185
bool operator[](const Key &) const
Return true if the entry exists, same as found()
Definition: HashSet.C:116
void operator=(const HashSet< Key, Hash > &)
Assignment operator.
Definition: HashSet.C:123
bool operator==(const HashSet< Key, Hash > &) const
Equality. Two hashtables are equal when their contents are equal.
Definition: HashSet.C:153
Hash function class for primitives. All non-primitives used to hash entries on hash tables likely nee...
Definition: Hash.H:52
void operator^=(const HashSet< Key, Hash > &)
Only retain unique entries (xor)
Definition: HashSet.C:210
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