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 :
136  List<T>(move(l))
137 {}
138 
139 
140 template<class T, class BaseType>
142 :
143  List<T>(move(l))
144 {}
145 
146 
147 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
148 
149 template<class T, class BaseType>
150 void Foam::ListCompactIO<T, BaseType>::operator=
151 (
152  const ListCompactIO<T, BaseType>& rhs
153 )
154 {
155  List<T>::operator=(rhs);
156 }
157 
158 
159 template<class T, class BaseType>
160 void Foam::ListCompactIO<T, BaseType>::operator=
161 (
163 )
164 {
165  List<T>::operator=(move(rhs));
166 }
167 
168 
169 template<class T, class BaseType>
171 {
172  List<T>::operator=(rhs);
173 }
174 
175 
176 template<class T, class BaseType>
178 {
179  List<T>::operator=(move(rhs));
180 }
181 
182 
183 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
184 
185 template<class T, class BaseType>
187 {
188  // Keep ascii writing same.
189  if (os.format() == IOstream::ASCII)
190  {
191  os << static_cast<const List<T>&>(l);
192  }
193  else
194  {
195  labelList start;
196  List<BaseType> elems;
197  l.convertToCompact(start, elems);
198  writeEntry(os, start);
199  writeEntry(os, elems);
200  }
201 }
202 
203 
204 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
205 
206 template<class T, class BaseType>
207 Foam::Istream& Foam::operator>>
208 (
209  Foam::Istream& is,
211 )
212 {
213  if (is.format() == IOstream::ASCII)
214  {
215  is >> static_cast<List<T>&>(L);
216  }
217  else
218  {
219  labelList start(is);
220  List<BaseType> elems(is);
221  L.convertFromCompact(start, elems);
222  }
223 
224  return is;
225 }
226 
227 
228 template<class T, class BaseType>
229 Foam::Ostream& Foam::operator<<
230 (
231  Foam::Ostream& os,
233 )
234 {
235  // Keep ascii writing same.
236  if (os.format() == IOstream::ASCII)
237  {
238  os << static_cast<const List<T>&>(L);
239  }
240  else
241  {
242  labelList start;
243  List<BaseType> elems;
244  L.convertToCompact(start, elems);
245  os << start << elems;
246  }
247 
248  return os;
249 }
250 
251 
252 // ************************************************************************* //
#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
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
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:319
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:163
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
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:377
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:53
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
const volScalarField & T
void setSize(const label)
Reset size of List.
Definition: List.C:281
bool overflows() const
Has too many elements in it?
Definition: ListCompactIO.C:31
void operator=(const ListCompactIO< T, BaseType > &)