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-2019 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 
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.size())
80 {
81  wordList& list = *this;
82 
83  label count = 0;
84  for (auto iter = tbl.cbegin(); iter != tbl.cend(); ++iter)
85  {
86  list[count++] = iter.key();
87  }
88 
89  this->sort();
90 }
91 
92 
94 {
95  is >> *this;
96 }
97 
98 
99 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
100 
102 {
103  wordList::clear();
104  lookup_.clear();
105 }
106 
107 
108 inline void Foam::hashedWordList::append(const word& name, bool unique)
109 {
110  // name is either unique or we don't care about duplicates
111  if (lookup_.insert(name, size()) || !unique)
112  {
114  }
115 }
116 
117 
118 inline const Foam::HashTable<Foam::label>&
120 {
121  const label lenList = wordList::size();
122  const label lenHash = lookup_.size();
123 
124  if ((lenList < lenHash) || (lenList && !lenHash))
125  {
126  rehash(); // Was somehow out of sync
127  }
128 
129  return lookup_;
130 }
131 
132 
133 // TBD (2019-01-07) - overload find() for consistency?
134 //
135 // inline Foam::label Foam::hashedWordList::find(const word& name) const
136 // {
137 // return lookup().lookup(name, -1); // -1 = not found or not hashed
138 // }
139 
140 
141 inline bool Foam::hashedWordList::found(const word& name) const
142 {
143  return lookup().found(name);
144 }
145 
146 
148 {
149  if (this == &list)
150  {
151  return; // Self-swap is a no-op
152  }
153 
154  wordList::swap(static_cast<wordList&>(list));
155  lookup_.swap(list.lookup_);
156 }
157 
158 
160 {
161  wordList::transfer(static_cast<wordList&>(list));
162  lookup_.transfer(list.lookup_);
163 }
164 
165 
166 inline void Foam::hashedWordList::transfer(wordList& list, bool unique)
167 {
168  wordList::transfer(list);
169  rehash(unique);
170 }
171 
172 
173 inline void Foam::hashedWordList::rehash(bool unique)
174 {
175  if (unique)
176  {
177  uniq();
178  }
179  else
180  {
181  rehash();
182  }
183 }
184 
185 
187 {
188  Foam::sort(*this);
189  rehash();
190 }
191 
192 
193 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
194 
195 inline const Foam::word& Foam::hashedWordList::operator[]
196 (
197  const label index
198 ) const
199 {
200  return wordList::operator[](index);
201 }
202 
203 
204 inline Foam::label Foam::hashedWordList::operator[](const word& name) const
205 {
206  return lookup_.lookup(name, -1); // -1 = not found or not hashed
207 }
208 
209 
210 inline bool Foam::hashedWordList::operator()(const word& name) const
211 {
212  return lookup_.found(name);
213 }
214 
215 
217 {
218  wordList::operator=(list);
219  rehash();
220 }
221 
222 
224 {
225  wordList::operator=(list);
226  rehash();
227 }
228 
229 
230 inline void Foam::hashedWordList::operator=(std::initializer_list<word> list)
231 {
232  wordList::operator=(list);
233  rehash();
234 }
235 
236 
238 {
239  wordList::transfer(static_cast<wordList&>(list));
240  lookup_ = std::move(list.lookup_);
241 }
242 
243 
245 {
246  wordList::transfer(list);
247  rehash();
248 }
249 
250 
251 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
252 
254 {
255  is >> static_cast<wordList&>(list);
256  list.rehash();
257 
258  return is;
259 }
260 
261 
262 // ************************************************************************* //
Foam::HashTable::size
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::hashedWordList::operator()
bool operator()(const word &name) const
Check hashed values for the specified name - same as found.
Definition: hashedWordListI.H:210
Foam::hashedWordList::append
void append(const word &name, bool unique=false)
Definition: hashedWordListI.H:108
Foam::HashTable::cbegin
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
Definition: HashTableIterI.H:248
Foam::hashedWordList::operator[]
const word & operator[](const label index) const
Return name corresponding to specified index.
Definition: hashedWordListI.H:196
Foam::hashedWordList::sort
void sort()
Sort the list and rehash the indices.
Definition: hashedWordListI.H:186
Foam::HashTable::cend
constexpr const_iterator cend() const noexcept
const_iterator to signal the end (for any HashTable)
Definition: HashTableIterI.H:272
Foam::List< word >::append
void append(const word &val)
Append an element at the end of the list.
Definition: ListI.H:175
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::wordList
List< word > wordList
A List of words.
Definition: fileName.H:62
Foam::hashedWordList::hashedWordList
hashedWordList()=default
Default construct an empty list.
Foam::hashedWordList
A wordList with hashed named lookup, which can be faster in some situations than using the normal lis...
Definition: hashedWordList.H:54
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::hashedWordList::found
bool found(const word &name) const
Search hashed values for the specified name.
Definition: hashedWordListI.H:141
Foam::List< word >::transfer
void transfer(List< word > &list)
Definition: List.C:456
Foam::sort
void sort(UList< T > &a)
Definition: UList.C:261
Foam::List< word >::operator=
void operator=(const UList< word > &a)
Assignment to UList operator. Takes linear time.
Definition: List.C:498
Foam::radiation::lookup
Lookup type of boundary radiation properties.
Definition: lookup.H:63
Foam::hashedWordList::operator=
void operator=(const hashedWordList &list)
Copy assignment. Rehashes the indices.
Definition: hashedWordListI.H:216
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
Foam::hashedWordList::lookup
const HashTable< label > & lookup() const
Return the hash of words/indices for inspection.
Definition: hashedWordListI.H:119
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::List< word >
Foam::UList
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:103
Foam::List< word >::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:116
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::hashedWordList::swap
void swap(hashedWordList &list)
Swap contents.
Definition: hashedWordListI.H:147
Foam::hashedWordList::transfer
void transfer(hashedWordList &list)
Definition: hashedWordListI.H:159
Foam::hashedWordList::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: hashedWordListI.H:101
Foam::hashedWordList::rehash
void rehash() const
Rebuild the lookup hash indices.
Definition: hashedWordList.C:59