CompactListList.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011 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 (nRows == 0)
131  {
132  clear();
133  }
134  if (nRows < size())
135  {
136  size_ = nRows;
137  offsets_.setSize(nRows+1);
138  m_.setSize(offsets_[nRows]);
139  }
140  else if (nRows > size())
141  {
143  (
144  "CompactListList<T, Container>::setSize(const label nRows)"
145  ) << "Cannot be used to extend the list from " << offsets_.size()
146  << " to " << nRows << nl
147  << " Please use one of the other setSize member functions"
148  << abort(FatalError);
149  }
150 }
151 
152 
153 template<class T, class Container>
155 (
156  const label nRows,
157  const label nData
158 )
159 {
160  size_ = nRows;
161  offsets_.setSize(nRows+1);
162  m_.setSize(nData);
163 }
164 
165 
166 template<class T, class Container>
168 (
169  const label nRows,
170  const label nData,
171  const T& t
172 )
173 {
174  size_ = nRows;
175  offsets_.setSize(nRows+1);
176  m_.setSize(nData, t);
177 }
178 
179 
180 template<class T, class Container>
182 {
183  size_ = rowSizes.size();
184  offsets_.setSize(rowSizes.size()+1);
185 
186  label sumSize = 0;
187  offsets_[0] = 0;
188  forAll(rowSizes, i)
189  {
190  sumSize += rowSizes[i];
191  offsets_[i+1] = sumSize;
192  }
193 
194  m_.setSize(sumSize);
195 }
196 
197 
198 template<class T, class Container>
200 {
201  labelList rowSizes(size());
202 
203  if (rowSizes.size() > 0)
204  {
205  forAll(rowSizes, i)
206  {
207  rowSizes[i] = offsets_[i+1] - offsets_[i];
208  }
209  }
210  return rowSizes;
211 }
212 
213 
214 template<class T, class Container>
216 {
217  size_ = 0;
218  offsets_.clear();
219  m_.clear();
220 }
221 
222 
223 template<class T, class Container>
225 (
227 )
228 {
229  size_ = a.size_;
230  offsets_.transfer(a.offsets_);
231  m_.transfer(a.m_);
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 // ************************************************************************* //
label size() const
Return the primary size, i.e. the number of rows.
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:390
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
void clear()
Clear the CompactListList, i.e. set sizes to zero.
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
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
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
void transfer(CompactListList< T, Container > &)
Transfer the contents of the argument CompactListList.
void setSize(const label nRows)
Reset size of CompactListList.
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
List< Container > operator()() const
Return as List<Container>
void clear()
Clear the list, i.e. set size to zero.
Definition: List.C:379
static const char nl
Definition: Ostream.H:260
void setSize(const label)
Reset size of List.
Definition: List.C:318
CompactListList()
Null constructor.
#define forAll(list, i)
Definition: UList.H:421
label k
Boltzmann constant.
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
errorManip< error > abort(error &err)
Definition: errorManip.H:131
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
A packed storage unstructured matrix of objects of type <T> using an offset table for access...
error FatalError
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
labelList sizes() const
Return sizes (to be used e.g. for construction)