HashTableI.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-------------------------------------------------------------------------------
11License
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\*---------------------------------------------------------------------------*/
28
29#include "error.H"
30
31// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
32
33template<class T, class Key, class Hash>
34inline Foam::label
36{
37 // capacity is always a power of two - this is the modulus
38 return Hash()(key) & (capacity_ - 1);
39}
40
41
42// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
43
44template<class T, class Key, class Hash>
45inline Foam::label Foam::HashTable<T, Key, Hash>::capacity() const noexcept
46{
47 return capacity_;
48}
49
50
51template<class T, class Key, class Hash>
52inline Foam::label Foam::HashTable<T, Key, Hash>::size() const noexcept
53{
54 return size_;
55}
56
57
58template<class T, class Key, class Hash>
59inline bool Foam::HashTable<T, Key, Hash>::empty() const noexcept
60{
61 return !size_;
62}
63
64
65template<class T, class Key, class Hash>
66inline T& Foam::HashTable<T, Key, Hash>::at(const Key& key)
67{
68 iterator iter(this->find(key));
69
70 if (!iter.good())
71 {
73 << key << " not found in table. Valid entries: "
74 << toc()
75 << exit(FatalError);
76 }
77
78 return iter.val();
79}
80
81
82template<class T, class Key, class Hash>
83inline const T& Foam::HashTable<T, Key, Hash>::at(const Key& key) const
84{
85 const const_iterator iter(this->cfind(key));
86
87 if (!iter.good())
88 {
90 << key << " not found in table. Valid entries: "
91 << toc()
92 << exit(FatalError);
93 }
94
95 return iter.val();
96}
97
98
99template<class T, class Key, class Hash>
100inline bool Foam::HashTable<T, Key, Hash>::found(const Key& key) const
101{
102 if (size_)
103 {
104 return Iterator<true>(this, key).good();
105 }
106
107 return false;
108}
109
110
111template<class T, class Key, class Hash>
114(
115 const Key& key
116)
117{
118 if (size_)
119 {
120 return iterator(Iterator<false>(this, key));
121 }
122
123 return iterator();
124}
125
126
127template<class T, class Key, class Hash>
130(
131 const Key& key
132) const
133{
134 return this->cfind(key);
135}
136
137
138template<class T, class Key, class Hash>
141(
142 const Key& key
143) const
144{
145 if (size_)
146 {
147 return const_iterator(Iterator<true>(this, key));
148 }
149
150 return const_iterator();
151}
152
153
154template<class T, class Key, class Hash>
155template<class... Args>
157(
158 const Key& key,
159 Args&&... args
160)
161{
162 return this->setEntry(false, key, std::forward<Args>(args)...);
163}
164
165
166template<class T, class Key, class Hash>
167template<class... Args>
169(
170 const Key& key,
171 Args&&... args
172)
173{
174 return this->setEntry(true, key, std::forward<Args>(args)...);
175}
176
177
178template<class T, class Key, class Hash>
180(
181 const Key& key,
182 const T& obj
183)
184{
185 return this->setEntry(false, key, obj);
186}
187
188
189template<class T, class Key, class Hash>
191(
192 const Key& key,
193 T&& obj
194)
195{
196 return this->setEntry(false, key, std::forward<T>(obj));
197}
198
199
200template<class T, class Key, class Hash>
202(
203 const Key& key,
204 const T& obj
205)
206{
207 return this->setEntry(true, key, obj); // Overwrite
208}
209
210
211template<class T, class Key, class Hash>
213(
214 const Key& key,
215 T&& obj
216)
217{
218 return this->setEntry(true, key, std::forward<T>(obj)); // Overwrite
219}
220
221
222template<class T, class Key, class Hash>
224(
225 const Key& key,
226 const T& deflt
227) const
228{
229 const const_iterator iter(this->cfind(key));
230 return iter.good() ? iter.val() : deflt;
231}
232
233
234// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
235
236template<class T, class Key, class Hash>
238{
239 return this->at(key);
240}
241
242
243template<class T, class Key, class Hash>
244inline const T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key) const
245{
246 return this->at(key);
247}
248
249
250template<class T, class Key, class Hash>
252{
253 iterator iter(this->find(key));
254
255 if (iter.good())
256 {
257 return iter.val();
258 }
259
260 this->emplace(key);
261 return find(key).val();
262}
263
265template<class T, class Key, class Hash>
268 const Key& key,
269 const T& deflt
271{
272 iterator iter(this->find(key));
274 if (iter.good())
275 {
276 return iter.val();
277 }
278
279 this->insert(key, deflt);
280 return find(key).val();
281}
282
283
284// ************************************************************************* //
bool found
Internally used base for iterator and const_iterator.
Definition: HashTable.H:597
bool good() const noexcept
True if iterator points to an entry.
Forward iterator with const access.
Definition: HashTable.H:794
reference val() const
Const access to referenced object (value)
Definition: HashTable.H:846
Forward iterator with non-const access.
Definition: HashTable.H:720
const_reference val() const
Const access to referenced object (value)
Definition: HashTable.H:764
A HashTable similar to std::unordered_map.
Definition: HashTable.H:123
bool empty() const noexcept
True if the hash table is empty.
Definition: HashTableI.H:59
const_iterator cfind(const Key &key) const
Find and return an const_iterator set at the hashed entry.
Definition: HashTableI.H:141
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:180
bool emplace_set(const Key &key, Args &&... args)
Emplace set an entry, overwriting any existing entries.
Definition: HashTableI.H:169
label capacity() const noexcept
The size of the underlying table.
Definition: HashTableI.H:45
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
friend Ostream & operator(Ostream &, const HashTable< T, Key, Hash > &tbl)
iterator find(const Key &key)
Find and return an iterator set at the hashed entry.
Definition: HashTableI.H:114
bool emplace(const Key &key, Args &&... args)
Emplace insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:157
T & at(const Key &key)
Find and return a hashed entry. FatalError if it does not exist.
Definition: HashTableI.H:66
T & operator[](const Key &key)
Find and return a hashed entry. FatalError if it does not exist.
Definition: HashTableI.H:237
Ostream & operator()() const
Output stream (master only).
Definition: ensightCaseI.H:74
Lookup type of boundary radiation properties.
Definition: lookup.H:66
bool set() const
Are all the vector set.
Definition: triadI.H:76
const volScalarField & T
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
error FatalError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
Foam::argList args(argc, argv)