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-2020 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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
30template<class T, class Key, class Hash>
31template<bool Const>
32inline constexpr
34:
35 entry_(nullptr),
36 container_(nullptr),
37 index_(0)
38{}
39
40
41template<class T, class Key, class Hash>
42template<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() method too
74//
75template<class T, class Key, class Hash>
76template<bool Const>
77inline 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
112template<class T, class Key, class Hash>
113template<bool Const>
114inline bool
116{
117 return entry_;
118}
119
120
121template<class T, class Key, class Hash>
122template<bool Const>
123inline bool
125{
126 return entry_;
127}
128
129
130template<class T, class Key, class Hash>
131template<bool Const>
133{
134 return entry_->key();
135}
136
137
138template<class T, class Key, class Hash>
139template<bool Const>
141(
142 Ostream& os
143) const
144{
145 if (entry_)
146 {
147 entry_->print(os);
148 }
149 return os;
150}
151
152
153template<class T, class Key, class Hash>
154template<bool Const>
156bool() const noexcept
157{
158 return entry_;
159}
160
161
162template<class T, class Key, class Hash>
163template<bool Const>
164template<bool Any>
166(
167 const Iterator<Any>& iter
168) const noexcept
169{
170 return entry_ == iter.entry_;
171}
172
173
174template<class T, class Key, class Hash>
175template<bool Const>
176template<bool Any>
178(
179 const Iterator<Any>& iter
180) const noexcept
181{
182 return entry_ != iter.entry_;
183}
184
185
186// * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
187
188template<class T, class Key, class Hash>
191{
192 this->increment();
193 return *this;
194}
195
196
197template<class T, class Key, class Hash>
200{
201 iterator iter(*this);
202 this->increment();
203 return iter;
204}
205
206
207// * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * //
208
209template<class T, class Key, class Hash>
212{
213 this->increment();
214 return *this;
215}
216
217
218template<class T, class Key, class Hash>
221{
222 const_iterator iter(*this);
223 this->increment();
224 return iter;
225}
226
227
228// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
229
230template<class T, class Key, class Hash>
233{
234 return iterator(Iterator<false>(this));
235}
236
237
238template<class T, class Key, class Hash>
241{
242 return const_iterator(Iterator<true>(this));
243}
244
245
246template<class T, class Key, class Hash>
249{
250 return const_iterator(Iterator<true>(this));
251}
252
253
254template<class T, class Key, class Hash>
257{
258 return iterator();
259}
260
261
262template<class T, class Key, class Hash>
265{
266 return const_iterator();
267}
268
269
270template<class T, class Key, class Hash>
271inline constexpr typename Foam::HashTable<T, Key, Hash>::const_iterator
273{
274 return const_iterator();
275}
276
277
278// ************************************************************************* //
bool found
void increment()
Move iterator forward.
Definition: CirculatorI.H:48
Internally used base for iterator and const_iterator.
Definition: HashTable.H:597
node_type * entry_
The selected entry.
Definition: HashTable.H:671
label index_
Index within the hash-table data.
Definition: HashTable.H:680
const Key & key() const
The key associated with the iterator.
table_type * container_
The hash-table container being iterated on.
Definition: HashTable.H:675
typename std::conditional< Const, const this_type, this_type >::type table_type
The HashTable container type.
Definition: HashTable.H:610
Forward iterator with const access.
Definition: HashTable.H:794
Forward iterator with non-const access.
Definition: HashTable.H:720
A HashTable similar to std::unordered_map.
Definition: HashTable.H:123
iterator begin()
iterator set to the beginning of the HashTable
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
friend Ostream & operator(Ostream &, const HashTable< T, Key, Hash > &tbl)
iterator end() noexcept
iterator to signal the end (for any HashTable)
constexpr const_iterator cend() const noexcept
const_iterator to signal the end (for any HashTable)
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
scalar print()
Print to screen.
bool
Definition: EEqn.H:20
OBJstream os(runTime.globalPath()/outputName)
const direction noexcept
Definition: Scalar.H:223