HashTableDetail.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) 2019-2025 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Class
27  Foam::Detail::HashTablePair
28 
29 Description
30  Internal storage type for HashTable
31 
32 SourceFiles
33  HashTableDetail.H
34 
35 \*---------------------------------------------------------------------------*/
36 
37 #ifndef Foam_HashTableDetail_H
38 #define Foam_HashTableDetail_H
39 
40 #include "zero.H"
41 #include <memory>
42 #include <utility>
43 #include <type_traits>
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 namespace Foam
48 {
49 
50 // Forward Declarations
51 class Ostream;
52 template<class T> class autoPtr;
53 
54 namespace Detail
55 {
56 
57 /*---------------------------------------------------------------------------*\
58  Class isPointerLike Declaration
59 \*---------------------------------------------------------------------------*/
60 
61 //- Pointer-like behaviour
62 template<class T> struct isPointerLike : std::false_type {};
63 
64 //- Pointer-like behaviour for autoPtr
65 template<class T> struct isPointerLike<autoPtr<T>> : std::true_type {};
66 
67 //- Pointer-like behaviour for std::unique_ptr
68 template<class T> struct isPointerLike<std::unique_ptr<T>> : std::true_type {};
69 
70 
71 /*---------------------------------------------------------------------------*\
72  Class HashTablePair Declaration
73 \*---------------------------------------------------------------------------*/
74 
75 //- Internal storage type for HashTable entries
76 // Comprises a (K,V) tuple and a linked-list entry for hash collisions.
77 // Could store key/val as std::pair, but no particular advantage
78 // unless the iterator dereference type changes.
79 template<class K, class V>
80 struct HashTablePair
81 {
82  // Data Types
83 
84  //- Type of key
85  typedef K key_type;
86 
87  //- Type of content
88  typedef V mapped_type;
89 
90  //- This class stores a value
91  static constexpr bool stores_value() noexcept
92  {
93  return true;
94  }
95 
96 
97  // Data
98 
99  //- The lookup key
100  const K key_;
102  //- The mapped data
103  V val_;
104 
105  //- Addressing (next in collision list)
107 
108 
109  // Generated Methods
110 
111  //- No copy construct
112  HashTablePair(const HashTablePair&) = delete;
113 
114  //- No copy assignment
115  void operator=(const HashTablePair&) = delete;
116 
118  // Constructors
119 
120  //- Construct from next pointer, key, contents
121  template<class... Args>
123  (
124  HashTablePair* next,
125  const key_type& key,
126  Args&&... args
127  )
128  :
129  key_(key),
130  val_(std::forward<Args>(args)...),
131  next_(next)
132  {}
133 
134 
135  // Member Functions
136 
137  //- The key
138  const key_type& key() const noexcept
139  {
140  return key_;
141  }
142 
143  //- Const access to the mapped value
144  const mapped_type& cval() const noexcept
145  {
146  return val_;
147  }
148 
149  //- Const access to the mapped value
150  const mapped_type& val() const noexcept
151  {
152  return val_;
153  }
154 
155  //- Non-const access to the mapped value
157  {
158  return val_;
159  }
160 
161 
162  //- Compare keys
163  bool operator<(const HashTablePair& rhs) const
164  {
165  return key_ < rhs.key_;
166  }
167 
168  //- Write (key, val) pair
169  void print(Ostream& os) const
170  {
171  os << key_;
172 
173  if constexpr
174  (
175  std::is_pointer_v<V>
177  )
178  {
179  // Pointer or pointer-like types
180  if (val_)
181  {
182  os << ' ' << *val_;
183  }
184  }
185  else
186  {
187  // Non-pointer types
188  os << ' ' << val_;
189  }
190  }
191 };
192 
193 
194 /*---------------------------------------------------------------------------*\
195  Class HashTableSingle Declaration
196 \*---------------------------------------------------------------------------*/
197 
198 //- Internal storage type for HashSet entries
199 // Comprises a single (K) value and a linked-list entry for hash collisions
200 template<class K>
201 struct HashTableSingle
202 {
203  // Data Types
204 
205  //- Type of key
206  typedef K key_type;
207 
208  //- Type of content (no content: placeholder)
209  typedef Foam::zero mapped_type;
210 
211  //- Content storage type to the entry
212  typedef key_type value_type;
213 
214  //- This class does not store any value
215  static constexpr bool stores_value() noexcept
216  {
217  return false;
218  }
219 
220 
221  // Data
222 
223  //- The lookup key == content
224  const K key_;
225 
226  //- Addressing (next in collision list)
228 
229 
230  // Generated Methods
231 
232  //- No copy construct
233  HashTableSingle(const HashTableSingle&) = delete;
234 
235  //- No copy assignment
236  void operator=(const HashTableSingle&) = delete;
237 
239  // Constructors
240 
241  //- Construct from next pointer, key, (unused) contents
242  template<class... Args>
244  (
246  const key_type& key,
247  Args&&...
248  )
249  :
251  next_(next)
252  {}
253 
254 
255  // Member Functions
256 
257  //- The key
258  const key_type& key() const noexcept
259  {
260  return key_;
261  }
262 
263  //- Const access to the (dummy) mapped value (for API only)
264  const mapped_type& cval() const noexcept
265  {
266  return Foam::zero::dummy;
267  }
268 
269  //- Const access to the (dummy) mapped value (for API only)
270  const mapped_type& val() const noexcept
271  {
272  return Foam::zero::dummy;
273  }
274 
275  //- Non-const access to the (dummy) mapped value (for API only)
277  {
278  return Foam::zero::dummy;
279  }
280 
281 
282  //- Compare keys
283  bool operator<(const HashTableSingle& rhs) const
284  {
285  return key_ < rhs.key_;
286  }
287 
288  //- Write the key. There is no value to write.
289  void print(Ostream& os) const
290  {
291  os << key_;
292  }
293 };
294 
295 
296 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
297 
298 } // End namespace Detail
299 } // End namespace Foam
300 
301 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
302 
303 #endif
304 
305 // ************************************************************************* //
const mapped_type & cval() const noexcept
Const access to the mapped value.
Internal storage type for HashSet entries.
const mapped_type & val() const noexcept
Const access to the mapped value.
HashTableSingle * next_
Addressing (next in collision list)
const mapped_type & cval() const noexcept
Const access to the (dummy) mapped value (for API only)
Pointer-like behaviour.
CGAL::Exact_predicates_exact_constructions_kernel K
HashTableSingle(const HashTableSingle &)=delete
No copy construct.
static constexpr bool stores_value() noexcept
This class stores a value.
void print(Ostream &os) const
Write the key. There is no value to write.
const key_type & key() const noexcept
The key.
V mapped_type
Type of content.
void operator=(const HashTableSingle &)=delete
No copy assignment.
static constexpr bool stores_value() noexcept
This class does not store any value.
static zero dummy
A static instance, when modifiable reference is required by an API.
Definition: zero.H:69
void print(Ostream &os) const
Write (key, val) pair.
HashTablePair * next_
Addressing (next in collision list)
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: scalarImpl.H:255
OBJstream os(runTime.globalPath()/outputName)
Foam::zero mapped_type
Type of content (no content: placeholder)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const K key_
The lookup key == content.
const key_type & key() const noexcept
The key.
bool operator<(const HashTablePair &rhs) const
Compare keys.
const K key_
The lookup key.
Internal storage type for HashTable entries.
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
bool operator<(const HashTableSingle &rhs) const
Compare keys.
key_type value_type
Content storage type to the entry.
const mapped_type & val() const noexcept
Const access to the (dummy) mapped value (for API only)
Foam::argList args(argc, argv)
void operator=(const HashTablePair &)=delete
No copy assignment.
HashTablePair(const HashTablePair &)=delete
No copy construct.
Namespace for OpenFOAM.