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-2022 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 * * * * * * * * * * * * * * //
29
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
42
44:
45 hashedWordList(list, false)
46{}
47
48
49inline Foam::hashedWordList::hashedWordList(const wordUList& list, bool unique)
50:
51 wordList(list)
52{
53 rehash(unique);
54}
55
56
58:
59 wordList(std::move(list))
60{
61 rehash(unique);
62}
63
64
65inline Foam::hashedWordList::hashedWordList(std::initializer_list<word> list)
66:
67 wordList(list)
68{
69 rehash();
70}
71
72
73template<class AnyType, class AnyHash>
75(
77)
78:
79 wordList(tbl.sortedToc())
80{
81 rehash();
82}
83
84
86{
87 is >> *this;
88}
89
90
91// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
92
94{
96 lookup_.clear();
97}
98
99
100inline void Foam::hashedWordList::append(const word& val)
101{
102 if (lookup_.insert(val, size()))
103 {
104 wordList::append(val);
105 }
106}
107
108
109inline Foam::label Foam::hashedWordList::appendUniq(const word& val)
110{
111 if (lookup_.insert(val, size()))
112 {
113 wordList::append(val);
114 return 1; // Increased list length by one
115 }
116
117 return 0; // No change
118}
119
120
123{
124 const label lenList = wordList::size();
125 const label lenHash = lookup_.size();
126
127 if ((lenList < lenHash) || (lenList && !lenHash))
128 {
129 rehash(); // Was somehow out of sync
130 }
131
132 return lookup_;
133}
134
135
136inline Foam::label Foam::hashedWordList::find(const word& val) const
137{
138 return lookup().lookup(val, -1); // -1 = not found or not hashed
139}
140
141
142inline bool Foam::hashedWordList::found(const word& val) const
143{
144 return lookup().found(val);
145}
146
147
149{
150 if (this == &list)
151 {
152 return; // Self-swap is a no-op
153 }
154
155 wordList::swap(static_cast<wordList&>(list));
156 lookup_.swap(list.lookup_);
157}
158
159
161{
162 wordList::transfer(static_cast<wordList&>(list));
163 lookup_.transfer(list.lookup_);
164}
165
166
167inline void Foam::hashedWordList::transfer(wordList& list, bool unique)
168{
169 wordList::transfer(list);
170 rehash(unique);
171}
172
173
174inline void Foam::hashedWordList::rehash(bool unique)
175{
176 if (unique)
177 {
178 uniq();
179 }
180 else
181 {
182 rehash();
183 }
184}
185
186
188{
189 Foam::sort(*this);
190 rehash();
191}
192
193
194// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
195
197(
198 const label index
199) const
200{
201 return wordList::operator[](index);
202}
203
204
205inline Foam::label Foam::hashedWordList::operator[](const word& val) const
206{
207 return lookup_.lookup(val, -1); // -1 = not found or not hashed
208}
209
210
211inline bool Foam::hashedWordList::operator()(const word& val) const
212{
213 return lookup_.found(val);
214}
215
216
218{
220 rehash();
221}
222
223
225{
227 rehash();
228}
229
230
231inline void Foam::hashedWordList::operator=(std::initializer_list<word> list)
232{
234 rehash();
235}
236
237
239{
240 wordList::transfer(static_cast<wordList&>(list));
241 lookup_ = std::move(list.lookup_);
242}
243
244
246{
247 wordList::transfer(list);
248 rehash();
249}
250
251
252// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
253
255{
256 is >> static_cast<wordList&>(list);
257 list.rehash();
258
259 return is;
260}
261
262
263// ************************************************************************* //
bool found
A HashTable similar to std::unordered_map.
Definition: HashTable.H:123
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
void operator=(const UList< word > &a)
Assignment to UList operator. Takes linear time.
Definition: List.C:480
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:116
Ostream & operator()() const
Output stream (master only).
Definition: ensightCaseI.H:74
label operator[](const label i) const
Processor-local element id from linear-list of addresses.
Definition: ensightPart.H:189
friend Ostream & operator(Ostream &, const faMatrix< Type > &)
A wordList with hashed named lookup, which can be faster in some situations than using the normal lis...
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.
void rehash() const
Rebuild the lookup hash indices.
void clear()
Clear the list, i.e. set size to zero.
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.
label appendUniq(const word &val)
Append an element if not already in the list.
Lookup type of boundary radiation properties.
Definition: lookup.H:66
lookup(const dictionary &dict, const polyPatch &pp)
Construct from components.
Definition: lookup.C:51
bool append() const noexcept
True if output format uses an append mode.
A class for handling words, derived from Foam::string.
Definition: word.H:68
Istream & operator>>(Istream &, directionInfo &)
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:342