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 -------------------------------------------------------------------------------
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 Foam::label Foam::HashTable<T, Key, Hash>::capacity() const noexcept
46 {
47  return capacity_;
48 }
49 
50 
51 template<class T, class Key, class Hash>
52 inline Foam::label Foam::HashTable<T, Key, Hash>::size() const noexcept
53 {
54  return size_;
55 }
56 
57 
58 template<class T, class Key, class Hash>
59 inline bool Foam::HashTable<T, Key, Hash>::empty() const noexcept
60 {
61  return !size_;
62 }
63 
64 
65 template<class T, class Key, class Hash>
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 
82 template<class T, class Key, class Hash>
83 inline 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 
99 template<class T, class Key, class Hash>
100 inline 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 
111 template<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 
127 template<class T, class Key, class Hash>
130 (
131  const Key& key
132 ) const
133 {
134  return this->cfind(key);
135 }
136 
137 
138 template<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 
154 template<class T, class Key, class Hash>
155 template<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 
166 template<class T, class Key, class Hash>
167 template<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 
178 template<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 
189 template<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 
200 template<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 
211 template<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 
222 template<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 
236 template<class T, class Key, class Hash>
238 {
239  return this->at(key);
240 }
241 
242 
243 template<class T, class Key, class Hash>
244 inline const T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key) const
245 {
246  return this->at(key);
247 }
248 
249 
250 template<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 
264 
265 template<class T, class Key, class Hash>
267 (
268  const Key& key,
269  const T& deflt
270 )
271 {
272  iterator iter(this->find(key));
273 
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 // ************************************************************************* //
insert
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
Foam::HashTable::size
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
Foam::HashTable::iterator
Forward iterator with non-const access.
Definition: HashTable.H:693
Foam::HashTable::emplace_set
bool emplace_set(const Key &key, Args &&... args)
Emplace set an entry, overwriting any existing entries.
Definition: HashTableI.H:169
Foam::HashTable::const_iterator
Forward iterator with const access.
Definition: HashTable.H:754
Foam::glTF::key
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
Foam::HashTable::operator[]
T & operator[](const Key &key)
Find and return a hashed entry. FatalError if it does not exist.
Definition: HashTableI.H:237
Foam::HashTable::insert
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:180
Foam::HashTable::emplace
bool emplace(const Key &key, Args &&... args)
Emplace insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:157
error.H
Foam::FatalError
error FatalError
Foam::HashTable::lookup
const T & lookup(const Key &key, const T &deflt) const
Return hashed entry if it exists, or return the given default.
Definition: HashTableI.H:224
Foam::ListOps::find
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Find index of the first occurrence that satisfies the predicate.
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::HashTable::find
iterator find(const Key &key)
Find and return an iterator set at the hashed entry.
Definition: HashTableI.H:114
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::HashTable::capacity
label capacity() const noexcept
The size of the underlying table.
Definition: HashTableI.H:45
Foam::HashTable::cfind
const_iterator cfind(const Key &key) const
Find and return an const_iterator set at the hashed entry.
Definition: HashTableI.H:141
Foam::HashTable::operator()
T & operator()(const Key &key)
Return existing entry or create a new entry.
Definition: HashTableI.H:251
Foam::HashTable::set
bool set(const Key &key, const T &obj)
Copy assign a new entry, overwriting existing entries.
Definition: HashTableI.H:202
Foam::HashTable::empty
bool empty() const noexcept
True if the hash table is empty.
Definition: HashTableI.H:59
Foam::HashTable::found
bool found(const Key &key) const
Return true if hashed entry is found in table.
Definition: HashTableI.H:100
args
Foam::argList args(argc, argv)
Foam::HashTable::operator
friend Ostream & operator(Ostream &, const HashTable< T, Key, Hash > &tbl)
Foam::HashTable::at
T & at(const Key &key)
Find and return a hashed entry. FatalError if it does not exist.
Definition: HashTableI.H:66