Loading...
Searching...
No Matches
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-2023 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
35Foam::HashTable<T, Key, Hash>::hashKeyIndex(const Key& key) const
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 T& Foam::HashTable<T, Key, Hash>::at(const Key& key)
46{
47 iterator iter(this->find(key));
48
49 if (!iter.good())
50 {
52 << key << " not found in table. Valid entries: "
53 << toc()
54 << exit(FatalError);
55 }
56
57 return iter.val();
58}
59
60
61template<class T, class Key, class Hash>
62inline const T& Foam::HashTable<T, Key, Hash>::at(const Key& key) const
63{
64 const const_iterator iter(this->cfind(key));
65
66 if (!iter.good())
67 {
69 << key << " not found in table. Valid entries: "
70 << toc()
71 << exit(FatalError);
72 }
73
74 return iter.val();
75}
76
77
78template<class T, class Key, class Hash>
79inline bool Foam::HashTable<T, Key, Hash>::contains(const Key& key) const
80{
81 if (size_)
82 {
83 return Iterator<true>(this, key).good();
84 }
86 return false;
87}
88
89
90template<class T, class Key, class Hash>
93(
94 const Key& key
95)
96{
97 if (size_)
98 {
99 return iterator(Iterator<false>(this, key));
100 }
102 return iterator();
103}
104
105
106template<class T, class Key, class Hash>
109(
110 const Key& key
111) const
113 return this->cfind(key);
114}
115
116
117template<class T, class Key, class Hash>
120(
121 const Key& key
122) const
123{
124 if (size_)
125 {
126 return const_iterator(Iterator<true>(this, key));
127 }
129 return const_iterator();
130}
131
132
133template<class T, class Key, class Hash>
134template<class... Args>
136(
137 const Key& key,
138 Args&&... args
139)
141 return this->setEntry(false, key, std::forward<Args>(args)...);
142}
143
144
145template<class T, class Key, class Hash>
146template<class... Args>
148(
149 const Key& key,
150 Args&&... args
152{
153 return this->setEntry(true, key, std::forward<Args>(args)...);
154}
155
156
157template<class T, class Key, class Hash>
159(
160 const Key& key,
161 const T& obj
163{
164 return this->setEntry(false, key, obj);
165}
166
167
168template<class T, class Key, class Hash>
170(
171 const Key& key,
172 T&& obj
174{
175 return this->setEntry(false, key, std::forward<T>(obj));
176}
177
178
179template<class T, class Key, class Hash>
181(
182 const Key& key,
183 const T& obj
185{
186 return this->setEntry(true, key, obj); // Overwrite
187}
188
189
190template<class T, class Key, class Hash>
192(
193 const Key& key,
194 T&& obj
195)
196{
197 return this->setEntry(true, key, std::forward<T>(obj)); // Overwrite
198}
199
200
201// FUTURE?
202// If 'key' already exists, assign \c std::forward<T>(obj) to the
203// entry. If it does not exist, uses insert()
204//
205// template<class T, class Key, class Hash>
206// inline void Foam::HashTable<T, Key, Hash>::insert_or_assign
207// (
208// const Key& key,
209// T&& obj
210// )
211// {
212// iterator iter(this->find(key));
213//
214// if (iter.good())
215// {
216// iter.val() = std::forward<T>(obj);
217// }
218// else
219// {
220// this->setEntry(true, key, std::forward<T>(obj));
221// // iter = this->find(key);
222// }
223// // return iter.val();
224// }
225
226
227template<class T, class Key, class Hash>
229(
230 const Key& key,
231 const T& deflt
232) const
233{
234 const const_iterator iter(this->cfind(key));
235 return iter.good() ? iter.val() : deflt;
236}
237
238
239// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
240
241template<class T, class Key, class Hash>
243{
244 return this->at(key);
245}
246
247
248template<class T, class Key, class Hash>
249inline const T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key) const
250{
251 return this->at(key);
252}
253
254
255template<class T, class Key, class Hash>
256inline T& Foam::HashTable<T, Key, Hash>::operator()(const Key& key)
257{
258 iterator iter(this->find(key));
259
260 if (iter.good())
261 {
262 return iter.val();
263 }
265 this->emplace(key);
266 return find(key).val();
267}
268
269
270template<class T, class Key, class Hash>
272(
273 const Key& key,
274 const T& deflt
275)
276{
277 iterator iter(this->find(key));
278
279 if (iter.good())
280 {
281 return iter.val();
282 }
283
284 this->insert(key, deflt);
285 return find(key).val();
286}
287
288
289// ************************************************************************* //
Internally used base for iterator and const_iterator.
Definition HashTable.H:872
bool good() const noexcept
True if iterator points to an entry.
Definition HashTable.H:990
Forward iterator with const access.
Definition HashTable.H:1135
reference val() const
Const access to referenced object (value).
Definition HashTable.H:1201
Forward iterator with non-const access.
Definition HashTable.H:1043
const_reference val() const
Const access to referenced object (value).
Definition HashTable.H:1097
T & operator()(const Key &key)
Return existing entry or create a new entry.
Definition HashTableI.H:249
List< Key > toc() const
The table of contents (the keys) in unsorted order.
Definition HashTable.C:141
bool set(const Key &key, const T &obj)
Copy assign a new entry, overwriting existing entries.
Definition HashTableI.H:174
const T & lookup(const Key &key, const T &deflt) const
Return hashed entry if it exists, or return the given default.
Definition HashTableI.H:222
bool contains(const Key &key) const
True if hashed key is contained (found) in table.
Definition HashTableI.H:72
const_iterator cfind(const Key &key) const
Find and return an const_iterator set at the hashed entry.
Definition HashTableI.H:113
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition HashTableI.H:152
bool emplace_set(const Key &key, Args &&... args)
Emplace set an entry, overwriting any existing entries.
Definition HashTableI.H:141
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:86
bool emplace(const Key &key, Args &&... args)
Emplace insert a new entry, not overwriting existing entries.
Definition HashTableI.H:129
T & at(const Key &key)
Find and return a hashed entry. FatalError if it does not exist.
Definition HashTableI.H:38
T & operator[](const Key &key)
Find and return a hashed entry. FatalError if it does not exist.
Definition HashTableI.H:235
const volScalarField & T
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
constexpr auto key(const Type &t) noexcept
Helper function to return the enum value.
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Foam::argList args(argc, argv)
nonInt insert("surfaceSum(((S|magSf)*S)")
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition Hash.H:48