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