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 -------------------------------------------------------------------------------
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 Class
28  Foam::CompactListList
29 
30 Description
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 
43 SourceFiles
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 
57 namespace Foam
58 {
59 
60 // Forward Declarations
61 template<class T> class CompactListList;
62 
63 template<class T> Istream& operator>>(Istream&, CompactListList<T>&);
64 template<class T> Ostream& operator<<(Ostream&, const CompactListList<T>&);
65 
66 // Common list types
67 
70 
71 
72 /*---------------------------------------------------------------------------*\
73  Class CompactListList Declaration
74 \*---------------------------------------------------------------------------*/
75 
76 template<class T>
77 class CompactListList
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 
121 public:
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  {
149  return NullObjectRef<CompactListList<T>>();
150  }
151 
152  //- Construct by packing together the list of lists
153  template<class SubListType = List<T>>
154  static CompactListList<T> pack
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);
179 
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
189  (
190  const label mRows, const label nVals, const 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
211 
212  // Access
213 
214  //- True if the number of rows/sublists is zero
215  inline bool empty() const noexcept;
216 
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;
221 
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;
227 
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;
278 
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);
315 
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);
328 
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 
336 
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;
373 
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
393  (
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);
423 
424  //- Assignment of all entries to zero
425  inline void operator=(const 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
444 
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;
450 
451 
452  // Iteration (FUTURE)
453 
454 
455  // Reading/writing
456 
457  //- Read CompactListList as offsets/values pair from Istream,
458  //- discards current list contents
459  Istream& readList(Istream& is);
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
469 
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
498  {
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)
516  {
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 };
532 
533 
534 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
535 
536 template<class T>
537 Istream& operator>>(Istream& is, CompactListList<T>& list)
538 {
539  return list.readList(is);
540 }
541 
542 template<class T>
543 Ostream& operator<<(Ostream& os, const CompactListList<T>& list)
544 {
545  return list.writeList(os, Detail::ListPolicy::short_length<T>::value);
546 }
547 
548 
549 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
550 
551 // Does not need std::swap or Foam::Swap() specialization
552 // since CompactListList is MoveConstructible and MoveAssignable
553 
554 
555 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
556 
557 } // End namespace Foam
558 
559 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
560 
561 #include "CompactListListI.H"
562 
563 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
564 
565 #ifdef NoRepository
566  #include "CompactListList.C"
567  #include "CompactListListIO.C"
568 #endif
569 
570 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
571 
572 #endif
573 
574 // ************************************************************************* //
label index(const label rowi, const label colj) const
Return flat index into packed values.
label localStart(const label i) const
Starting offset for given row.
const T & const_reference
The type used for reading from constant value_type objects.
T * pointer
The pointer type for non-const access to value_type items.
T & reference
The type used for storing into value_type objects.
const labelList & offsets() const noexcept
Return the offset table (= size()+1)
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.
label whichColumn(const label rowi, const label i) const
Get column within specified row that corresponds to global index.
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)
CompactListList< label > labelCompactListList
A CompactListList of labels.
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write CompactListList as offsets/values pair.
label toGlobal(const label rowi, const label i) const
From local index on rowi to global (flat) indexing into packed values.
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:52
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void swap(CompactListList< T > &other)
Swap contents.
char * data_bytes() noexcept
Return pointer to underlying values storage, reinterpreted as byte data.
static CompactListList< T > pack(const UList< SubListType > &lists, const bool checkOverflow=false)
Construct by packing together the list of lists.
static const CompactListList< T > & null() noexcept
Return a null CompactListList (reference to a nullObject).
void clear()
Clear addressing and contents.
Base for lists with indirect addressing, templated on the list contents type and the addressing type...
UList< label > labelUList
A UList of labels.
Definition: UList.H:78
label localSize(const label i) const
Size of given row.
void transfer(CompactListList< T > &list)
Transfer contents into this and annul the argument.
const char * cdata_bytes() const noexcept
Return const pointer to underlying values storage, reinterpreted as byte data.
void setLocalSize(const label rowi, const label len)
Alter local addressing size for given row, does not change content.
label findRow(const label i) const
Find row where global index comes from. Binary search.
List< labelRange > ranges() const
Return start/size ranges for all sub-lists.
dimensionedScalar pos(const dimensionedScalar &ds)
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
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.
const T * const_pointer
The pointer type for const access to value_type items.
List< SubListType > unpack() const
Return non-compact list of lists.
labelRange range(const label i) const
Return start/size range for given sub-list.
labelList sizes() const
The local row sizes. Same as localSizes.
Istream & operator>>(Istream &, directionInfo &)
T * data() noexcept
Return pointer to the first data in values()
CompactListList() noexcept=default
Default construct.
label size_type
The type to represent the size of a CompactListList.
const T * cdata() const noexcept
Return const pointer to the first data in values()
void resize(const label mRows)
Reset size of CompactListList.
#define FOAM_DEPRECATED_STRICT(since, replacement)
Definition: stdFoam.H:55
const SubList< T > localList(const label i) const
Return const access to sub-list (no subscript checking)
std::streamsize byteSize() const
Number of contiguous bytes for the values data, runtime FatalError if type is not contiguous...
A packed storage of objects of type <T> using an offset table for access.
void setSize(const label mRows)
Redimension - same as resize()
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
T value_type
The value type the list contains.
OBJstream os(runTime.globalPath()/outputName)
label totalSize() const noexcept
The total addressed size, which corresponds to the end (back) offset and also the sum of all localSiz...
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Ostream & writeMatrix(Ostream &os, const label shortLen=0) const
Write CompactListList as a formatted matrix of values (ASCII)
Istream & readList(Istream &is)
Read CompactListList as offsets/values pair from Istream, discards current list contents.
OutputIter copy_unpack(OutputIter d_iter, const label pos=0, label len=-1) const
Unpack sub-list copies in the range defined by pos and len with bounding behaviour like List::slice()...
labelList localSizes() const
The local row sizes.
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)
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
const List< T > & values() const noexcept
Return the packed values.
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.
List< label > labelList
A List of labels.
Definition: List.H:62
const List< T > & m() const noexcept
Const access to the packed values.
Namespace for OpenFOAM.