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-2020 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 hashedWordList_H
41 #define 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 number and list of words,
99  // optionally eliminating duplicates
100  hashedWordList(const label len, const char** array, bool unique=false);
101 
102  //- Construct from a nullptr-terminated list of words,
103  //- optionally eliminating duplicates
104  hashedWordList(const char** array, bool unique=false);
105 
106  //- Construct from Istream
107  inline hashedWordList(Istream& is);
108 
109 
110  // Member Functions
111 
112  //- Clear the list, i.e. set size to zero.
113  inline void clear();
114 
115  //- Append an element at the end of the list,
116  //- optionally avoid append if it would be a duplicate entry
117  inline void append(const word& name, bool unique=false);
118 
119  //- Search hashed values for the specified name
120  inline bool found(const word& name) const;
121 
122  //- Return the hash of words/indices for inspection
123  inline const HashTable<label>& lookup() const;
124 
125  //- Swap contents
126  inline void swap(hashedWordList& list);
127 
128  //- Transfer contents of the argument into this list
129  //- and annul the argument list, optionally eliminating duplicates
130  inline void transfer(hashedWordList& list);
131 
132  //- Transfer the contents of the argument List into this list
133  //- and annul the argument list, optionally eliminating duplicates
134  inline void transfer(wordList& list, bool unique=false);
135 
136  //- Rebuild the lookup hash indices
137  void rehash() const;
138 
139  //- Rebuild the lookup hash indices, or make unique entries first.
140  inline void rehash(bool unique);
141 
142  //- Adjust the list (if needed) to eliminate duplicate entries,
143  //- and rehash the indices
144  void uniq();
145 
146  //- Sort the list and rehash the indices
147  inline void sort();
148 
149 
150  // Member Operators
151 
152  //- Return name corresponding to specified index.
153  // Fatal for out of range values.
154  inline const word& operator[](const label index) const;
155 
156  //- Return index corresponding to specified name, or -1 on failure
157  inline label operator[](const word& name) const;
158 
159  //- Check hashed values for the specified name - same as found.
160  // Can be used as a unary predicate.
161  inline bool operator()(const word& name) 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  //- Deprecated(2019-01) Is the specified name found in the list?
185  // \deprecated(2019-01) - use found() method
186  FOAM_DEPRECATED_FOR(2019-01, "found() method")
187  bool contains(const word& name) const
188  {
189  return this->found(name);
190  }
191 };
192 
193 
194 //- Read from an input stream. Rehashes the indices.
195 inline Istream& operator>>(Istream& is, hashedWordList& list);
196 
197 
198 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
199 
200 } // End namespace Foam
201 
202 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
203 
204 #include "hashedWordListI.H"
205 
206 #endif
207 
208 // ************************************************************************* //
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
HashTable.H
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::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::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
hashedWordListI.H
wordList.H
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
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::hashedWordList::operator=
void operator=(const hashedWordList &list)
Copy assignment. Rehashes the indices.
Definition: hashedWordListI.H:216
Foam::HashTable< label >
Foam::hashedWordList::lookup
const HashTable< label > & lookup() const
Return the hash of words/indices for inspection.
Definition: hashedWordListI.H:119
Foam::hashedWordList::contains
bool contains(const word &name) const
Deprecated(2019-01) Is the specified name found in the list?
Definition: hashedWordList.H:186
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
bool
bool
Definition: EEqn.H:20
Foam::FOAM_DEPRECATED_FOR
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:69
Foam::hashedWordList::uniq
void uniq()
Definition: hashedWordList.C:73
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