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-2023 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 - for pointer types
169  template<class TypeT = V>
170  typename std::enable_if
171  <
172  (
173  std::is_pointer<TypeT>::value
175  ),
176  void
177  >::type
178  print(Ostream& os) const
179  {
180  os << key_;
181 
182  if (val_)
183  {
184  os << ' ' << *val_;
185  }
186  }
187 
188  //- Write (key, val) pair - for non-pointer types
189  template<class TypeT = V>
190  typename std::enable_if
191  <
192  (
193  !std::is_pointer<TypeT>::value
195  ),
196  void
197  >::type
198  print(Ostream& os) const
199  {
200  os << key_ << ' ' << val_;
201  }
202 };
203 
204 
205 /*---------------------------------------------------------------------------*\
206  Class HashTableSingle Declaration
207 \*---------------------------------------------------------------------------*/
208 
209 //- Internal storage type for HashSet entries
210 // Comprises a single (K) value and a linked-list entry for hash collisions
211 template<class K>
213 {
214  // Data Types
215 
216  //- Type of key
217  typedef K key_type;
218 
219  //- Type of content (no content: placeholder)
220  typedef Foam::zero mapped_type;
221 
222  //- Content storage type to the entry
223  typedef key_type value_type;
224 
225  //- This class does not store any value
226  static constexpr bool stores_value() noexcept
227  {
228  return false;
229  }
230 
231 
232  // Data
233 
234  //- The lookup key == content
235  const K key_;
236 
237  //- Addressing (next in collision list)
239 
240 
241  // Generated Methods
242 
243  //- No copy construct
244  HashTableSingle(const HashTableSingle&) = delete;
245 
246  //- No copy assignment
247  void operator=(const HashTableSingle&) = delete;
248 
249 
250  // Constructors
252  //- Construct from next pointer, key, (unused) contents
253  template<class... Args>
255  (
256  HashTableSingle* next,
257  const key_type& key,
258  Args&&...
259  )
260  :
261  key_(key),
262  next_(next)
263  {}
264 
265 
266  // Member Functions
267 
268  //- The key
269  const key_type& key() const noexcept
270  {
271  return key_;
272  }
274  //- Const access to the (dummy) mapped value (for API only)
275  const mapped_type& cval() const noexcept
276  {
277  return Foam::zero::dummy;
278  }
279 
280  //- Const access to the (dummy) mapped value (for API only)
281  const mapped_type& val() const noexcept
282  {
283  return Foam::zero::dummy;
284  }
285 
286  //- Non-const access to the (dummy) mapped value (for API only)
288  {
290  }
291 
292 
293  //- Compare keys
294  bool operator<(const HashTableSingle& rhs) const
295  {
296  return key_ < rhs.key_;
297  }
298 
299  //- Write the key. There is no value to write.
300  void print(Ostream& os) const
301  {
302  os << key_;
303  }
304 };
305 
306 
307 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
308 
309 } // End namespace Detail
310 } // End namespace Foam
311 
312 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
313 
314 #endif
315 
316 // ************************************************************************* //
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.
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
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
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: Scalar.H:258
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.
std::enable_if<(std::is_pointer< TypeT >::value||Detail::isPointerLike< TypeT >::value), void >::type print(Ostream &os) const
Write (key, val) pair - for pointer types.
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.