Loading...
Searching...
No Matches
hashedWordListI.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) 2016-2023 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26\*---------------------------------------------------------------------------*/
27
28// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31:
32 hashedWordList(static_cast<const wordUList&>(list), false)
33{}
34
35
37:
38 wordList(std::move(static_cast<wordList&>(list))),
39 lookup_(std::move(list.lookup_))
40{}
41
44:
45 hashedWordList(list, false)
46{}
47
48
49inline Foam::hashedWordList::hashedWordList(const wordUList& list, bool unique)
51 wordList(list)
52{
53 rehash(unique);
54}
55
56
57inline Foam::hashedWordList::hashedWordList(wordList&& list, bool unique)
59 wordList(std::move(list))
60{
61 rehash(unique);
62}
63
64
65inline Foam::hashedWordList::hashedWordList(std::initializer_list<word> list)
66:
68{
69 rehash();
70}
71
72
73template<class AnyType, class AnyHash>
75(
77)
79 wordList(tbl.sortedToc())
80{
81 rehash();
82}
83
84
87 is >> *this;
88}
89
90
91// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
92
94{
96 lookup_.clear();
97}
98
99
100inline Foam::label Foam::hashedWordList::push_uniq(const word& val)
101{
102 if (lookup_.insert(val, size()))
103 {
105 return 1; // Increased list length by one
107
108 return 0; // No change
109}
110
111
114{
115 const label lenList = wordList::size();
116 const label lenHash = lookup_.size();
117
118 if ((lenList < lenHash) || (lenList && !lenHash))
119 {
120 rehash(); // Was somehow out of sync
121 }
122
123 return lookup_;
124}
125
127inline Foam::label Foam::hashedWordList::find(const word& val) const
128{
129 return lookup().lookup(val, -1); // -1 = not found or not hashed
130}
131
133inline bool Foam::hashedWordList::contains(const word& val) const
134{
135 return lookup().contains(val);
136}
137
138
140{
141 if (this == &list)
142 {
143 return; // Self-swap is a no-op
145
146 wordList::swap(static_cast<wordList&>(list));
147 lookup_.swap(list.lookup_);
148}
149
150
152{
153 wordList::transfer(static_cast<wordList&>(list));
154 lookup_.transfer(list.lookup_);
155}
156
157
158inline void Foam::hashedWordList::transfer(wordList& list, bool unique)
159{
160 wordList::transfer(list);
161 rehash(unique);
162}
163
164
165inline void Foam::hashedWordList::rehash(bool unique)
166{
167 if (unique)
168 {
169 uniq();
170 }
171 else
172 {
173 rehash();
174 }
175}
176
177
178inline void Foam::hashedWordList::sort()
179{
181 rehash();
182}
183
184
185// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
186
187inline const Foam::word& Foam::hashedWordList::operator[]
188(
189 const label index
190) const
191{
192 return wordList::operator[](index);
193}
194
196inline Foam::label Foam::hashedWordList::operator[](const word& val) const
197{
198 return lookup_.lookup(val, -1); // -1 = not found or not hashed
199}
200
202inline bool Foam::hashedWordList::operator()(const word& val) const
203{
204 return lookup_.contains(val);
205}
206
207
209{
211 rehash();
212}
213
214
216{
218 rehash();
219}
220
221
222inline void Foam::hashedWordList::operator=(std::initializer_list<word> list)
223{
225 rehash();
226}
227
228
230{
231 wordList::transfer(static_cast<wordList&>(list));
232 lookup_ = std::move(list.lookup_);
233}
234
235
237{
239 rehash();
240}
241
242
243// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
244
245inline Foam::Istream& Foam::operator>>(Istream& is, hashedWordList& list)
246{
247 is >> static_cast<wordList&>(list);
248 list.rehash();
249
250 return is;
251}
252
253
254// ************************************************************************* //
A HashTable similar to std::unordered_map.
Definition HashTable.H:124
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
void transfer(List< word > &list)
void operator=(const UList< word > &list)
void push_back(const word &val)
void swap(UList< word > &list) noexcept
label size() const noexcept
Definition UList.H:706
void size(const label n)
Definition UList.H:118
word & operator[](const label i)
A wordList with hashed named lookup, which can be faster in some situations than using the normal lis...
void uniq()
Adjust the list (if needed) to eliminate duplicate entries, and rehash the indices.
void transfer(hashedWordList &list)
Transfer contents of the argument into this list and annul the argument list, optionally eliminating ...
void operator=(const hashedWordList &list)
Copy assignment. Rehashes the indices.
label find(const word &val) const
Find index of the value (searches the hash).
hashedWordList()=default
Default construct an empty list.
void swap(hashedWordList &list)
Swap contents.
void sort()
Inplace sort list and rehash the indices.
label push_uniq(const word &val)
Append an element if not already in the list.
bool contains(const word &val) const
Is the value contained in the list (searches the hash).
void rehash() const
Rebuild the lookup hash indices.
void clear()
Clear the list, i.e. set size to zero.
bool operator()(const word &val) const
Check hashed values for the specified name - same as contains().
const word & operator[](const label index) const
Return name corresponding to specified index.
const HashTable< label > & lookup() const
Return the hash of words/indices for inspection.
A class for handling words, derived from Foam::string.
Definition word.H:66
List< word > wordList
List of word.
Definition fileName.H:60
UList< word > wordUList
UList of word.
Definition wordList.H:34
Istream & operator>>(Istream &, directionInfo &)
void sort(UList< T > &list)
Sort the list.
Definition UList.C:283