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 -------------------------------------------------------------------------------
10 License
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 
49 inline Foam::hashedWordList::hashedWordList(const wordUList& list, bool unique)
50 :
51  wordList(list)
52 {
53  rehash(unique);
54 }
55 
56 
57 inline Foam::hashedWordList::hashedWordList(wordList&& list, bool unique)
58 :
59  wordList(std::move(list))
60 {
61  rehash(unique);
62 }
63 
64 
65 inline Foam::hashedWordList::hashedWordList(std::initializer_list<word> list)
66 :
67  wordList(list)
68 {
69  rehash();
70 }
71 
72 
73 template<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 
100 inline Foam::label Foam::hashedWordList::push_uniq(const word& val)
101 {
102  if (lookup_.insert(val, size()))
103  {
104  wordList::push_back(val);
105  return 1; // Increased list length by one
106  }
107 
108  return 0; // No change
109 }
110 
111 
112 inline const Foam::HashTable<Foam::label>&
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 
127 inline Foam::label Foam::hashedWordList::find(const word& val) const
128 {
129  return lookup().lookup(val, -1); // -1 = not found or not hashed
130 }
131 
133 inline 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
144  }
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 
158 inline void Foam::hashedWordList::transfer(wordList& list, bool unique)
159 {
160  wordList::transfer(list);
161  rehash(unique);
162 }
163 
164 
165 inline void Foam::hashedWordList::rehash(bool unique)
166 {
167  if (unique)
168  {
169  uniq();
170  }
171  else
172  {
173  rehash();
174  }
175 }
176 
177 
178 inline void Foam::hashedWordList::sort()
179 {
180  Foam::sort(*this);
181  rehash();
182 }
183 
184 
185 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
186 
187 inline const Foam::word& Foam::hashedWordList::operator[]
188 (
189  const label index
190 ) const
191 {
192  return wordList::operator[](index);
193 }
194 
196 inline Foam::label Foam::hashedWordList::operator[](const word& val) const
197 {
198  return lookup_.lookup(val, -1); // -1 = not found or not hashed
199 }
200 
202 inline bool Foam::hashedWordList::operator()(const word& val) const
203 {
204  return lookup_.contains(val);
205 }
206 
207 
209 {
210  wordList::operator=(list);
211  rehash();
212 }
213 
214 
216 {
217  wordList::operator=(list);
218  rehash();
219 }
220 
221 
222 inline void Foam::hashedWordList::operator=(std::initializer_list<word> list)
223 {
224  wordList::operator=(list);
225  rehash();
226 }
227 
228 
230 {
231  wordList::transfer(static_cast<wordList&>(list));
232  lookup_ = std::move(list.lookup_);
233 }
234 
235 
236 inline void Foam::hashedWordList::operator=(wordList&& list)
237 {
239  rehash();
240 }
241 
242 
243 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
244 
245 inline 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 // ************************************************************************* //
void swap(UList< T > &list) noexcept
Swap content with another UList of the same type in constant time.
Definition: UListI.H:505
void transfer(List< word > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition: List.C:326
lookup(const dictionary &dict, const polyPatch &pp)
Construct from components.
Definition: lookup.C:44
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
bool contains(const word &val) const
Is the value contained in the list (searches the hash).
Lookup type of boundary radiation properties.
Definition: lookup.H:57
void push_back(const word &val)
Append an element at the end of the list.
Definition: ListI.H:227
const word & operator[](const label index) const
Return name corresponding to specified index.
T & operator[](const label i)
Return element of UList.
Definition: UListI.H:361
void sort()
Inplace sort list and rehash the indices.
hashedWordList()=default
Default construct an empty list.
void operator=(const UList< word > &list)
Assignment to UList operator. Takes linear time.
Definition: List.C:360
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:137
A class for handling words, derived from Foam::string.
Definition: word.H:63
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:296
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().
A HashTable similar to std::unordered_map.
Definition: HashTable.H:108
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.
label size() const noexcept
The number of elements in the container.
Definition: UList.H:671
void clear()
Clear the list, i.e. set size to zero.
const HashTable< label > & lookup() const
Return the hash of words/indices for inspection.
List< word > wordList
List of word.
Definition: fileName.H:59
A wordList with hashed named lookup, which can be faster in some situations than using the normal lis...
List< label > sortedToc(const UList< bool > &bools)
Return the (sorted) values corresponding to &#39;true&#39; entries.
Definition: BitOps.C:195
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).