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 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 // same as HashTable::erase()
170 template<class Key, class Hash>
172 {
173  // Remove rhs elements from lhs
174  for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
175  {
176  this->erase(iter.key());
177  }
178 }
179 
180 
181 /* * * * * * * * * * * * * * * * Global operators * * * * * * * * * * * * * */
182 
183 template<class Key, class Hash>
185 Foam::operator|
186 (
187  const HashSet<Key, Hash>& hash1,
188  const HashSet<Key, Hash>& hash2
189 )
190 {
191  HashSet<Key, Hash> out(hash1);
192  out |= hash2;
193  return out;
194 }
195 
196 
197 template<class Key, class Hash>
199 Foam::operator&
200 (
201  const HashSet<Key, Hash>& hash1,
202  const HashSet<Key, Hash>& hash2
203 )
204 {
205  HashSet<Key, Hash> out(hash1);
206  out &= hash2;
207  return out;
208 }
209 
210 
211 template<class Key, class Hash>
213 Foam::operator^
214 (
215  const HashSet<Key, Hash>& hash1,
216  const HashSet<Key, Hash>& hash2
217 )
218 {
219  HashSet<Key, Hash> out(hash1);
220  out ^= hash2;
221  return out;
222 }
223 
224 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
225 
226 #endif
227 
228 // ************************************************************************* //
HashSet(const label size=128)
Construct given initial size.
Definition: HashSet.H:73
bool erase(const iterator &)
Erase a hashedEntry specified by given iterator.
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
Definition: HashTableI.H:514
An STL-conforming const_iterator.
Definition: HashTable.H:470
HashTable< nil, Key, Hash >::const_iterator const_iterator
Definition: HashSet.H:67
An STL-conforming iterator.
Definition: HashTable.H:415
An STL-conforming hash table.
Definition: HashTable.H:61
bool operator!=(const HashSet< Key, Hash > &) const
The opposite of the equality operation.
Definition: HashSet.C:120
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
void operator&=(const HashSet< Key, Hash > &)
Only retain entries found in both HashSets.
Definition: HashSet.C:138
A zero-sized class without any storage. Used, for example, in HashSet.
Definition: nil.H:58
static iteratorEnd cend()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:100
Hash function class for primitives. All non-primitives used to hash entries on hash tables likely nee...
Definition: Hash.H:54
#define forAll(list, i)
Definition: UList.H:421
void operator^=(const HashSet< Key, Hash > &)
Only retain unique entries (xor)
Definition: HashSet.C:152
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:171
void operator|=(const HashSet< Key, Hash > &)
Combine entries from HashSets.
Definition: HashSet.C:127
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
A HashTable with keys but without contents.
Definition: HashSet.H:59
static iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:106
iterator begin()
Iterator set to the beginning of the HashTable.
bool operator==(const HashSet< Key, Hash > &) const
Equality. Two hashtables are equal when their contents are equal.
Definition: HashSet.C:95
bool found(const Key &) const
Return true if hashedEntry is found in table.
bool operator[](const Key &) const
Return true if the entry exists, same as found()
Definition: HashSet.C:88
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:116