HashPtrTable.C
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-2020 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 #include "HashPtrTable.H"
31 
32 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 
34 template<class T, class Key, class Hash>
36 (
37  const HashPtrTable<T, Key, Hash>& rhs
38 )
39 :
40  parent_type(rhs.capacity())
41 {
42  for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
43  {
44  const Key& k = iter.key();
45  const T* ptr = iter.val();
46 
47  if (ptr)
48  {
49  this->set(k, new T(*ptr));
50  }
51  else
52  {
53  this->set(k, nullptr);
54  }
55  }
56 }
57 
58 
59 template<class T, class Key, class Hash>
61 (
62  HashPtrTable<T, Key, Hash>&& rhs
63 )
64 :
65  parent_type(std::move(rhs))
66 {}
67 
68 
69 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
70 
71 template<class T, class Key, class Hash>
73 {
74  clear();
75 }
76 
77 
78 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
79 
80 template<class T, class Key, class Hash>
82 {
83  if (iter.good())
84  {
85  autoPtr<T> old(iter.val());
86  this->parent_type::erase(iter);
87  return old;
88  }
89 
90  return nullptr;
91 }
92 
93 
94 template<class T, class Key, class Hash>
96 {
97  iterator iter(this->find(key));
98  return this->remove(iter);
99 }
100 
101 
102 template<class T, class Key, class Hash>
104 {
105  if (iter.good())
106  {
107  T* ptr = iter.val();
108 
109  if (this->parent_type::erase(iter))
110  {
111  delete ptr;
112  return true;
113  }
114  }
115 
116  return false;
117 }
118 
119 
120 template<class T, class Key, class Hash>
122 {
123  iterator iter(this->find(key));
124  return this->erase(iter);
125 }
126 
127 
128 template<class T, class Key, class Hash>
130 {
131  for (iterator iter = this->begin(); iter != this->end(); ++iter)
132  {
133  delete iter.val();
134  }
135 
136  this->parent_type::clear();
137 }
138 
139 
140 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
141 
142 template<class T, class Key, class Hash>
143 void Foam::HashPtrTable<T, Key, Hash>::operator=
144 (
145  const HashPtrTable<T, Key, Hash>& rhs
146 )
147 {
148  if (this == &rhs)
149  {
150  return; // Self-assignment is a no-op
151  }
152 
153  this->clear();
154 
155  for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
156  {
157  const Key& k = iter.key();
158  const T* ptr = iter.val();
159 
160  if (ptr)
161  {
162  this->set(k, new T(*ptr));
163  }
164  else
165  {
166  this->set(k, nullptr);
167  }
168  }
169 }
170 
171 
172 template<class T, class Key, class Hash>
174 (
175  HashPtrTable<T, Key, Hash>&& rhs
176 )
177 {
178  if (this == &rhs)
179  {
180  return; // Self-assignment is a no-op
181  }
182 
183  this->clear();
184  this->transfer(rhs);
185 }
186 
187 
188 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
189 
190 #include "HashPtrTableIO.C"
191 
192 // ************************************************************************* //
Foam::HashPtrTable::erase
bool erase(iterator &iter)
Definition: HashPtrTable.C:103
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
stdFoam::begin
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:97
Foam::HashPtrTable::remove
autoPtr< T > remove(iterator &iter)
Remove entry specified by given iterator.
Definition: HashPtrTable.C:81
Foam::HashPtrTable::~HashPtrTable
~HashPtrTable()
Destructor.
Definition: HashPtrTable.C:72
erase
srcOptions erase("case")
Foam::HashPtrTable::clear
void clear()
Clear all entries from table and delete any allocated pointers.
Definition: HashPtrTable.C:129
error.H
HashPtrTableIO.C
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::HashPtrTable::HashPtrTable
HashPtrTable()
Default construct with default table capacity.
Definition: HashPtrTableI.H:33
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
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::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
clear
patchWriters clear()
Foam::HashPtrTable
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
Definition: HashPtrTable.H:54
HashPtrTable.H
k
label k
Boltzmann constant.
Definition: LISASMDCalcMethod2.H:41
Foam::HashPtrTable< exprResult >::iterator
typename parent_type::iterator iterator
Definition: HashPtrTable.H:88