HashSet.H
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-2021 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 Class
25  Foam::HashSet
26 
27 Description
28  A HashTable with keys but without contents.
29 
30 Typedef
31  Foam::wordHashSet
32 
33 Description
34  A HashSet with (the default) word keys.
35 
36 Typedef
37  Foam::labelHashSet
38 
39 Description
40  A HashSet with label keys.
41 
42 \*---------------------------------------------------------------------------*/
43 
44 #ifndef HashSet_H
45 #define HashSet_H
46 
47 #include "HashTable.H"
48 #include "nil.H"
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 
55 /*---------------------------------------------------------------------------*\
56  Class HashSet Declaration
57 \*---------------------------------------------------------------------------*/
58 
59 template<class Key=word, class Hash=string::hash>
60 class HashSet
61 :
62  public HashTable<nil, Key, Hash>
63 {
64 
65 public:
66 
69 
70 
71  // Constructors
72 
73  //- Construct given initial size
74  HashSet(const label size = 128)
75  :
76  HashTable<nil, Key, Hash>(size)
77  {}
78 
79  //- Construct from Istream
80  HashSet(Istream& is)
81  :
82  HashTable<nil, Key, Hash>(is)
83  {}
84 
85  //- Construct from UList of Key
86  HashSet(const UList<Key>&);
87 
88  //- Construct from FixedList of Key
89  template<unsigned Size>
91 
92  //- Copy constructor
93  HashSet(const HashSet<Key, Hash>& hs) = default;
94 
95  //- Move constructor
96  HashSet(HashSet<Key, Hash>&& hs) = default;
97 
98  //- Construct from the keys of another HashTable,
99  // the type of values held is arbitrary.
100  template<class AnyType, class AnyHash>
102 
103  //- Construct from an initialiser list
104  HashSet(std::initializer_list<Key>);
105 
106 
107  // Member Functions
108 
109  // Edit
110 
111  //- Insert a new entry
112  bool insert(const Key& key)
113  {
114  return HashTable<nil, Key, Hash>::insert(key, nil());
115  }
116 
117  //- Insert keys from a UList of Key
118  // Return the number of new elements inserted
119  label insert(const UList<Key>&);
120 
121  //- Same as insert (cannot overwrite nil content)
122  bool set(const Key& key)
123  {
124  return insert(key);
125  }
126 
127  //- Same as insert (cannot overwrite nil content)
128  label set(const UList<Key>& lst)
129  {
130  return insert(lst);
131  }
132 
133  //- Unset the specified key - same as erase
134  bool unset(const Key& key)
135  {
137  }
138 
139 
140  // Member Operators
141 
142  //- Return true if the entry exists, same as found()
143  inline bool operator[](const Key&) const;
144 
145  //- Assignment operator
146  void operator=(const HashSet<Key, Hash>&);
147 
148  //- Move assignment operator
150 
151  //- Equality. Two hashtables are equal when their contents are equal.
152  // Independent of table size or order.
153  bool operator==(const HashSet<Key, Hash>&) const;
154 
155  //- The opposite of the equality operation.
156  bool operator!=(const HashSet<Key, Hash>&) const;
157 
158 
159  //- Combine entries from HashSets
160  void operator|=(const HashSet<Key, Hash>&);
161 
162  //- Only retain entries found in both HashSets
163  void operator&=(const HashSet<Key, Hash>&);
164 
165  //- Only retain unique entries (xor)
166  void operator^=(const HashSet<Key, Hash>&);
167 
168  //- Add entries listed in the given HashSet to this HashSet
169  inline void operator+=(const HashSet<Key, Hash>& rhs)
170  {
171  this->operator|=(rhs);
172  }
173 
174  //- Remove entries listed in the given HashSet from this HashSet
175  void operator-=(const HashSet<Key, Hash>&);
176 };
177 
178 
179 // Global Operators
180 
181 //- Combine entries from HashSets
182 template<class Key, class Hash>
183 HashSet<Key,Hash> operator|
184 (
185  const HashSet<Key,Hash>& hash1,
186  const HashSet<Key,Hash>& hash2
187 );
188 
189 
190 //- Create a HashSet that only contains entries found in both HashSets
191 template<class Key, class Hash>
192 HashSet<Key,Hash> operator&
193 (
194  const HashSet<Key,Hash>& hash1,
195  const HashSet<Key,Hash>& hash2
196 );
197 
198 
199 //- Create a HashSet that only contains unique entries (xor)
200 template<class Key, class Hash>
201 HashSet<Key,Hash> operator^
202 (
203  const HashSet<Key,Hash>& hash1,
204  const HashSet<Key,Hash>& hash2
205 );
206 
207 
208 //- A HashSet with word keys.
209 typedef HashSet<> wordHashSet;
210 
211 //- A HashSet with label keys.
213 
214 
215 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
216 
217 } // End namespace Foam
218 
219 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
220 
221 #ifdef NoRepository
222  #include "HashSet.C"
223 #endif
224 
225 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
226 
227 #endif
228 
229 // ************************************************************************* //
void operator+=(const HashSet< Key, Hash > &rhs)
Add entries listed in the given HashSet to this HashSet.
Definition: HashSet.H:168
A HashTable with keys but without contents.
Definition: HashSet.H:59
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
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:54
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
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
bool erase(const iterator &)
Erase a hashedEntry specified by given iterator.
Definition: HashTable.C:371
void operator-=(const HashSet< Key, Hash > &)
Remove entries listed in the given HashSet from this HashSet.
Definition: HashSet.C:228
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
HashSet< label, Hash< label > > labelHashSet
A HashSet with label keys.
Definition: HashSet.H:211
bool unset(const Key &key)
Unset the specified key - same as erase.
Definition: HashSet.H:133
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 operator!=(const HashSet< Key, Hash > &) const
The opposite of the equality operation.
Definition: HashSet.C:178
HashSet wordHashSet
A HashSet with word keys.
Definition: HashSet.H:208
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
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
Namespace for OpenFOAM.
A zero-sized class without any storage. Used, for example, in HashSet.
Definition: nil.H:58