ListListOps.C
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) 2011-2013 OpenFOAM Foundation
9  Copyright (C) 2018,2026 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 template<class T, class AccessOp>
31 (
32  const UList<T>& lists,
33  AccessOp aop
34 )
35 {
36  labelList output(lists.size());
37  auto out = output.begin();
38 
39  for (const auto& sub : lists)
40  {
41  *out = aop(sub).size();
42  ++out;
43  }
44 
45  return output;
46 }
47 
48 
49 template<class T, class AccessOp>
51 (
52  const UList<T>& lists,
53  AccessOp aop
54 )
55 {
56  label len = 0;
57 
58  for (const auto& sub : lists)
59  {
60  len += aop(sub).size();
61  }
62 
63  return len;
64 }
65 
66 
67 template<class T>
68 Foam::label Foam::ListListOps::totalSize(const UList<T>& lists)
69 {
70  label len = 0;
71 
72  for (const auto& sub : lists)
73  {
74  len += sub.size();
75  }
76 
77  return len;
78 }
79 
80 
81 template<class ListType, class T>
82 ListType Foam::ListListOps::concat(List<T>& lists)
83 {
84  label len = 0;
85 
86  for (const auto& sub : lists)
87  {
88  len += sub.size();
89  }
90 
91  ListType output(len);
92  output.resize(len); // Consistent sizing (eg, DynamicList)
93 
94  auto out = output.begin();
95 
96  for (auto& sub : lists)
97  {
98  out = std::move(sub.begin(), sub.end(), out);
99  }
100 
101  lists.clear();
103  return output;
104 }
105 
106 
107 template<class AccessType, class T, class AccessOp>
109 (
110  const UList<T>& lists,
111  AccessOp aop
112 )
113 {
114  label len = 0;
115 
116  for (const auto& sub : lists)
117  {
118  len += aop(sub).size();
119  }
120 
121  AccessType output(len);
122  auto out = output.begin();
123 
124  for (const auto& sub : lists)
125  {
126  for (const auto& item : aop(sub))
127  {
128  *out = item;
129  ++out;
130  }
131  }
133  return output;
134 }
135 
136 
137 template<class AccessType, class T, class AccessOp, class OffsetOp>
139 (
140  const UList<T>& lists,
141  const labelUList& offsets,
142  AccessOp aop,
143  OffsetOp oop
144 )
145 {
146  label len = 0;
147 
148  for (const auto& sub : lists)
149  {
150  len += aop(sub).size();
151  }
152 
153  AccessType output(len);
154  auto out = output.begin();
155  auto off = offsets.begin();
156 
157  label offset = 0;
158  for (const auto& sub : lists)
159  {
160  for (const auto& item : aop(sub))
161  {
162  *out = oop(item, offset);
163  ++out;
164  }
165 
166  offset += *off;
167  ++off;
168  }
169 
170  return output;
171 }
172 
173 
174 // ************************************************************************* //
ListType concat(List< T > &lists)
Concatenate sub-lists (moving their content) into a single list.
Definition: ListListOps.C:75
label totalSize(const UList< T > &lists)
The total size of all sub-lists.
AccessType combine(const UList< T > &lists, AccessOp aop=accessOp< T >())
Combines sub-lists into a single list.
Definition: ListListOps.C:102
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:403
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
label sumSizes(const UList< T > &lists, AccessOp aop=accessOp< T >())
The total size of all sub-lists.
labelList subSizes(const UList< T > &lists, AccessOp aop=accessOp< T >())
Return the sizes of the sub-lists.
List< label > labelList
A List of labels.
Definition: List.H:61
AccessType combineOffset(const UList< T > &lists, const labelUList &offsets, AccessOp aop, OffsetOp oop=offsetOp< T >())
Like combine but also offsets sublists based on passed sizes.
Definition: ListListOps.C:132