Loading...
Searching...
No Matches
DictionaryBase.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) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2020-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
27Class
28 Foam::DictionaryBase
29
30Description
31 Base dictionary class templated on both the form of doubly-linked list
32 it uses as well as the type it holds.
33
34 The double templating allows for the instantiation of forms with or
35 without storage management.
36
37Note
38 The IDLListType parameter should itself be a template but this confused
39 gcc 2.95.2 so it has to be instantiated for T when an instantiation of
40 DictionaryBase is requested
41
42See also
43 Dictionary and UDictionary
44
45SourceFiles
46 DictionaryBase.C
47 DictionaryBaseIO.C
48
49\*---------------------------------------------------------------------------*/
50
51#ifndef Foam_DictionaryBase_H
52#define Foam_DictionaryBase_H
53
54#include "HashTable.H"
55#include "wordList.H"
56
57// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
58
59namespace Foam
60{
61
62// Forward Declarations
64template<class IDLListType, class T>
65class DictionaryBase;
66
67template<class IDLListType, class T>
69
70
71/*---------------------------------------------------------------------------*\
72 Class DictionaryBase Declaration
73\*---------------------------------------------------------------------------*/
74
75template<class IDLListType, class T>
77:
78 public IDLListType
79{
80protected:
81
82 // Protected Data
83
84 //- HashTable of the entries held on the IDLListType for quick lookup
86
87
88 // Protected Member Functions
89
90 //- Add an entry to the HashTable
91 bool addHashEntry(const word& key, T* ptr)
92 {
93 return hashedTs_.insert(key, ptr);
94 }
95
96 //- Add the IDLListType entries into the HashTable
97 void addEntries();
99
100public:
101
102 // Constructors
103
104 //- Default construct: empty without allocation (capacity=0).
105 DictionaryBase() = default;
106
107 //- Construct empty with initial table capacity
108 explicit DictionaryBase(const label initialCapacity)
109 :
110 hashedTs_(initialCapacity)
111 {}
112
113 //- Copy construct
115
116 //- Construct from Istream using given Istream constructor class
117 template<class INew>
118 DictionaryBase(Istream& is, const INew& inew);
119
120 //- Construct from Istream using default Istream constructor class
122
123
124 // Member Functions
125
126 // Search and lookup
128 //- Search for given keyword
129 bool contains(const word& keyword) const;
130
131 //- Find and return an entry, nullptr on failure.
132 const T* cfind(const word& keyword) const;
133
134 //- Find and return an entry, nullptr on failure.
135 T* find(const word& keyword);
136
137 //- Find and return entry, FatalError on failure.
138 const T* lookup(const word& keyword) const;
139
140 //- Find and return entry, FatalError on failure.
141 T* lookup(const word& keyword);
143 //- The table of contents (as a sorted list)
144 wordList toc() const
145 {
146 return hashedTs_.sortedToc();
148
149 //- The table of contents as a sorted list
150 wordList sortedToc() const
151 {
152 return hashedTs_.sortedToc();
153 }
154
155 //- The table of contents sorted using the specified comparator
156 template<class Compare>
157 wordList sortedToc(const Compare& comp) const
158 {
159 return hashedTs_.sortedToc(comp);
160 }
161
163 // Editing
164
165 //- Add to front of dictionary
166 void push_front(const word& keyword, T* ptr);
168 //- Add to back of dictionary
169 void push_back(const word& keyword, T* ptr);
170
171 //- Remove and return entry specified by keyword.
172 // Return nullptr if the keyword was not found.
173 T* remove(const word& keyword);
174
175 //- Clear the dictionary
176 void clear();
177
178 //- Transfer the contents of the argument into this DictionaryBase
179 // and annul the argument.
181
182
183 // Member Operators
185 //- Copy assignment
186 void operator=(const DictionaryBase&);
187
188 //- Find and return entry
189 const T* operator[](const word& key) const
190 {
191 return lookup(key);
192 }
193
194 //- Find and return entry
195 T* operator[](const word& key)
196 {
197 return lookup(key);
198 }
199
201 // Ostream Operator
202
204 (
205 Ostream&,
207 );
208
209
210 // Housekeeping
211
212 //- Same as contains()
213 bool found(const word& key) const { return this->contains(key); }
214
215 //- Deprecated(2020-03) use cfind()
216 // \deprecated(2020-03) - use cfind() method
217 FOAM_DEPRECATED_FOR(2020-03, "cfind() method")
218 const T* lookupPtr(const word& k) const { return this->cfind(k); }
220 //- Deprecated(2020-03) use find()
221 // \deprecated(2020-03) - use find() method
222 FOAM_DEPRECATED_FOR(2020-03, "find() method")
223 T* lookupPtr(const word& k) { return this->find(k); }
224
225 //- Add to front of dictionary
226 //FOAM_DEPRECATED_FOR(2022-10, "push_front()")
227 void insert(const word& k, T* ptr) { this->push_front(k, ptr); }
228
229 //- Add to front of dictionary
230 //FOAM_DEPRECATED_FOR(2022-10, "push_front()")
231 void prepend(const word& k, T* ptr) { this->push_front(k, ptr); }
233 //- Add to back of dictionary
234 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
235 void append(const word& k, T* ptr) { this->push_back(k, ptr); }
236};
237
238
239// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
241} // End namespace Foam
242
243// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
244
245#ifdef NoRepository
246 #include "DictionaryBase.C"
247 #include "DictionaryBaseIO.C"
248#endif
249
250// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
251
252#endif
253
254// ************************************************************************* //
label k
bool found
Base dictionary class templated on both the form of doubly-linked list it uses as well as the type it...
void prepend(const word &k, T *ptr)
Add to front of dictionary.
T * operator[](const word &key)
Find and return entry.
void addEntries()
Add the IDLListType entries into the HashTable.
void append(const word &k, T *ptr)
Add to back of dictionary.
bool contains(const word &keyword) const
Search for given keyword.
DictionaryBase()=default
Default construct: empty without allocation (capacity=0).
void transfer(DictionaryBase< IDLListType, T > &dict)
Transfer the contents of the argument into this DictionaryBase.
const T * lookup(const word &keyword) const
Find and return entry, FatalError on failure.
wordList toc() const
The table of contents (as a sorted list).
void push_front(const word &keyword, T *ptr)
Add to front of dictionary.
wordList sortedToc() const
The table of contents as a sorted list.
const T * lookupPtr(const word &k) const
DictionaryBase(const DictionaryBase &dict)
Copy construct.
void insert(const word &k, T *ptr)
Add to front of dictionary.
void operator=(const DictionaryBase &)
Copy assignment.
void push_back(const word &keyword, T *ptr)
Add to back of dictionary.
T * find(const word &keyword)
Find and return an entry, nullptr on failure.
bool addHashEntry(const word &key, T *ptr)
Add an entry to the HashTable.
T * remove(const word &keyword)
Remove and return entry specified by keyword.
wordList sortedToc(const Compare &comp) const
The table of contents sorted using the specified comparator.
DictionaryBase(const label initialCapacity)
Construct empty with initial table capacity.
DictionaryBase(Istream &is, const INew &inew)
Construct from Istream using given Istream constructor class.
void clear()
Clear the dictionary.
bool found(const word &key) const
Same as contains().
const T * cfind(const word &keyword) const
Find and return an entry, nullptr on failure.
const T * operator[](const word &key) const
Find and return entry.
T * lookup(const word &keyword)
Find and return entry, FatalError on failure.
friend Ostream & operator(Ostream &, const DictionaryBase< IDLListType, T > &)
DictionaryBase(Istream &is)
Construct from Istream using default Istream constructor class.
A HashTable similar to std::unordered_map.
Definition HashTable.H:124
A helper class when constructing from an Istream or dictionary.
Definition INew.H:47
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A class for handling words, derived from Foam::string.
Definition word.H:66
Namespace for OpenFOAM.
List< word > wordList
List of word.
Definition fileName.H:60
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
dictionary dict
nonInt insert("surfaceSum(((S|magSf)*S)")
#define FOAM_DEPRECATED_FOR(since, replacement)
Definition stdFoam.H:43