HashTable.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::HashTable
29 
30 Description
31  A HashTable similar to \c std::unordered_map.
32 
33  The entries are considered \a unordered since their placement
34  depends on the method used to generate the hash key index, the
35  table capacity, insertion order etc. When the key order is
36  important, use the sortedToc() method to obtain a list of sorted
37  keys and use that for further access, or the sorted() method
38  to obtain a UPtrList of entries to traverse in sorted order.
39 
40  Internally the table uses closed addressing into a flat storage space
41  with collisions handled by linked-list chaining.
42 
43  The end iterator of all hash-tables has a nullptr to the hash entry.
44  Thus avoid separate allocation for each table and use a single one with
45  a nullptr. The hash-table iterators always have an entry-pointer as the
46  first member data, which allows reinterpret_cast from anything else with
47  a nullptr as its first data member.
48  The nullObject is such an item (with a nullptr data member).
49 
50 Note
51  For historical reasons, dereferencing the table iterator
52  (eg, \a *iter) returns a reference to the stored object value
53  rather than the stored key/val pair like std::unordered_map does.
54 
55  The HashTable iterator:
56  \code
57  forAllConstIters(table, iter)
58  {
59  Info<< "val:" << *iter << nl
60  << "key:" << iter.key() << nl;
61  << "val:" << iter.val() << nl;
62  }
63  \endcode
64  whereas for the \c std::unordered_map iterator:
65  \code
66  forAllConstIters(stdmap, iter)
67  {
68  Info<< "key/val:" << *iter << nl
69  << "key:" << iter->first << nl
70  << "val:" << iter->second << nl;
71  }
72  \endcode
73  This difference is most evident when using range-for syntax.
74 
75 SourceFiles
76  HashTableI.H
77  HashTableIterI.H
78  HashTable.C
79  HashTableIO.C
80  HashTableIter.C
81 
82 \*---------------------------------------------------------------------------*/
83 
84 #ifndef Foam_HashTable_H
85 #define Foam_HashTable_H
86 
87 #include "stdFoam.H"
88 #include "word.H"
89 #include "zero.H"
90 #include "Hash.H"
91 #include "HashTableDetail.H"
92 #include "HashTableCore.H"
93 
94 #include <iterator>
95 
96 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
97 
98 namespace Foam
99 {
101 // Forward Declarations
103 template<class T> class List;
104 template<class T> class UList;
105 template<class T> class UPtrList;
106 template<class T, unsigned N> class FixedList;
107 template<class T, class Key, class Hash> class HashTable;
108 
109 template<class T, class Key, class Hash>
111 
112 template<class T, class Key, class Hash>
113 Ostream& operator<<(Ostream&, const HashTable<T, Key, Hash>&);
114 
115 /*---------------------------------------------------------------------------*\
116  Class HashTable Declaration
117 \*---------------------------------------------------------------------------*/
118 
119 template<class T, class Key=word, class Hash=Foam::Hash<Key>>
120 class HashTable
121 :
122  public HashTableCore
123 {
124 public:
125 
126  // Data Types
127 
128  //- The template instance used for this HashTable
130 
131  //- A table entry (node) that encapsulates the key/val tuple
132  //- with an additional linked-list entry for hash collisions
133  typedef typename std::conditional
134  <
138  >::type node_type;
139 
140 
141  // STL type definitions
142 
143  //- The second template parameter, type of keys used.
144  typedef Key key_type;
146  //- The first template parameter, type of objects contained.
147  typedef T mapped_type;
148 
149  //- Same as mapped_type for OpenFOAM HashTables
150  // Note that this is different than the std::map definition.
151  typedef T value_type;
152 
153  //- The third template parameter, the hash index method.
154  typedef Hash hasher;
155 
156  //- Pointer type for storing into value_type objects.
157  // This type is usually 'value_type*'.
158  typedef T* pointer;
159 
160  //- Reference to the stored value_type.
161  // This type is usually 'value_type&'.
162  typedef T& reference;
163 
164  //- Const pointer type for the stored value_type.
165  typedef const T* const_pointer;
166 
167  //- Const reference to the stored value_type.
168  typedef const T& const_reference;
170  //- The type to represent the difference between two iterators
171  typedef label difference_type;
172 
173  //- The type that can represent the size of a HashTable.
174  typedef label size_type;
175 
177  //- Forward iterator with non-const access
178  class iterator;
179 
180  //- Forward iterator with const access
181  class const_iterator;
182 
183 
184 private:
185 
186  // Private Data
187 
188  //- The number of nodes currently stored in table
189  label size_;
190 
191  //- Number of nodes allocated in table
192  label capacity_;
193 
194  //- The table of primary nodes
195  node_type** table_;
197 
198  // Private Member Functions
199 
200  //- Return the hash index of the Key within the current table size.
201  // No checks for zero-sized tables.
202  inline label hashKeyIndex(const Key& key) const;
203 
204  //- Assign a new hash-entry to a possibly already existing key.
205  // \return True if the new entry was set.
206  template<class... Args>
207  bool setEntry(const bool overwrite, const Key& key, Args&&... args);
208 
209  //- Read hash table
210  Istream& readTable(Istream& is);
211 
212  //- Write hash table
213  Ostream& writeTable(Ostream& os) const;
214 
215 
216 protected:
217 
218  //- Internally used base for iterator and const_iterator
219  template<bool Const> class Iterator;
220 
221  //- An iterator with const access to HashTable internals.
222  friend class Iterator<true>;
223 
224  //- An iterator with non-const access to HashTable internals.
225  friend class Iterator<false>;
226 
227 
228 public:
229 
230  // Constructors
231 
232  //- Default construct with default (128) table capacity
233  HashTable();
234 
235  //- Construct given initial table capacity
236  explicit HashTable(const label size);
237 
238  //- Construct from Istream with default table capacity
239  HashTable(Istream& is, const label size = 128);
240 
241  //- Copy construct
242  HashTable(const this_type& ht);
243 
244  //- Move construct
245  HashTable(this_type&& rhs);
246 
247  //- Construct from an initializer list
248  // Duplicate entries are handled by overwriting
249  HashTable(std::initializer_list<std::pair<Key, T>> list);
250 
251 
252  //- Destructor
253  ~HashTable();
254 
255 
256  // Member Functions
257 
258  // Access
259 
260  //- The size of the underlying table
261  inline label capacity() const noexcept;
262 
263  //- The number of elements in table
264  inline label size() const noexcept;
265 
266  //- True if the hash table is empty
267  inline bool empty() const noexcept;
268 
269  //- Find and return a hashed entry. FatalError if it does not exist.
270  inline T& at(const Key& key);
271 
272  //- Find and return a hashed entry. FatalError if it does not exist.
273  inline const T& at(const Key& key) const;
274 
275  //- True if hashed key is found in table
276  inline bool found(const Key& key) const;
277 
278  //- Find and return an iterator set at the hashed entry
279  // If not found iterator = end()
280  inline iterator find(const Key& key);
281 
282  //- Find and return an const_iterator set at the hashed entry
283  // If not found iterator = end()
284  inline const_iterator find(const Key& key) const;
285 
286  //- Find and return an const_iterator set at the hashed entry
287  // If not found iterator = end()
288  inline const_iterator cfind(const Key& key) const;
289 
290  //- Return hashed entry if it exists, or return the given default
291  inline const T& lookup(const Key& key, const T& deflt) const;
292 
293 
294  // Table of contents
295 
296  //- The table of contents (the keys) in unsorted order.
297  List<Key> toc() const;
298 
299  //- The table of contents (the keys) in sorted order
300  List<Key> sortedToc() const;
301 
302  //- The table of contents (the keys) sorted according to the
303  //- specified comparator
304  template<class Compare>
305  List<Key> sortedToc(const Compare& comp) const;
306 
307  //- The table of contents (the keys) selected according to the
308  //- unary predicate applied to the \b keys.
309  // \param invert changes the logic to select when the predicate
310  // is false
311  // \return sorted list of selected keys
312  template<class UnaryPredicate>
313  List<Key> tocKeys
314  (
315  const UnaryPredicate& pred,
316  const bool invert = false
317  ) const;
318 
319  //- The table of contents (the keys) selected according to the
320  //- unary predicate applied to the \b values.
321  // \param invert changes the logic to select when the predicate
322  // is false
323  // \return sorted list of selected keys
324  template<class UnaryPredicate>
325  List<Key> tocValues
326  (
327  const UnaryPredicate& pred,
328  const bool invert = false
329  ) const;
330 
331  //- The table of contents (the keys) selected according to the
332  //- binary predicate applied to the \b keys and \b values.
333  // \param invert changes the logic to select when the predicate
334  // is false
335  // \return sorted list of selected keys
336  template<class BinaryPredicate>
337  List<Key> tocEntries
338  (
339  const BinaryPredicate& pred,
340  const bool invert = false
341  ) const;
342 
343 
344  // Sorted entries
345 
346  //- Const access to the hash-table contents in sorted order
347  //- (sorted by keys).
348  // The lifetime of the returned content cannot exceed the parent!
349  UPtrList<const node_type> csorted() const;
350 
351  //- Const access to the hash-table contents in sorted order
352  //- (sorted by keys).
353  // The lifetime of the returned content cannot exceed the parent!
354  UPtrList<const node_type> sorted() const;
355 
356  //- Non-const access to the hash-table contents in sorted order
357  //- (sorted by keys).
358  // The lifetime of the returned content cannot exceed the parent!
360 
361 
362  // Counting
363 
364  //- Count the number of keys that satisfy the unary predicate
365  // \param invert changes the logic to select when the predicate
366  // is false
367  template<class UnaryPredicate>
368  label countKeys
369  (
370  const UnaryPredicate& pred,
371  const bool invert = false
372  ) const;
373 
374  //- Count the number of values that satisfy the unary predicate
375  // \param invert changes the logic to select when the predicate
376  // is false
377  template<class UnaryPredicate>
378  label countValues
379  (
380  const UnaryPredicate& pred,
381  const bool invert = false
382  ) const;
383 
384  //- Count the number of entries that satisfy the binary predicate.
385  // \param invert changes the logic to select when the predicate
386  // is false
387  template<class BinaryPredicate>
388  label countEntries
389  (
390  const BinaryPredicate& pred,
391  const bool invert = false
392  ) const;
393 
394 
395  // Edit
396 
397  //- Emplace insert a new entry, not overwriting existing entries.
398  // \return True if the entry did not previously exist in the table.
399  template<class... Args>
400  inline bool emplace(const Key& key, Args&&... args);
401 
402  //- Emplace set an entry, overwriting any existing entries.
403  // \return True, since it always overwrites any entries.
404  template<class... Args>
405  inline bool emplace_set(const Key& key, Args&&... args);
406 
407  //- Copy insert a new entry, not overwriting existing entries.
408  // \return True if the entry did not previously exist in the table.
409  inline bool insert(const Key& key, const T& obj);
410 
411  //- Move insert a new entry, not overwriting existing entries.
412  // \return True if the entry did not previously exist in the table.
413  inline bool insert(const Key& key, T&& obj);
414 
415  //- Copy assign a new entry, overwriting existing entries.
416  // \return True, since it always overwrites any entries.
417  inline bool set(const Key& key, const T& obj);
418 
419  //- Move assign a new entry, overwriting existing entries.
420  // \return True, since it always overwrites any entries.
421  inline bool set(const Key& key, T&& obj);
422 
423  //- Erase an entry specified by given iterator
424  // This invalidates the iterator until the next ++ operation.
425  //
426  // Includes a safeguard against the end-iterator such that the
427  // following is safe:
428  // \code
429  // auto iter = table.find(unknownKey);
430  // table.erase(iter);
431  // \endcode
432  // which is what \code table.erase(unknownKey) \endcode does anyhow.
433  //
434  // \return True if the corresponding entry existed and was removed
435  bool erase(const iterator& iter);
436 
437  //- Erase an entry specified by the given key
438  // \return True if the entry existed and was removed
439  bool erase(const Key& key);
440 
441  //- Remove table entries given by keys of the other hash-table.
442  //
443  // The other hash-table must have the same type of key, but the
444  // type of values held and the hashing function are arbitrary.
445  //
446  // \return The number of items removed
447  template<class AnyType, class AnyHash>
448  label erase(const HashTable<AnyType, Key, AnyHash>& other);
449 
450  //- Remove table entries given by the listed keys
451  // \return The number of items removed
452  inline label erase(std::initializer_list<Key> keys);
453 
454  //- Remove multiple entries using an iterator range of keys
455  template<class InputIter>
456  inline label erase(InputIter first, InputIter last);
457 
458  //- Remove table entries given by the listed keys
459  // \return The number of items removed
460  template<unsigned N>
461  inline label erase(const FixedList<Key, N>& keys);
462 
463  //- Remove table entries given by the listed keys
464  // \return The number of items removed
465  inline label erase(const UList<Key>& keys);
466 
467  //- Retain table entries given by keys of the other hash-table.
468  //
469  // The other hash-table must have the same type of key, but the
470  // type of values held and the hashing function are arbitrary.
471  //
472  // \return The number of items changed (removed)
473  template<class AnyType, class AnyHash>
474  label retain(const HashTable<AnyType, Key, AnyHash>& other);
475 
476  //- Generalized means to filter table entries based on their keys.
477  // Keep (or optionally prune) entries with keys that satisfy
478  // the unary predicate, which has the following signature:
479  // \code
480  // bool operator()(const Key& k);
481  // \endcode
482  //
483  // For example,
484  // \code
485  // wordRes goodFields = ...;
486  // allFieldNames.filterKeys
487  // (
488  // [&goodFields](const word& k){ return goodFields.match(k); }
489  // );
490  // \endcode
491  //
492  // \return The number of items changed (removed)
493  template<class UnaryPredicate>
494  label filterKeys
495  (
496  const UnaryPredicate& pred,
497  const bool pruning = false
498  );
499 
500  //- Generalized means to filter table entries based on their values.
501  // Keep (or optionally prune) entries with values that satisfy
502  // the unary predicate, which has the following signature:
503  // \code
504  // bool operator()(const T& v);
505  // \endcode
506  //
507  // \return The number of items changed (removed)
508  template<class UnaryPredicate>
509  label filterValues
510  (
511  const UnaryPredicate& pred,
512  const bool pruning = false
513  );
514 
515  //- Generalized means to filter table entries based on their key/value.
516  // Keep (or optionally prune) entries with keys/values that satisfy
517  // the binary predicate, which has the following signature:
518  // \code
519  // bool operator()(const Key& k, const T& v);
520  // \endcode
521  //
522  // \return The number of items changed (removed)
523  template<class BinaryPredicate>
524  label filterEntries
525  (
526  const BinaryPredicate& pred,
527  const bool pruning = false
528  );
529 
530 
531  //- Resize the hash table for efficiency
532  void resize(const label sz);
533 
534  //- Clear all entries from table
535  void clear();
536 
537  //- Clear the table entries and the table itself.
538  // Equivalent to clear() followed by resize(0)
539  void clearStorage();
540 
541  //- Swap contents into this table
542  void swap(HashTable<T, Key, Hash>& rhs);
543 
544  //- Transfer contents into this table.
545  void transfer(HashTable<T, Key, Hash>& rhs);
546 
547 
548  // Member Operators
549 
550  //- Find and return a hashed entry. FatalError if it does not exist.
551  inline T& operator[](const Key& key);
552 
553  //- Find and return a hashed entry. FatalError if it does not exist.
554  inline const T& operator[](const Key& key) const;
555 
556  //- Return existing entry or create a new entry.
557  // A newly created entry is created as a nameless T() and is thus
558  // value-initialized. For primitives, this will be zero.
559  inline T& operator()(const Key& key);
560 
561  //- Return existing entry or insert a new entry.
562  inline T& operator()(const Key& key, const T& deflt);
563 
564  //- Copy assign
565  void operator=(const this_type& rhs);
566 
567  //- Copy assign from an initializer list
568  // Duplicate entries are handled by overwriting
569  void operator=(std::initializer_list<std::pair<Key, T>> rhs);
570 
571  //- Move assign
572  void operator=(this_type&& rhs);
573 
574  //- Equality. Tables are equal if all keys and values are equal,
575  //- independent of order or underlying storage size.
576  bool operator==(const this_type& rhs) const;
577 
578  //- The opposite of the equality operation.
579  bool operator!=(const this_type& rhs) const;
580 
581  //- Add entries into this HashTable
582  this_type& operator+=(const this_type& rhs);
583 
584 
585 protected:
586 
587  // Iterators and helpers
588 
589  //- The iterator base for HashTable (internal use only).
590  // Note: data and functions are protected, to allow reuse by iterator
591  // and prevent most external usage.
592  // iterator and const_iterator have the same size, allowing
593  // us to reinterpret_cast between them (if desired)
594 
595  template<bool Const>
596  class Iterator
597  {
598  public:
599 
600  // Typedefs
601  using iterator_category = std::forward_iterator_tag;
603 
604  //- The HashTable container type
605  using table_type = typename std::conditional
606  <
607  Const,
608  const this_type,
609  this_type
610  >::type;
611 
612  //- The node-type being addressed
613  using node_type = typename std::conditional
614  <
615  Const,
616  const this_type::node_type,
618  >::type;
619 
620  //- The key type
622 
623  //- The object type being addressed
624  using mapped_type = typename std::conditional
625  <
626  Const,
629  >::type;
630 
631 
632  protected:
633 
634  // Protected Data
635 
636  //- The selected entry.
637  // MUST be the first member for easy comparison between iterators
638  // and to support reinterpret_cast from nullObject
639  node_type* entry_;
640 
641  //- The hash-table container being iterated on.
642  // Uses pointer for default copy/assignment
643  table_type* container_;
644 
645  //- Index within the hash-table data.
646  // A signed value, since iterator_erase() needs a negative value
647  // to mark the position.
648  label index_;
649 
650  // Friendship with HashTable, for begin/find constructors
651  friend class HashTable;
652 
653 
654  // Protected Constructors
655 
656  //- Default construct. Also the same as the end iterator
657  inline constexpr Iterator() noexcept;
658 
659  //- Construct from begin of hash-table
660  inline explicit Iterator(table_type* tbl);
661 
662  //- Construct by finding key in hash table
663  Iterator(table_type* tbl, const Key& key);
664 
665 
666  // Protected Member Functions
667 
668  //- Increment to the next position
669  inline void increment();
670 
671  //- Permit explicit cast to the other (const/non-const) iterator
672  template<bool Any>
673  explicit operator const Iterator<Any>&() const
674  {
675  return *reinterpret_cast<const Iterator<Any>*>(this);
676  }
677 
678 
679  public:
680 
681  // Member Functions
682 
683  //- True if iterator points to an entry
684  // This can be used directly instead of comparing to end()
685  bool good() const noexcept { return entry_; }
686 
687  //- True if iterator points to an entry - same as good()
688  bool found() const noexcept { return entry_; }
689 
690  //- The key associated with the iterator
691  const Key& key() const { return entry_->key(); }
692 
693  //- Write the (key, val) pair
694  inline Ostream& print(Ostream& os) const;
695 
696 
697  // Member Operators
698 
699  //- True if iterator points to an entry
700  // This can be used directly instead of comparing to end()
701  explicit operator bool() const noexcept { return entry_; }
702 
703  //- Compare hash-entry element pointers.
704  // Independent of const/non-const access
705  template<bool Any>
706  bool operator==(const Iterator<Any>& iter) const noexcept
707  {
708  return (entry_ == iter.entry_);
709  }
710 
711  template<bool Any>
712  bool operator!=(const Iterator<Any>& iter) const noexcept
713  {
714  return (entry_ != iter.entry_);
715  }
716  };
717 
718  //- Low-level entry erasure using iterator internals.
719  // This invalidates the iterator until the next ++ operation.
720  // \return True if the corresponding entry existed and was removed
721  bool iterator_erase(node_type*& entry, label& index);
722 
723 public:
724 
725  //- Forward iterator with non-const access
726  class iterator
727  :
728  public Iterator<false>
729  {
730  public:
731 
732  // Typedefs
733  using iterator_category = std::forward_iterator_tag;
735 
740  using pointer = this_type::pointer;
744 
745 
746  // Constructors
747 
748  //- Default construct (end iterator)
749  iterator() = default;
750 
751  //- Copy construct from similar access type
752  explicit iterator(const Iterator<false>& iter)
753  :
754  Iterator<false>(iter)
755  {}
756 
757 
758  // Member Functions/Operators
759 
760  //- Const access to the entry (node)
761  const node_type* node() const noexcept
762  {
764  }
765 
766  //- Non-const access to the entry (node)
768  {
770  }
771 
772  //- Const access to referenced object (value)
773  const_reference val() const
774  {
775  return Iterator<false>::entry_->cval();
776  }
777 
778  //- Non-const access to referenced object (value)
779  reference val()
780  {
781  return Iterator<false>::entry_->val();
782  }
783 
784  //- Const access to referenced object (value)
785  const_reference operator*() const { return this->val(); }
786  const_reference operator()() const { return this->val(); }
787 
788  //- Non-const access to referenced object (value)
789  reference operator*() { return this->val(); }
790  reference operator()() { return this->val(); }
791 
792  inline iterator& operator++();
793  inline iterator operator++(int);
794  };
795 
796 
797  // STL const_iterator
798 
799  //- Forward iterator with const access
800  class const_iterator
801  :
802  public Iterator<true>
803  {
804  public:
805 
806  // Typedefs
807  using iterator_category = std::forward_iterator_tag;
809 
812  using mapped_type = const this_type::mapped_type;
813  using value_type = const this_type::value_type;
816 
818  // Generated Methods
819 
820  //- Default construct (end iterator)
821  const_iterator() = default;
822 
823  //- Copy construct
824  const_iterator(const const_iterator&) = default;
825 
826  //- Copy assignment
828 
829 
830  // Constructors
831 
832  //- Copy construct from any access type
833  template<bool Any>
834  const_iterator(const Iterator<Any>& iter)
835  :
836  Iterator<true>(static_cast<const Iterator<Any>&>(iter))
837  {}
838 
839  //- Implicit conversion from dissimilar access type
840  const_iterator(const iterator& iter)
841  :
842  const_iterator(reinterpret_cast<const const_iterator&>(iter))
843  {}
844 
845 
846  // Member Functions/Operators
847 
848  //- Const access to the entry (node)
849  const node_type* node() const noexcept
850  {
851  return Iterator<true>::entry_;
852  }
853 
854  //- Const access to referenced object (value)
855  reference val() const
856  {
857  return Iterator<true>::entry_->cval();
858  }
859 
860  //- Const access to referenced object (value)
861  reference operator*() const { return this->val(); }
862  reference operator()() const { return this->val(); }
863 
864  inline const_iterator& operator++();
865  inline const_iterator operator++(int);
866 
867 
868  // Assignment
869 
870  // Allow assign from iterator to const_iterator
871  const_iterator& operator=(const iterator& iter)
872  {
873  return this->operator=
874  (
875  reinterpret_cast<const const_iterator&>(iter)
876  );
877  }
878  };
879 
880 
881  // Iterator (keys)
882 
883  //- An iterator wrapper for returning a reference to the key
884  template<class Iter>
885  class key_iterator_base
886  :
887  public Iter
888  {
889  public:
890 
892  using pointer = const Key*;
893  using reference = const Key&;
894 
895  //- Default construct (end iterator)
896  constexpr key_iterator_base() noexcept
897  :
898  Iter()
899  {}
900 
901  //- Copy construct with implicit conversion
902  explicit key_iterator_base(const Iter& iter)
903  :
904  Iter(iter)
905  {}
906 
907  //- Return the key
908  reference operator*() const { return this->key(); }
909  reference operator()() const { return this->key(); }
910 
911  inline key_iterator_base& operator++()
912  {
913  this->increment();
914  return *this;
915  }
916 
917  inline key_iterator_base operator++(int)
918  {
919  key_iterator_base iter(*this);
920  this->increment();
921  return iter;
922  }
923  };
924 
926  //- Forward iterator returning the key
928 
929  //- Forward const iterator returning the key
931 
932  //- A const iterator begin/end pair for iterating over keys
934  {
936  }
937 
938 
939  // Iterator access
940 
941  //- iterator set to the beginning of the HashTable
942  inline iterator begin();
943 
944  //- const_iterator set to the beginning of the HashTable
945  inline const_iterator begin() const;
946 
947  //- const_iterator set to the beginning of the HashTable
948  inline const_iterator cbegin() const;
949 
950  //- iterator to signal the end (for any HashTable)
951  inline iterator end() noexcept;
952 
953  //- const_iterator to signal the end (for any HashTable)
954  inline const_iterator end() const noexcept;
955 
956  //- const_iterator to signal the end (for any HashTable)
957  inline constexpr const_iterator cend() const noexcept;
958 
960  // Reading/writing
961 
962  //- Print information
963  Ostream& printInfo(Ostream& os) const;
964 
965  //- Write unordered keys (list), with line-breaks
966  //- when length exceeds shortLen.
967  // Using '0' suppresses line-breaks entirely.
968  Ostream& writeKeys(Ostream& os, const label shortLen=0) const;
969 
970 
971  // IOstream Operators
972 
973  friend Istream& operator>> <T, Key, Hash>
974  (
975  Istream&,
976  HashTable<T, Key, Hash>& tbl
977  );
979  friend Ostream& operator<< <T, Key, Hash>
980  (
981  Ostream&,
982  const HashTable<T, Key, Hash>& tbl
983  );
984 };
987 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
989 } // End namespace Foam
991 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
993 #include "HashTableI.H"
996 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
997 
998 #ifndef NoHashTableC // Excluded from token.H
999 #ifdef NoRepository
1000  #include "HashTable.C"
1001 #endif
1002 #endif
1003 
1004 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
1005 
1006 #endif
1007 
1008 // ************************************************************************* //
reference operator*() const
Return the key.
Definition: HashTable.H:1202
label countValues(const UnaryPredicate &pred, const bool invert=false) const
Count the number of values that satisfy the unary predicate.
node_type * entry_
The selected entry.
Definition: HashTable.H:855
List< Key > tocKeys(const UnaryPredicate &pred, const bool invert=false) const
The table of contents (the keys) selected according to the unary predicate applied to the keys...
reference val() const
Const access to referenced object (value)
Definition: HashTable.H:1139
Factory class for creating a begin/end pair for any const iterator.
Definition: HashTableCore.H:87
const_reference operator()() const
Definition: HashTable.H:1052
type
Types of root.
Definition: Roots.H:52
bool emplace(const Key &key, Args &&... args)
Emplace insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:150
const node_type * node() const noexcept
Const access to the entry (node)
Definition: HashTable.H:1019
bool operator!=(const this_type &rhs) const
The opposite of the equality operation.
Definition: HashTable.C:901
Internally used base for iterator and const_iterator.
Definition: HashTable.H:263
Forward iterator with const access.
Definition: HashTable.H:1070
label filterValues(const UnaryPredicate &pred, const bool pruning=false)
Generalized means to filter table entries based on their values.
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:101
std::forward_iterator_tag iterator_category
Definition: HashTable.H:806
label retain(const HashTable< AnyType, Key, AnyHash > &other)
Retain table entries given by keys of the other hash-table.
bool found(const Key &key) const
True if hashed key is found in table.
Definition: HashTableI.H:93
this_type::value_type value_type
Definition: HashTable.H:991
this_type::key_type key_type
The key type.
Definition: HashTable.H:832
Internal storage type for HashSet entries.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
Ostream & print(Ostream &os, UIntType value, char off='0', char on='1')
Print 0/1 bits in the (unsigned) integral type.
Definition: BitOps.H:289
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
this_type::reference reference
Definition: HashTable.H:993
iterator()=default
Default construct (end iterator)
friend Ostream & operator(Ostream &, const HashTable< T, Key, Hash > &tbl)
label size_type
The type that can represent the size of a HashTable.
Definition: HashTable.H:196
Hash hasher
The third template parameter, the hash index method.
Definition: HashTable.H:162
UPtrList< const node_type > csorted() const
Const access to the hash-table contents in sorted order (sorted by keys).
Definition: HashTable.C:155
HashTable< T, Key, Hash > this_type
The template instance used for this HashTable.
Definition: HashTable.H:126
T & at(const Key &key)
Find and return a hashed entry. FatalError if it does not exist.
Definition: HashTableI.H:59
const_reference val() const
Const access to referenced object (value)
Definition: HashTable.H:1035
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:45
bool iterator_erase(node_type *&entry, label &index)
Low-level entry erasure using iterator internals.
Definition: HashTableIter.C:59
Lookup type of boundary radiation properties.
Definition: lookup.H:57
iterator end() noexcept
iterator to signal the end (for any HashTable)
typename std::conditional< Const, const this_type::node_type, this_type::node_type >::type node_type
The node-type being addressed.
Definition: HashTable.H:827
const_reference operator*() const
Const access to referenced object (value)
Definition: HashTable.H:1051
const_iterator & operator=(const const_iterator &)=default
Copy assignment.
void resize(const label sz)
Resize the hash table for efficiency.
Definition: HashTable.C:594
bool operator==(const this_type &rhs) const
Equality. Tables are equal if all keys and values are equal, independent of order or underlying stora...
Definition: HashTable.C:875
List< Key > tocEntries(const BinaryPredicate &pred, const bool invert=false) const
The table of contents (the keys) selected according to the binary predicate applied to the keys and v...
bool emplace_set(const Key &key, Args &&... args)
Emplace set an entry, overwriting any existing entries.
Definition: HashTableI.H:162
~HashTable()
Destructor.
Definition: HashTable.C:102
T * pointer
Pointer type for storing into value_type objects.
Definition: HashTable.H:169
Forward iterator with non-const access.
Definition: HashTable.H:978
const node_type * node() const noexcept
Const access to the entry (node)
Definition: HashTable.H:1131
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:173
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:752
label countEntries(const BinaryPredicate &pred, const bool invert=false) const
Count the number of entries that satisfy the binary predicate.
void swap(HashTable< T, Key, Hash > &rhs)
Swap contents into this table.
Definition: HashTable.C:698
reference operator*() const
Const access to referenced object (value)
Definition: HashTable.H:1147
reference operator()() const
Definition: HashTable.H:1148
this_type::key_type value_type
Definition: HashTable.H:1179
const_iterator cfind(const Key &key) const
Find and return an const_iterator set at the hashed entry.
Definition: HashTableI.H:134
Ostream & writeKeys(Ostream &os, const label shortLen=0) const
Write unordered keys (list), with line-breaks when length exceeds shortLen.
Definition: HashTableIO.C:90
label capacity() const noexcept
The size of the underlying table.
Definition: HashTableI.H:38
A class for handling words, derived from Foam::string.
Definition: word.H:63
iterator find(const Key &key)
Find and return an iterator set at the hashed entry.
Definition: HashTableI.H:107
label filterEntries(const BinaryPredicate &pred, const bool pruning=false)
Generalized means to filter table entries based on their key/value.
Ostream & printInfo(Ostream &os) const
Print information.
Definition: HashTableIO.C:53
Istream & operator>>(Istream &, directionInfo &)
void clear()
Clear all entries from table.
Definition: HashTable.C:671
this_type::node_type node_type
Definition: HashTable.H:988
constexpr key_iterator_base() noexcept
Default construct (end iterator)
Definition: HashTable.H:1186
this_type::const_pointer const_pointer
Definition: HashTable.H:994
T mapped_type
The first template parameter, type of objects contained.
Definition: HashTable.H:150
this_type::pointer pointer
Definition: HashTable.H:992
label difference_type
The type to represent the difference between two iterators.
Definition: HashTable.H:191
iterator begin()
iterator set to the beginning of the HashTable
const_iterator()=default
Default construct (end iterator)
this_type::key_type key_type
Definition: HashTable.H:989
typename std::conditional< Const, const this_type::mapped_type, this_type::mapped_type >::type mapped_type
The object type being addressed.
Definition: HashTable.H:842
label countKeys(const UnaryPredicate &pred, const bool invert=false) const
Count the number of keys that satisfy the unary predicate.
A HashTable similar to std::unordered_map.
Definition: HashTable.H:102
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: HashTable.H:100
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:99
key_iterator_base & operator++()
Definition: HashTable.H:1205
const T & const_reference
Const reference to the stored value_type.
Definition: HashTable.H:186
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
const direction noexcept
Definition: Scalar.H:258
bool empty() const noexcept
True if the hash table is empty.
Definition: HashTableI.H:52
An iterator wrapper for returning a reference to the key.
Definition: HashTable.H:1173
this_type::const_reference const_reference
Definition: HashTable.H:995
T value_type
Same as mapped_type for OpenFOAM HashTables.
Definition: HashTable.H:157
Key key_type
The second template parameter, type of keys used.
Definition: HashTable.H:145
OBJstream os(runTime.globalPath()/outputName)
HashTable()
Default construct with default (128) table capacity.
Definition: HashTable.C:33
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
std::conditional< std::is_same< zero::null, typename std::remove_cv< T >::type >::value, Detail::HashTableSingle< Key >, Detail::HashTablePair< Key, T > >::type node_type
A table entry (node) that encapsulates the key/val tuple with an additional linked-list entry for has...
Definition: HashTable.H:137
const Vector< label > N(dict.get< Vector< label >>("N"))
bool erase(const iterator &iter)
Erase an entry specified by given iterator.
Definition: HashTable.C:433
std::forward_iterator_tag iterator_category
Definition: HashTable.H:985
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:29
Bits that are independent of HashTable template parameters.
Definition: HashTableCore.H:54
constexpr Iterator() noexcept
Default construct. Also the same as the end iterator.
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:47
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
List< Key > sortedToc() const
The table of contents (the keys) in sorted order.
Definition: HashTable.C:130
void transfer(HashTable< T, Key, Hash > &rhs)
Transfer contents into this table.
Definition: HashTable.C:712
UPtrList< const node_type > sorted() const
Const access to the hash-table contents in sorted order (sorted by keys).
Definition: HashTable.C:174
T & reference
Reference to the stored value_type.
Definition: HashTable.H:176
this_type::difference_type difference_type
Definition: HashTable.H:807
Internal storage type for HashTable entries.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:69
label filterKeys(const UnaryPredicate &pred, const bool pruning=false)
Generalized means to filter table entries based on their keys.
Includes some standard C++ headers, defines global macros and templates used in multiple places by Op...
this_type::difference_type difference_type
Definition: HashTable.H:986
friend class Iterator< false >
An iterator with non-const access to HashTable internals.
Definition: HashTable.H:273
const T * const_pointer
Const pointer type for the stored value_type.
Definition: HashTable.H:181
List< Key > toc() const
The table of contents (the keys) in unsorted order.
Definition: HashTable.C:115
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
this_type::mapped_type mapped_type
Definition: HashTable.H:990
Foam::argList args(argc, argv)
const_iterator_pair< const_key_iterator, this_type > keys() const
A const iterator begin/end pair for iterating over keys.
Definition: HashTable.H:1233
constexpr const_iterator cend() const noexcept
const_iterator to signal the end (for any HashTable)
bool set(const Key &key, const T &obj)
Copy assign a new entry, overwriting existing entries.
Definition: HashTableI.H:195
Namespace for OpenFOAM.
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:63
void clearStorage()
Clear the table entries and the table itself.
Definition: HashTable.C:690
List< Key > tocValues(const UnaryPredicate &pred, const bool invert=false) const
The table of contents (the keys) selected according to the unary predicate applied to the values...