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-2019 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>
33 :
34  entry_(nullptr),
35  container_(nullptr),
36  index_(0)
37 {}
38 
39 
40 template<class T, class Key, class Hash>
41 template<bool Const>
43 (
44  bool, // Future use and to avoid implicit construct
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() method too
74 //
75 template<class T, class Key, class Hash>
76 template<bool Const>
77 inline void
79 {
80  if (index_ < 0)
81  {
82  // Negative index is a special value from erase
83  //
84  // Saved as (-index-1), retrieved as (-(index-1)) but to with an
85  // extra (-1) to compensate for the ++ in the following while loop
86  index_ = -(index_+1) - 1;
87  }
88  else if (index_ < container_->capacity_ && entry_ && entry_->next_)
89  {
90  // Move to next element on the linked-list
91  entry_ = entry_->next_;
92  return;
93  }
94 
95  // Move to the next non-nullptr table entry
96  while
97  (
98  ++index_ < container_->capacity_
99  && !(entry_ = container_->table_[index_])
100  )
101  {}
102 
103  if (index_ >= container_->capacity_)
104  {
105  // Nothing found - make it an end iterator
106  entry_ = nullptr;
107  index_ = 0;
108  }
109 }
110 
111 
112 template<class T, class Key, class Hash>
113 template<bool Const>
114 inline bool
116 {
117  return entry_;
118 }
119 
120 
121 template<class T, class Key, class Hash>
122 template<bool Const>
123 inline bool
125 {
126  return entry_;
127 }
128 
129 
130 template<class T, class Key, class Hash>
131 template<bool Const>
133 {
134  return entry_->key();
135 }
136 
137 
138 template<class T, class Key, class Hash>
139 template<bool Const>
141 (
142  Ostream& os
143 ) const
144 {
145  if (entry_)
146  {
147  entry_->print(os);
148  }
149  return os;
150 }
151 
152 
153 template<class T, class Key, class Hash>
154 template<bool Const>
155 inline Foam::HashTable<T, Key, Hash>::Iterator<Const>::operator
156 bool() const noexcept
157 {
158  return entry_;
159 }
160 
161 
162 template<class T, class Key, class Hash>
163 template<bool Const>
164 inline bool Foam::HashTable<T, Key, Hash>::Iterator<Const>::operator==
165 (
166  const Iterator<true>& iter
167 ) const
168 {
169  return entry_ == iter.entry_;
170 }
171 
172 
173 template<class T, class Key, class Hash>
174 template<bool Const>
175 inline bool Foam::HashTable<T, Key, Hash>::Iterator<Const>::operator!=
176 (
177  const Iterator<true>& iter
178 ) const
179 {
180  return entry_ != iter.entry_;
181 }
182 
183 
184 template<class T, class Key, class Hash>
185 template<bool Const>
186 inline bool Foam::HashTable<T, Key, Hash>::Iterator<Const>::operator==
187 (
188  const Iterator<false>& iter
189 ) const
190 {
191  return entry_ == iter.entry_;
192 }
193 
194 
195 template<class T, class Key, class Hash>
196 template<bool Const>
197 inline bool Foam::HashTable<T, Key, Hash>::Iterator<Const>::operator!=
198 (
199  const Iterator<false>& iter
200 ) const
201 {
202  return entry_ != iter.entry_;
203 }
204 
205 
206 // * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
207 
208 template<class T, class Key, class Hash>
211 {
212  this->increment();
213  return *this;
214 }
215 
216 
217 template<class T, class Key, class Hash>
220 {
221  iterator iter(*this);
222  this->increment();
223  return iter;
224 }
225 
226 
227 // * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * //
228 
229 template<class T, class Key, class Hash>
232 {
233  this->increment();
234  return *this;
235 }
236 
237 
238 template<class T, class Key, class Hash>
241 {
242  const_iterator iter(*this);
243  this->increment();
244  return iter;
245 }
246 
247 
248 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
249 
250 template<class T, class Key, class Hash>
253 {
254  return iterator(Iterator<false>(true, this));
255 }
256 
257 
258 template<class T, class Key, class Hash>
261 {
262  return const_iterator(Iterator<true>(true, this));
263 }
264 
265 
266 template<class T, class Key, class Hash>
269 {
270  return const_iterator(Iterator<true>(true, this));
271 }
272 
273 
274 template<class T, class Key, class Hash>
275 inline const typename Foam::HashTable<T, Key, Hash>::iterator&
277 {
278  return iterator_end<iterator>();
279 }
280 
281 
282 template<class T, class Key, class Hash>
285 {
286  return iterator_cend<const_iterator>();
287 }
288 
289 
290 template<class T, class Key, class Hash>
293 {
294  return iterator_cend<const_iterator>();
295 }
296 
297 
298 // ************************************************************************* //
Foam::HashTable::iterator
Forward iterator with non-const access.
Definition: HashTable.H:668
Foam::HashTable::iterator::operator++
iterator & operator++()
Definition: HashTableIterI.H:210
Foam::HashTable::cbegin
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
Definition: HashTableIterI.H:268
Foam::HashTable::Iterator< false >::increment
void increment()
Increment to the next position.
Definition: HashTableIterI.H:78
Foam::HashTable::const_iterator
Forward iterator with const access.
Definition: HashTable.H:726
Foam::IOstream::print
virtual void print(Ostream &os) const
Print description of IOstream to Ostream.
Definition: IOstream.C:75
Foam::HashTable::Iterator::entry_
node_type * entry_
The selected entry.
Definition: HashTable.H:623
Foam::HashTable::cend
const const_iterator & cend() const
const_iterator to signal the end for any HashTable
Definition: HashTableIterI.H:292
Foam::HashTable::const_iterator::operator++
const_iterator & operator++()
Definition: HashTableIterI.H:231
Foam::HashTable::Iterator< true >
friend class Iterator< true >
An iterator with const access to HashTable internals.
Definition: HashTable.H:196
Foam::HashTable::Iterator< false >
friend class Iterator< false >
An iterator with non-const access to HashTable internals.
Definition: HashTable.H:202
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
Foam::HashTable::begin
iterator begin()
iterator set to the beginning of the HashTable
Definition: HashTableIterI.H:252
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
bool
bool
Definition: EEqn.H:20
Foam::HashTable::Iterator
Internally used base for iterator and const_iterator.
Definition: HashTable.H:196
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::HashTable::found
bool found(const Key &key) const
Return true if hashed entry is found in table.
Definition: HashTableI.H:100
Foam::HashTable::end
const iterator & end()
iterator to signal the end for any HashTable
Definition: HashTableIterI.H:276