hashedWordList.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) 2016-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::hashedWordList
29 
30 Description
31  A wordList with hashed named lookup, which can be faster in some
32  situations than using the normal list find/found methods.
33 
34 SourceFiles
35  hashedWordListI.H
36  hashedWordList.C
37 
38 \*---------------------------------------------------------------------------*/
39 
40 #ifndef Foam_hashedWordList_H
41 #define Foam_hashedWordList_H
42 
43 #include "wordList.H"
44 #include "HashTable.H"
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 
51 /*---------------------------------------------------------------------------*\
52  Class hashedWordList Declaration
53 \*---------------------------------------------------------------------------*/
54 
55 class hashedWordList
56 :
57  public wordList
58 {
59  // Private Data
60 
61  //- Lookup HashTable of words vs list-indices
62  mutable HashTable<label> lookup_;
63 
64 public:
65 
66  // Constructors
67 
68  //- Default construct an empty list
69  hashedWordList() = default;
70 
71  //- Copy construct
72  inline hashedWordList(const hashedWordList& list);
73 
74  //- Move construct
75  inline hashedWordList(hashedWordList&& list);
76 
77  //- Copy construct from list of words
78  inline explicit hashedWordList(const wordUList& list);
79 
80  //- Copy construct from list of words, eliminating duplicates
81  inline hashedWordList(const wordUList& list, bool unique);
82 
83  //- Move construct from list of words, optionally eliminating duplicates
84  inline hashedWordList(wordList&& list, bool unique=false);
85 
86  //- Construct from an initializer list
87  inline hashedWordList(std::initializer_list<word> list);
88 
89  //- Construct from the word keys of any HashTable, sorting immediately.
90  // This also handles a wordHashSet, which is derived from a HashTable.
91  // The result is similar to a HashTable::sortedToc.
92  template<class AnyType, class AnyHash>
93  inline explicit hashedWordList
94  (
96  );
97 
98  //- Construct from Istream
99  inline explicit hashedWordList(Istream& is);
100 
101 
102  // Member Functions
103 
104  //- Clear the list, i.e. set size to zero.
105  inline void clear();
106 
107  //- Append an element if not already in the list.
108  // \return the change in list length
109  inline label push_uniq(const word& val);
110 
111  //- Return the hash of words/indices for inspection
112  inline const HashTable<label>& lookup() const;
113 
114  //- Swap contents
115  inline void swap(hashedWordList& list);
116 
117  //- Transfer contents of the argument into this list
118  //- and annul the argument list, optionally eliminating duplicates
119  inline void transfer(hashedWordList& list);
120 
121  //- Transfer the contents of the argument List into this list
122  //- and annul the argument list, optionally eliminating duplicates
123  inline void transfer(wordList& list, bool unique=false);
124 
125  //- Rebuild the lookup hash indices
126  void rehash() const;
127 
128  //- Rebuild the lookup hash indices, or make unique entries first.
129  inline void rehash(bool unique);
130 
131  //- Adjust the list (if needed) to eliminate duplicate entries,
132  //- and rehash the indices
133  void uniq();
134 
135  //- Inplace sort list and rehash the indices
136  inline void sort();
137 
138 
139  // Search
140 
141  //- Find index of the value (searches the hash).
142  // \return position in list or -1 if not found.
143  inline label find(const word& val) const;
144 
145  //- Is the value contained in the list (searches the hash).
146  inline bool contains(const word& val) const;
147 
148 
149  // Member Operators
150 
151  //- Return name corresponding to specified index.
152  // Fatal for out of range values.
153  inline const word& operator[](const label index) const;
154 
155  //- Find index of the value (searches the hash) - same as find().
156  // \return position in list or -1 if not found.
157  inline label operator[](const word& val) const;
158 
159  //- Check hashed values for the specified name - same as contains().
160  // Can be used as a unary predicate.
161  inline bool operator()(const word& val) const;
162 
163 
164  // Assignment
165 
166  //- Copy assignment. Rehashes the indices.
167  inline void operator=(const hashedWordList& list);
168 
169  //- Copy assignment from list of words. Rehashes the indices.
170  inline void operator=(const wordUList& list);
171 
172  //- Copy assignment from initializer list. Rehashes the indices.
173  inline void operator=(std::initializer_list<word> list);
174 
175  //- Move assignment operator.
176  inline void operator=(hashedWordList&& list);
177 
178  //- Move assignment from list of words. Rehashes the indices.
179  inline void operator=(wordList&& list);
180 
181 
182  // Housekeeping
183 
184  //- Same as contains(), searches the hash.
185  bool found(const word& val) const { return this->contains(val); }
186 
187  //- Same as push_uniq()
188  FOAM_DEPRECATED_FOR(2022-05, "push_uniq method")
189  void append(const word& val) { this->push_uniq(val); }
190 
191  //- Same as push_uniq()
192  FOAM_DEPRECATED_FOR(2022-10, "push_uniq method")
193  void push_back(const word& val) { this->push_uniq(val); }
194 
195  //- Same as push_uniq()
196  FOAM_DEPRECATED_STRICT(2022-10, "push_uniq method")
197  label appendUniq(const word& val) { return this->push_uniq(val); }
198 };
199 
200 
201 //- Read from an input stream. Rehashes the indices.
202 inline Istream& operator>>(Istream& is, hashedWordList& list);
203 
204 
205 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
206 
207 } // End namespace Foam
208 
209 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
210 
211 #include "hashedWordListI.H"
212 
213 #endif
214 
215 // ************************************************************************* //
void rehash() const
Rebuild the lookup hash indices.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void append(const word &val)
Same as push_uniq()
bool contains(const word &val) const
Is the value contained in the list (searches the hash).
label appendUniq(const word &val)
Same as push_uniq()
const word & operator[](const label index) const
Return name corresponding to specified index.
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:65
void push_back(const word &val)
Same as push_uniq()
void sort()
Inplace sort list and rehash the indices.
hashedWordList()=default
Default construct an empty list.
A class for handling words, derived from Foam::string.
Definition: word.H:63
Istream & operator>>(Istream &, directionInfo &)
label push_uniq(const word &val)
Append an element if not already in the list.
bool operator()(const word &val) const
Check hashed values for the specified name - same as contains().
#define FOAM_DEPRECATED_STRICT(since, replacement)
Definition: stdFoam.H:55
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
void operator=(const hashedWordList &list)
Copy assignment. Rehashes the indices.
void clear()
Clear the list, i.e. set size to zero.
const HashTable< label > & lookup() const
Return the hash of words/indices for inspection.
bool found(const word &val) const
Same as contains(), searches the hash.
A wordList with hashed named lookup, which can be faster in some situations than using the normal lis...
void transfer(hashedWordList &list)
Transfer contents of the argument into this list and annul the argument list, optionally eliminating ...
void swap(hashedWordList &list)
Swap contents.
label find(const word &val) const
Find index of the value (searches the hash).
Namespace for OpenFOAM.
void uniq()
Adjust the list (if needed) to eliminate duplicate entries, and rehash the indices.