DictionaryBase.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) 2011-2018 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 "DictionaryBase.H"
27 
28 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
29 
30 template<class IDLListType, class T>
32 {
33  for
34  (
35  typename IDLListType::iterator iter = this->begin();
36  iter != this->end();
37  ++iter
38  )
39  {
40  this->hashedTs_.insert((*iter).keyword(), &(*iter));
41  }
42 }
43 
44 
45 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
46 
47 template<class IDLListType, class T>
49 :
50  hashedTs_(size)
51 {}
52 
53 
54 template<class IDLListType, class T>
56 (
57  const DictionaryBase& dict
58 )
59 :
60  IDLListType(dict)
61 {
62  addEntries();
63 }
64 
65 
66 template<class IDLListType, class T>
67 template<class INew>
69 (
70  Istream& is,
71  const INew& iNew
72 )
73 :
74  IDLListType(is, iNew)
75 {
76  addEntries();
77 }
78 
79 
80 template<class IDLListType, class T>
82 :
83  IDLListType(is)
84 {
85  addEntries();
86 }
87 
88 
89 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
90 
91 template<class IDLListType, class T>
93 {
94  return hashedTs_.found(keyword);
95 }
96 
97 
98 template<class IDLListType, class T>
100 (
101  const word& keyword
102 ) const
103 {
104  typename HashTable<T*>::const_iterator iter = hashedTs_.find(keyword);
105 
106  if (iter != hashedTs_.end())
107  {
108  return *iter;
109  }
110  else
111  {
112  return nullptr;
113  }
114 }
115 
116 
117 template<class IDLListType, class T>
119 {
120  typename HashTable<T*>::iterator iter = hashedTs_.find(keyword);
121 
122  if (iter != hashedTs_.end())
123  {
124  return *iter;
125  }
126  else
127  {
128  return nullptr;
129  }
130 }
131 
132 
133 template<class IDLListType, class T>
135 {
136  typename HashTable<T*>::const_iterator iter = hashedTs_.find(keyword);
137 
138  if (iter == hashedTs_.end())
139  {
141  << keyword << " is undefined"
142  << exit(FatalError);
143  }
144 
145  return *iter;
146 }
147 
148 
149 template<class IDLListType, class T>
151 {
152  typename HashTable<T*>::iterator iter = hashedTs_.find(keyword);
153 
154  if (iter == hashedTs_.end())
155  {
157  << keyword << " is undefined"
158  << exit(FatalError);
159  }
160 
161  return *iter;
162 }
163 
164 
165 template<class IDLListType, class T>
167 {
168  wordList keywords(this->size());
169 
170  label i = 0;
171  for
172  (
173  typename IDLListType::const_iterator iter = this->begin();
174  iter != this->end();
175  ++iter
176  )
177  {
178  keywords[i++] = iter().keyword();
179  }
180 
181  return keywords;
182 }
183 
184 
185 template<class IDLListType, class T>
187 {
188  return hashedTs_.sortedToc();
189 }
190 
191 
192 template<class IDLListType, class T>
194 {
195  // NOTE: we should probably check that HashTable::insert actually worked
196  hashedTs_.insert(keyword, tPtr);
197  IDLListType::insert(tPtr);
198 }
199 
200 
201 template<class IDLListType, class T>
203 {
204  // NOTE: we should probably check that HashTable::insert actually worked
205  hashedTs_.insert(keyword, tPtr);
206  IDLListType::append(tPtr);
207 }
208 
209 
210 template<class IDLListType, class T>
212 {
213  typename HashTable<T*>::iterator iter = hashedTs_.find(keyword);
214 
215  if (iter != hashedTs_.end())
216  {
217  T* tPtr = IDLListType::remove(iter());
218  hashedTs_.erase(iter);
219  return tPtr;
220  }
221  else
222  {
223  return nullptr;
224  }
225 }
226 
227 
228 template<class IDLListType, class T>
230 {
232  hashedTs_.clear();
233 }
234 
235 
236 template<class IDLListType, class T>
238 (
240 )
241 {
242  IDLListType::transfer(dict);
243  hashedTs_.transfer(dict.hashedTs_);
244 }
245 
246 
247 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
248 
249 template<class IDLListType, class T>
250 void Foam::DictionaryBase<IDLListType, T>::operator=
251 (
253 )
254 {
255  // Check for assignment to self
256  if (this == &dict)
257  {
259  << "attempted assignment to self"
260  << abort(FatalError);
261  }
262 
263  IDLListType::operator=(dict);
264  this->hashedTs_.clear();
265  this->addEntries();
266 }
267 
268 
269 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
270 
271 #include "DictionaryBaseIO.C"
272 
273 // ************************************************************************* //
dictionary dict
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
bool found(const word &) const
Search DictionaryBase for given keyword.
An STL-conforming const_iterator.
Definition: HashTable.H:481
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Base dictionary class templated on both the form of doubly-linked list it uses as well as the type it...
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
Reads the data description and data portions of a DictionaryBase File.
tUEqn clear()
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
wordList sortedToc() const
Return the table of contents as a sorted list.
const T * lookupPtr(const word &) const
Find and return an entry if present, otherwise return nullptr.
wordList toc() const
Return the table of contents.
void append(const word &, T *)
Add at tail of dictionary.
DictionaryBase(const label size=128)
Construct given initial table size.
void insert(const word &, T *)
Add at head of dictionary.
An STL-conforming iterator.
Definition: HashTable.H:426
A class for handling words, derived from string.
Definition: word.H:59
timeIndices insert(timeIndex, timeDirs[timeI].value())
HashTable< T * > hashedTs_
HashTable of the entries held on the IDLListType for quick lookup.
void clear()
Clear the dictionary.
errorManip< error > abort(error &err)
Definition: errorManip.H:131
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
const volScalarField & T
T * remove(const word &)
Remove and return entry specified by keyword.
void transfer(DictionaryBase< IDLListType, T > &)
Transfer the contents of the argument into this DictionaryBase.
const T * lookup(const word &) const
Find and return entry.
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:49