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  //- Emplace set an entry, overwriting any existing entries.
368  // \return True, since it always overwrites any entries.
369  template<class... Args>
370  inline bool emplace_set(const Key& key, Args&&... args);
371 
372  //- Copy insert a new entry, not overwriting existing entries.
373  // \return True if the entry did not previously exist in the table.
374  inline bool insert(const Key& key, const T& obj);
375 
376  //- Move insert a new entry, not overwriting existing entries.
377  // \return True if the entry did not previously exist in the table.
378  inline bool insert(const Key& key, T&& obj);
379 
380  //- Copy assign a new entry, overwriting existing entries.
381  // \return True, since it always overwrites any entries.
382  inline bool set(const Key& key, const T& obj);
383 
384  //- Move assign a new entry, overwriting existing entries.
385  // \return True, since it always overwrites any entries.
386  inline bool set(const Key& key, T&& obj);
387 
388  //- Erase an entry specified by given iterator
389  // This invalidates the iterator until the next ++ operation.
390  //
391  // Includes a safeguard against the end-iterator such that the
392  // following is safe:
393  // \code
394  // auto iter = table.find(unknownKey);
395  // table.erase(iter);
396  // \endcode
397  // which is what \code table.erase(unknownKey) \endcode does anyhow.
398  //
399  // \return True if the corresponding entry existed and was removed
400  bool erase(const iterator& iter);
401 
402  //- Erase an entry specified by the given key
403  // \return True if the entry existed and was removed
404  bool erase(const Key& key);
405 
406  //- Remove table entries given by keys of the other hash-table.
407  //
408  // The other hash-table must have the same type of key, but the
409  // type of values held and the hashing function are arbitrary.
410  //
411  // \return The number of items removed
412  template<class AnyType, class AnyHash>
413  label erase(const HashTable<AnyType, Key, AnyHash>& other);
414 
415  //- Remove table entries given by the listed keys
416  // \return The number of items removed
417  inline label erase(std::initializer_list<Key> keys);
418 
419  //- Remove multiple entries using an iterator range of keys
420  template<class InputIter>
421  inline label erase(InputIter first, InputIter last);
422 
423  //- Remove table entries given by the listed keys
424  // \return The number of items removed
425  template<unsigned N>
426  inline label erase(const FixedList<Key, N>& keys);
427 
428  //- Remove table entries given by the listed keys
429  // \return The number of items removed
430  inline label erase(const UList<Key>& keys);
431 
432  //- Retain table entries given by keys of the other hash-table.
433  //
434  // The other hash-table must have the same type of key, but the
435  // type of values held and the hashing function are arbitrary.
436  //
437  // \return The number of items changed (removed)
438  template<class AnyType, class AnyHash>
439  label retain(const HashTable<AnyType, Key, AnyHash>& other);
440 
441  //- Generalized means to filter table entries based on their keys.
442  // Keep (or optionally prune) entries with keys that satisfy
443  // the unary predicate, which has the following signature:
444  // \code
445  // bool operator()(const Key& k);
446  // \endcode
447  //
448  // For example,
449  // \code
450  // wordRes goodFields = ...;
451  // allFieldNames.filterKeys
452  // (
453  // [&goodFields](const word& k){ return goodFields.match(k); }
454  // );
455  // \endcode
456  //
457  // \return The number of items changed (removed)
458  template<class UnaryPredicate>
459  label filterKeys
460  (
461  const UnaryPredicate& pred,
462  const bool pruning = false
463  );
464 
465  //- Generalized means to filter table entries based on their values.
466  // Keep (or optionally prune) entries with values that satisfy
467  // the unary predicate, which has the following signature:
468  // \code
469  // bool operator()(const T& v);
470  // \endcode
471  //
472  // \return The number of items changed (removed)
473  template<class UnaryPredicate>
474  label filterValues
475  (
476  const UnaryPredicate& pred,
477  const bool pruning = false
478  );
479 
480  //- Generalized means to filter table entries based on their key/value.
481  // Keep (or optionally prune) entries with keys/values that satisfy
482  // the binary predicate, which has the following signature:
483  // \code
484  // bool operator()(const Key& k, const T& v);
485  // \endcode
486  //
487  // \return The number of items changed (removed)
488  template<class BinaryPredicate>
489  label filterEntries
490  (
491  const BinaryPredicate& pred,
492  const bool pruning = false
493  );
494 
495 
496  //- Resize the hash table for efficiency
497  void resize(const label sz);
498 
499  //- Clear all entries from table
500  void clear();
501 
502  //- Clear the table entries and the table itself.
503  // Equivalent to clear() followed by resize(0)
504  void clearStorage();
505 
506  //- Swap contents into this table
507  void swap(HashTable<T, Key, Hash>& rhs);
508 
509  //- Transfer contents into this table.
510  void transfer(HashTable<T, Key, Hash>& rhs);
511 
512 
513  // Member Operators
514 
515  //- Find and return a hashed entry. FatalError if it does not exist.
516  inline T& operator[](const Key& key);
517 
518  //- Find and return a hashed entry. FatalError if it does not exist.
519  inline const T& operator[](const Key& key) const;
520 
521  //- Return existing entry or create a new entry.
522  // A newly created entry is created as a nameless T() and is thus
523  // value-initialized. For primitives, this will be zero.
524  inline T& operator()(const Key& key);
525 
526  //- Return existing entry or insert a new entry.
527  inline T& operator()(const Key& key, const T& deflt);
528 
529  //- Copy assign
530  void operator=(const this_type& rhs);
531 
532  //- Copy assign from an initializer list
533  // Duplicate entries are handled by overwriting
534  void operator=(std::initializer_list<std::pair<Key, T>> rhs);
535 
536  //- Move assign
537  void operator=(this_type&& rhs);
538 
539  //- Equality. Tables are equal if all keys and values are equal,
540  //- independent of order or underlying storage size.
541  bool operator==(const this_type& rhs) const;
542 
543  //- The opposite of the equality operation.
544  bool operator!=(const this_type& rhs) const;
545 
546  //- Add entries into this HashTable
547  this_type& operator+=(const this_type& rhs);
548 
549 
550 protected:
551 
552  // Iterators and helpers
553 
554  //- The iterator base for HashTable (internal use only).
555  // Note: data and functions are protected, to allow reuse by iterator
556  // and prevent most external usage.
557  // iterator and const_iterator have the same size, allowing
558  // us to reinterpret_cast between them (if desired)
559 
560  template<bool Const>
561  class Iterator
562  {
563  public:
564 
565  // Typedefs
566  using iterator_category = std::forward_iterator_tag;
568 
569  //- The HashTable container type
570  using table_type = typename std::conditional
571  <
572  Const,
573  const this_type,
575  >::type;
576 
577  //- The node-type being addressed
578  using node_type = typename std::conditional
579  <
580  Const,
581  const this_type::node_type,
582  this_type::node_type
583  >::type;
584 
585  //- The key type
587 
588  //- The object type being addressed
589  using mapped_type = typename std::conditional
590  <
591  Const,
594  >::type;
595 
596 
597  // Member Functions
598 
599  //- True if iterator points to an entry
600  // This can be used directly instead of comparing to end()
601  inline bool good() const noexcept;
602 
603  //- True if iterator points to an entry - same as good()
604  inline bool found() const noexcept;
605 
606  //- The key associated with the iterator
607  inline const Key& key() const;
608 
609  //- Write the (key, val) pair
610  inline Ostream& print(Ostream& os) const;
611 
612 
613  // Member Operators
614 
615  //- True if iterator points to an entry
616  // This can be used directly instead of comparing to end()
617  explicit inline operator bool() const noexcept;
618 
619  //- Compare hash-entry element pointers.
620  // Independent of const/non-const access
621  template<bool Any>
622  inline bool operator==(const Iterator<Any>& iter) const noexcept;
623 
624  template<bool Any>
625  inline bool operator!=(const Iterator<Any>& iter) const noexcept;
626 
627 
628  protected:
629  friend class HashTable; // For begin/find constructors
630 
631  // Protected Data
632 
633  //- The selected entry.
634  // MUST be the first member for easy comparison between iterators
635  // and to support reinterpret_cast from nullObject
636  node_type* entry_;
637 
638  //- The hash-table container being iterated on.
639  // Uses pointer for default copy/assignment
640  table_type* container_;
641 
642  //- Index within the hash-table data.
643  // A signed value, since iterator_erase() needs a negative value
644  // to mark the position.
645  label index_;
646 
647 
648  // Protected Constructors
649 
650  //- Default construct (end iterator)
651  inline constexpr Iterator() noexcept;
652 
653  //- Construct from begin of hash-table
654  inline explicit Iterator(table_type* tbl);
655 
656  //- Construct by finding key in hash table
657  Iterator(table_type* tbl, const Key& key);
658 
659 
660  // Protected Member Functions
661 
662  //- Increment to the next position
663  inline void increment();
664 
665  //- Permit explicit cast to the other (const/non-const) iterator
666  template<bool Any>
667  explicit operator const Iterator<Any>&() const
668  {
669  return *reinterpret_cast<const Iterator<Any>*>(this);
670  }
671  };
672 
673 
674  //- Low-level entry erasure using iterator internals.
675  // This invalidates the iterator until the next ++ operation.
676  // \return True if the corresponding entry existed and was removed
677  bool iterator_erase(node_type*& entry, label& index);
678 
679 public:
680 
681  //- Forward iterator with non-const access
682  class iterator
683  :
684  public Iterator<false>
685  {
686  public:
687 
688  // Typedefs
689  using iterator_category = std::forward_iterator_tag;
691 
699 
700 
701  // Constructors
702 
703  //- Default construct (end iterator)
704  iterator() = default;
705 
706  //- Copy construct from similar access type
707  explicit iterator(const Iterator<false>& iter)
708  :
709  Iterator<false>(iter)
710  {}
711 
712 
713  // Member Functions/Operators
714 
715  //- Const access to referenced object (value)
716  inline const_reference val() const
717  {
718  return Iterator<false>::entry_->cval();
719  }
720 
721  //- Non-const access to referenced object (value)
722  inline reference val()
723  {
724  return Iterator<false>::entry_->val();
725  }
726 
727  //- Const access to referenced object (value)
728  inline const_reference operator*() const { return this->val(); }
729  inline const_reference operator()() const { return this->val(); }
730 
731  //- Non-const access to referenced object (value)
732  inline reference operator*() { return this->val(); }
733  inline reference operator()() { return this->val(); }
734 
735  inline iterator& operator++();
736  inline iterator operator++(int);
737  };
738 
739 
740  // STL const_iterator
741 
742  //- Forward iterator with const access
743  class const_iterator
744  :
745  public Iterator<true>
746  {
747  public:
748 
749  // Typedefs
750  using iterator_category = std::forward_iterator_tag;
752 
758 
759 
760  // Generated Methods
761 
762  //- Default construct (end iterator)
763  const_iterator() = default;
764 
765  //- Copy construct
766  const_iterator(const const_iterator&) = default;
767 
768  //- Copy assignment
769  const_iterator& operator=(const const_iterator&) = default;
770 
771 
772  // Constructors
773 
774  //- Copy construct from any access type
775  template<bool Any>
776  const_iterator(const Iterator<Any>& iter)
777  :
778  Iterator<true>(static_cast<const Iterator<Any>&>(iter))
779  {}
780 
781  //- Implicit conversion from dissimilar access type
782  const_iterator(const iterator& iter)
783  :
784  const_iterator(reinterpret_cast<const const_iterator&>(iter))
785  {}
786 
787 
788  // Member Functions/Operators
789 
790  //- Const access to referenced object (value)
791  inline reference val() const
792  {
793  return Iterator<true>::entry_->cval();
794  }
795 
796  //- Const access to referenced object (value)
797  inline reference operator*() const { return this->val(); }
798  inline reference operator()() const { return this->val(); }
799 
800  inline const_iterator& operator++();
801  inline const_iterator operator++(int);
802 
803 
804  // Assignment
805 
806  // Allow assign from iterator to const_iterator
807  const_iterator& operator=(const iterator& iter)
808  {
809  return this->operator=
810  (
811  reinterpret_cast<const const_iterator&>(iter)
812  );
813  }
814  };
815 
816 
817  // Iterator (keys)
818 
819  //- An iterator wrapper for returning a reference to the key
820  template<class Iter>
821  class key_iterator_base
822  :
823  public Iter
824  {
825  public:
826 
828  using pointer = const Key*;
829  using reference = const Key&;
830 
831  //- Default construct (end iterator)
832  constexpr key_iterator_base() noexcept
833  :
834  Iter()
835  {}
836 
837  //- Copy construct with implicit conversion
838  explicit key_iterator_base(const Iter& iter)
839  :
840  Iter(iter)
841  {}
842 
843  //- Return the key
844  inline reference operator*() const { return this->key(); }
845  inline reference operator()() const { return this->key(); }
846 
847  inline key_iterator_base& operator++()
848  {
849  this->increment();
850  return *this;
851  }
852 
853  inline key_iterator_base operator++(int)
854  {
855  key_iterator_base iter(*this);
856  this->increment();
857  return iter;
858  }
859  };
860 
861 
862  //- Forward iterator returning the key
863  using key_iterator = key_iterator_base<iterator>;
864 
865  //- Forward const iterator returning the key
866  using const_key_iterator = key_iterator_base<const_iterator>;
867 
868  //- A const iterator begin/end pair for iterating over keys
869  const_iterator_pair<const_key_iterator, this_type> keys() const
870  {
871  return const_iterator_pair<const_key_iterator, this_type>(*this);
872  }
873 
874 
875  // Iterator access
876 
877  //- iterator set to the beginning of the HashTable
878  inline iterator begin();
879 
880  //- const_iterator set to the beginning of the HashTable
881  inline const_iterator begin() const;
882 
883  //- const_iterator set to the beginning of the HashTable
884  inline const_iterator cbegin() const;
885 
886  //- iterator to signal the end (for any HashTable)
887  inline iterator end() noexcept;
888 
889  //- const_iterator to signal the end (for any HashTable)
890  inline const_iterator end() const noexcept;
891 
892  //- const_iterator to signal the end (for any HashTable)
893  inline constexpr const_iterator cend() const noexcept;
894 
895 
896  // Writing
897 
898  //- Print information
899  Ostream& printInfo(Ostream& os) const;
900 
901  //- Write unordered keys (list), with line-breaks
902  //- when length exceeds shortLen.
903  // Using '0' suppresses line-breaks entirely.
904  Ostream& writeKeys(Ostream& os, const label shortLen=0) const;
905 };
906 
907 
908 // IOstream Operators
909 
910 template<class T, class Key, class Hash>
911 Istream& operator>>(Istream& is, HashTable<T, Key, Hash>& tbl);
912 
913 template<class T, class Key, class Hash>
914 Ostream& operator<<(Ostream& os, const HashTable<T, Key, Hash>& tbl);
915 
916 
917 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
918 
919 } // End namespace Foam
920 
921 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
922 
923 #include "HashTableI.H"
924 #include "HashTableIterI.H"
925 
926 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
927 
928 #ifndef NoHashTableC
929 #ifdef NoRepository
930  #include "HashTable.C"
931 #endif
932 #endif
933 
934 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
935 
936 #endif
937 
938 // ************************************************************************* //
Foam::HashTable::iterator::val
const_reference val() const
Const access to referenced object (value)
Definition: HashTable.H:715
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:828
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:790
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:695
HashTableCore.H
Foam::HashTable::const_iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: HashTable.H:749
Foam::HashTable::iterator::iterator
iterator(const Iterator< false > &iter)
Copy construct from similar access type.
Definition: HashTable.H:706
Foam::HashTable::iterator
Forward iterator with non-const access.
Definition: HashTable.H:681
Foam::HashTable::emplace_set
bool emplace_set(const Key &key, Args &&... args)
Emplace set an entry, overwriting any existing entries.
Definition: HashTableI.H:169
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:844
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:721
Foam::HashTable< Foam::phase * >::key_iterator
key_iterator_base< iterator > key_iterator
Forward iterator returning the key.
Definition: HashTable.H:862
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:692
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:732
Foam::HashTable::const_iterator
Forward iterator with const access.
Definition: HashTable.H:742
Foam::HashTable::insert
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:180
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:796
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:820
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:837
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:731
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:693
Foam::HashTable::Iterator< true >::key_type
this_type::key_type key_type
The key type.
Definition: HashTable.H:585
Foam::HashTable::key_iterator_base::pointer
const Key * pointer
Definition: HashTable.H:827
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:752
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:593
Foam::HashTable::const_iterator::pointer
this_type::const_pointer pointer
Definition: HashTable.H:755
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:826
Foam::HashTable::Iterator< true >::iterator_category
std::forward_iterator_tag iterator_category
Definition: HashTable.H:565
Foam::HashTable::iterator::const_pointer
this_type::const_pointer const_pointer
Definition: HashTable.H:696
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:694
Foam::HashTable::const_iterator::const_iterator
const_iterator(const iterator &iter)
Implicit conversion from dissimilar access type.
Definition: HashTable.H:781
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:797
Foam::HashTable::const_iterator::const_iterator
const_iterator(const Iterator< Any > &iter)
Copy construct from any access type.
Definition: HashTable.H:775
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:865
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:582
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:754
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:753
Foam::HashTable::iterator::operator*
const_reference operator*() const
Const access to referenced object (value)
Definition: HashTable.H:727
Foam::HashTable::key_iterator_base::key_iterator_base
constexpr key_iterator_base() noexcept
Default construct (end iterator)
Definition: HashTable.H:831
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:566
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:868
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:689
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:691
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:756
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
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:574
Foam::HashTable::const_iterator::operator=
const_iterator & operator=(const iterator &iter)
Definition: HashTable.H:806
Foam::HashTable::iterator::const_reference
this_type::const_reference const_reference
Definition: HashTable.H:697
Foam::HashTable::key_iterator_base::operator++
key_iterator_base & operator++()
Definition: HashTable.H:846
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:199
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:728
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:202
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:750
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:843
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:852
HashTableDetail.H
Foam::HashTable::iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: HashTable.H:688
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