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-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
33
34template<class T>
36{
37 return NullObjectRef<CompactListList<T>>();
38}
39
40
41// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
42
43template<class T>
45{
46 if (offsets_.size() == 1)
47 {
48 offsets_.clear();
49 }
50 if (offsets_.empty())
51 {
52 values_.clear();
53 }
54}
55
56
57// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
58
59template<class T>
61(
62 const CompactListList<T>& list
63)
64:
65 offsets_(list.offsets_),
66 values_(list.values_)
67{}
68
69
70template<class T>
72(
74)
75:
76 offsets_(std::move(list.offsets_)),
77 values_(std::move(list.values_))
78{}
79
80
81template<class T>
83(
85 bool reuse
86)
87:
88 offsets_(list.offsets_, reuse),
89 values_(list.values_, reuse)
90{}
91
92
93
94template<class T>
96(
97 const label mRows,
98 const label nVals
99)
100:
101 offsets_(mRows+1, Zero),
102 values_(nVals)
103{
104 // Optionally: enforceSizeSanity();
105}
106
107
108template<class T>
110(
111 const label mRows,
112 const label nVals,
113 const Foam::zero
114)
115:
116 offsets_(mRows+1, Zero),
117 values_(nVals, Zero)
118{
119 // Optionally: enforceSizeSanity();
120}
121
122
123template<class T>
125(
126 const label mRows,
127 const label nVals,
128 const T& val
129)
130:
131 offsets_(mRows+1, Zero),
132 values_(nVals, val)
133{
134 // Optionally: enforceSizeSanity();
135}
136
137
138template<class T>
141{
142 return autoPtr<CompactListList<T>>::New(*this);
143}
144
145
146// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
147
148template<class T>
150{
151 return offsets_;
152}
153
154
155template<class T>
157{
158 return offsets_;
159}
160
161
162template<class T>
164{
165 return values_;
166}
167
168
169template<class T>
171{
172 return values_;
173}
174
175
176template<class T>
178{
179 return values_.cdata();
180}
181
182
183template<class T>
185{
186 return values_.data();
187}
188
189
190template<class T>
192{
193 return values_.cdata_bytes();
194}
195
196
197template<class T>
199{
200 return values_.data_bytes();
201}
202
203
204template<class T>
205inline std::streamsize Foam::CompactListList<T>::size_bytes() const noexcept
206{
207 return values_.size_bytes();
208}
209
210
211template<class T>
213{
214 return (offsets_.size() <= 1);
215}
216
217
218template<class T>
219inline Foam::label Foam::CompactListList<T>::size() const noexcept
220{
221 const label len = (offsets_.size() - 1);
222 return (len < 1) ? static_cast<label>(0) : len;
223}
224
225
226template<class T>
228{
229 return localSizes();
230}
231
232
233template<class T>
234inline Foam::label Foam::CompactListList<T>::totalSize() const
235{
236 const label len = (offsets_.size() - 1);
237 return (len < 1) ? static_cast<label>(0) : offsets_[len];
238}
239
240
241template<class T>
242inline Foam::label Foam::CompactListList<T>::maxSize() const
243{
244 return this->maxNonLocalSize(-1);
245}
246
247
248template<class T>
249inline const Foam::labelUList
251{
252 const label len = (offsets_.size() - 1);
253
254 if (len < 1) return labelUList::null();
255
256 return labelList::subList(offsets_, len);
257}
258
259
260template<class T>
261inline Foam::label Foam::CompactListList<T>::localStart(const label i) const
262{
263 return offsets_[i];
264}
265
266
267template<class T>
268inline Foam::label Foam::CompactListList<T>::localEnd(const label i) const
269{
270 return offsets_[i+1];
271}
272
273
274template<class T>
275inline Foam::label Foam::CompactListList<T>::localSize(const label i) const
276{
277 return offsets_[i+1] - offsets_[i];
278}
279
280
281template<class T>
282inline Foam::UList<T>
284{
285 return SubList<T>(values_, (offsets_[i+1] - offsets_[i]), offsets_[i]);
286}
287
288
289template<class T>
290inline const Foam::UList<T>
292{
293 return SubList<T>(values_, (offsets_[i+1] - offsets_[i]), offsets_[i]);
294}
295
296
297template<class T>
299(
300 const label rowi,
301 const label i
302) const
303{
304 return i + offsets_[rowi];
305}
306
307
308template<class T>
310(
311 const label rowi,
312 const label i
313) const
314{
315 const label locali = i - offsets_[rowi];
316
317 if (locali < 0 || i >= offsets_[rowi+1])
318 {
320 << "Index " << i << " does not belong on row "
321 << rowi << nl << "Offsets:" << offsets_
322 << abort(FatalError);
323 }
324
325 return locali;
326}
327
328
329template<class T>
330inline Foam::label Foam::CompactListList<T>::findRow(const label i) const
331{
332 return (i < 0 || i >= totalSize()) ? -1 : findLower(offsets_, i+1);
333}
334
335
336template<class T>
337inline Foam::label Foam::CompactListList<T>::whichRow(const label i) const
338{
339 const label rowi = findRow(i);
340
341 if (rowi < 0)
342 {
344 << "Index " << i << " outside of range" << nl
345 << "Offsets:" << offsets_
346 << abort(FatalError);
347 }
348
349 return rowi;
350}
351
352
353template<class T>
355{
356 offsets_.clear();
357 values_.clear();
358}
359
360
361template<class T>
362inline void Foam::CompactListList<T>::resize(const label mRows)
363{
364 if (mRows == 0)
365 {
366 // Clear
367 offsets_.clear();
368 values_.clear();
369 }
370 else if (mRows < size())
371 {
372 // Shrink
373 offsets_.resize(mRows+1);
374 values_.resize(offsets_[mRows]);
375 }
376 else if (mRows > size())
377 {
378 // Grow
380 << "Cannot be used to extend the list from " << size()
381 << " to " << mRows << nl
382 << " Please use a different resize() function"
383 << abort(FatalError);
384 }
385}
386
387
388template<class T>
390(
391 const label mRows,
392 const label nVals
393)
394{
395 offsets_.resize(mRows+1, Zero);
396 values_.resize(nVals);
397}
398
399
400template<class T>
402(
403 const label mRows,
404 const label nVals
405)
406{
407 offsets_.resize(mRows+1, Zero);
408 values_.resize_nocopy(nVals);
409}
410
411
412template<class T>
414(
415 const label mRows,
416 const label nVals,
417 const T& val
418)
419{
420 offsets_.resize(mRows+1, Zero);
421 values_.resize(nVals, val);
422 // Optionally: enforceSizeSanity();
423}
424
425
426template<class T>
427inline void Foam::CompactListList<T>::setSize(const label mRows)
428{
429 this->resize(mRows);
430}
431
432
433template<class T>
435(
436 const label mRows,
437 const label nVals
438)
439{
440 this->resize(mRows+1, nVals);
441}
442
443
444template<class T>
446(
447 const label mRows,
448 const label nVals,
449 const T& val
450)
451{
452 this->resize(mRows+1, nVals, val);
453}
454
455
456template<class T>
458(
459 const labelUList& listSizes
460)
461{
462 this->resize(listSizes);
463}
464
465
466// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
467
468template<class T>
470(
471 const CompactListList<T>& list
472)
473{
474 if (this == &list)
475 {
476 return; // Self-assignment is a no-op
477 }
478
479 offsets_ = list.offsets_,
480 values_ = list.values_;
481}
482
483
484template<class T>
486(
487 CompactListList<T>&& list
488)
489{
490 if (this == &list)
491 {
492 return; // Self-assignment is a no-op
493 }
494
495 offsets_.transfer(list.offsets_);
496 values_.transfer(list.values_);
497}
498
499
500template<class T>
502{
503 values_ = val;
504}
505
506
507template<class T>
509{
510 values_ = Zero;
511}
512
513
514template<class T>
515inline Foam::UList<T>
517{
518 return this->localList(i);
519}
520
521
522template<class T>
523inline const Foam::UList<T>
525{
526 return this->localList(i);
527}
528
529
530template<class T>
532(
533 const label i,
534 const label j
535)
536{
537 return values_[toGlobal(i, j)];
538}
539
540
541template<class T>
543(
544 const label i,
545 const label j
546) const
547{
548 return values_[toGlobal(i, j)];
549}
550
551
552// ************************************************************************* //
Various functions to operate on Lists.
A packed storage unstructured matrix of objects of type <T> using an offset table for access.
label maxSize() const
The max row length used.
void resize_nocopy(const label mRows, const label nVals)
Redimension without preserving existing content.
char * data_bytes() noexcept
bool empty() const noexcept
True if the number of rows/sublists is zero.
const T * cdata() const noexcept
Return const pointer to the first data in values()
label whichRow(const label i) const
Which row does global index come from? Binary search.
label findRow(const label i) const
Find row where global index comes from. Binary search.
const labelUList localStarts() const
The local row starts.
void transfer(CompactListList< T > &list)
Transfer contents into this and annul the argument.
friend Ostream & operator(Ostream &, const CompactListList< T > &)
Write CompactListList as offsets/values pair.
static const CompactListList< T > & null()
Return a CompactListList reference to a nullObject.
label size() const noexcept
The primary size (the number of rows/sublists)
void operator=(const CompactListList< T > &list)
Copy assignment.
label totalSize() const
The total addressed size.
T * data() noexcept
Return pointer to the first data in values()
labelList sizes() const
The local row sizes. Same as localSizes.
const char * cdata_bytes() const noexcept
label localEnd(const label i) const
End offset (exclusive) for given row.
const List< T > & values() const noexcept
Return the packed matrix of values.
void clear()
Clear addressing and contents.
std::streamsize size_bytes() const noexcept
label toLocal(const label rowi, const label i) const
From global to local index on rowi.
const labelList & offsets() const noexcept
Return the offset table (= size()+1)
UList< T > localList(const label i)
Return non-const access to sub-list (no subscript checking)
CompactListList() noexcept=default
Default construct.
void setSize(const label mRows)
Redimension - same as resize()
UList< T > operator[](const label i)
Return row as UList - same as localList method.
autoPtr< CompactListList< T > > clone() const
Clone.
label toGlobal(const label rowi, const label i) const
virtual bool resize()
Resize the ODE solver.
Definition: Euler.C:53
SubList< label > subList
Declare type of subList.
Definition: List.H:111
A List obtained as a section of another List.
Definition: SubList.H:70
static const UList< label > & null()
Return a UList reference to a nullObject.
Definition: UListI.H:53
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
label localSize() const
My local size.
Definition: globalIndexI.H:207
label localStart() const
My local start.
Definition: globalIndexI.H:195
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:63
const volScalarField & T
patchWriters resize(patchIds.size())
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
label findLower(const ListType &input, const T &val, const label start, const ComparePredicate &comp)
errorManip< error > abort(error &err)
Definition: errorManip.H:144
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
const direction noexcept
Definition: Scalar.H:223
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh > > &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
error FatalError
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53