Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
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// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33
34template<class T>
35inline void Foam::CompactListList<T>::enforceSizeSanity()
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
50template<class T>
52(
53 const CompactListList<T>& list
54)
56 offsets_(list.offsets_),
57 values_(list.values_)
58{}
59
60
61template<class T>
63(
65)
67 offsets_(std::move(list.offsets_)),
68 values_(std::move(list.values_))
69{}
70
71
72template<class T>
74(
76 bool reuse
77)
79 offsets_(list.offsets_, reuse),
80 values_(list.values_, reuse)
81{}
82
83
84template<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
98template<class T>
100(
101 const label mRows,
102 const label nVals,
104)
105:
106 offsets_(mRows+1, Foam::zero{}),
107 values_(nVals, Foam::zero{})
108{
109 // Optionally: enforceSizeSanity();
110}
111
112
113template<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)
124 // Optionally: enforceSizeSanity();
125}
126
127
128template<class T>
131{
133}
134
135
136// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
137
138template<class T>
140{
141 return values_.cdata();
142}
143
144
145template<class T>
147{
148 return values_.data();
149}
150
151
152template<class T>
154{
155 return values_.cdata_bytes();
156}
157
158
159template<class T>
161{
162 return values_.data_bytes();
163}
164
165
166template<class T>
167inline std::streamsize Foam::CompactListList<T>::size_bytes() const noexcept
168{
169 return values_.size_bytes();
170}
171
172
173template<class T>
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
183template<class T>
185{
186 return (offsets_.size() == 2);
187}
188
189
190template<class T>
191inline Foam::label Foam::CompactListList<T>::length() const noexcept
193 const label len = (offsets_.size() - 1);
194 return (len < 1) ? static_cast<label>(0) : len;
195}
196
197
198template<class T>
199inline Foam::label Foam::CompactListList<T>::size() const noexcept
201 const label len = (offsets_.size() - 1);
202 return (len < 1) ? static_cast<label>(0) : len;
203}
204
205
206template<class T>
208{
209 return localSizes();
210}
211
212
213template<class T>
215{
216 return offsets_.empty() ? 0 : *(offsets_.cdata() + offsets_.size()-1);
217}
218
219
220template<class T>
221inline Foam::label Foam::CompactListList<T>::maxSize() const
223 return this->maxNonLocalSize(-1);
224}
225
226
227template<class T>
228inline 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
239template<class T>
240inline Foam::label Foam::CompactListList<T>::localStart(const label i) const
241{
242 return offsets_[i];
243}
244
245
246template<class T>
247inline Foam::label Foam::CompactListList<T>::localEnd(const label i) const
248{
249 return offsets_[i+1];
250}
251
252
253template<class T>
254inline Foam::label Foam::CompactListList<T>::localSize(const label i) const
256 return offsets_[i+1] - offsets_[i];
257}
258
259
260template<class T>
261inline const Foam::SubList<T>
262Foam::CompactListList<T>::localList(const label i) const
264 return SubList<T>(values_, (offsets_[i+1] - offsets_[i]), offsets_[i]);
265}
266
267
268template<class T>
269inline Foam::SubList<T>
271{
272 return SubList<T>(values_, (offsets_[i+1] - offsets_[i]), offsets_[i]);
273}
274
275
276template<class T>
277inline 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
287template<class T>
288inline 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);
303
304 return locali;
305}
306
307
308template<class T>
309inline 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
315template<class T>
316inline 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);
327
328 return rowi;
329}
330
331
332template<class T>
335 offsets_.clear();
336 values_.clear();
337}
338
339
340template<class T>
341inline 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();
360 offsets_.resize(mRows+1, endOffset);
361 }
362}
363
364
365template<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
386template<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
407template<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);
426}
427
428
429// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
430
431template<class T>
433(
434 const CompactListList<T>& list
435)
436{
437 if (this == &list)
438 {
439 return; // Self-assignment is a no-op
440 }
442 offsets_ = list.offsets_,
443 values_ = list.values_;
444}
445
446
447template<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
463template<class T>
465{
466 values_ = val;
467}
468
469
470template<class T>
473 values_ = Foam::zero{};
474}
475
476
477template<class T>
478inline const Foam::SubList<T>
479Foam::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
486template<class T>
487inline Foam::SubList<T>
490 // return SubList<T>(values_, (offsets_[i+1] - offsets_[i]), offsets_[i]);
491 return this->localList(i);
492}
493
494
495template<class T>
497(
498 const label i,
499 const label j
501{
502 return values_[toGlobal(i, j)];
503}
504
505
506template<class T>
508(
509 const label i,
510 const label j
511) const
512{
513 return values_[toGlobal(i, j)];
514}
515
516
517// ************************************************************************* //
Various functions to operate on Lists.
A packed storage of objects of type <T> using an offset table for access.
const SubList< T > operator[](const label i) const
Return const access to sub-list (no subscript checking).
bool single() const noexcept
True if content is a single row/sublist only. Such content could be flattened out into a straight lis...
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
Return pointer to underlying values storage, reinterpreted as byte data.
label localSize(const label i) const
Size of given row.
label totalSize() const noexcept
The total addressed size, which corresponds to the end (back) offset and also the sum of all localSiz...
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.
label length() const noexcept
The primary size (the number of rows/sublists).
label localStart(const label i) const
Starting offset for given row.
label size() const noexcept
The primary size (the number of rows/sublists).
void operator=(const CompactListList< T > &list)
Copy assignment.
T * data() noexcept
Return pointer to the first data in values().
const SubList< T > localList(const label i) const
Return const access to sub-list (no subscript checking).
labelList sizes() const
The local row sizes. Same as localSizes.
void resize(const label mRows)
Reset size of CompactListList.
const char * cdata_bytes() const noexcept
Return const pointer to underlying values storage, reinterpreted as byte data.
labelList localSizes() const
The local row sizes.
label localEnd(const label i) const
End offset (exclusive) for given row.
void clear()
Clear addressing and contents.
std::streamsize size_bytes() const noexcept
Number of contiguous bytes for the values data, no runtime check that the type is actually contiguous...
label toLocal(const label rowi, const label i) const
From global to local index on rowi.
CompactListList() noexcept=default
Default construct.
autoPtr< CompactListList< T > > clone() const
Clone.
label toGlobal(const label rowi, const label i) const
From local index on rowi to global (flat) indexing into packed values.
SubList< label > subList
Definition List.H:129
A non-owning sub-view of a List (allocated or unallocated storage).
Definition SubList.H:61
static const UList< label > & null() noexcept
Definition UList.H:225
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
const volScalarField & T
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
Namespace for OpenFOAM.
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.
List< label > labelList
A List of labels.
Definition List.H:62
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.
errorManip< error > abort(error &err)
Definition errorManip.H:139
const direction noexcept
Definition scalarImpl.H:265
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
UList< label > labelUList
A UList of labels.
Definition UList.H:75
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50