HashList.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2022-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "HashList.H"
27 
28 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
29 
30 namespace Foam
31 {
32  template<class Type, class Key, class Hash>
33  const Key HashList<Type, Key, Hash>::nullKey = Key::null;
34 }
35 
36 
37 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
38 
39 template<class Type, class Key, class Hash>
41 :
42  DynamicList<Tuple2<Key, Type>>(size, Tuple2<Key, Type>(nullKey, Type()))
43 {}
44 
45 
46 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
47 
48 template<class Type, class Key, class Hash>
50 {
51  return DynamicList<Tuple2<Key, Type>>::size();
52 }
53 
54 
55 template<class Type, class Key, class Hash>
57 {
59  (
60  Tuple2<Key, Type>(nullKey, Type())
61  );
62 }
63 
64 
65 template<class Type, class Key, class Hash>
67 {
69 
70  clear();
71 }
72 
73 
74 template<class Type, class Key, class Hash>
75 bool Foam::HashList<Type, Key, Hash>::insert(const Key& k, const Type& t)
76 {
77  DynamicList<Tuple2<Key, Type>>& map = *this;
78 
79  const label n = map.size();
80 
81  const unsigned h = Hash()(k);
82 
83  for (label i = 0; i < n; i ++)
84  {
85  const label hi = (h + i) % n;
86 
87  if (map[hi].first() == nullKey)
88  {
89  map[hi] = Tuple2<Key, Type>(k, t);
90  return true;
91  }
92 
93  if (map[hi].first() == k)
94  {
95  return false;
96  }
97  }
98 
100  << "Hash list is full"
101  << exit(FatalError);
102 
103  return false;
104 }
105 
106 
107 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
108 
109 template<class Type, class Key, class Hash>
110 const Type& Foam::HashList<Type, Key, Hash>::operator[](const Key& k) const
111 {
112  const DynamicList<Tuple2<Key, Type>>& map = *this;
113 
114  const label n = map.size();
115 
116  const unsigned h = Hash()(k);
117 
118  for (label i = 0; i < n; i ++)
119  {
120  const label hi = (h + i) % n;
121 
122  if (map[hi].first() == k)
123  {
124  return map[hi].second();
125  }
126  }
127 
129  << "Hash list does not contain key \"" << k << "\""
130  << exit(FatalError);
131 
132  return NullObjectRef<Type>();
133 }
134 
135 
136 // ************************************************************************* //
label k
label n
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:78
HashList(const label size)
Construct given a size.
Definition: HashList.C:40
void resizeAndClear(const label newSize)
Resize and clear all elements.
Definition: HashList.C:66
label capacity() const
The size of the underlying list.
Definition: HashList.C:49
const Type & operator[](const Key &k) const
Retrieve from the hash list.
Definition: HashList.C:110
bool insert(const Key &k, const Type &t)
Insert into the hash list. Return true if the value was newly.
Definition: HashList.C:75
static const Key nullKey
Null key value for unset elements in the list.
Definition: HashList.H:66
void clear()
Clear all elements.
Definition: HashList.C:56
Hash function class for primitives. All non-primitives used to hash entries on hash tables likely nee...
Definition: Hash.H:53
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
A 2-tuple for storing two objects of different types.
Definition: Tuple2.H:66
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
tUEqn clear()
const dimensionedScalar h
Planck constant.
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
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:59
labelList first(const UList< labelPair > &p)
Definition: patchToPatch.C:39
error FatalError
triSurfaceToAgglom resize(surfacesMesh.size())