HashSet.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2016 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<class AnyType, class AnyHash>
48 (
50 )
51 :
53 {
54  for
55  (
57  cit = h.cbegin();
58  cit != h.cend();
59  ++cit
60  )
61  {
62  this->insert(cit.key());
63  }
64 }
65 
66 
67 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
68 
69 template<class Key, class Hash>
71 {
72  label count = 0;
73  forAll(lst, elemI)
74  {
75  if (this->insert(lst[elemI]))
76  {
77  ++count;
78  }
79  }
80 
81  return count;
82 }
83 
84 
85 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
86 
87 template<class Key, class Hash>
88 inline bool Foam::HashSet<Key, Hash>::operator[](const Key& key) const
89 {
90  return this->found(key);
91 }
92 
93 
94 template<class Key, class Hash>
96 {
97  // Are all lhs elements in rhs?
98  for (const_iterator iter = this->cbegin(); iter != this->cend(); ++iter)
99  {
100  if (!rhs.found(iter.key()))
101  {
102  return false;
103  }
104  }
105 
106  // Are all rhs elements in lhs?
107  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
108  {
109  if (!this->found(iter.key()))
110  {
111  return false;
112  }
113  }
114 
115  return true;
116 }
117 
118 
119 template<class Key, class Hash>
121 {
122  return !(operator==(rhs));
123 }
124 
125 
126 template<class Key, class Hash>
128 {
129  // Add rhs elements into lhs
130  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
131  {
132  this->insert(iter.key());
133  }
134 }
135 
136 
137 template<class Key, class Hash>
139 {
140  // Remove elements not also found in rhs
141  for (iterator iter = this->begin(); iter != this->end(); ++iter)
142  {
143  if (!rhs.found(iter.key()))
144  {
145  this->erase(iter);
146  }
147  }
148 }
149 
150 
151 template<class Key, class Hash>
153 {
154  // Add missed rhs elements, remove duplicate elements
155  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
156  {
157  if (this->found(iter.key()))
158  {
159  this->erase(iter.key());
160  }
161  else
162  {
163  this->insert(iter.key());
164  }
165  }
166 }
167 
168 
169 template<class Key, class Hash>
171 {
172  // Remove rhs elements from lhs
173  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
174  {
175  this->erase(iter.key());
176  }
177 }
178 
179 
180 /* * * * * * * * * * * * * * * * Global operators * * * * * * * * * * * * * */
181 
182 template<class Key, class Hash>
184 Foam::operator|
185 (
186  const HashSet<Key, Hash>& hash1,
187  const HashSet<Key, Hash>& hash2
188 )
189 {
190  HashSet<Key, Hash> out(hash1);
191  out |= hash2;
192  return out;
193 }
194 
195 
196 template<class Key, class Hash>
198 Foam::operator&
199 (
200  const HashSet<Key, Hash>& hash1,
201  const HashSet<Key, Hash>& hash2
202 )
203 {
204  HashSet<Key, Hash> out(hash1);
205  out &= hash2;
206  return out;
207 }
208 
209 
210 template<class Key, class Hash>
212 Foam::operator^
213 (
214  const HashSet<Key, Hash>& hash1,
215  const HashSet<Key, Hash>& hash2
216 )
217 {
218  HashSet<Key, Hash> out(hash1);
219  out ^= hash2;
220  return out;
221 }
222 
223 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
224 
225 #endif
226 
227 // ************************************************************************* //
void operator&=(const HashSet< Key, Hash > &)
Only retain entries found in both HashSets.
Definition: HashSet.C:138
A HashTable with keys but without contents.
Definition: HashSet.H:59
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
HashTable< nil, Key, Hash >::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, Key, Hash >::iterator iterator
Definition: HashSet.H:66
An STL-conforming const_iterator.
Definition: HashTable.H:470
static iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:106
label size() const
Return number of elements in table.
Definition: HashTableI.H:65
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:116
bool erase(const iterator &)
Erase a hashedEntry specified by given iterator.
void operator-=(const HashSet< Key, Hash > &)
Remove entries listed in the given HashSet from this HashSet.
Definition: HashSet.C:170
bool operator==(const HashSet< Key, Hash > &) const
Equality. Two hashtables are equal when their contents are equal.
Definition: HashSet.C:95
HashSet(const label size=128)
Construct given initial size.
Definition: HashSet.H:73
bool operator!=(const HashSet< Key, Hash > &) const
The opposite of the equality operation.
Definition: HashSet.C:120
iterator begin()
Iterator set to the beginning of the HashTable.
An STL-conforming hash table.
Definition: HashTable.H:61
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
bool found(const Key &) const
Return true if hashedEntry is found in table.
void operator|=(const HashSet< Key, Hash > &)
Combine entries from HashSets.
Definition: HashSet.C:127
bool operator[](const Key &) const
Return true if the entry exists, same as found()
Definition: HashSet.C:88
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:152
static iteratorEnd cend()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:100
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
Definition: HashTableI.H:514
A zero-sized class without any storage. Used, for example, in HashSet.
Definition: nil.H:58