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-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 Class
28  Foam::CompactListList
29 
30 Description
31  A packed storage unstructured matrix 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 index
86  static void reportOverflowAndExit
87  (
88  const label idx,
89  const labelUList& listSizes = labelUList::null()
90  );
91 
92  //- Construct by packing together the list of lists
93  template<class ListListType>
94  static CompactListList<T> packImpl
95  (
96  const ListListType& lists,
97  const bool checkOverflow = false
98  );
99 
100  //- Avoid poorly sized offsets/values
101  void enforceSizeSanity();
102 
103  //- The max of localSizes, excluding the specified row
104  label maxNonLocalSize(const label rowi) const;
105 
106 
107 public:
108 
109  // STL type definitions
110 
111  //- The value type the list contains
112  typedef T value_type;
113 
114  //- The pointer type for non-const access to value_type items
115  typedef T* pointer;
116 
117  //- The pointer type for const access to value_type items
118  typedef const T* const_pointer;
119 
120  //- The type used for storing into value_type objects
121  typedef T& reference;
122 
123  //- The type used for reading from constant value_type objects.
124  typedef const T& const_reference;
125 
126  //- The type to represent the size of a CompactListList
127  typedef label size_type;
128 
129 
130  // Static Member Functions
132  //- Return a CompactListList reference to a nullObject
133  inline static const CompactListList<T>& null();
134 
135  //- Construct by packing together the list of lists
136  template<class SubListType = List<T>>
137  static CompactListList<T> pack
138  (
139  const UList<SubListType>& lists,
140  const bool checkOverflow = false
141  );
142 
143  //- Construct by packing together an indirect list of lists
144  template<class SubListType, class Addr>
145  static CompactListList<T> pack
146  (
148  const bool checkOverflow = false
149  );
150 
151 
152  // Constructors
153 
154  //- Default construct
155  CompactListList() noexcept = default;
156 
157  //- Copy construct
158  inline CompactListList(const CompactListList<T>& list);
159 
160  //- Move construct
161  inline CompactListList(CompactListList<T>&& list);
162 
163  //- Copy/move construct as specified.
164  inline CompactListList(CompactListList<T>& list, bool reuse);
165 
166  //- Construct from number of rows and number of values.
167  inline CompactListList(const label mRows, const label nVals);
168 
169  //- Construct from number of rows, number of values
170  //- initializing all elements to zero
171  inline CompactListList
172  (
173  const label mRows, const label nVals, const Foam::zero
174  );
175 
176  //- Construct from number of rows, number of values
177  //- and uniform value for all elements.
178  inline CompactListList(const label mRows, const label nVals, const T&);
179 
180  //- Construct given list of row-sizes.
181  explicit CompactListList(const labelUList& listSizes);
182 
183  //- Construct given list of row-sizes and a uniform value
184  CompactListList(const labelUList& listSizes, const T& val);
185 
186  //- Construct from Istream.
187  explicit CompactListList(Istream& is);
188 
189  //- Clone
190  inline autoPtr<CompactListList<T>> clone() const;
191 
192 
193  // Member Functions
194 
195  // Access
196 
197  //- True if the number of rows/sublists is zero
198  inline bool empty() const noexcept;
199 
200  //- The primary size (the number of rows/sublists)
201  inline label size() const noexcept;
202 
203  //- The total addressed size
204  inline label totalSize() const;
205 
206  //- Return the offset table (= size()+1)
207  inline const labelList& offsets() const noexcept;
208 
209  //- Return non-const access to the offset table
210  inline labelList& offsets() noexcept;
211 
212  //- Return the packed matrix of values
213  inline const List<T>& values() const noexcept;
214 
215  //- Return non-const access to the packed matrix of values
216  inline List<T>& values() noexcept;
217 
218 
219  //- Return const pointer to the first data in values()
220  inline const T* cdata() const noexcept;
221 
222  //- Return pointer to the first data in values()
223  inline T* data() noexcept;
224 
225  //- Return const pointer to underlying values storage,
226  //- reinterpreted as byte data.
227  // \note Only meaningful for contiguous data
228  inline const char* cdata_bytes() const noexcept;
229 
230  //- Return pointer to underlying values storage,
231  //- reinterpreted as byte data.
232  // \note Only meaningful for contiguous data
233  inline char* data_bytes() noexcept;
234 
235  //- Number of contiguous bytes for the values data,
236  //- no runtime check that the type is actually contiguous
237  // \note Only meaningful for contiguous data
238  inline std::streamsize size_bytes() const noexcept;
239 
240  //- Number of contiguous bytes for the values data,
241  //- runtime FatalError if type is not contiguous
242  std::streamsize byteSize() const;
243 
244 
245  // Queries
246 
247  //- The local row sizes. Same as localSizes
248  inline labelList sizes() const;
249 
250  //- The local row starts
251  inline const labelUList localStarts() const;
252 
253  //- The local row sizes
254  labelList localSizes() const;
255 
256  //- Starting offset for given row
257  inline label localStart(const label i) const;
258 
259  //- End offset (exclusive) for given row
260  inline label localEnd(const label i) const;
261 
262  //- Size of given row
263  inline label localSize(const label i) const;
264 
265  //- Return start/size ranges for all sub-lists
266  List<labelRange> ranges() const;
267 
268  //- Return start/size range for given sub-list
269  labelRange range(const label i) const;
270 
271  //- The max row length used
272  inline label maxSize() const;
273 
274 
275  // Edit
276 
277  //- Clear addressing and contents
278  inline void clear();
279 
280  //- Reset size of CompactListList.
281  // \note this form only allows truncation of the CompactListList.
282  inline void resize(const label mRows);
283 
284  //- Redimension CompactListList
285  inline void resize(const label mRows, const label nVals);
286 
287  //- Redimension \em without preserving existing content
288  inline void resize_nocopy(const label mRows, const label nVals);
289 
290  //- Redimension CompactListList and fill new elements with value.
291  inline void resize(const label mRows, const label nVals, const T&);
292 
293  //- Reset dimensions of CompactListList
294  void resize(const labelUList& listSizes);
295 
296  //- Alter local addressing size for given row, does not change content
297  void setLocalSize(const label rowi, const label len);
298 
299 
300  //- Redimension - same as resize()
301  inline void setSize(const label mRows);
302 
303  //- Redimension - same as resize()
304  inline void setSize(const label mRows, const label nVals);
305 
306  //- Redimension - same as resize()
307  inline void setSize(const label mRows, const label nVals, const T&);
308 
309  //- Reset sizes - same as resize()
310  inline void setSize(const labelUList& listSizes);
311 
312  //- Swap contents
313  void swap(CompactListList<T>& other);
314 
315  //- Transfer contents into this and annul the argument
316  void transfer(CompactListList<T>& list);
317 
318 
319  // Global addressing queries
320 
321  //- From local index on rowi to global (flat) indexing
322  //- into packed values
323  inline label toGlobal(const label rowi, const label i) const;
324 
325  //- From global to local index on rowi
326  inline label toLocal(const label rowi, const label i) const;
327 
328  //- Find row where global index comes from. Binary search.
329  // \return -1 if out-of-bounds
330  inline label findRow(const label i) const;
331 
332  //- Which row does global index come from? Binary search.
333  // FatalError if out-of-bounds
334  inline label whichRow(const label i) const;
335 
336 
337  // Pack / Unpack
338 
339  //- Return non-compact list of lists
340  template<class SubListType = List<T>>
341  List<SubListType> unpack() const;
342 
343  //- Return non-compact list of lists for the range of sub-lists
344  template<class SubListType = List<T>>
345  List<SubListType> unpack(const labelRange& range) const;
346 
347 
348  // Assignment
349 
350  //- Copy assignment
351  inline void operator=(const CompactListList<T>& list);
352 
353  //- Move assignment
354  inline void operator=(CompactListList<T>&& list);
355 
356  //- Assignment of all entries to the given value
357  inline void operator=(const T& val);
358 
359  //- Assignment of all entries to zero
360  inline void operator=(const Foam::zero);
361 
362 
363  // Row-based access
364 
365  //- Return non-const access to sub-list (no subscript checking)
366  inline UList<T> localList(const label i);
367 
368  //- Return const access to sub-list (no subscript checking)
369  inline const UList<T> localList(const label i) const;
370 
371  //- Return row as UList - same as localList method
372  inline UList<T> operator[](const label i);
373 
374  //- Return row as const UList - same as localList method
375  inline const UList<T> operator[](const label i) const;
376 
377 
378  // Element access
379 
380  //- Return subscript-checked element.
381  inline T& operator()(const label i, const label j);
382 
383  //- Return const subscript-checked element.
384  inline const T& operator()(const label i, const label j) const;
385 
386 
387  // Reading/writing
388 
389  //- Read CompactListList as offsets/values pair from Istream,
390  //- discards current list contents
391  Istream& readList(Istream& is);
392 
393  //- Write CompactListList as offsets/values pair
394  Ostream& writeList(Ostream& os, const label shortLen=0) const;
395 
396 
397  // IO Operators
398 
399  //- Read CompactListList offsets/values pair from Istream,
400  //- discarding existing contents
401  friend Istream& operator>> <T>
402  (
403  Istream&,
405  );
406 
407  //- Write CompactListList as offsets/values pair
408  friend Ostream& operator<< <T>
409  (
410  Ostream&,
411  const CompactListList<T>&
412  );
413 
414 
415  // Housekeeping
416 
417  //- Const access to the packed matrix of values
418  const List<T>& m() const noexcept { return values_; }
419 
420  //- Non-const access to the packed matrix of values
421  List<T>& m() noexcept { return values_; }
422 
423  //- Return flat index into packed values
424  label index(const label rowi, const label colj) const
425  {
426  return this->toGlobal(rowi, colj);
427  }
428 
429  //- Get column within specified row that corresponds to global index
430  label whichColumn(const label rowi, const label i) const
431  {
432  return this->toLocal(rowi, i);
433  }
434 };
435 
436 
437 // Note: uses default Foam::Swap (move construct/assignment)
438 
439 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
440 
441 template<class T>
442 Istream& operator>>(Istream& is, CompactListList<T>& list)
443 {
444  return list.readList(is);
445 }
446 
447 template<class T>
448 Ostream& operator<<(Ostream& os, const CompactListList<T>& list)
449 {
450  return list.writeList(os, Detail::ListPolicy::short_length<T>::value);
451 }
452 
453 
454 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
455 
456 } // End namespace Foam
457 
458 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
459 
460 #include "CompactListListI.H"
461 
462 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
463 
464 #ifdef NoRepository
465  #include "CompactListList.C"
466  #include "CompactListListIO.C"
467 #endif
468 
469 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
470 
471 #endif
472 
473 // ************************************************************************* //
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.
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)
const labelList & offsets() const noexcept
Return the offset table (= size()+1)
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:51
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.
const List< T > & values() const noexcept
Return the packed matrix of values.
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.
label totalSize() const
The total addressed size.
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 &)
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.
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:99
std::streamsize byteSize() const
Number of contiguous bytes for the values data, runtime FatalError if type is not contiguous...
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()
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
const direction noexcept
Definition: Scalar.H:258
T value_type
The value type the list contains.
OBJstream os(runTime.globalPath()/outputName)
Database for solution data, solver performance and other reduced data.
Definition: data.H:52
static const UList< label > & null()
Return a UList reference to a nullObject.
Definition: UListI.H:45
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Istream & readList(Istream &is)
Read CompactListList as offsets/values pair from Istream, discards current list contents.
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.
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.
List< label > labelList
A List of labels.
Definition: List.H:62
const List< T > & m() const noexcept
Const access to the packed matrix of values.
Namespace for OpenFOAM.