Loading...
Searching...
No Matches
subsetAdjacency.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) 2024 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
12
13Description
14 Subsetting of an adjacency matrix (as CompactListList).
15 Can be relocated elsewhere.
16
17\*---------------------------------------------------------------------------*/
18
19#include "CompactListList.H"
20#include "bitSet.H"
21#include "ListOps.H"
22#include "Map.H"
23
24namespace Foam
25{
26
27// Perform a subset of the adjacency matrix
29(
30 const bitSet& select, // could also be labelHashSet
32 labelList& subMap
33)
34{
35 // Corresponds to cellMap etc (the original selection)
36 subMap = select.sortedToc();
37
38 // Ensure that the subMap corresponds to a valid subset
39 {
40 label validSize = 0;
41
42 const label nTotal = input.size();
43
44 forAllReverse(subMap, i)
45 {
46 if (subMap[i] < nTotal)
47 {
48 validSize = i + 1;
49 break;
50 }
51 }
52
53 subMap.resize(validSize);
54 }
55
56
57 // Assumed to be sparse - use Map for reverse lookup
58 const Map<label> reverseMap(invertToMap(subMap));
59
60
61 // Pass 1: determine the selected sub-sizes
62 labelList sizes(subMap.size(), Foam::zero{});
63
64 forAll(subMap, idx)
65 {
66 for (const label nbr : input[subMap[idx]])
67 {
68 if
69 (
70 select.test(nbr)
71 && reverseMap.contains(nbr) // extra consistency (paranoid)
72 )
73 {
74 ++sizes[idx];
75 }
76 }
77 }
78
79
80 CompactListList<label> output(sizes);
81
82 // Reuse sizes as output offset into output.values()
83 sizes = labelList::subList(output.offsets(), output.size());
84 labelList& values = output.values();
85
86
87 // Pass 2: extract sub-adjacent matrix
88
89 label newNbr = -1;
90
91 forAll(subMap, idx)
92 {
93 for (const label nbr : input[subMap[idx]])
94 {
95 if
96 (
97 select.test(nbr)
98 && (newNbr = reverseMap.lookup(nbr, -1)) >= 0
99 )
100 {
101 values[sizes[idx]++] = newNbr;
102 }
103 }
104 }
105
106 return output;
107}
108
109
110// Perform a subset of the adjacency matrix
111CompactListList<label> subsetAdjacency
112(
113 const labelRange& slice,
115 labelList& subMap
116)
117{
118 // Ensure that the selection corresponds to a valid subset
119 const labelRange select = slice.subset0(input.size());
120
121 // Corresponds to cellMap etc (the original selection)
122 subMap = Foam::identity(select);
123
124
125 // Pass 1: determine the selected sub-sizes
126 labelList sizes(subMap.size(), Foam::zero{});
127
128 forAll(subMap, idx)
129 {
130 for (const label nbr : input[subMap[idx]])
131 {
132 if (select.contains(nbr))
133 {
134 ++sizes[idx];
135 }
136 }
137 }
138
139
140 CompactListList<label> output(sizes);
141
142 // Reuse sizes as output offset into output.values()
143 sizes = labelList::subList(output.offsets(), output.size());
144 labelList& values = output.values();
145
146
147 // Pass 2: extract sub-adjacent matrix
148
149 const label localOffset = select.start();
150
151 forAll(subMap, idx)
152 {
153 for (const label nbr : input[subMap[idx]])
154 {
155 if (select.contains(nbr))
156 {
157 values[sizes[idx]++] = nbr - localOffset;
158 }
159 }
160 }
161
162 return output;
163}
164
165} // End namespace Foam
166
167
168// ************************************************************************* //
Various functions to operate on Lists.
A packed storage of objects of type <T> using an offset table for access.
const labelList & offsets() const noexcept
Return the offset table (= size()+1).
const List< T > & values() const noexcept
Return the packed values.
label size() const noexcept
The primary size (the number of rows/sublists).
const T & lookup(const Key &key, const T &deflt) const
Return hashed entry if it exists, or return the given default.
Definition HashTableI.H:222
bool contains(const Key &key) const
True if hashed key is contained (found) in table.
Definition HashTableI.H:72
SubList< label > subList
Definition List.H:129
void resize(const label len)
Adjust allocated size of list.
Definition ListI.H:153
A HashTable to objects of type <T> with a label key.
Definition Map.H:54
bool test(const label i) const
Test bool value at specified position, always false for out-of-range access.
Definition UList.H:852
bool contains(const T &val) const
True if the value is contained in the list.
Definition UListI.H:302
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition bitSet.H:61
A range or interval of labels defined by a start and a size.
Definition labelRange.H:66
labelRange subset0(label len) const
Calculate the intersection with the given 0/size range.
Definition labelRangeI.H:87
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
Namespace for OpenFOAM.
Map< label > invertToMap(const labelUList &values)
Create inverse mapping, which is a lookup table into the given list.
Definition ListOps.C:105
List< label > labelList
A List of labels.
Definition List.H:62
CompactListList< label > subsetAdjacency(const bitSet &select, const CompactListList< label > &input, labelList &subMap)
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i), works like std::iota() but returning a...
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition stdFoam.H:315