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 -------------------------------------------------------------------------------
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 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
33 
34 template<class T>
36 {
37  return NullObjectRef<CompactListList<T>>();
38 }
39 
40 
41 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
42 
43 template<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 
59 template<class T>
61 (
62  const CompactListList<T>& list
63 )
64 :
65  offsets_(list.offsets_),
66  values_(list.values_)
67 {}
68 
69 
70 template<class T>
72 (
73  CompactListList<T>&& list
74 )
75 :
76  offsets_(std::move(list.offsets_)),
77  values_(std::move(list.values_))
78 {}
79 
80 
81 template<class T>
83 (
84  CompactListList<T>& list,
85  bool reuse
86 )
87 :
88  offsets_(list.offsets_, reuse),
89  values_(list.values_, reuse)
90 {}
91 
92 
93 
94 template<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 
108 template<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 
123 template<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 
138 template<class T>
141 {
143 }
144 
145 
146 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
147 
148 template<class T>
150 {
151  return offsets_;
152 }
153 
154 
155 template<class T>
157 {
158  return offsets_;
159 }
160 
161 
162 template<class T>
164 {
165  return values_;
166 }
167 
168 
169 template<class T>
171 {
172  return values_;
173 }
174 
175 
176 template<class T>
178 {
179  return values_.cdata();
180 }
181 
182 
183 template<class T>
185 {
186  return values_.data();
187 }
188 
189 
190 template<class T>
192 {
193  return values_.cdata_bytes();
194 }
195 
196 
197 template<class T>
199 {
200  return values_.data_bytes();
201 }
202 
203 
204 template<class T>
205 inline std::streamsize Foam::CompactListList<T>::size_bytes() const noexcept
206 {
207  return values_.size_bytes();
208 }
209 
210 
211 template<class T>
213 {
214  return (offsets_.size() <= 1);
215 }
216 
217 
218 template<class T>
219 inline 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 
226 template<class T>
228 {
229  return localSizes();
230 }
231 
232 
233 template<class T>
234 inline 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 
241 template<class T>
242 inline Foam::label Foam::CompactListList<T>::maxSize() const
243 {
244  return this->maxNonLocalSize(-1);
245 }
246 
247 
248 template<class T>
249 inline 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 
260 template<class T>
261 inline Foam::label Foam::CompactListList<T>::localStart(const label i) const
262 {
263  return offsets_[i];
264 }
265 
266 
267 template<class T>
268 inline Foam::label Foam::CompactListList<T>::localEnd(const label i) const
269 {
270  return offsets_[i+1];
271 }
272 
273 
274 template<class T>
275 inline Foam::label Foam::CompactListList<T>::localSize(const label i) const
276 {
277  return offsets_[i+1] - offsets_[i];
278 }
279 
280 
281 template<class T>
282 inline Foam::UList<T>
284 {
285  return SubList<T>(values_, (offsets_[i+1] - offsets_[i]), offsets_[i]);
286 }
287 
288 
289 template<class T>
290 inline const Foam::UList<T>
291 Foam::CompactListList<T>::localList(const label i) const
292 {
293  return SubList<T>(values_, (offsets_[i+1] - offsets_[i]), offsets_[i]);
294 }
295 
296 
297 template<class T>
298 inline Foam::label Foam::CompactListList<T>::toGlobal
299 (
300  const label rowi,
301  const label i
302 ) const
303 {
304  return i + offsets_[rowi];
305 }
306 
307 
308 template<class T>
309 inline Foam::label Foam::CompactListList<T>::toLocal
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 
329 template<class T>
330 inline 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 
336 template<class T>
337 inline 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 
353 template<class T>
355 {
356  offsets_.clear();
357  values_.clear();
358 }
359 
360 
361 template<class T>
362 inline 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"
384  }
385 }
386 
387 
388 template<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 
400 template<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 
412 template<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 
426 template<class T>
427 inline void Foam::CompactListList<T>::setSize(const label mRows)
428 {
429  this->resize(mRows);
430 }
431 
432 
433 template<class T>
435 (
436  const label mRows,
437  const label nVals
438 )
439 {
440  this->resize(mRows+1, nVals);
441 }
442 
443 
444 template<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 
456 template<class T>
458 (
459  const labelUList& listSizes
460 )
461 {
462  this->resize(listSizes);
463 }
464 
465 
466 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
467 
468 template<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 
484 template<class T>
486 (
487  CompactListList<T>&& list
488 )
489 {
490  if (this == &list)
491  {
492  return; // Self-assignment is a no-op
493  }
495  offsets_.transfer(list.offsets_);
496  values_.transfer(list.values_);
497 }
498 
499 
500 template<class T>
501 inline void Foam::CompactListList<T>::operator=(const T& val)
502 {
503  values_ = val;
504 }
505 
506 
507 template<class T>
509 {
510  values_ = Zero;
511 }
512 
513 
514 template<class T>
515 inline Foam::UList<T>
517 {
518  return this->localList(i);
519 }
520 
521 
522 template<class T>
523 inline const Foam::UList<T>
524 Foam::CompactListList<T>::operator[](const label i) const
525 {
526  return this->localList(i);
527 }
528 
529 
530 template<class T>
532 (
533  const label i,
534  const label j
535 )
536 {
537  return values_[toGlobal(i, j)];
538 }
539 
540 
541 template<class T>
543 (
544  const label i,
545  const label j
546 ) const
547 {
548  return values_[toGlobal(i, j)];
549 }
550 
551 
552 // ************************************************************************* //
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)
patchWriters resize(patchIds.size())
const labelList & offsets() const noexcept
Return the offset table (= size()+1)
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:578
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:49
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.
UList< T > localList(const label i)
Return non-const access to sub-list (no subscript checking)
char * data_bytes() noexcept
Return pointer to underlying values storage, reinterpreted as byte data.
const List< T > & values() const noexcept
Return the packed matrix of values.
void clear()
Clear addressing and contents.
static const CompactListList< T > & null()
Return a CompactListList reference to a nullObject.
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.
label totalSize() const
The total addressed size.
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.
UList< T > operator[](const label i)
Return row as UList - same as localList method.
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
A packed storage unstructured matrix of objects of type <T> using an offset table for access...
void setSize(const label mRows)
Redimension - same as resize()
const direction noexcept
Definition: Scalar.H:258
const volScalarField & T
label whichRow(const label i) const
Which row does global index come from? Binary search.
label maxSize() const
The max row length used.
void operator=(const CompactListList< T > &list)
Copy assignment.
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
friend Ostream & operator(Ostream &, const CompactListList< T > &)
Write CompactListList as offsets/values pair.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:133