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-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 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 
104  // Member Functions
105 
106  // Edit
107 
108  //- Insert a new entry
109  bool insert(const Key& key)
110  {
111  return HashTable<nil, Key, Hash>::insert(key, nil());
112  }
113 
114  //- Insert keys from a UList of Key
115  // Return the number of new elements inserted
116  label insert(const UList<Key>&);
117 
118  //- Same as insert (cannot overwrite nil content)
119  bool set(const Key& key)
120  {
121  return insert(key);
122  }
123 
124  //- Same as insert (cannot overwrite nil content)
125  label set(const UList<Key>& lst)
126  {
127  return insert(lst);
128  }
129 
130  //- Unset the specified key - same as erase
131  bool unset(const Key& key)
132  {
134  }
135 
136 
137  // Member Operators
138 
139  //- Return true if the entry exists, same as found()
140  inline bool operator[](const Key&) const;
141 
142  //- Assignment operator
143  void operator=(const HashSet<Key, Hash>&);
144 
145  //- Move assignment operator
147 
148  //- Equality. Two hashtables are equal when their contents are equal.
149  // Independent of table size or order.
150  bool operator==(const HashSet<Key, Hash>&) const;
151 
152  //- The opposite of the equality operation.
153  bool operator!=(const HashSet<Key, Hash>&) const;
154 
155 
156  //- Combine entries from HashSets
157  void operator|=(const HashSet<Key, Hash>&);
158 
159  //- Only retain entries found in both HashSets
160  void operator&=(const HashSet<Key, Hash>&);
161 
162  //- Only retain unique entries (xor)
163  void operator^=(const HashSet<Key, Hash>&);
164 
165  //- Add entries listed in the given HashSet to this HashSet
166  inline void operator+=(const HashSet<Key, Hash>& rhs)
167  {
168  this->operator|=(rhs);
169  }
170 
171  //- Remove entries listed in the given HashSet from this HashSet
172  void operator-=(const HashSet<Key, Hash>&);
173 };
174 
175 
176 // Global Operators
177 
178 //- Combine entries from HashSets
179 template<class Key, class Hash>
180 HashSet<Key,Hash> operator|
181 (
182  const HashSet<Key,Hash>& hash1,
183  const HashSet<Key,Hash>& hash2
184 );
185 
186 
187 //- Create a HashSet that only contains entries found in both HashSets
188 template<class Key, class Hash>
189 HashSet<Key,Hash> operator&
190 (
191  const HashSet<Key,Hash>& hash1,
192  const HashSet<Key,Hash>& hash2
193 );
194 
195 
196 //- Create a HashSet that only contains unique entries (xor)
197 template<class Key, class Hash>
198 HashSet<Key,Hash> operator^
199 (
200  const HashSet<Key,Hash>& hash1,
201  const HashSet<Key,Hash>& hash2
202 );
203 
204 
205 //- A HashSet with word keys.
206 typedef HashSet<> wordHashSet;
207 
208 //- A HashSet with label keys.
210 
211 
212 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
213 
214 } // End namespace Foam
215 
216 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
217 
218 #ifdef NoRepository
219  #include "HashSet.C"
220 #endif
221 
222 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
223 
224 #endif
225 
226 // ************************************************************************* //
void operator+=(const HashSet< Key, Hash > &rhs)
Add entries listed in the given HashSet to this HashSet.
Definition: HashSet.H:165
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:108
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:213
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:208
bool unset(const Key &key)
Unset the specified key - same as erase.
Definition: HashSet.H:130
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:163
HashSet wordHashSet
A HashSet with word keys.
Definition: HashSet.H:205
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: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
Namespace for OpenFOAM.
A zero-sized class without any storage. Used, for example, in HashSet.
Definition: nil.H:58