DictionaryBase.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) 2019-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 "DictionaryBase.H"
30 
31 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
32 
33 template<class IDLListType, class T>
35 {
36  for (auto iter = this->begin(); iter != this->end(); ++iter)
37  {
38  this->hashedTs_.insert((*iter).keyword(), &(*iter));
39  }
40 }
41 
42 
43 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
44 
45 template<class IDLListType, class T>
47 :
48  hashedTs_(size)
49 {}
50 
51 
52 template<class IDLListType, class T>
54 (
55  const DictionaryBase& dict
56 )
57 :
58  IDLListType(dict)
59 {
60  addEntries();
61 }
62 
63 
64 template<class IDLListType, class T>
65 template<class INew>
67 (
68  Istream& is,
69  const INew& iNew
70 )
71 :
72  IDLListType(is, iNew)
73 {
74  addEntries();
75 }
76 
77 
78 template<class IDLListType, class T>
80 :
81  IDLListType(is)
82 {
83  addEntries();
84 }
85 
86 
87 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
88 
89 template<class IDLListType, class T>
91 {
92  return hashedTs_.found(keyword);
93 }
94 
95 
96 template<class IDLListType, class T>
98 (
99  const word& keyword
100 ) const
101 {
102  const auto iter = hashedTs_.cfind(keyword);
103 
104  if (iter.found())
105  {
106  return *iter;
107  }
108 
109  return nullptr;
110 }
111 
112 
113 template<class IDLListType, class T>
115 {
116  auto iter = hashedTs_.find(keyword);
117 
118  if (iter.found())
119  {
120  return *iter;
121  }
122 
123  return nullptr;
124 }
125 
126 
127 template<class IDLListType, class T>
129 {
130  const auto iter = hashedTs_.cfind(keyword);
131 
132  if (!iter.found())
133  {
135  << "'" << keyword << "' not found"
136  << exit(FatalError);
137  }
138 
139  return *iter;
140 }
141 
142 
143 template<class IDLListType, class T>
145 {
146  auto iter = hashedTs_.find(keyword);
147 
148  if (!iter.found())
149  {
151  << "'" << keyword << "' not found"
152  << exit(FatalError);
153  }
154 
155  return *iter;
156 }
157 
158 
159 template<class IDLListType, class T>
161 {
162  // Cannot rely on the items themselves having a keyword() method
163  // so simply return the toc() from the hashed entries
164  // Make it sorted, since anything else would have no meaning.
165  return hashedTs_.sortedToc();
166 }
167 
168 
169 template<class IDLListType, class T>
171 {
172  return hashedTs_.sortedToc();
173 }
174 
175 
176 template<class IDLListType, class T>
177 template<class Compare>
179 (
180  const Compare& comp
181 ) const
182 {
183  return hashedTs_.sortedToc(comp);
184 }
185 
186 
187 template<class IDLListType, class T>
189 {
190  // NOTE: we should probably check that HashTable::insert actually worked
191  hashedTs_.insert(keyword, tPtr);
192  IDLListType::insert(tPtr);
193 }
194 
195 
196 template<class IDLListType, class T>
198 {
199  // NOTE: we should probably check that HashTable::insert actually worked
200  hashedTs_.insert(keyword, tPtr);
201  IDLListType::append(tPtr);
202 }
203 
204 
205 template<class IDLListType, class T>
207 {
208  auto iter = hashedTs_.find(keyword);
209 
210  if (iter.found())
211  {
212  T* ptr = IDLListType::remove(iter());
213  hashedTs_.erase(iter);
214  return ptr;
215  }
216 
217  return nullptr;
218 }
219 
220 
221 template<class IDLListType, class T>
223 {
225  hashedTs_.clear();
226 }
227 
228 
229 template<class IDLListType, class T>
231 (
233 )
234 {
235  if (this == &dict)
236  {
237  return; // Self-assignment is a no-op
238  }
239 
240  IDLListType::transfer(dict);
241  hashedTs_.transfer(dict.hashedTs_);
242 }
243 
244 
245 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
246 
247 template<class IDLListType, class T>
249 (
251 )
252 {
253  if (this == &dict)
254  {
255  return; // Self-assignment is a no-op
256  }
257 
258  IDLListType::operator=(dict);
259  this->hashedTs_.clear();
260  this->addEntries();
261 }
262 
263 
264 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
265 
266 #include "DictionaryBaseIO.C"
267 
268 // ************************************************************************* //
insert
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
stdFoam::begin
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:97
Foam::DictionaryBase::toc
wordList toc() const
Return the table of contents (as a sorted list)
Definition: DictionaryBase.C:160
Foam::DictionaryBase::transfer
void transfer(DictionaryBase< IDLListType, T > &dict)
Transfer the contents of the argument into this DictionaryBase.
Definition: DictionaryBase.C:231
Foam::DictionaryBase::addEntries
void addEntries()
Definition: DictionaryBase.C:34
append
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
Foam::DictionaryBase::sortedToc
wordList sortedToc() const
Return the table of contents as a sorted list.
Definition: DictionaryBase.C:170
Foam::INew
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:51
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::DictionaryBase::DictionaryBase
DictionaryBase(const label size=128)
Construct with given or default (128) table capacity.
Definition: DictionaryBase.C:46
Foam::DictionaryBase::remove
T * remove(const word &keyword)
Remove and return entry specified by keyword.
Definition: DictionaryBase.C:206
Foam::DictionaryBase::found
bool found(const word &keyword) const
Search for given keyword.
Definition: DictionaryBase.C:90
Foam::DictionaryBase::append
void append(const word &keyword, T *)
Add at tail of dictionary.
Definition: DictionaryBase.C:197
Foam::DictionaryBase::lookup
const T * lookup(const word &keyword) const
Find and return entry, FatalError on failure.
Definition: DictionaryBase.C:128
dict
dictionary dict
Definition: searchingEngine.H:14
DictionaryBase.H
Foam::FatalError
error FatalError
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::DictionaryBase::find
T * find(const word &keyword)
Find and return an entry, nullptr on failure.
Definition: DictionaryBase.C:114
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::DictionaryBase::insert
void insert(const word &keyword, T *)
Add at head of dictionary.
Definition: DictionaryBase.C:188
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
DictionaryBaseIO.C
Foam::DictionaryBase::clear
void clear()
Clear the dictionary.
Definition: DictionaryBase.C:222
clear
patchWriters clear()
Foam::List< word >
Foam::DictionaryBase
Base dictionary class templated on both the form of doubly-linked list it uses as well as the type it...
Definition: DictionaryBase.H:64
Foam::DictionaryBase::cfind
const T * cfind(const word &keyword) const
Find and return an entry, nullptr on failure.
Definition: DictionaryBase.C:98