HashOps.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) 2018-2021 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 Namespace
27  Foam::HashSetOps
28 
29 Description
30  Various operations for HashSet.
31 
32 Namespace
33  Foam::HashTableOps
34 
35 Description
36  Various operations for HashTable.
37 
38 SourceFiles
39  HashOps.H
40 
41 \*---------------------------------------------------------------------------*/
42 
43 #ifndef Foam_HashOps_H
44 #define Foam_HashOps_H
45 
46 #include "HashSet.H"
47 #include "List.H"
48 
49 namespace Foam
50 {
51 
52 // Forward Declarations
53 class bitSet;
54 
55 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56 
57 /*---------------------------------------------------------------------------*\
58  Namespace HashSetOps Declaration
59 \*---------------------------------------------------------------------------*/
60 
61 namespace HashSetOps
62 {
63 
64 //- Combine HashSet operation. Equivalent to 'a |= b'
65 template<class Key=word, class HashType=Foam::Hash<Key>>
66 struct plusEqOp
67 {
69 
70  void operator()(value_type& a, const value_type& b) const
71  {
72  a |= b;
73  }
74 };
75 
76 
77 //- Convert a bitset to a labelHashSet of the indices used.
78 //
79 // \param select the bitset for which an \a on entry corresponds
80 // to an index in the output labelHashSet
81 //
82 // \return a labelHashSet of the selected indices
83 //
84 // This is equivalent of the following code, but more efficiently implemented.
85 // \code
86 // bitSet select = ...;
87 // return labelHashSet(select.toc());
88 // \endcode
90 
91 
92 //- Convert a list of bools to a labelHashSet of the indices used.
93 //
94 // \param select the boolList for which a \a true entry corresponds
95 // to an index in the output labelHashSet
96 //
97 // \return a labelHashSet of the selected indices
99 
100 
101 //- Transform the \a on locations to a bitSet.
102 // Ignored any negative values (invalid positions in a bitset).
103 //
104 // \param locations the list of positions corresponding to an \a on bit.
105 //
106 // \return a bitset
107 //
108 // \see BitSetOps::create for other possibilities
109 bitSet bitset(const labelHashSet& locations);
110 
111 
112 //- Transform the \a on locations to a boolList, with \a true for each
113 //- non-negative location and \a false for all others.
114 //
115 // \param locations the list of positions corresponding to an \a on bit.
116 //
117 // \return a boolList
118 //
119 // \note The operation necessarily discards any negative values since these
120 // are invalid positions in a boolList.
121 List<bool> bools(const labelHashSet& locations);
122 
123 } // End namespace HashSetOps
124 
125 
126 /*---------------------------------------------------------------------------*\
127  Namespace HashTableOps Declaration
128 \*---------------------------------------------------------------------------*/
129 
130 namespace HashTableOps
131 {
132 
133 //- Combine HashTable operation. Equivalent to 'a += b'
134 template<class T, class Key=word, class HashType=Foam::Hash<Key>>
135 struct plusEqOp
136 {
137  typedef HashTable<T, Key, HashType> value_type;
138 
139  void operator()(value_type& a, const value_type& b) const
140  {
141  a += b;
142  }
143 };
144 
145 
146 //- List of values from HashTable, optionally sorted.
147 template<class T, class Key, class Hash>
149 (
151  const bool doSort=false
152 )
153 {
154  List<T> output(tbl.size());
155 
156  label i=0;
157 
158  forAllConstIters(tbl, iter)
159  {
160  output[i] = iter.val();
161  ++i;
162  }
163 
164  if (doSort)
165  {
167  }
168 
169  return output;
170 }
171 
172 } // End namespace HashTableOps
173 
174 
175 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
176 
177 } // End namespace Foam
178 
179 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
180 
181 #endif
182 
183 // ************************************************************************* //
Combine HashSet operation. Equivalent to &#39;a |= b&#39;.
Definition: HashOps.H:65
A HashTable with keys but without contents that is similar to std::unordered_set. ...
Definition: HashSet.H:73
HashTable< T, Key, HashType > value_type
Definition: HashOps.H:150
labelHashSet used(const bitSet &select)
Convert a bitset to a labelHashSet of the indices used.
Definition: HashOps.C:26
List< bool > select(const label n, const labelUList &locations)
Construct a selection list of bools (all false) with the given pre-size, subsequently add specified l...
Definition: BitOps.C:134
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
label size() const noexcept
The number of elements in table.
Definition: HashTable.H:342
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
void operator()(value_type &a, const value_type &b) const
Definition: HashOps.H:152
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:296
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
List< bool > bools(const labelHashSet &locations)
Transform the on locations to a boolList, with true for each non-negative location and false for all ...
Definition: HashOps.C:72
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:59
HashSet< Key, HashType > value_type
Definition: HashOps.H:67
void operator()(value_type &a, const value_type &b) const
Definition: HashOps.H:69
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:44
bitSet bitset(const labelHashSet &locations)
Transform the on locations to a bitSet.
Definition: HashOps.C:63
Namespace for OpenFOAM.
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28