Loading...
Searching...
No Matches
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-2023 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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 "HashPtrTable.H"
30#include "autoPtr.H"
31#include "error.H"
32
33// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
34
35template<class T, class Key, class Hash>
37(
38 const HashPtrTable<T, Key, Hash>& rhs
39)
40:
41 parent_type(rhs.capacity())
42{
43 for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
44 {
45 const Key& k = iter.key();
46 const T* ptr = iter.val();
47
48 if (ptr)
49 {
50 this->set(k, new T(*ptr));
51 }
52 else
53 {
54 this->set(k, nullptr);
55 }
56 }
57}
58
59
60// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
61
62template<class T, class Key, class Hash>
64{
66}
67
68
69// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
70
71template<class T, class Key, class Hash>
73{
74 if (iter.good())
75 {
76 autoPtr<T> old(iter.val());
77 iter.val() = nullptr;
78 return old;
79 }
80
81 return nullptr;
82}
83
84
85template<class T, class Key, class Hash>
88 iterator iter(this->find(key));
89 return this->release(iter);
90}
91
92
93template<class T, class Key, class Hash>
95{
96 if (iter.good())
97 {
98 autoPtr<T> old(iter.val());
99 this->parent_type::erase(iter);
100 return old;
102
103 return nullptr;
104}
105
106
107template<class T, class Key, class Hash>
110 iterator iter(this->find(key));
111 return this->remove(iter);
112}
113
114
115template<class T, class Key, class Hash>
117{
118 if (iter.good())
119 {
120 T* ptr = iter.val();
121
122 if (this->parent_type::erase(iter))
123 {
124 delete ptr;
125 return true;
126 }
128
129 return false;
130}
131
132
133template<class T, class Key, class Hash>
136 iterator iter(this->find(key));
137 return this->erase(iter);
138}
139
140
141template<class T, class Key, class Hash>
143{
144 for (iterator iter = this->begin(); iter != this->end(); ++iter)
145 {
146 delete iter.val();
148
149 this->parent_type::clear();
150}
151
152
153template<class T, class Key, class Hash>
155(
156 HashPtrTable<T, Key, Hash>& source
157)
158{
159 // Use parent merge (of raw pointer entries)
160 // and by-pass any pointer deletions etc.
161 parent_type::merge(static_cast<parent_type&>(source));
162}
163
164
165template<class T, class Key, class Hash>
167(
169)
170{
171 // Use parent merge (of raw pointer entries)
172 // and by-pass any pointer deletions etc.
173 parent_type::merge(static_cast<parent_type&>(source));
174}
175
176
177// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
178
179template<class T, class Key, class Hash>
181(
182 const HashPtrTable<T, Key, Hash>& rhs
183)
184{
185 if (this == &rhs)
186 {
187 return; // Self-assignment is a no-op
188 }
189
190 this->clear();
191
192 for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
193 {
194 const Key& k = iter.key();
195 const T* ptr = iter.val();
196
197 if (ptr)
198 {
199 this->set(k, new T(*ptr));
200 }
201 else
202 {
203 this->set(k, nullptr);
204 }
205 }
206}
207
208
209template<class T, class Key, class Hash>
210void Foam::HashPtrTable<T, Key, Hash>::operator=
211(
213)
214{
215 if (this == &rhs)
216 {
217 return; // Self-assignment is a no-op
218 }
219
220 this->clear();
221 this->transfer(rhs);
222}
223
224
225// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
226
227#include "HashPtrTableIO.C"
228
229// ************************************************************************* //
label k
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
typename parent_type::iterator iterator
constexpr HashPtrTable() noexcept=default
Default construct: empty without allocation (capacity=0).
autoPtr< T > remove(iterator &iter)
Remove entry specified by given iterator.
void merge(HashPtrTable< T, Key, Hash > &source)
Attempts to extract entries from source parameter and insert them into this, does not overwrite exist...
bool erase(iterator &iter)
Erase entry specified by given iterator and delete the allocated pointer.
bool set(const word &key, IOobject *ptr)
typename parent_type::const_iterator const_iterator
void clear()
Clear all entries from table and delete any allocated pointers.
autoPtr< T > release(iterator &iter)
Release ownership of the pointer and replace with a nullptr.
HashTable< T *, Key, Hash > parent_type
The template instance used for the parent HashTable.
~HashPtrTable()
Destructor.
iterator begin()
iterator set to the beginning of the HashTable
void merge(HashTable< T, Key, Hash > &source)
void transfer(HashTable< T, Key, Hash > &rhs)
Transfer contents into this table.
Definition HashTable.C:794
friend Ostream & operator(Ostream &, const HashTable< T, Key, Hash > &tbl)
iterator find(const Key &key)
Find and return an iterator set at the hashed entry.
Definition HashTableI.H:86
bool erase(const iterator &iter)
iterator end() noexcept
iterator to signal the end (for any HashTable)
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
const volScalarField & T
surface1 clear()
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
srcOptions erase("case")