CompactListList.C
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-2019 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 \*---------------------------------------------------------------------------*/
25 
26 #include "CompactListList.H"
27 
28 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
29 
30 template<class T, class Container>
32 :
33  size_(ll.size()),
34  offsets_(ll.size()+1)
35 {
36  label sumSize = 0;
37  offsets_[0] = 0;
38  forAll(ll, i)
39  {
40  sumSize += ll[i].size();
41  offsets_[i+1] = sumSize;
42  }
43 
44  m_.setSize(sumSize);
45 
46  label k = 0;
47  forAll(ll, i)
48  {
49  const Container& lli = ll[i];
50 
51  forAll(lli, j)
52  {
53  m_[k++] = lli[j];
54  }
55  }
56 }
57 
58 
59 template<class T, class Container>
61 (
62  const labelUList& rowSizes
63 )
64 :
65  size_(rowSizes.size()),
66  offsets_(rowSizes.size()+1)
67 {
68  label sumSize = 0;
69  offsets_[0] = 0;
70  forAll(rowSizes, i)
71  {
72  sumSize += rowSizes[i];
73  offsets_[i+1] = sumSize;
74  }
75 
76  m_.setSize(sumSize);
77 }
78 
79 
80 template<class T, class Container>
82 (
83  const labelUList& rowSizes,
84  const T& t
85 )
86 :
87  size_(rowSizes.size()),
88  offsets_(rowSizes.size()+1)
89 {
90  label sumSize = 0;
91  offsets_[0] = 0;
92  forAll(rowSizes, i)
93  {
94  sumSize += rowSizes[i];
95  offsets_[i+1] = sumSize;
96  }
97 
98  m_.setSize(sumSize, t);
99 }
100 
101 
102 template<class T, class Container>
104 (
106 )
107 {
108  transfer(lst);
109 }
110 
111 
112 template<class T, class Container>
114 (
116  bool reuse
117 )
118 :
119  size_(lst.size()),
120  offsets_(lst.offsets_, reuse),
121  m_(lst.m_, reuse)
122 {}
123 
124 
125 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
126 
127 template<class T, class Container>
129 {
130  if (mRows == 0)
131  {
132  clear();
133  }
134  if (mRows < size())
135  {
136  size_ = mRows;
137  offsets_.setSize(mRows+1);
138  m_.setSize(offsets_[mRows]);
139  }
140  else if (mRows > size())
141  {
143  << "Cannot be used to extend the list from " << offsets_.size()
144  << " to " << mRows << nl
145  << " Please use one of the other setSize member functions"
146  << abort(FatalError);
147  }
148 }
149 
150 
151 template<class T, class Container>
153 (
154  const label mRows,
155  const label nData
156 )
157 {
158  size_ = mRows;
159  offsets_.setSize(mRows+1);
160  m_.setSize(nData);
161 }
162 
163 
164 template<class T, class Container>
166 (
167  const label mRows,
168  const label nData,
169  const T& t
170 )
171 {
172  size_ = mRows;
173  offsets_.setSize(mRows+1);
174  m_.setSize(nData, t);
175 }
176 
177 
178 template<class T, class Container>
180 {
181  size_ = rowSizes.size();
182  offsets_.setSize(rowSizes.size()+1);
183 
184  label sumSize = 0;
185  offsets_[0] = 0;
186  forAll(rowSizes, i)
187  {
188  sumSize += rowSizes[i];
189  offsets_[i+1] = sumSize;
190  }
191 
192  m_.setSize(sumSize);
193 }
194 
195 
196 template<class T, class Container>
198 {
199  labelList rowSizes(size());
200 
201  if (rowSizes.size() > 0)
202  {
203  forAll(rowSizes, i)
204  {
205  rowSizes[i] = offsets_[i+1] - offsets_[i];
206  }
207  }
208  return rowSizes;
209 }
210 
211 
212 template<class T, class Container>
214 {
215  size_ = 0;
216  offsets_.clear();
217  m_.clear();
218 }
219 
220 
221 template<class T, class Container>
223 (
225 )
226 {
227  size_ = a.size_;
228  offsets_.transfer(a.offsets_);
229  m_.transfer(a.m_);
230 
231  a.size_ = 0;
232 }
233 
234 
235 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
236 
237 template<class T, class Container>
239 const
240 {
241  List<Container> ll(size());
242 
243  forAll(ll, i)
244  {
245  ll[i] = Container(operator[](i));
246  }
247 
248  return ll;
249 }
250 
251 
252 // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //
253 
254 #include "CompactListListIO.C"
255 
256 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
label k
Boltzmann constant.
void clear()
Clear the CompactListList, i.e. set sizes to zero.
void transfer(CompactListList< T, Container > &)
Transfer the contents of the argument CompactListList.
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:125
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.
CompactListList()
Null constructor.
errorManip< error > abort(error &err)
Definition: errorManip.H:131
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:60
A packed storage unstructured matrix of objects of type <T> using an offset table for access...
void setSize(const label mRows)
Reset size of CompactListList.
static const char nl
Definition: Ostream.H:260
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
void setSize(const label)
Reset size of List.
Definition: List.C:281
List< Container > operator()() const
Return as List<Container>
label size() const
Return the number of elements in the UList.
Definition: UListI.H:311
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342