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