HashTableIterI.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) 2017-2022 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 // * * * * * * * * * * * * * * * iterator base * * * * * * * * * * * * * * * //
29 
30 template<class T, class Key, class Hash>
31 template<bool Const>
32 inline constexpr
34 :
35  entry_(nullptr),
36  container_(nullptr),
37  index_(0)
38 {}
39 
40 
41 template<class T, class Key, class Hash>
42 template<bool Const>
44 (
45  table_type* tbl
46 )
47 :
48  entry_(nullptr),
49  container_(tbl),
50  index_(0)
51 {
52  if (container_ && container_->size_)
53  {
54  // Locate the first non-nullptr table entry
55  while
56  (
57  !(entry_ = container_->table_[index_])
58  && ++index_ < container_->capacity_
59  )
60  {}
61 
62  if (index_ >= container_->capacity_)
63  {
64  // Nothing found - make it an end iterator
65  entry_ = nullptr;
66  index_ = 0;
67  }
68  }
69 }
70 
71 
72 //
73 // Any changes here may need changes in iterator_erase(), iterator_extract()
74 // methods too
75 //
76 template<class T, class Key, class Hash>
77 template<bool Const>
78 inline void
80 {
81  if (index_ < 0)
82  {
83  // Negative index is a special value from erase
84  //
85  // Saved as (-index-1), retrieved as (-(index-1)) but to with an
86  // extra (-1) to compensate for the ++ in the following while loop
87  index_ = -(index_+1) - 1;
88  }
89  else if (index_ < container_->capacity_ && entry_ && entry_->next_)
90  {
91  // Move to next element on the linked-list
92  entry_ = entry_->next_;
93  return;
94  }
95 
96  // Move to the next non-nullptr table entry
97  while
98  (
99  ++index_ < container_->capacity_
100  && !(entry_ = container_->table_[index_])
101  )
102  {}
103 
104  if (index_ >= container_->capacity_)
105  {
106  // Nothing found - make it an end iterator
107  entry_ = nullptr;
108  index_ = 0;
109  }
110 }
111 
112 
113 template<class T, class Key, class Hash>
114 template<bool Const>
116 (
117  Ostream& os
118 ) const
119 {
120  if (entry_)
121  {
122  entry_->print(os);
123  }
124  return os;
125 }
126 
127 
128 // * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
129 
130 template<class T, class Key, class Hash>
133 {
134  this->increment();
135  return *this;
136 }
137 
138 
139 template<class T, class Key, class Hash>
142 {
143  iterator iter(*this);
144  this->increment();
145  return iter;
146 }
147 
148 
149 // * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * //
150 
151 template<class T, class Key, class Hash>
154 {
155  this->increment();
156  return *this;
157 }
158 
159 
160 template<class T, class Key, class Hash>
163 {
164  const_iterator iter(*this);
165  this->increment();
166  return iter;
167 }
168 
169 
170 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
171 
172 template<class T, class Key, class Hash>
175 {
176  return iterator(Iterator<false>(this));
177 }
178 
179 
180 template<class T, class Key, class Hash>
183 {
184  return const_iterator(Iterator<true>(this));
185 }
186 
187 
188 template<class T, class Key, class Hash>
191 {
192  return const_iterator(Iterator<true>(this));
193 }
194 
195 
196 template<class T, class Key, class Hash>
199 {
200  return iterator();
201 }
202 
203 
204 template<class T, class Key, class Hash>
207 {
208  return const_iterator();
209 }
210 
211 
212 template<class T, class Key, class Hash>
213 inline constexpr typename Foam::HashTable<T, Key, Hash>::const_iterator
215 {
216  return const_iterator();
217 }
218 
219 
220 // ************************************************************************* //
void increment()
Increment to the next position.
Forward iterator with const access.
Definition: HashTable.H:1116
Ostream & print(Ostream &os, UIntType value, char off='0', char on='1')
Print 0/1 bits in the (unsigned) integral type.
Definition: BitOps.H:323
iterator end() noexcept
iterator to signal the end (for any HashTable)
Forward iterator with non-const access.
Definition: HashTable.H:1024
friend class Iterator< true >
Allow iterator access to HashTable internals.
Definition: HashTable.H:833
iterator begin()
iterator set to the beginning of the HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:108
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: IOstream.C:67
friend class Iterator< false >
Allow iterator access to HashTable internals.
Definition: HashTable.H:843
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
constexpr const_iterator cend() const noexcept
const_iterator to signal the end (for any HashTable)