ListCompactIO.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) 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 "ListCompactIO.H"
27 
28 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
29 
30 template<class T, class BaseType>
32 {
33  label size = 0;
34  forAll(*this, i)
35  {
36  label oldSize = size;
37  size += this->operator[](i).size();
38  if (size < oldSize)
39  {
40  return true;
41  }
42  }
43  return false;
44 }
45 
46 
47 template<class T, class BaseType>
49 (
50  labelList& start,
51  List<BaseType>& elems
52 ) const
53 {
54  start.setSize(this->size() + 1);
55 
56  start[0] = 0;
57  for (label i = 1; i < start.size(); i++)
58  {
59  label prev = start[i-1];
60  start[i] = prev + this->operator[](i-1).size();
61 
62  if (start[i] < prev)
63  {
65  << "Overall number of elements " << start[i]
66  << " of ListCompactIO of size "
67  << this->size() << " overflows the representation of a label"
68  << endl << "Please recompile with a larger representation"
69  << " for label" << exit(FatalError);
70  }
71  }
72 
73  elems.setSize(start[start.size() - 1]);
74 
75  label elemi = 0;
76  forAll(*this, i)
77  {
78  const T& subList = this->operator[](i);
79 
80  forAll(subList, j)
81  {
82  elems[elemi++] = subList[j];
83  }
84  }
85 }
86 
87 
88 template<class T, class BaseType>
90 (
91  const labelList& start,
92  const List<BaseType>& elems
93 )
94 {
95  this->setSize(start.size() - 1);
96 
97  forAll(*this, i)
98  {
99  T& subList = this->operator[](i);
100 
101  label index = start[i];
102  subList.setSize(start[i+1] - index);
103 
104  forAll(subList, j)
105  {
106  subList[j] = elems[index++];
107  }
108  }
109 }
110 
111 
112 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
113 
114 template<class T, class BaseType>
116 {}
117 
118 
119 template<class T, class BaseType>
121 :
122  List<T>(l)
123 {}
124 
125 
126 template<class T, class BaseType>
128 {
129  is >> *this;
130 }
131 
132 
133 template<class T, class BaseType>
135 (
137 )
138 :
139  List<T>(l)
140 {}
141 
142 
143 template<class T, class BaseType>
145 :
146  List<T>(move(l))
147 {}
148 
149 
150 template<class T, class BaseType>
152 :
153  List<T>(move(l))
154 {}
155 
156 
157 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
158 
159 template<class T, class BaseType>
160 void Foam::ListCompactIO<T, BaseType>::operator=
161 (
162  const ListCompactIO<T, BaseType>& rhs
163 )
164 {
165  List<T>::operator=(rhs);
166 }
167 
168 
169 template<class T, class BaseType>
170 void Foam::ListCompactIO<T, BaseType>::operator=
171 (
173 )
174 {
175  List<T>::operator=(move(rhs));
176 }
177 
178 
179 template<class T, class BaseType>
181 {
182  List<T>::operator=(rhs);
183 }
184 
185 
186 template<class T, class BaseType>
188 {
189  List<T>::operator=(move(rhs));
190 }
191 
192 
193 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
194 
195 template<class T, class BaseType>
197 {
198  // Keep ascii writing same.
199  if (os.format() == IOstream::ASCII)
200  {
201  os << static_cast<const List<T>&>(l);
202  }
203  else
204  {
205  labelList start;
206  List<BaseType> elems;
207  l.convertToCompact(start, elems);
208  writeEntry(os, start);
209  writeEntry(os, elems);
210  }
211 }
212 
213 
214 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
215 
216 template<class T, class BaseType>
217 Foam::Istream& Foam::operator>>
218 (
219  Foam::Istream& is,
221 )
222 {
223  if (is.format() == IOstream::ASCII)
224  {
225  is >> static_cast<List<T>&>(L);
226  }
227  else
228  {
229  labelList start(is);
230  List<BaseType> elems(is);
231  L.convertFromCompact(start, elems);
232  }
233 
234  return is;
235 }
236 
237 
238 template<class T, class BaseType>
239 Foam::Ostream& Foam::operator<<
240 (
241  Foam::Ostream& os,
243 )
244 {
245  // Keep ascii writing same.
246  if (os.format() == IOstream::ASCII)
247  {
248  os << static_cast<const List<T>&>(L);
249  }
250  else
251  {
252  labelList start;
253  List<BaseType> elems;
254  L.convertToCompact(start, elems);
255  os << start << elems;
256  }
257 
258  return os;
259 }
260 
261 
262 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
void convertToCompact(labelList &start, List< BaseType > &elems) const
Definition: ListCompactIO.C:49
ListCompactIO()
Construct null.
void convertFromCompact(const labelList &start, const List< BaseType > &elems)
Definition: ListCompactIO.C:90
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
A List of objects of type <T> with input and output using a compact storage. Behaves like List except...
Definition: ListCompactIO.H:52
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
points setSize(newPointi)
A List obtained as a section of another List.
Definition: SubList.H:53
streamFormat format() const
Return current stream format.
Definition: IOstream.H:374
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
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
void setSize(const label)
Reset size of List.
Definition: List.C:281
const volScalarField & T
bool overflows() const
Has too many elements in it?
Definition: ListCompactIO.C:31
void operator=(const ListCompactIO< T, BaseType > &)