FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/HashTable.H>
48 #include <OpenFOAM/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>& lst)
87  :
88  HashTable<nil, Key, Hash>(2*lst.size())
89  {
90  forAll(lst, i)
91  {
92  insert(lst[i]);
93  }
94  }
95 
96  //- Construct as copy
98  :
99  HashTable<nil, Key, Hash>(hs)
100  {}
102  //- Construct by transferring the parameter contents
104  :
105  HashTable<nil, Key, Hash>(hs)
106  {}
108  //- Construct by transferring the parameter contents
110  :
111  HashTable<nil, Key, Hash>(hs)
112  {}
113 
114  //- Construct from the keys of another HashTable,
115  // the type of values held is arbitrary.
116  template<class AnyType, class AnyHash>
118 
119 
120  // Member Functions
121 
122  // Edit
124  //- Insert a new entry
125  bool insert(const Key& key)
126  {
127  return HashTable<nil, Key, Hash>::insert(key, nil());
128  }
130  //- Same as insert (cannot overwrite nil content)
131  bool set(const Key& key)
132  {
133  return HashTable<nil, Key, Hash>::insert(key, nil());
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  //- Equality. Two hashtables are equal when their contents are equal.
143  // Independent of table size or order.
144  bool operator==(const HashSet<Key, Hash>&) const;
145 
146  //- The opposite of the equality operation.
147  bool operator!=(const HashSet<Key, Hash>&) const;
148 
149 
150  //- Combine entries from HashSets
151  void operator|=(const HashSet<Key, Hash>&);
152 
153  //- Only retain entries found in both HashSets
154  void operator&=(const HashSet<Key, Hash>&);
155 
156  //- Only retain unique entries (xor)
157  void operator^=(const HashSet<Key, Hash>&);
159  //- Add entries listed in the given HashSet to this HashSet
160  inline void operator+=(const HashSet<Key, Hash>& rhs)
161  {
162  this->operator|=(rhs);
163  }
164 
165  //- Remove entries listed in the given HashSet from this HashSet
166  void operator-=(const HashSet<Key, Hash>&);
167 };
168 
169 
170 // Global Operators
171 
172 //- Combine entries from HashSets
173 template<class Key, class Hash>
174 HashSet<Key,Hash> operator|
175 (
176  const HashSet<Key,Hash>& hash1,
177  const HashSet<Key,Hash>& hash2
178 );
179 
180 
181 //- Create a HashSet that only contains entries found in both 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 unique entries (xor)
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 
199 //- A HashSet with word keys.
200 typedef HashSet<> wordHashSet;
202 //- A HashSet with label keys.
204 
205 
206 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
207 
208 } // End namespace Foam
209 
210 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
211 
212 #ifdef NoRepository
213 # include "HashSet.C"
214 #endif
215 
216 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
217 
218 #endif
219 
220 // ************************ vim: set sw=4 sts=4 et: ************************ //