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