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 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
12 
13 Description
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 
24 namespace Foam
25 {
26 
27 // Perform a subset of the adjacency matrix
28 CompactListList<label> subsetAdjacency
29 (
30  const bitSet& select, // could also be labelHashSet
31  const CompactListList<label>& input,
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 
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
111 CompactListList<label> subsetAdjacency
112 (
113  const labelRange& slice,
114  const CompactListList<label>& input,
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 
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 // ************************************************************************* //
CompactListList< label > subsetAdjacency(const bitSet &select, const CompactListList< label > &input, labelList &subMap)
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:153
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:52
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
SubList< label > subList
Declare type of subList.
Definition: List.H:144
bool contains(const T &val) const
True if the value is contained in the list.
Definition: UListI.H:293
Various functions to operate on Lists.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
bool contains(const Key &key) const
True if hashed key is contained (found) in table.
Definition: HashTableI.H:72
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...
Definition: labelLists.C:44
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:33
A packed storage of objects of type <T> using an offset table for access.
std::enable_if< std::is_same< bool, TypeT >::value, bool >::type test(const label i) const
Test bool value at specified position, always false for out-of-range access.
Definition: UList.H:778
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
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:44
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: stdFoam.H:437
Map< label > invertToMap(const labelUList &values)
Create inverse mapping, which is a lookup table into the given list.
Definition: ListOps.C:107
Namespace for OpenFOAM.