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 | Copyright (C) 2011-2013 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 // * * * * * * * * * * * * * Private 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 // Find and return T
92 template<class IDLListType, class T>
94 {
95  return hashedTs_.found(keyword);
96 }
97 
98 
99 // Find and return T*, return NULL if not found
100 template<class IDLListType, class T>
102 (
103  const word& keyword
104 ) const
105 {
106  typename HashTable<T*>::const_iterator iter = hashedTs_.find(keyword);
107 
108  if (iter != hashedTs_.end())
109  {
110  return *iter;
111  }
112  else
113  {
114  return NULL;
115  }
116 }
117 
118 
119 // Find and return T*, return NULL if not found
120 template<class IDLListType, class T>
122 {
123  typename HashTable<T*>::iterator iter = hashedTs_.find(keyword);
124 
125  if (iter != hashedTs_.end())
126  {
127  return *iter;
128  }
129  else
130  {
131  return NULL;
132  }
133 }
134 
135 
136 // Find and return T*, FatalError if keyword not found
137 template<class IDLListType, class T>
139 {
140  typename HashTable<T*>::const_iterator iter = hashedTs_.find(keyword);
141 
142  if (iter == hashedTs_.end())
143  {
145  (
146  "DictionaryBase<IDLListType, T>::lookup(const word&) const"
147  ) << keyword << " is undefined"
148  << exit(FatalError);
149  }
150 
151  return *iter;
152 }
153 
154 
155 // Find and return T*, FatalError if keyword not found
156 template<class IDLListType, class T>
158 {
159  typename HashTable<T*>::iterator iter = hashedTs_.find(keyword);
160 
161  if (iter == hashedTs_.end())
162  {
164  (
165  "DictionaryBase<IDLListType, T>::lookup(const word&)"
166  ) << keyword << " is undefined"
167  << exit(FatalError);
168  }
169 
170  return *iter;
171 }
172 
173 
174 // Return the table of contents
175 template<class IDLListType, class T>
177 {
178  wordList keywords(this->size());
179 
180  label i = 0;
181  for
182  (
183  typename IDLListType::const_iterator iter = this->begin();
184  iter != this->end();
185  ++iter
186  )
187  {
188  keywords[i++] = iter().keyword();
189  }
190 
191  return keywords;
192 }
193 
194 
195 // Add at head of dictionary
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::insert(tPtr);
202 }
203 
204 
205 // Add at tail of dictionary
206 template<class IDLListType, class T>
208 {
209  // NOTE: we should probably check that HashTable::insert actually worked
210  hashedTs_.insert(keyword, tPtr);
211  IDLListType::append(tPtr);
212 }
213 
214 
215 template<class IDLListType, class T>
217 {
218  typename HashTable<T*>::iterator iter = hashedTs_.find(keyword);
219 
220  if (iter != hashedTs_.end())
221  {
222  T* tPtr = IDLListType::remove(iter());
223  hashedTs_.erase(iter);
224  return tPtr;
225  }
226  else
227  {
228  return NULL;
229  }
230 }
231 
232 
233 template<class IDLListType, class T>
235 {
237  hashedTs_.clear();
238 }
239 
240 
241 template<class IDLListType, class T>
243 (
245 )
246 {
247  IDLListType::transfer(dict);
249 }
250 
251 
252 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
253 
254 template<class IDLListType, class T>
255 void Foam::DictionaryBase<IDLListType, T>::operator=
256 (
258 )
259 {
260  // Check for assignment to self
261  if (this == &dict)
262  {
263  FatalErrorIn("DictionaryBase::operator=(const DictionaryBase&)")
264  << "attempted assignment to self"
265  << abort(FatalError);
266  }
267 
268  IDLListType::operator=(dict);
269  this->hashedTs_.clear();
270  this->addEntries();
271 }
272 
273 
274 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
275 
276 #include "DictionaryBaseIO.C"
277 
278 // ************************************************************************* //
void append(const word &, T *)
Add at tail of dictionary.
HashTable< T * > hashedTs_
HashTable of the entries held on the IDLListType for quick lookup.
bool erase(const iterator &)
Erase a hashedEntry specified by given iterator.
Definition: HashTable.C:379
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
An STL-conforming hash table.
Definition: HashTable.H:61
void insert(const word &, T *)
Add at head of dictionary.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A class for handling words, derived from string.
Definition: word.H:59
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
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:49
timeIndices insert(timeIndex, timeDirs[timeI].value())
dictionary dict
const T * lookup(const word &) const
Find and return entry.
void clear()
Clear all entries from table.
Definition: HashTable.C:473
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 * lookupPtr(const word &) const
Find and return an entry if present, otherwise return NULL.
errorManip< error > abort(error &err)
Definition: errorManip.H:131
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
error FatalError
wordList toc() const
Return the table of contents.
bool found(const word &) const
Search DictionaryBase for given keyword.
void transfer(HashTable< T, Key, Hash > &)
Transfer the contents of the argument table into this table.
Definition: HashTable.C:518
void clear()
Clear the dictionary.
static iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:106
Reads the data description and data portions of a DictionaryBase File.
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
Definition: HashTable.C:139
UEqn clear()
bool found(const Key &) const
Return true if hashedEntry is found in table.
Definition: HashTable.C:109
DictionaryBase(const label size=128)
Construct given initial table size.
Base dictionary class templated on both the form of doubly-linked list it uses as well as the type it...