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-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 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 HashOps_H
44 #define 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 Hash=string::hash>
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
89 labelHashSet used(const bitSet& select);
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
98 labelHashSet used(const UList<bool>& select);
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 Hash=string::hash>
135 struct plusEqOp
136 {
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 (
150  const HashTable<T, Key, Hash>& tbl,
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 // ************************************************************************* //
Foam::HashTable::size
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
Foam::output
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:66
Foam::HashTableOps::plusEqOp
Combine HashTable operation. Equivalent to 'a += b'.
Definition: HashOps.H:135
List.H
Foam::HashSetOps::bools
List< bool > bools(const labelHashSet &locations)
Definition: HashOps.C:81
Foam::bitSet
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:63
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
Foam::HashSetOps::plusEqOp::operator()
void operator()(value_type &a, const value_type &b) const
Definition: HashOps.H:70
Foam::HashSetOps::bitset
bitSet bitset(const labelHashSet &locations)
Transform the on locations to a bitSet.
Definition: HashOps.C:72
Foam::HashSet
A HashTable with keys but without contents that is similar to std::unordered_set.
Definition: HashSet.H:83
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::sort
void sort(UList< T > &a)
Definition: UList.C:254
HashSet.H
Foam::HashSetOps::plusEqOp
Combine HashSet operation. Equivalent to 'a |= b'.
Definition: HashOps.H:66
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::HashTableOps::plusEqOp::value_type
HashTable< T, Key, Hash > value_type
Definition: HashOps.H:137
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
forAllConstIters
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
Foam::HashTableOps::plusEqOp::operator()
void operator()(value_type &a, const value_type &b) const
Definition: HashOps.H:139
Foam::List< bool >
Foam::HashSetOps::used
labelHashSet used(const bitSet &select)
Convert a bitset to a labelHashSet of the indices used.
Definition: HashOps.C:35
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::HashSetOps::plusEqOp::value_type
HashSet< Key, Hash > value_type
Definition: HashOps.H:68