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 -------------------------------------------------------------------------------
11 License
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 
33 template<class T, class Key, class Hash>
34 inline 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 
44 template<class T, class Key, class Hash>
45 inline 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 
61 template<class T, class Key, class Hash>
62 inline 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 
78 template<class T, class Key, class Hash>
79 inline bool Foam::HashTable<T, Key, Hash>::contains(const Key& key) const
80 {
81  if (size_)
82  {
83  return Iterator<true>(this, key).good();
84  }
85 
86  return false;
87 }
88 
89 
90 template<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  }
101 
102  return iterator();
103 }
104 
105 
106 template<class T, class Key, class Hash>
109 (
110  const Key& key
111 ) const
112 {
113  return this->cfind(key);
114 }
115 
116 
117 template<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  }
128 
129  return const_iterator();
130 }
131 
132 
133 template<class T, class Key, class Hash>
134 template<class... Args>
136 (
137  const Key& key,
138  Args&&... args
139 )
140 {
141  return this->setEntry(false, key, std::forward<Args>(args)...);
142 }
143 
144 
145 template<class T, class Key, class Hash>
146 template<class... Args>
148 (
149  const Key& key,
150  Args&&... args
151 )
152 {
153  return this->setEntry(true, key, std::forward<Args>(args)...);
154 }
155 
156 
157 template<class T, class Key, class Hash>
159 (
160  const Key& key,
161  const T& obj
162 )
163 {
164  return this->setEntry(false, key, obj);
165 }
166 
167 
168 template<class T, class Key, class Hash>
170 (
171  const Key& key,
172  T&& obj
173 )
174 {
175  return this->setEntry(false, key, std::forward<T>(obj));
176 }
177 
178 
179 template<class T, class Key, class Hash>
181 (
182  const Key& key,
183  const T& obj
184 )
185 {
186  return this->setEntry(true, key, obj); // Overwrite
187 }
188 
189 
190 template<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 
227 template<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 
241 template<class T, class Key, class Hash>
243 {
244  return this->at(key);
245 }
246 
247 
248 template<class T, class Key, class Hash>
249 inline const T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key) const
250 {
251  return this->at(key);
252 }
253 
254 
255 template<class T, class Key, class Hash>
257 {
258  iterator iter(this->find(key));
259 
260  if (iter.good())
261  {
262  return iter.val();
263  }
264 
265  this->emplace(key);
266  return find(key).val();
267 }
268 
269 
270 template<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 // ************************************************************************* //
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as ListOps::find_if.
Definition: ListOps.H:795
bool emplace(const Key &key, Args &&... args)
Emplace insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:129
Forward iterator with const access.
Definition: HashTable.H:1116
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
friend Ostream & operator(Ostream &, const HashTable< T, Key, Hash > &tbl)
T & at(const Key &key)
Find and return a hashed entry. FatalError if it does not exist.
Definition: HashTableI.H:38
bool emplace_set(const Key &key, Args &&... args)
Emplace set an entry, overwriting any existing entries.
Definition: HashTableI.H:141
Forward iterator with non-const access.
Definition: HashTable.H:1024
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:152
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
iterator find(const Key &key)
Find and return an iterator set at the hashed entry.
Definition: HashTableI.H:86
T & operator()(const Key &key)
Return existing entry or create a new entry.
Definition: HashTableI.H:249
A HashTable similar to std::unordered_map.
Definition: HashTable.H:108
const volScalarField & T
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
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:47
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
Foam::argList args(argc, argv)
List< label > toc(const UList< bool > &bools)
Return the (sorted) values corresponding to &#39;true&#39; entries.
Definition: BitOps.C:158
bool set(const Key &key, const T &obj)
Copy assign a new entry, overwriting existing entries.
Definition: HashTableI.H:174
T & operator[](const Key &key)
Find and return a hashed entry. FatalError if it does not exist.
Definition: HashTableI.H:235