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-2019 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>
46 {
47  return capacity_;
48 }
49 
50 
51 template<class T, class Key, class Hash>
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>
66 inline 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 
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>
168 (
169  const Key& key,
170  const T& obj
171 )
172 {
173  return this->setEntry(false, key, obj);
174 }
175 
176 
177 template<class T, class Key, class Hash>
179 (
180  const Key& key,
181  T&& obj
182 )
183 {
184  return this->setEntry(false, key, std::forward<T>(obj));
185 }
186 
187 
188 template<class T, class Key, class Hash>
190 (
191  const Key& key,
192  const T& obj
193 )
194 {
195  return this->setEntry(true, key, obj); // Overwrite
196 }
197 
198 
199 template<class T, class Key, class Hash>
201 (
202  const Key& key,
203  T&& obj
204 )
205 {
206  return this->setEntry(true, key, std::forward<T>(obj)); // Overwrite
207 }
208 
209 
210 template<class T, class Key, class Hash>
212 (
213  const Key& key,
214  const T& deflt
215 ) const
216 {
217  const const_iterator iter(this->cfind(key));
218  return iter.good() ? iter.val() : deflt;
219 }
220 
221 
222 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
223 
224 template<class T, class Key, class Hash>
226 {
227  return this->at(key);
228 }
229 
230 
231 template<class T, class Key, class Hash>
232 inline const T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key) const
233 {
234  return this->at(key);
235 }
236 
237 
238 template<class T, class Key, class Hash>
240 {
241  iterator iter(this->find(key));
242 
243  if (iter.good())
244  {
245  return iter.val();
246  }
247 
248  this->emplace(key);
249  return find(key).val();
250 }
251 
252 
253 template<class T, class Key, class Hash>
255 (
256  const Key& key,
257  const T& deflt
258 )
259 {
260  iterator iter(this->find(key));
261 
262  if (iter.good())
263  {
264  return iter.val();
265  }
266 
267  this->insert(key, deflt);
268  return find(key).val();
269 }
270 
271 
272 // ************************************************************************* //
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:668
Foam::HashTable::const_iterator
Forward iterator with const access.
Definition: HashTable.H:726
Foam::HashTable::operator[]
T & operator[](const Key &key)
Find and return a hashed entry. FatalError if it does not exist.
Definition: HashTableI.H:225
Foam::HashTable::insert
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:168
Foam::HashTable::emplace
bool emplace(const Key &key, Args &&... args)
Emplace insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:157
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
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:212
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:355
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:239
Foam::HashTable::set
bool set(const Key &key, const T &obj)
Copy assign a new entry, overwriting existing entries.
Definition: HashTableI.H:190
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::at
T & at(const Key &key)
Find and return a hashed entry. FatalError if it does not exist.
Definition: HashTableI.H:66