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-2023 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 values_.cdata();
152 }
153 
154 
155 template<class T>
157 {
158  return values_.data();
159 }
160 
161 
162 template<class T>
164 {
165  return values_.cdata_bytes();
166 }
167 
168 
169 template<class T>
171 {
172  return values_.data_bytes();
173 }
174 
175 
176 template<class T>
177 inline std::streamsize Foam::CompactListList<T>::size_bytes() const noexcept
178 {
179  return values_.size_bytes();
180 }
181 
182 
183 template<class T>
184 inline bool Foam::CompactListList<T>::empty() const noexcept
185 {
186  // Note: could (should?) also check total size??
187  // const label len = (offsets_.size() - 1);
188  // return (len < 1) || (*(offsets_.cdata() + len) == 0);
189  return (offsets_.size() <= 1);
190 }
191 
192 
193 template<class T>
195 {
196  return (offsets_.size() == 2);
197 }
198 
199 
200 template<class T>
201 inline Foam::label Foam::CompactListList<T>::length() const noexcept
202 {
203  const label len = (offsets_.size() - 1);
204  return (len < 1) ? static_cast<label>(0) : len;
205 }
206 
207 
208 template<class T>
209 inline Foam::label Foam::CompactListList<T>::size() const noexcept
210 {
211  const label len = (offsets_.size() - 1);
212  return (len < 1) ? static_cast<label>(0) : len;
213 }
214 
215 
216 template<class T>
218 {
219  return localSizes();
220 }
221 
222 
223 template<class T>
225 {
226  return offsets_.empty() ? 0 : *(offsets_.cdata() + offsets_.size()-1);
227 }
228 
229 
230 template<class T>
231 inline Foam::label Foam::CompactListList<T>::maxSize() const
232 {
233  return this->maxNonLocalSize(-1);
234 }
235 
236 
237 template<class T>
238 inline const Foam::labelUList
240 {
241  const label len = (offsets_.size() - 1);
242 
243  if (len < 1) return labelUList::null();
244 
245  return labelList::subList(offsets_, len);
246 }
247 
248 
249 template<class T>
250 inline Foam::label Foam::CompactListList<T>::localStart(const label i) const
251 {
252  return offsets_[i];
253 }
254 
255 
256 template<class T>
257 inline Foam::label Foam::CompactListList<T>::localEnd(const label i) const
258 {
259  return offsets_[i+1];
260 }
261 
262 
263 template<class T>
264 inline Foam::label Foam::CompactListList<T>::localSize(const label i) const
265 {
266  return offsets_[i+1] - offsets_[i];
267 }
268 
269 
270 template<class T>
271 inline Foam::UList<T>
273 {
274  return SubList<T>(values_, (offsets_[i+1] - offsets_[i]), offsets_[i]);
275 }
276 
277 
278 template<class T>
279 inline const Foam::UList<T>
280 Foam::CompactListList<T>::localList(const label i) const
281 {
282  return SubList<T>(values_, (offsets_[i+1] - offsets_[i]), offsets_[i]);
283 }
284 
285 
286 template<class T>
287 inline Foam::label Foam::CompactListList<T>::toGlobal
288 (
289  const label rowi,
290  const label i
291 ) const
292 {
293  return i + offsets_[rowi];
294 }
295 
296 
297 template<class T>
298 inline Foam::label Foam::CompactListList<T>::toLocal
299 (
300  const label rowi,
301  const label i
302 ) const
303 {
304  const label locali = i - offsets_[rowi];
305 
306  if (locali < 0 || i >= offsets_[rowi+1])
307  {
309  << "Index " << i << " does not belong on row "
310  << rowi << nl << "Offsets:" << offsets_
311  << abort(FatalError);
312  }
313 
314  return locali;
315 }
316 
317 
318 template<class T>
319 inline Foam::label Foam::CompactListList<T>::findRow(const label i) const
320 {
321  return (i < 0 || i >= totalSize()) ? -1 : findLower(offsets_, i+1);
322 }
323 
324 
325 template<class T>
326 inline Foam::label Foam::CompactListList<T>::whichRow(const label i) const
327 {
328  const label rowi = findRow(i);
329 
330  if (rowi < 0)
331  {
333  << "Index " << i << " outside of range" << nl
334  << "Offsets:" << offsets_
335  << abort(FatalError);
336  }
337 
338  return rowi;
339 }
340 
341 
342 template<class T>
344 {
345  offsets_.clear();
346  values_.clear();
347 }
348 
349 
350 template<class T>
351 inline void Foam::CompactListList<T>::resize(const label mRows)
352 {
353  if (mRows == 0)
354  {
355  // Clear
356  offsets_.clear();
357  values_.clear();
358  }
359  else if (mRows < size())
360  {
361  // Shrink
362  offsets_.resize(mRows+1);
363  values_.resize(offsets_[mRows]);
364  }
365  else if (mRows > size())
366  {
367  // Extend number of rows, each with local size of 0
368  const label endOffset = offsets_.empty() ? 0 : offsets_.back();
369 
370  offsets_.resize(mRows+1, endOffset);
371  }
372 }
373 
374 
375 template<class T>
377 (
378  const label mRows,
379  const label nVals
380 )
381 {
382  if (mRows < 1)
383  {
384  // Enforce sizing sanity
385  offsets_.clear();
386  values_.clear();
387  }
388  else
389  {
390  offsets_.resize(mRows+1, Zero);
391  values_.resize(nVals);
392  }
393 }
394 
395 
396 template<class T>
398 (
399  const label mRows,
400  const label nVals
401 )
402 {
403  if (mRows < 1)
404  {
405  // Enforce sizing sanity
406  offsets_.clear();
407  values_.clear();
408  }
409  else
410  {
411  offsets_.resize(mRows+1, Zero);
412  values_.resize_nocopy(nVals);
413  }
414 }
415 
416 
417 template<class T>
419 (
420  const label mRows,
421  const label nVals,
422  const T& val
423 )
424 {
425  if (mRows < 1)
426  {
427  // Enforce sizing sanity
428  offsets_.clear();
429  values_.clear();
430  }
431  else
432  {
433  offsets_.resize(mRows+1, Zero);
434  values_.resize(nVals, val);
435  }
436 }
437 
438 
439 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
440 
441 template<class T>
443 (
444  const CompactListList<T>& list
445 )
446 {
447  if (this == &list)
448  {
449  return; // Self-assignment is a no-op
450  }
451 
452  offsets_ = list.offsets_,
453  values_ = list.values_;
454 }
455 
456 
457 template<class T>
459 (
460  CompactListList<T>&& list
461 )
462 {
463  if (this == &list)
464  {
465  return; // Self-assignment is a no-op
466  }
468  offsets_.transfer(list.offsets_);
469  values_.transfer(list.values_);
470 }
471 
472 
473 template<class T>
474 inline void Foam::CompactListList<T>::operator=(const T& val)
475 {
476  values_ = val;
477 }
478 
479 
480 template<class T>
482 {
483  values_ = Foam::zero{};
484 }
485 
486 
487 template<class T>
488 inline Foam::UList<T>
490 {
491  return this->localList(i);
492 }
493 
494 
495 template<class T>
496 inline const Foam::UList<T>
497 Foam::CompactListList<T>::operator[](const label i) const
498 {
499  return this->localList(i);
500 }
501 
502 
503 template<class T>
505 (
506  const label i,
507  const label j
508 )
509 {
510  return values_[toGlobal(i, j)];
511 }
512 
513 
514 template<class T>
516 (
517  const label i,
518  const label j
519 ) const
520 {
521  return values_[toGlobal(i, j)];
522 }
523 
524 
525 // ************************************************************************* //
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:598
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.
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.
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.
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 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...
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.
label length() const noexcept
The primary size (the number of rows/sublists)
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
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.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127