Loading...
Searching...
No Matches
CompactListList.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
27Class
28 Foam::CompactListList
29
30Description
31 A packed storage of objects of type <T>
32 using an offset table for access.
33
34 The offset table is the size of the number of rows+1
35 whose elements are the
36 accumulated sizes of the rows, i.e.
37 - offset[i] gives the index of first element of row i
38 - offset[i+1] - offset[i] is the number of elements in row i
39
40 Note that an empty CompactListList should have empty offsets
41 (not size 1).
42
43SourceFiles
44 CompactListList.C
45 CompactListListI.H
46 CompactListListIO.C
47
48\*---------------------------------------------------------------------------*/
49
50#ifndef Foam_CompactListList_H
51#define Foam_CompactListList_H
52
53#include "List.H"
54
55// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56
57namespace Foam
58{
59
60// Forward Declarations
61template<class T> class CompactListList;
62
63template<class T> Istream& operator>>(Istream&, CompactListList<T>&);
64template<class T> Ostream& operator<<(Ostream&, const CompactListList<T>&);
65
66// Common list types
67
70
71
72/*---------------------------------------------------------------------------*\
73 Class CompactListList Declaration
74\*---------------------------------------------------------------------------*/
75
76template<class T>
78{
79 // Private Data
80
81 //- Offset (addressing) table
82 labelList offsets_;
83
84 //- Packed (matrix) of values
85 List<T> values_;
86
87
88 // Private Member Functions
89
90 //- Report overflow at specified (non-negative) index
91 static void reportOverflowAndExit
92 (
93 const label idx,
94 const label prevOffset = -1, // The last valid offset value
95 const label count = 0 // The count to add to prevOffset
96 );
97
98 //- Construct by packing together the list of lists
99 template<class ListListType>
100 static CompactListList<T> pack_impl
101 (
102 const ListListType& lists,
103 const bool checkOverflow = false
104 );
105
106 //- Avoid poorly sized offsets/values
107 inline void enforceSizeSanity();
108
109 //- The max of localSizes, excluding the specified row
110 label maxNonLocalSize(const label rowi) const;
111
112 //- Change the offset table based on the sizes
113 //- and return the total number of values to be stored but
114 //- does not touch the values array (unless the list is empty)
115 label resize_offsets
116 (
117 const labelUList& listSizes,
118 const bool checkOverflow = false
119 );
120
121public:
122
123 // STL type definitions
124
125 //- The value type the list contains
126 typedef T value_type;
127
128 //- The pointer type for non-const access to value_type items
129 typedef T* pointer;
130
131 //- The pointer type for const access to value_type items
132 typedef const T* const_pointer;
133
134 //- The type used for storing into value_type objects
135 typedef T& reference;
136
137 //- The type used for reading from constant value_type objects.
138 typedef const T& const_reference;
139
140 //- The type to represent the size of a CompactListList
141 typedef label size_type;
143
144 // Static Member Functions
145
146 //- Return a null CompactListList (reference to a nullObject).
148 {
150 }
151
152 //- Construct by packing together the list of lists
153 template<class SubListType = List<T>>
155 (
156 const UList<SubListType>& lists,
157 const bool checkOverflow = false
158 );
159
160 //- Construct by packing together an indirect list of lists
161 template<class SubListType, class Addr>
163 (
165 const bool checkOverflow = false
166 );
167
168
169 // Constructors
171 //- Default construct
172 CompactListList() noexcept = default;
173
174 //- Copy construct
175 inline CompactListList(const CompactListList<T>& list);
176
177 //- Move construct
178 inline CompactListList(CompactListList<T>&& list);
180 //- Copy/move construct as specified.
181 inline CompactListList(CompactListList<T>& list, bool reuse);
182
183 //- Construct from number of rows and number of values.
184 inline CompactListList(const label mRows, const label nVals);
185
186 //- Construct from number of rows, number of values
187 //- initializing all elements to zero
188 inline CompactListList
190 const label mRows, const label nVals, Foam::zero
191 );
192
193 //- Construct from number of rows, number of values
194 //- and uniform value for all elements.
195 inline CompactListList(const label mRows, const label nVals, const T&);
196
197 //- Construct given list of row-sizes.
198 explicit CompactListList(const labelUList& listSizes);
199
200 //- Construct given list of row-sizes and a uniform value
201 CompactListList(const labelUList& listSizes, const T& val);
202
203 //- Construct from Istream.
204 explicit CompactListList(Istream& is);
205
206 //- Clone
207 inline autoPtr<CompactListList<T>> clone() const;
208
209
210 // Member Functions
212 // Access
213
214 //- True if the number of rows/sublists is zero
215 inline bool empty() const noexcept;
217 //- True if content is a single row/sublist only.
218 //- Such content could be flattened out into a straight list
219 //- (for example).
220 inline bool single() const noexcept;
222 //- The primary size (the number of rows/sublists)
223 inline label length() const noexcept;
224
225 //- The primary size (the number of rows/sublists)
226 inline label size() const noexcept;
228 //- The total addressed size, which corresponds to the
229 //- end (back) offset and also the sum of all localSizes.
230 inline label totalSize() const noexcept;
231
232 //- Return the offset table (= size()+1)
233 const labelList& offsets() const noexcept { return offsets_; }
234
235 //- Return non-const access to the offset table
236 labelList& offsets() noexcept { return offsets_; }
237
238 //- Return the packed values
239 const List<T>& values() const noexcept { return values_; }
240
241 //- Return non-const access to the packed values
242 List<T>& values() noexcept { return values_; }
243
244
245 //- Return const pointer to the first data in values()
246 inline const T* cdata() const noexcept;
247
248 //- Return pointer to the first data in values()
249 inline T* data() noexcept;
250
251 //- Return const pointer to underlying values storage,
252 //- reinterpreted as byte data.
253 // \note Only meaningful for contiguous data
254 inline const char* cdata_bytes() const noexcept;
255
256 //- Return pointer to underlying values storage,
257 //- reinterpreted as byte data.
258 // \note Only meaningful for contiguous data
259 inline char* data_bytes() noexcept;
260
261 //- Number of contiguous bytes for the values data,
262 //- no runtime check that the type is actually contiguous
263 // \note Only meaningful for contiguous data
264 inline std::streamsize size_bytes() const noexcept;
265
266 //- Number of contiguous bytes for the values data,
267 //- runtime FatalError if type is not contiguous
268 std::streamsize byteSize() const;
269
270
271 // Queries
272
273 //- The local row sizes. Same as localSizes
274 inline labelList sizes() const;
275
276 //- The local row starts
277 inline const labelUList localStarts() const;
279 //- The local row sizes
280 labelList localSizes() const;
281
282 //- Starting offset for given row
283 inline label localStart(const label i) const;
284
285 //- End offset (exclusive) for given row
286 inline label localEnd(const label i) const;
287
288 //- Size of given row
289 inline label localSize(const label i) const;
290
291 //- Return start/size ranges for all sub-lists
292 List<labelRange> ranges() const;
293
294 //- Return start/size range for given sub-list
295 labelRange range(const label i) const;
296
297 //- The max row length used
298 inline label maxSize() const;
300
301 // Edit
302
303 //- Clear addressing and contents
304 inline void clear();
305
306 //- Reset size of CompactListList.
307 // \note this form only allows truncation of the CompactListList.
308 inline void resize(const label mRows);
310 //- Redimension CompactListList
311 inline void resize(const label mRows, const label nVals);
312
313 //- Redimension \em without preserving existing content
314 inline void resize_nocopy(const label mRows, const label nVals);
316 //- Redimension CompactListList and fill new elements with value.
317 inline void resize(const label mRows, const label nVals, const T&);
318
319 //- Reset dimensions of CompactListList
320 void resize(const labelUList& listSizes);
321
322 //- Reset dimensions of CompactListList
323 //- \em without preserving existing content
324 void resize_nocopy(const labelUList& listSizes);
325
326 //- Alter local addressing size for given row, does not change content
327 void setLocalSize(const label rowi, const label len);
329
330 //- Swap contents
331 void swap(CompactListList<T>& other);
332
333 //- Transfer contents into this and annul the argument
334 void transfer(CompactListList<T>& list);
335
337 // Global addressing queries
338
339 //- From local index on rowi to global (flat) indexing
340 //- into packed values
341 inline label toGlobal(const label rowi, const label i) const;
342
343 //- From global to local index on rowi
344 inline label toLocal(const label rowi, const label i) const;
345
346 //- Find row where global index comes from. Binary search.
347 // \return -1 if out-of-bounds
348 inline label findRow(const label i) const;
349
350 //- Which row does global index come from? Binary search.
351 // FatalError if out-of-bounds
352 inline label whichRow(const label i) const;
353
354
355 // Pack / Unpack
356
357 //- Unpack sub-list copies in the range defined by \p pos and \p len
358 //- with bounding behaviour like List::slice() by copy constructing
359 //- begin at the destination iterator \p d_iter.
360 //
361 // \returns Output iterator to the element in the destination range,
362 // one past the last element copied.
363 template<class SubListType, class OutputIter>
364 OutputIter copy_unpack
365 (
367 OutputIter d_iter,
369 const label pos = 0,
371 label len = -1
372 ) const;
374 //- Unpack sub-list copies in the specified range.
375 //
376 // \returns Output iterator to the element in the destination range,
377 // one past the last element copied.
378 template<class SubListType, class OutputIter>
379 OutputIter copy_unpack
380 (
382 OutputIter d_iter,
384 const labelRange& range
385 ) const;
386
387 //- Unpack sub-list copies for the specified indices
388 //
389 // \returns Output iterator to the element in the destination range,
390 // one past the last element copied.
391 template<class SubListType, class OutputIter>
392 OutputIter copy_unpack
395 OutputIter d_iter,
397 const labelUList& indices
398 ) const;
399
400 //- Return non-compact list of lists
401 template<class SubListType = List<T>>
402 List<SubListType> unpack() const;
403
404 //- Return non-compact list of lists for the range of sub-lists
405 template<class SubListType = List<T>>
406 List<SubListType> unpack(const labelRange& range) const;
407
408 //- Return non-compact list of lists for specified indices
409 template<class SubListType = List<T>>
410 List<SubListType> unpack(const labelUList& indices) const;
411
412
413 // Assignment
414
415 //- Copy assignment
416 inline void operator=(const CompactListList<T>& list);
417
418 //- Move assignment
419 inline void operator=(CompactListList<T>&& list);
420
421 //- Assignment of all entries to the given value
422 inline void operator=(const T& val);
424 //- Assignment of all entries to zero
425 inline void operator=(Foam::zero);
426
427
428 // Row-based access
429
430 //- Return const access to sub-list (no subscript checking)
431 inline const SubList<T> localList(const label i) const;
432
433 //- Return non-const access to sub-list (no subscript checking)
434 inline SubList<T> localList(const label i);
435
436 //- Return const access to sub-list (no subscript checking)
437 inline const SubList<T> operator[](const label i) const;
438
439 //- Return non-const access to sub-list (no subscript checking)
440 inline SubList<T> operator[](const label i);
441
442
443 // Element access
445 //- Return subscript-checked element.
446 inline T& operator()(const label i, const label j);
447
448 //- Return const subscript-checked element.
449 inline const T& operator()(const label i, const label j) const;
451
452 // Iteration (FUTURE)
453
454
455 // Reading/writing
456
457 //- Read CompactListList as offsets/values pair from Istream,
458 //- discards current list contents
460
461 //- Write CompactListList as offsets/values pair
462 Ostream& writeList(Ostream& os, const label shortLen=0) const;
463
464 //- Write CompactListList as a formatted matrix of values (ASCII)
465 Ostream& writeMatrix(Ostream& os, const label shortLen=0) const;
466
467
468 // IO Operators
470 //- Read CompactListList offsets/values pair from Istream,
471 //- discarding existing contents
472 friend Istream& operator>> <T>
473 (
474 Istream&,
476 );
477
478 //- Write CompactListList as offsets/values pair
479 friend Ostream& operator<< <T>
480 (
481 Ostream&,
482 const CompactListList<T>&
483 );
484
485
486 // Housekeeping
487
488 //- Const access to the packed values
489 FOAM_DEPRECATED_STRICT(2022-03, "values()")
490 const List<T>& m() const noexcept { return values_; }
491
492 //- Non-const access to the packed values
493 FOAM_DEPRECATED_STRICT(2022-03, "values()")
494 List<T>& m() noexcept { return values_; }
495
496 //- Return flat index into packed values
497 label index(const label rowi, const label colj) const
499 return this->toGlobal(rowi, colj);
500 }
501
502 //- Get column within specified row that corresponds to global index
503 label whichColumn(const label rowi, const label i) const
504 {
505 return this->toLocal(rowi, i);
506 }
507
508 //- Redimension - same as resize()
509 void setSize(const label mRows)
510 {
511 this->resize(mRows);
512 }
513
514 //- Redimension - same as resize()
515 void setSize(const label mRows, const label nVals)
517 this->resize(mRows, nVals);
518 }
519
520 //- Redimension - same as resize()
521 void setSize(const label mRows, const label nVals, const T& val)
522 {
523 this->resize(mRows, nVals, val);
524 }
525
526 //- Reset sizes - same as resize()
527 void setSize(const labelUList& listSizes)
528 {
529 this->resize(listSizes);
530 }
531};
533
534// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
535
536template<class T>
538{
539 return list.readList(is);
540}
541
542template<class T>
546}
547
548
549// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
551// Does not need std::swap or Foam::Swap() specialization
552// since CompactListList is MoveConstructible and MoveAssignable
553
554
555// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
557} // End namespace Foam
558
559// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
560
561#include "CompactListListI.H"
562
563// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
565#ifdef NoRepository
566 #include "CompactListList.C"
567 #include "CompactListListIO.C"
568#endif
570// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
571
572#endif
573
574// ************************************************************************* //
A packed storage of objects of type <T> using an offset table for access.
bool single() const noexcept
labelList & offsets() noexcept
Return non-const access to the offset table.
List< SubListType > unpack() const
const List< label > & m() const noexcept
void resize_nocopy(const label mRows, const label nVals)
char * data_bytes() noexcept
label localSize(const label i) const
label totalSize() const noexcept
void setSize(const labelUList &listSizes)
Reset sizes - same as resize().
void setSize(const label mRows, const label nVals, const T &val)
Redimension - same as resize().
bool empty() const noexcept
Ostream & writeMatrix(Ostream &os, const label shortLen=0) const
const labelList & offsets() const noexcept
const T * cdata() const noexcept
Return const pointer to the first data in values().
static const CompactListList< T > & null() noexcept
Return a null CompactListList (reference to a nullObject).
const List< T > & values() const noexcept
Return the packed values.
label whichRow(const label i) const
List< labelRange > ranges() const
label findRow(const label i) const
static CompactListList< T > pack(const IndirectListBase< SubListType, Addr > &lists, const bool checkOverflow=false)
Construct by packing together an indirect list of lists.
const labelUList localStarts() const
labelRange range(const label i) const
static CompactListList< T > pack(const UList< SubListType > &lists, const bool checkOverflow=false)
Construct by packing together the list of lists.
void transfer(CompactListList< label > &list)
label length() const noexcept
label localStart(const label i) const
label size() const noexcept
std::streamsize byteSize() const
const SubList< label > localList(const label i) const
List< T > & values() noexcept
Return non-const access to the packed values.
void setLocalSize(const label rowi, const label len)
void resize(const label mRows)
const char * cdata_bytes() const noexcept
label index(const label rowi, const label colj) const
Return flat index into packed values.
labelList localSizes() const
label localEnd(const label i) const
Ostream & writeList(Ostream &os, const label shortLen=0) const
Istream & readList(Istream &is)
void swap(CompactListList< label > &other)
std::streamsize size_bytes() const noexcept
label whichColumn(const label rowi, const label i) const
Get column within specified row that corresponds to global index.
label toLocal(const label rowi, const label i) const
void setSize(const label mRows, const label nVals)
Redimension - same as resize().
CompactListList() noexcept=default
Default construct.
void setSize(const label mRows)
Redimension - same as resize().
autoPtr< CompactListList< label > > clone() const
OutputIter copy_unpack(OutputIter d_iter, const label pos=0, label len=-1) const
label toGlobal(const label rowi, const label i) const
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A non-owning sub-view of a List (allocated or unallocated storage).
Definition SubList.H:61
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A range or interval of labels defined by a start and a size.
Definition labelRange.H:66
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
dimensionedScalar pos(const dimensionedScalar &ds)
const T & NullObjectRef() noexcept
Const reference (of type T) to the nullObject.
Definition nullObject.H:228
List< label > labelList
A List of labels.
Definition List.H:62
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
CompactListList< label > labelCompactListList
A CompactListList of labels.
Istream & operator>>(Istream &, directionInfo &)
const direction noexcept
Definition scalarImpl.H:265
UList< label > labelUList
A UList of labels.
Definition UList.H:75
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
#define FOAM_DEPRECATED_STRICT(since, replacement)
Definition stdFoam.H:53
Number of items before requiring line-breaks in the list output.
Definition ListPolicy.H:56