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-2015 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>
138 const T* Foam::DictionaryBase<IDLListType, T>::lookup(const word& keyword) const
139 {
140  typename HashTable<T*>::const_iterator iter = hashedTs_.find(keyword);
141 
142  if (iter == hashedTs_.end())
143  {
145  << keyword << " is undefined"
146  << exit(FatalError);
147  }
148 
149  return *iter;
150 }
151 
152 
153 // Find and return T*, FatalError if keyword not found
154 template<class IDLListType, class T>
156 {
157  typename HashTable<T*>::iterator iter = hashedTs_.find(keyword);
158 
159  if (iter == hashedTs_.end())
160  {
162  << keyword << " is undefined"
163  << exit(FatalError);
164  }
165 
166  return *iter;
167 }
168 
169 
170 // Return the table of contents
171 template<class IDLListType, class T>
173 {
174  wordList keywords(this->size());
175 
176  label i = 0;
177  for
178  (
179  typename IDLListType::const_iterator iter = this->begin();
180  iter != this->end();
181  ++iter
182  )
183  {
184  keywords[i++] = iter().keyword();
185  }
186 
187  return keywords;
188 }
189 
190 
191 // Add at head of dictionary
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 // Add at tail of dictionary
202 template<class IDLListType, class T>
204 {
205  // NOTE: we should probably check that HashTable::insert actually worked
206  hashedTs_.insert(keyword, tPtr);
207  IDLListType::append(tPtr);
208 }
209 
210 
211 template<class IDLListType, class T>
213 {
214  typename HashTable<T*>::iterator iter = hashedTs_.find(keyword);
215 
216  if (iter != hashedTs_.end())
217  {
218  T* tPtr = IDLListType::remove(iter());
219  hashedTs_.erase(iter);
220  return tPtr;
221  }
222  else
223  {
224  return NULL;
225  }
226 }
227 
228 
229 template<class IDLListType, class T>
231 {
233  hashedTs_.clear();
234 }
235 
236 
237 template<class IDLListType, class T>
239 (
241 )
242 {
243  IDLListType::transfer(dict);
244  hashedTs_.transfer(dict.hashedTs_);
245 }
246 
247 
248 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
249 
250 template<class IDLListType, class T>
252 (
254 )
255 {
256  // Check for assignment to self
257  if (this == &dict)
258  {
260  << "attempted assignment to self"
261  << abort(FatalError);
262  }
263 
264  IDLListType::operator=(dict);
265  this->hashedTs_.clear();
266  this->addEntries();
267 }
268 
269 
270 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
271 
272 #include "DictionaryBaseIO.C"
273 
274 // ************************************************************************* //
Foam::HashTable< T * >::iterator
friend class iterator
Declare friendship with the iterator.
Definition: HashTable.H:189
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::DictionaryBase::toc
wordList toc() const
Return the table of contents.
Definition: DictionaryBase.C:172
clear
UEqn clear()
Foam::DictionaryBase::lookupPtr
const T * lookupPtr(const word &) const
Find and return an entry if present, otherwise return NULL.
Foam::DictionaryBase::addEntries
void addEntries()
Definition: DictionaryBase.C:31
Foam::HashTable< T * >::const_iterator
friend class const_iterator
Declare friendship with the const_iterator.
Definition: HashTable.H:192
Foam::DictionaryBase::append
void append(const word &, T *)
Add at tail of dictionary.
Definition: DictionaryBase.C:203
Foam::INew
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:49
Foam::DictionaryBase::insert
void insert(const word &, T *)
Add at head of dictionary.
Definition: DictionaryBase.C:193
Foam::label
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
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::DictionaryBase::DictionaryBase
DictionaryBase(const label size=128)
Construct given initial table size.
Foam::DictionaryBase::remove
T * remove(const word &)
Remove and return entry specified by keyword.
Definition: DictionaryBase.C:212
dict
dictionary dict
Definition: searchingEngine.H:14
DictionaryBase.H
Foam::FatalError
error FatalError
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::HashTable::find
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::HashTable
An STL-conforming hash table.
Definition: HashTable.H:61
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
DictionaryBaseIO.C
Reads the data description and data portions of a DictionaryBase File.
T
const volScalarField & T
Definition: createFields.H:25
Foam::DictionaryBase::clear
void clear()
Clear the dictionary.
Definition: DictionaryBase.C:230
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
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:61
Foam::DictionaryBase::lookup
const T * lookup(const word &) const
Find and return entry.
Foam::DictionaryBase::found
bool found(const word &) const
Search DictionaryBase for given keyword.
Definition: DictionaryBase.C:93
Foam::DictionaryBase::transfer
void transfer(DictionaryBase< IDLListType, T > &)
Transfer the contents of the argument into this DictionaryBase.
Definition: DictionaryBase.C:239
insert
timeIndices insert(timeIndex, timeDirs[timeI].value())