CompactListList.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::CompactListList
26 
27 Description
28  A packed storage unstructured matrix of objects of type <T>
29  using an offset table for access.
30 
31  The offset table is the size of the number of rows+1
32  whose elements are the
33  accumulated sizes of the rows, i.e.
34  - offset[i] gives the index of first element of row i
35  - offset[i+1] - offset[i] is the number of elements in row i
36 
37  Storage is allocated on free-store during construction.
38 
39  As a special case a null-constructed CompactListList has an empty
40  offsets_ (instead of size 1).
41 
42 SourceFiles
43  CompactListList.C
44  CompactListListI.H
45  CompactListListIO.C
46 
47 \*---------------------------------------------------------------------------*/
48 
49 #ifndef CompactListList_H
50 #define CompactListList_H
51 
52 #include "labelList.H"
53 
54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55 
56 namespace Foam
57 {
58 
59 // Forward declaration of friend functions and operators
60 
61 template<class T, class Container> class CompactListList;
62 
63 template<class T, class Container> Istream& operator>>
64 (
65  Istream&,
66  CompactListList<T, Container>&
67 );
68 template<class T, class Container> Ostream& operator<<
69 (
70  Ostream&,
71  const CompactListList<T, Container>&
72 );
73 
74 
75 /*---------------------------------------------------------------------------*\
76  Class CompactListList Declaration
77 \*---------------------------------------------------------------------------*/
78 
79 template<class T, class Container = List<T>>
80 class CompactListList
81 {
82  // Private data
83 
84  label size_;
85 
86  //- Offset table
87  List<label> offsets_;
88 
89  //- Packed matrix of data
90  List<T> m_;
91 
92 
93 public:
94 
95  // Static Member Functions
96 
97  //- Return a null CompactListList
98  inline static const CompactListList<T, Container>& null();
99 
100  // Constructors
101 
102  //- Null constructor.
103  inline CompactListList();
104 
105  //- Construct by converting given List<List<T>>
106  explicit CompactListList(const List<Container>&);
107 
108  //- Construct given size of offset table (number of rows)
109  // and number of data.
110  inline CompactListList(const label mRows, const label nData);
111 
112  //- Construct given size of offset table (number of rows),
113  // the number of data and a value for all elements.
114  inline CompactListList(const label mRows, const label nData, const T&);
115 
116  //- Construct given list of row-sizes.
117  explicit CompactListList(const labelUList& rowSizes);
118 
119  //- Construct given list of row-sizes
120  CompactListList(const labelUList& rowSizes, const T&);
121 
122  //- Construct by transferring the parameter contents
123  explicit CompactListList(const Xfer<CompactListList<T, Container>>&);
124 
125  //- Construct as copy or re-use as specified.
126  CompactListList(CompactListList<T, Container>&, bool reuse);
127 
128  //- Construct from Istream.
129  CompactListList(Istream&);
130 
131  //- Clone
132  inline autoPtr<CompactListList<T, Container>> clone() const;
133 
134 
135  // Member Functions
136 
137  // Access
138 
139  //- Return the primary size, i.e. the number of rows
140  inline label size() const;
141 
142  //- Return true if the number of rows is zero
143  inline bool empty() const;
144 
145  //- Return the offset table (= size()+1)
146  inline const List<label>& offsets() const;
147 
148  //- Return non-const access to the offset table
149  inline List<label>& offsets();
150 
151  //- Return the packed matrix of data
152  inline const List<T>& m() const;
153 
154  //- Return non-const access to the packed matrix of data
155  inline List<T>& m();
156 
157 
158  // Edit
159 
160  //- Reset size of CompactListList.
161  // This form only allows contraction of the CompactListList.
162  void setSize(const label mRows);
163 
164  //- Reset size of CompactListList.
165  void setSize(const label mRows, const label nData);
166 
167  //- Reset sizes of CompactListList and value for new elements.
168  void setSize(const label mRows, const label nData, const T&);
169 
170  //- Reset size of CompactListList.
171  void setSize(const labelUList& rowSizes);
172 
173  //- Reset size of CompactListList.
174  // This form only allows contraction of the CompactListList.
175  inline void resize(const label mRows);
176 
177  //- Reset size of CompactListList.
178  inline void resize(const label mRows, const label nData);
179 
180  //- Reset sizes of CompactListList and value for new elements.
181  inline void resize(const label mRows, const label nData, const T&);
182 
183  //- Reset size of CompactListList.
184  inline void resize(const labelUList& rowSizes);
185 
186  //- Clear the CompactListList, i.e. set sizes to zero.
187  void clear();
188 
189  //- Return sizes (to be used e.g. for construction)
190  labelList sizes() const;
191 
192  //- Transfer the contents of the argument CompactListList
193  // into this CompactListList and annul the argument list.
194  void transfer(CompactListList<T, Container>&);
195 
196  //- Transfer the contents to the Xfer container
197  inline Xfer<CompactListList<T, Container>> xfer();
198 
199  // Other
200 
201  //- Return index into m
202  inline label index(const label row, const label col) const;
203 
204  //- Get row for index into m.
205  inline label whichRow(const label index) const;
206 
207  //- Get column index (j) given above row
208  inline label whichColumn(const label row, const label index) const;
209 
210 
211  // Member operators
212 
213  //- Return subscript-checked row as UList.
214  inline UList<T> operator[](const label i);
215 
216  //- Return const subscript-checked row as UList.
217  inline const UList<T> operator[](const label i) const;
218 
219  //- Return subscript-checked element.
220  inline T& operator()(const label i, const label j);
221 
222  //- Return const subscript-checked element.
223  inline const T& operator()(const label i, const label j) const;
224 
225  //- Return as List<Container>
226  List<Container> operator()() const;
227 
228  //- Assignment of all entries to the given value
229  inline void operator=(const T&);
230 
231 
232  // Istream operator
233 
234  //- Read CompactListList from Istream, discarding contents
235  // of existing CompactListList.
236  friend Istream& operator>> <T, Container>
237  (
238  Istream&,
239  CompactListList<T, Container>&
240  );
241 
242  // Write CompactListList to Ostream.
243  friend Ostream& operator<< <T, Container>
244  (
245  Ostream&,
246  const CompactListList<T, Container>&
247  );
248 };
249 
250 
251 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
252 
253 } // End namespace Foam
254 
255 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
256 
257  #include "CompactListListI.H"
258 
259 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
260 
261 #ifdef NoRepository
262  #include "CompactListList.C"
263 #endif
264 
265 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
266 
267 #endif
268 
269 // ************************************************************************* //
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
const List< T > & m() const
Return the packed matrix of data.
void clear()
Clear the CompactListList, i.e. set sizes to zero.
UList< label > labelUList
Definition: UList.H:64
void transfer(CompactListList< T, Container > &)
Transfer the contents of the argument CompactListList.
void operator=(const T &)
Assignment of all entries to the given value.
labelList sizes() const
Return sizes (to be used e.g. for construction)
label size() const
Return the primary size, i.e. the number of rows.
UList< T > operator[](const label i)
Return subscript-checked row as UList.
CompactListList()
Null constructor.
List< label > labelList
A List of labels.
Definition: labelList.H:56
label whichColumn(const label row, const label index) const
Get column index (j) given above row.
void resize(const label mRows)
Reset size of CompactListList.
void setSize(const label mRows)
Reset size of CompactListList.
autoPtr< CompactListList< T, Container > > clone() const
Clone.
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
static const CompactListList< T, Container > & null()
Return a null CompactListList.
const List< label > & offsets() const
Return the offset table (= size()+1)
label index(const label row, const label col) const
Return index into m.
List< Container > operator()() const
Return as List<Container>
label whichRow(const label index) const
Get row for index into m.
Namespace for OpenFOAM.
bool empty() const
Return true if the number of rows is zero.
Xfer< CompactListList< T, Container > > xfer()
Transfer the contents to the Xfer container.