ListHashTable.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-2022 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::ListHashTable
26 
27 Description
28  STL conforming hash table using contiguous lists rather than linked lists.
29 
30  Note:
31  Is slower to insert than the standard HashTable, but should be more
32  memory efficient and faster to access.
33 
34 SourceFiles
35  ListHashTableI.H
36  ListHashTable.C
37  ListHashTableIO.C
38 
39 \*---------------------------------------------------------------------------*/
40 
41 #ifndef ListHashTable_H
42 #define ListHashTable_H
43 
44 #include "label.H"
45 #include "uLabel.H"
46 #include "word.H"
47 #include "className.H"
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 // Forward declaration of friend functions and operators
55 
56 template<class T> class List;
57 template<class T, class Key, class Hash> class ListHashTable;
58 
59 template<class T, class Key, class Hash> Istream& operator>>
60 (
61  Istream&,
63 );
64 
65 template<class T, class Key, class Hash> Ostream& operator<<
66 (
67  Ostream&,
69 );
70 
71 
72 /*---------------------------------------------------------------------------*\
73  Class ListHashTableCore Declaration
74 \*---------------------------------------------------------------------------*/
75 
76 //- Template-invariant bits for ListHashTable
77 struct ListHashTableCore
78 {
79  //- Return a canonical (power-of-two) size
80  static label canonicalSize(const label);
81 
82  //- Construct null
84  {}
85 
86  //- Define template name and debug
87  ClassName("ListHashTable");
88 
89  //- A zero-sized end iterator
90  struct iteratorEnd
91  {
92  //- Construct null
93  iteratorEnd()
94  {}
95  };
96 };
97 
98 
99 
100 /*---------------------------------------------------------------------------*\
101  Class ListHashTable Declaration
102 \*---------------------------------------------------------------------------*/
103 
104 template<class T, class Key=word, class Hash=string::hash>
105 class ListHashTable
106 :
107  public ListHashTableCore
108 {
109  // Private Data type for table entries
110 
111  //- The lookup keys, ordered per hash value
112  List<List<Key>> keys_;
113 
114  //- For each key the corresponding object.
115  List<List<T>> objects_;
116 
117  //- The current number of elements in table
118  label nElmts_;
119 
120  //- Return a canonical (power-of-two) size
121  static label canonicalSize(const label);
122 
123  //- Return the hash index of the Key within the current table size.
124  // No checks for zero-sized tables.
125  inline label hashKeyIndex(const Key&) const;
126 
127  //- Assign a new hashed entry to a possibly already existing key
128  bool set(const Key&, const T& newElmt, bool protect);
129 
130 
131 public:
132 
133 
134  // Forward declaration of STL iterators
135 
136  template<class TRef, class TableRef>
137  class Iterator;
138 
139  typedef Iterator
140  <
141  T&,
143  > iterator;
144 
145  typedef Iterator
146  <
147  const T&,
149  > const_iterator;
150 
151 
152  // Declare friendship with the iterators
153 
154  friend class Iterator
155  <
156  T&,
158  >;
159 
160  friend class Iterator
161  <
162  const T&,
164  >;
165 
166 
167  // Constructors
168 
169  //- Construct given initial table size
170  ListHashTable(const label size = 128);
171 
172  //- Construct from Istream
173  ListHashTable(Istream&, const label size = 128);
174 
175  //- Copy constructor
177 
178  //- Move constructor
180 
181 
182  //- Destructor
183  ~ListHashTable();
184 
185 
186  // Member Functions
187 
188  // Access
189 
190  //- Return number of elements in table.
191  inline label size() const;
192 
193  //- Return true if the hash table is empty
194  inline bool empty() const;
195 
196  //- Return true if hashed entry is found in table
197  bool found(const Key& key) const;
198 
199  //- Find and return an iterator set at the hashed entry
200  // If not found iterator = end()
201  iterator find(const Key& key);
202 
203  //- Find and return an const_iterator set at the hashed entry
204  // If not found iterator = end()
205  const_iterator find(const Key& key) const;
206 
207  //- Return the table of contents
208  List<Key> toc() const;
209 
210  //- Print information
211  Ostream& printInfo(Ostream&) const;
212 
213 
214  // Edit
215 
216  //- Insert a new hashed entry
217  bool insert(const Key& key, const T& newElmt);
218 
219  //- Assign a new hashed entry, overwriting existing entries
220  inline bool set(const Key&, const T& newElmt);
221 
222  //- Erase an hashed entry specified by given iterator
223  bool erase(const iterator& it);
224 
225  //- Erase an hashed entry specified by given key if in table
226  bool erase(const Key& key);
227 
228  //- Resize the hash table for efficiency
229  void resize(const label newSize);
230 
231  //- Remove entries in the given hash table from this hash table
232  // Return the number of elements removed
234 
235  //- Clear all entries from table
236  void clear();
237 
238  //- Clear the table entries and the table itself.
239  // Equivalent to clear() followed by resize(1)
240  void clearStorage();
241 
242  //- Transfer the contents of the argument table into this table
243  // and annul the argument table.
244  void transfer(ListHashTable<T, Key, Hash>&);
245 
246 
247  // Member Operators
248 
249  //- Find and return an hashed entry
250  inline T& operator[](const Key&);
251 
252  //- Find and return an hashed entry
253  inline const T& operator[](const Key&) const;
254 
255  //- Find and return an hashed entry, create it null if not present.
256  inline T& operator()(const Key&);
257 
258  //- Assignment operator
259  void operator=(const ListHashTable<T, Key, Hash>&);
260 
261  //- Move assignment operator
262  void operator=(ListHashTable<T, Key, Hash>&&);
263 
264  //- Equality. Two hash tables are equal if all contents of first are
265  // also in second and vice versa.
266  bool operator==(const ListHashTable<T, Key, Hash>&) const;
267 
268  //- The opposite of the equality operation.
269  bool operator!=(const ListHashTable<T, Key, Hash>&) const;
270 
271 
272  // STL type definitions
273 
274  //- Type of values the ListHashTable contains.
275  typedef T value_type;
276 
277  //- Type that can be used for storing into ListHashTable::value_type
278  // objects. This type is usually List::value_type&.
279  typedef T& reference;
280 
281  //- Type that can be used for storing into constant
282  // ListHashTable::value_type objects. This type is usually const
283  // ListHashTable::value_type&.
284  typedef const T& const_reference;
285 
286  //- The type that can represent the size of a ListHashTable.
287  typedef label size_type;
288 
289 
290  // STL iterator
291 
292  //- An STL iterator
293  template<class TRef, class TableRef>
294  class Iterator
295  {
296  friend class ListHashTable;
297 
298  template<class TRef2, class TableRef2>
299  friend class Iterator;
300 
301  // Private Data
302 
303  //- Reference to the ListHashTable this is an iterator for
304  TableRef hashTable_;
305 
306  //- Current hash index
307  label hashIndex_;
308 
309  //- Index of current element at hashIndex
310  label elemIndex_;
311 
312 
313  public:
314 
315  // Constructors
316 
317  //- Construct from hash table, hash index and element index
318  inline Iterator
319  (
320  TableRef,
321  label hashIndex_,
322  label elemIndex_
323  );
324 
325  //- Construct from the non-const iterator
326  inline Iterator(const iterator&);
327 
328 
329  // Member Operators
330 
331  inline void operator=(const iterator&);
332 
333  inline bool operator==(const iterator&) const;
334  inline bool operator==(const const_iterator&) const;
335 
336  inline bool operator!=(const iterator&) const;
337  inline bool operator!=(const const_iterator&) const;
338 
339  inline TRef operator*();
340  inline TRef operator()();
341 
342  inline Iterator& operator++();
343  inline Iterator operator++(int);
344 
345  inline const Key& key() const;
346  };
347 
348 
349  //- Iterator set to the beginning of the ListHashTable
350  inline iterator begin();
351 
352  //- Iterator set to beyond the end of the ListHashTable
353  inline const iterator& end();
354 
355  //- const_iterator set to the beginning of the ListHashTable
356  inline const_iterator cbegin() const;
357 
358  //- const_iterator set to beyond the end of the ListHashTable
359  inline const const_iterator& cend() const;
360 
361  //- const_iterator set to the beginning of the ListHashTable
362  inline const_iterator begin() const;
363 
364  //- const_iterator set to beyond the end of the ListHashTable
365  inline const const_iterator& end() const;
366 
367  // IOstream Operator
368 
369  friend Istream& operator>> <T, Key, Hash>
370  (
371  Istream&,
373  );
374 
375  friend Ostream& operator<< <T, Key, Hash>
376  (
377  Ostream&,
379  );
380 
381 
382 private:
383 
384  //- Iterator returned by end()
385  iterator endIter_;
386 
387  //- const_iterator returned by end()
388  const_iterator endConstIter_;
389 };
390 
391 
392 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
393 
394 } // End namespace Foam
395 
396 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
397 
398  #include "ListHashTableI.H"
399 
400 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
401 
402 #ifndef NoListHashTableC
403 #ifdef NoRepository
404  #include "ListHashTable.C"
405 #endif
406 #endif
407 
408 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
409 
410 #endif
411 
412 // ************************************************************************* //
T value_type
Type of values the ListHashTable contains.
A zero-sized end iterator.
Definition: ListHashTable.H:89
tmp< fvMatrix< Type > > operator*(const volScalarField::Internal &, const fvMatrix< Type > &)
tUEqn clear()
const T & const_reference
Type that can be used for storing into constant.
Template-invariant bits for ListHashTable.
Definition: ListHashTable.H:76
srcOptions erase("case")
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
ListHashTableCore()
Construct null.
Definition: ListHashTable.H:82
static label canonicalSize(const label)
Return a canonical (power-of-two) size.
Definition: ListHashTable.C:35
T & reference
Type that can be used for storing into ListHashTable::value_type.
void insert(const scalar, DynamicList< floatScalar > &)
Append scalar to given DynamicList.
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
triSurfaceToAgglom resize(surfacesMesh.size())
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
STL conforming hash table using contiguous lists rather than linked lists.
Definition: ListHashTable.H:56
Macro definitions for declaring ClassName(), NamespaceName(), etc.
Hash function class for primitives. All non-primitives used to hash entries on hash tables likely nee...
Definition: Hash.H:52
bool operator!=(const particle &, const particle &)
Definition: particle.C:1282
bool found
ClassName("ListHashTable")
Define template name and debug.
Namespace for OpenFOAM.