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-------------------------------------------------------------------------------
10License
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
26Namespace
27 Foam::HashSetOps
28
29Description
30 Various operations for HashSet.
31
32Namespace
33 Foam::HashTableOps
34
35Description
36 Various operations for HashTable.
37
38SourceFiles
39 HashOps.H
40
41\*---------------------------------------------------------------------------*/
42
43#ifndef HashOps_H
44#define HashOps_H
45
46#include "HashSet.H"
47#include "List.H"
48
49namespace Foam
50{
51
52// Forward Declarations
53class bitSet;
54
55// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56
57/*---------------------------------------------------------------------------*\
58 Namespace HashSetOps Declaration
59\*---------------------------------------------------------------------------*/
60
61namespace HashSetOps
62{
63
64//- Combine HashSet operation. Equivalent to 'a |= b'
65template<class Key=word, class HashType=Foam::Hash<Key>>
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
89labelHashSet 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
98labelHashSet 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
109bitSet 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.
121List<bool> bools(const labelHashSet& locations);
122
123} // End namespace HashSetOps
124
125
126/*---------------------------------------------------------------------------*\
127 Namespace HashTableOps Declaration
128\*---------------------------------------------------------------------------*/
129
130namespace HashTableOps
131{
132
133//- Combine HashTable operation. Equivalent to 'a += b'
134template<class T, class Key=word, class HashType=Foam::Hash<Key>>
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.
147template<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// ************************************************************************* //
A HashTable with keys but without contents that is similar to std::unordered_set.
Definition: HashSet.H:96
A HashTable similar to std::unordered_map.
Definition: HashTable.H:123
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:66
labelHashSet used(const bitSet &select)
Convert a bitset to a labelHashSet of the indices used.
Definition: HashOps.C:35
bitSet bitset(const labelHashSet &locations)
Transform the on locations to a bitSet.
Definition: HashOps.C:72
List< bool > bools(const labelHashSet &locations)
Definition: HashOps.C:81
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
Namespace for OpenFOAM.
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:342
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:66
volScalarField & b
Definition: createFields.H:27
#define forAllConstIters(container, iter)
Iterate across all elements of the container object with const access.
Definition: stdFoam.H:278
Combine HashSet operation. Equivalent to 'a |= b'.
Definition: HashOps.H:67
HashSet< Key, HashType > value_type
Definition: HashOps.H:68
void operator()(value_type &a, const value_type &b) const
Definition: HashOps.H:70
Combine HashTable operation. Equivalent to 'a += b'.
Definition: HashOps.H:136
void operator()(value_type &a, const value_type &b) const
Definition: HashOps.H:139
HashTable< T, Key, HashType > value_type
Definition: HashOps.H:137