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 -------------------------------------------------------------------------------
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 Class
28  Foam::DictionaryBase
29 
30 Description
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 
37 Note
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 
42 See also
43  Dictionary and UDictionary
44 
45 SourceFiles
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 
59 namespace Foam
60 {
61 
62 // Forward Declarations
63 
64 template<class IDLListType, class T>
65 class DictionaryBase;
66 
67 template<class IDLListType, class T>
68 Ostream& operator<<(Ostream&, const DictionaryBase<IDLListType, T>&);
69 
70 
71 /*---------------------------------------------------------------------------*\
72  Class DictionaryBase Declaration
73 \*---------------------------------------------------------------------------*/
74 
75 template<class IDLListType, class T>
76 class DictionaryBase
77 :
78  public IDLListType
79 {
80 protected:
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 the IDLListType entries into the HashTable
91  void addEntries();
92 
93 
94 public:
95 
96  // Constructors
97 
98  //- Construct with given or default (128) table capacity
99  explicit DictionaryBase(const label size = 128);
100 
101  //- Copy construct
103 
104  //- Construct from Istream using given Istream constructor class
105  template<class INew>
106  DictionaryBase(Istream& is, const INew& inew);
107 
108  //- Construct from Istream using default Istream constructor class
109  DictionaryBase(Istream& is);
110 
111 
112  // Member Functions
113 
114  // Search and lookup
115 
116  //- Search for given keyword
117  bool contains(const word& keyword) const;
118 
119  //- Find and return an entry, nullptr on failure.
120  const T* cfind(const word& keyword) const;
121 
122  //- Find and return an entry, nullptr on failure.
123  T* find(const word& keyword);
124 
125  //- Find and return entry, FatalError on failure.
126  const T* lookup(const word& keyword) const;
127 
128  //- Find and return entry, FatalError on failure.
129  T* lookup(const word& keyword);
130 
131  //- Return the table of contents (as a sorted list)
132  wordList toc() const;
133 
134  //- Return the table of contents as a sorted list
135  wordList sortedToc() const;
136 
137  //- Return table of contents sorted using the specified comparator
138  template<class Compare>
139  wordList sortedToc(const Compare& comp) const;
140 
141 
142  // Editing
143 
144  //- Add to front of dictionary
145  void push_front(const word& keyword, T* ptr);
146 
147  //- Add to back of dictionary
148  void push_back(const word& keyword, T* ptr);
149 
150  //- Remove and return entry specified by keyword.
151  // Return nullptr if the keyword was not found.
152  T* remove(const word& keyword);
153 
154  //- Clear the dictionary
155  void clear();
156 
157  //- Transfer the contents of the argument into this DictionaryBase
158  // and annul the argument.
160 
161 
162  // Member Operators
163 
164  //- Copy assignment
165  void operator=(const DictionaryBase&);
166 
167  //- Find and return entry
168  const T* operator[](const word& key) const
169  {
170  return lookup(key);
171  }
172 
173  //- Find and return entry
174  T* operator[](const word& key)
175  {
176  return lookup(key);
177  }
178 
179 
180  // Ostream Operator
181 
182  friend Ostream& operator<< <IDLListType, T>
183  (
184  Ostream&,
185  const DictionaryBase<IDLListType, T>&
186  );
187 
188 
189  // Housekeeping
190 
191  //- Same as contains()
192  bool found(const word& key) const { return this->contains(key); }
193 
194  //- Deprecated(2020-03) use cfind()
195  // \deprecated(2020-03) - use cfind() method
196  FOAM_DEPRECATED_FOR(2020-03, "cfind() method")
197  const T* lookupPtr(const word& keyword) const
198  {
199  return this->cfind(keyword);
200  }
201 
202  //- Deprecated(2020-03) use find()
203  // \deprecated(2020-03) - use find() method
204  FOAM_DEPRECATED_FOR(2020-03, "find() method")
205  T* lookupPtr(const word& keyword)
206  {
207  return this->find(keyword);
208  }
209 
210  //- Add to front of dictionary
211  //FOAM_DEPRECATED_FOR(2022-10, "push_front()")
212  void insert(const word& keyword, T* ptr)
213  {
214  this->push_front(keyword, ptr);
215  }
216 
217  //- Add to front of dictionary
218  //FOAM_DEPRECATED_FOR(2022-10, "push_front()")
219  void prepend(const word& keyword, T* ptr)
220  {
221  this->push_front(keyword, ptr);
222  }
223 
224  //- Add to back of dictionary
225  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
226  void append(const word& keyword, T* ptr)
227  {
228  this->push_back(keyword, ptr);
229  }
230 };
231 
232 
233 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
234 
235 } // End namespace Foam
236 
237 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
238 
239 #ifdef NoRepository
240  #include "DictionaryBase.C"
241 #endif
242 
243 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
244 
245 #endif
246 
247 // ************************************************************************* //
dictionary dict
Base dictionary class templated on both the form of doubly-linked list it uses as well as the type it...
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void operator=(const DictionaryBase &)
Copy assignment.
wordList sortedToc() const
Return the table of contents as a sorted list.
wordList toc() const
Return the table of contents (as a sorted list)
void transfer(DictionaryBase< IDLListType, T > &dict)
Transfer the contents of the argument into this DictionaryBase.
const T * operator[](const word &key) const
Find and return entry.
DictionaryBase(const label size=128)
Construct with given or default (128) table capacity.
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:65
bool contains(const word &keyword) const
Search for given keyword.
const T * lookupPtr(const word &keyword) const
Deprecated(2020-03) use cfind()
A class for handling words, derived from Foam::string.
Definition: word.H:63
HashTable< T * > hashedTs_
HashTable of the entries held on the IDLListType for quick lookup.
void clear()
Clear the dictionary.
A HashTable similar to std::unordered_map.
Definition: HashTable.H:102
void push_back(const word &keyword, T *ptr)
Add to back of dictionary.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
void insert(const word &keyword, T *ptr)
Add to front of dictionary.
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const T * lookup(const word &keyword) const
Find and return entry, FatalError on failure.
const T * cfind(const word &keyword) const
Find and return an entry, nullptr on failure.
void prepend(const word &keyword, T *ptr)
Add to front of dictionary.
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
void append(const word &keyword, T *ptr)
Add to back of dictionary.
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:46
bool found(const word &key) const
Same as contains()
T * find(const word &keyword)
Find and return an entry, nullptr on failure.
void push_front(const word &keyword, T *ptr)
Add to front of dictionary.
Namespace for OpenFOAM.