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 an entry to the HashTable
91  bool addHashEntry(const word& key, T* ptr)
92  {
93  return hashedTs_.insert(key, ptr);
94  }
95 
96  //- Add the IDLListType entries into the HashTable
97  void addEntries();
98 
99 
100 public:
101 
102  // Constructors
103 
104  //- Default construct: empty without allocation (capacity=0).
105  DictionaryBase() = default;
106 
107  //- Construct empty with initial table capacity
108  explicit DictionaryBase(const label initialCapacity)
109  :
110  hashedTs_(initialCapacity)
111  {}
112 
113  //- Copy construct
115 
116  //- Construct from Istream using given Istream constructor class
117  template<class INew>
118  DictionaryBase(Istream& is, const INew& inew);
119 
120  //- Construct from Istream using default Istream constructor class
121  DictionaryBase(Istream& is);
122 
123 
124  // Member Functions
125 
126  // Search and lookup
127 
128  //- Search for given keyword
129  bool contains(const word& keyword) const;
130 
131  //- Find and return an entry, nullptr on failure.
132  const T* cfind(const word& keyword) const;
133 
134  //- Find and return an entry, nullptr on failure.
135  T* find(const word& keyword);
136 
137  //- Find and return entry, FatalError on failure.
138  const T* lookup(const word& keyword) const;
139 
140  //- Find and return entry, FatalError on failure.
141  T* lookup(const word& keyword);
142 
143  //- The table of contents (as a sorted list)
144  wordList toc() const
145  {
146  return hashedTs_.sortedToc();
147  }
148 
149  //- The table of contents as a sorted list
150  wordList sortedToc() const
151  {
152  return hashedTs_.sortedToc();
153  }
154 
155  //- The table of contents sorted using the specified comparator
156  template<class Compare>
157  wordList sortedToc(const Compare& comp) const
158  {
159  return hashedTs_.sortedToc(comp);
160  }
161 
162 
163  // Editing
164 
165  //- Add to front of dictionary
166  void push_front(const word& keyword, T* ptr);
168  //- Add to back of dictionary
169  void push_back(const word& keyword, T* ptr);
170 
171  //- Remove and return entry specified by keyword.
172  // Return nullptr if the keyword was not found.
173  T* remove(const word& keyword);
174 
175  //- Clear the dictionary
176  void clear();
177 
178  //- Transfer the contents of the argument into this DictionaryBase
179  // and annul the argument.
181 
182 
183  // Member Operators
185  //- Copy assignment
186  void operator=(const DictionaryBase&);
187 
188  //- Find and return entry
189  const T* operator[](const word& key) const
190  {
191  return lookup(key);
192  }
193 
194  //- Find and return entry
195  T* operator[](const word& key)
196  {
197  return lookup(key);
198  }
199 
200 
201  // Ostream Operator
202 
203  friend Ostream& operator<< <IDLListType, T>
204  (
205  Ostream&,
206  const DictionaryBase<IDLListType, T>&
207  );
208 
209 
210  // Housekeeping
211 
212  //- Same as contains()
213  bool found(const word& key) const { return this->contains(key); }
214 
215  //- Deprecated(2020-03) use cfind()
216  // \deprecated(2020-03) - use cfind() method
217  FOAM_DEPRECATED_FOR(2020-03, "cfind() method")
218  const T* lookupPtr(const word& k) const { return this->cfind(k); }
219 
220  //- Deprecated(2020-03) use find()
221  // \deprecated(2020-03) - use find() method
222  FOAM_DEPRECATED_FOR(2020-03, "find() method")
223  T* lookupPtr(const word& k) { return this->find(k); }
224 
225  //- Add to front of dictionary
226  //FOAM_DEPRECATED_FOR(2022-10, "push_front()")
227  void insert(const word& k, T* ptr) { this->push_front(k, ptr); }
228 
229  //- Add to front of dictionary
230  //FOAM_DEPRECATED_FOR(2022-10, "push_front()")
231  void prepend(const word& k, T* ptr) { this->push_front(k, ptr); }
233  //- Add to back of dictionary
234  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
235  void append(const word& k, T* ptr) { this->push_back(k, ptr); }
236 };
237 
238 
239 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
241 } // End namespace Foam
242 
243 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
244 
245 #ifdef NoRepository
246  #include "DictionaryBase.C"
247  #include "DictionaryBaseIO.C"
248 #endif
249 
250 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
251 
252 #endif
253 
254 // ************************************************************************* //
dictionary dict
Base dictionary class templated on both the form of doubly-linked list it uses as well as the type it...
void addEntries()
Add the IDLListType entries into the HashTable.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void prepend(const word &k, T *ptr)
Add to front of dictionary.
void operator=(const DictionaryBase &)
Copy assignment.
void append(const word &k, T *ptr)
Add to back of dictionary.
label k
Boltzmann constant.
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.
const T * lookupPtr(const word &k) const
Deprecated(2020-03) use cfind()
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:65
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:152
bool contains(const word &keyword) const
Search for given keyword.
DictionaryBase()=default
Default construct: empty without allocation (capacity=0).
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:108
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:56
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
void insert(const word &k, T *ptr)
Add to front of dictionary.
const T * lookup(const word &keyword) const
Find and return entry, FatalError on failure.
List< word > wordList
List of word.
Definition: fileName.H:59
const T * cfind(const word &keyword) const
Find and return an entry, nullptr on failure.
wordList sortedToc() const
The table of contents as a sorted list.
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
List< Key > sortedToc() const
The table of contents (the keys) in sorted order.
Definition: HashTable.C:139
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:46
bool found(const word &key) const
Same as contains()
bool addHashEntry(const word &key, T *ptr)
Add an entry to the HashTable.
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.
wordList toc() const
The table of contents (as a sorted list)
Namespace for OpenFOAM.