CompactListListI.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2019-2024 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 #include "ListOps.H"
30 #include "SubList.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 template<class T>
36 {
37  if (offsets_.size() == 1)
38  {
39  offsets_.clear();
40  }
41  if (offsets_.empty())
42  {
43  values_.clear();
44  }
45 }
46 
47 
48 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
49 
50 template<class T>
52 (
53  const CompactListList<T>& list
54 )
55 :
56  offsets_(list.offsets_),
57  values_(list.values_)
58 {}
59 
60 
61 template<class T>
63 (
64  CompactListList<T>&& list
65 )
66 :
67  offsets_(std::move(list.offsets_)),
68  values_(std::move(list.values_))
69 {}
70 
71 
72 template<class T>
74 (
75  CompactListList<T>& list,
76  bool reuse
77 )
78 :
79  offsets_(list.offsets_, reuse),
80  values_(list.values_, reuse)
81 {}
82 
83 
84 template<class T>
86 (
87  const label mRows,
88  const label nVals
89 )
90 :
91  offsets_(mRows+1, Foam::zero{}),
92  values_(nVals)
93 {
94  // Optionally: enforceSizeSanity();
95 }
96 
97 
98 template<class T>
100 (
101  const label mRows,
102  const label nVals,
103  const Foam::zero
104 )
105 :
106  offsets_(mRows+1, Foam::zero{}),
107  values_(nVals, Foam::zero{})
108 {
109  // Optionally: enforceSizeSanity();
110 }
111 
112 
113 template<class T>
115 (
116  const label mRows,
117  const label nVals,
118  const T& val
119 )
120 :
121  offsets_(mRows+1, Foam::zero{}),
122  values_(nVals, val)
123 {
124  // Optionally: enforceSizeSanity();
125 }
126 
127 
128 template<class T>
131 {
133 }
134 
135 
136 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
137 
138 template<class T>
140 {
141  return values_.cdata();
142 }
143 
144 
145 template<class T>
147 {
148  return values_.data();
149 }
150 
151 
152 template<class T>
154 {
155  return values_.cdata_bytes();
156 }
157 
158 
159 template<class T>
161 {
162  return values_.data_bytes();
163 }
164 
165 
166 template<class T>
167 inline std::streamsize Foam::CompactListList<T>::size_bytes() const noexcept
168 {
169  return values_.size_bytes();
170 }
171 
172 
173 template<class T>
174 inline bool Foam::CompactListList<T>::empty() const noexcept
175 {
176  // Note: could (should?) also check total size??
177  // const label len = (offsets_.size() - 1);
178  // return (len < 1) || (*(offsets_.cdata() + len) == 0);
179  return (offsets_.size() <= 1);
180 }
181 
182 
183 template<class T>
185 {
186  return (offsets_.size() == 2);
187 }
188 
189 
190 template<class T>
191 inline Foam::label Foam::CompactListList<T>::length() const noexcept
192 {
193  const label len = (offsets_.size() - 1);
194  return (len < 1) ? static_cast<label>(0) : len;
195 }
196 
197 
198 template<class T>
199 inline Foam::label Foam::CompactListList<T>::size() const noexcept
200 {
201  const label len = (offsets_.size() - 1);
202  return (len < 1) ? static_cast<label>(0) : len;
203 }
204 
205 
206 template<class T>
208 {
209  return localSizes();
210 }
211 
212 
213 template<class T>
215 {
216  return offsets_.empty() ? 0 : *(offsets_.cdata() + offsets_.size()-1);
217 }
218 
219 
220 template<class T>
221 inline Foam::label Foam::CompactListList<T>::maxSize() const
222 {
223  return this->maxNonLocalSize(-1);
224 }
225 
226 
227 template<class T>
228 inline const Foam::labelUList
230 {
231  const label len = (offsets_.size() - 1);
232 
233  if (len < 1) return labelUList::null();
234 
235  return labelList::subList(offsets_, len);
236 }
237 
238 
239 template<class T>
240 inline Foam::label Foam::CompactListList<T>::localStart(const label i) const
241 {
242  return offsets_[i];
243 }
244 
245 
246 template<class T>
247 inline Foam::label Foam::CompactListList<T>::localEnd(const label i) const
248 {
249  return offsets_[i+1];
250 }
251 
252 
253 template<class T>
254 inline Foam::label Foam::CompactListList<T>::localSize(const label i) const
255 {
256  return offsets_[i+1] - offsets_[i];
257 }
258 
259 
260 template<class T>
261 inline const Foam::SubList<T>
262 Foam::CompactListList<T>::localList(const label i) const
263 {
264  return SubList<T>(values_, (offsets_[i+1] - offsets_[i]), offsets_[i]);
265 }
266 
267 
268 template<class T>
269 inline Foam::SubList<T>
271 {
272  return SubList<T>(values_, (offsets_[i+1] - offsets_[i]), offsets_[i]);
273 }
274 
275 
276 template<class T>
277 inline Foam::label Foam::CompactListList<T>::toGlobal
278 (
279  const label rowi,
280  const label i
281 ) const
282 {
283  return i + offsets_[rowi];
284 }
285 
286 
287 template<class T>
288 inline Foam::label Foam::CompactListList<T>::toLocal
289 (
290  const label rowi,
291  const label i
292 ) const
293 {
294  const label locali = i - offsets_[rowi];
295 
296  if (locali < 0 || i >= offsets_[rowi+1])
297  {
299  << "Index " << i << " does not belong on row "
300  << rowi << nl << "Offsets:" << offsets_
301  << abort(FatalError);
302  }
303 
304  return locali;
305 }
306 
307 
308 template<class T>
309 inline Foam::label Foam::CompactListList<T>::findRow(const label i) const
310 {
311  return (i < 0 || i >= totalSize()) ? -1 : findLower(offsets_, i+1);
312 }
313 
314 
315 template<class T>
316 inline Foam::label Foam::CompactListList<T>::whichRow(const label i) const
317 {
318  const label rowi = findRow(i);
319 
320  if (rowi < 0)
321  {
323  << "Index " << i << " outside of range" << nl
324  << "Offsets:" << offsets_
325  << abort(FatalError);
326  }
327 
328  return rowi;
329 }
330 
331 
332 template<class T>
334 {
335  offsets_.clear();
336  values_.clear();
337 }
338 
339 
340 template<class T>
341 inline void Foam::CompactListList<T>::resize(const label mRows)
342 {
343  if (mRows == 0)
344  {
345  // Clear
346  offsets_.clear();
347  values_.clear();
348  }
349  else if (mRows < size())
350  {
351  // Shrink
352  offsets_.resize(mRows+1);
353  values_.resize(offsets_[mRows]);
354  }
355  else if (mRows > size())
356  {
357  // Extend number of rows, each with local size of 0
358  const label endOffset = offsets_.empty() ? 0 : offsets_.back();
359 
360  offsets_.resize(mRows+1, endOffset);
361  }
362 }
363 
364 
365 template<class T>
367 (
368  const label mRows,
369  const label nVals
370 )
371 {
372  if (mRows < 1)
373  {
374  // Enforce sizing sanity
375  offsets_.clear();
376  values_.clear();
377  }
378  else
379  {
380  offsets_.resize(mRows+1, Foam::zero{});
381  values_.resize(nVals);
382  }
383 }
384 
385 
386 template<class T>
388 (
389  const label mRows,
390  const label nVals
391 )
392 {
393  if (mRows < 1)
394  {
395  // Enforce sizing sanity
396  offsets_.clear();
397  values_.clear();
398  }
399  else
400  {
401  offsets_.resize(mRows+1, Foam::zero{});
402  values_.resize_nocopy(nVals);
403  }
404 }
405 
406 
407 template<class T>
409 (
410  const label mRows,
411  const label nVals,
412  const T& val
413 )
414 {
415  if (mRows < 1)
416  {
417  // Enforce sizing sanity
418  offsets_.clear();
419  values_.clear();
420  }
421  else
422  {
423  offsets_.resize(mRows+1, Foam::zero{});
424  values_.resize(nVals, val);
425  }
426 }
427 
428 
429 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
430 
431 template<class T>
433 (
434  const CompactListList<T>& list
435 )
436 {
437  if (this == &list)
438  {
439  return; // Self-assignment is a no-op
440  }
441 
442  offsets_ = list.offsets_,
443  values_ = list.values_;
444 }
445 
446 
447 template<class T>
449 (
450  CompactListList<T>&& list
451 )
452 {
453  if (this == &list)
454  {
455  return; // Self-assignment is a no-op
456  }
458  offsets_.transfer(list.offsets_);
459  values_.transfer(list.values_);
460 }
461 
462 
463 template<class T>
464 inline void Foam::CompactListList<T>::operator=(const T& val)
465 {
466  values_ = val;
467 }
468 
469 
470 template<class T>
472 {
473  values_ = Foam::zero{};
474 }
475 
476 
477 template<class T>
478 inline const Foam::SubList<T>
479 Foam::CompactListList<T>::operator[](const label i) const
480 {
481  // return SubList<T>(values_, (offsets_[i+1] - offsets_[i]), offsets_[i]);
482  return this->localList(i);
483 }
484 
485 
486 template<class T>
487 inline Foam::SubList<T>
489 {
490  // return SubList<T>(values_, (offsets_[i+1] - offsets_[i]), offsets_[i]);
491  return this->localList(i);
492 }
493 
494 
495 template<class T>
497 (
498  const label i,
499  const label j
500 )
501 {
502  return values_[toGlobal(i, j)];
503 }
504 
505 
506 template<class T>
508 (
509  const label i,
510  const label j
511 ) const
512 {
513  return values_[toGlobal(i, j)];
514 }
515 
516 
517 // ************************************************************************* //
label localStart(const label i) const
Starting offset for given row.
label findLower(const ListType &input, const T &val, const label start, const ComparePredicate &comp)
Binary search to find the index of the last element in a sorted list that is less than value...
label toLocal(const label rowi, const label i) const
From global to local index on rowi.
bool empty() const noexcept
True if the number of rows/sublists is zero.
std::streamsize size_bytes() const noexcept
Number of contiguous bytes for the values data, no runtime check that the type is actually contiguous...
label size() const noexcept
The primary size (the number of rows/sublists)
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:608
label toGlobal(const label rowi, const label i) const
From local index on rowi to global (flat) indexing into packed values.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
char * data_bytes() noexcept
Return pointer to underlying values storage, reinterpreted as byte data.
SubList< label > subList
Declare type of subList.
Definition: List.H:144
void clear()
Clear addressing and contents.
label localSize(const label i) const
Size of given row.
void transfer(CompactListList< T > &list)
Transfer contents into this and annul the argument.
Various functions to operate on Lists.
const char * cdata_bytes() const noexcept
Return const pointer to underlying values storage, reinterpreted as byte data.
label findRow(const label i) const
Find row where global index comes from. Binary search.
A List obtained as a section of another List.
Definition: SubList.H:50
label localEnd(const label i) const
End offset (exclusive) for given row.
autoPtr< CompactListList< T > > clone() const
Clone.
labelList sizes() const
The local row sizes. Same as localSizes.
T * data() noexcept
Return pointer to the first data in values()
CompactListList() noexcept=default
Default construct.
const T * cdata() const noexcept
Return const pointer to the first data in values()
void resize(const label mRows)
Reset size of CompactListList.
errorManip< error > abort(error &err)
Definition: errorManip.H:139
const SubList< T > localList(const label i) const
Return const access to sub-list (no subscript checking)
const SubList< T > operator[](const label i) const
Return const access to sub-list (no subscript checking)
A packed storage of objects of type <T> using an offset table for access.
const direction noexcept
Definition: Scalar.H:258
label totalSize() const noexcept
The total addressed size, which corresponds to the end (back) offset and also the sum of all localSiz...
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
label whichRow(const label i) const
Which row does global index come from? Binary search.
label maxSize() const
The max row length used.
label length() const noexcept
The primary size (the number of rows/sublists)
void operator=(const CompactListList< T > &list)
Copy assignment.
static const UList< label > & null() noexcept
Return a null UList (reference to a nullObject). Behaves like an empty UList.
Definition: UList.H:233
const labelUList localStarts() const
The local row starts.
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
void resize_nocopy(const label mRows, const label nVals)
Redimension without preserving existing content.
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
bool single() const noexcept
True if content is a single row/sublist only. Such content could be flattened out into a straight lis...
friend Ostream & operator(Ostream &, const CompactListList< T > &)
Write CompactListList as offsets/values pair.
Namespace for OpenFOAM.