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-2023 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  List<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 List<Tuple2<Key, Type>>::size();
52 }
53 
54 
55 template<class Type, class Key, class Hash>
57 {
58  List<Tuple2<Key, Type>>::operator=
59  (
60  Tuple2<Key, Type>(nullKey, Type())
61  );
62 }
63 
64 
65 template<class Type, class Key, class Hash>
67 {
69  (
70  newSize,
71  Tuple2<Key, Type>(nullKey, Type())
72  );
73 }
74 
75 
76 template<class Type, class Key, class Hash>
77 bool Foam::HashList<Type, Key, Hash>::insert(const Key& k, const Type& t)
78 {
79  List<Tuple2<Key, Type>>& map = *this;
80 
81  const label n = map.size();
82 
83  const unsigned h = Hash()(k);
84 
85  for (label i = 0; i < n; i ++)
86  {
87  const label hi = (h + i) % n;
88 
89  if (map[hi].first() == nullKey)
90  {
91  map[hi] = Tuple2<Key, Type>(k, t);
92  return true;
93  }
94 
95  if (map[hi].first() == k)
96  {
97  return false;
98  }
99  }
100 
102  << "Hash list is full"
103  << exit(FatalError);
104 
105  return false;
106 }
107 
108 
109 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
110 
111 template<class Type, class Key, class Hash>
112 const Type& Foam::HashList<Type, Key, Hash>::operator[](const Key& k) const
113 {
114  const List<Tuple2<Key, Type>>& map = *this;
115 
116  const label n = map.size();
117 
118  const unsigned h = Hash()(k);
119 
120  for (label i = 0; i < n; i ++)
121  {
122  const label hi = (h + i) % n;
123 
124  if (map[hi].first() == k)
125  {
126  return map[hi].second();
127  }
128  }
129 
131  << "Hash list does not contain key \"" << k << "\""
132  << exit(FatalError);
133 
134  return NullObjectRef<Type>();
135 }
136 
137 
138 // ************************************************************************* //
label k
label n
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:112
bool insert(const Key &k, const Type &t)
Insert into the hash list. Return true if the value was newly.
Definition: HashList.C:77
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
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:91
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:63
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
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())