DynamicListIO.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2021-2023 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "List.H"
29 #include "Istream.H"
30 #include "token.H"
31 #include "contiguous.H"
32 #include <memory>
33 
34 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
35 
36 template<class T, int SizeMin>
38 :
39  List<T>(),
40  capacity_(0)
41 {
42  this->readList(is);
43 }
44 
45 
46 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
47 
48 template<class T, int SizeMin>
50 {
51  DynamicList<T, SizeMin>& list = *this;
52 
54 
55  token tok(is);
56 
57  is.fatalCheck
58  (
59  "DynamicList<T>::readBracketList(Istream&) : reading first token"
60  );
61 
62  if (!tok.isPunctuation(token::BEGIN_LIST))
63  {
64  is.putBack(tok);
65  return false;
66  }
67 
68  {
69  // "(...)" : read element-wise.
70  // Uses chunk-wise reading to avoid too many re-allocations
71  // and avoids relocation of contiguous memory until all of the reading
72  // is completed. Chunks are wrapped as unique_ptr to ensure proper
73  // cleanup on failure.
74 
75  // The choice of chunk-size is somewhat arbitrary...
76  constexpr label chunkSize = 128;
77  typedef std::unique_ptr<List<T>> chunkType;
78 
79  is >> tok;
81 
82  if (tok.isPunctuation(token::END_LIST))
83  {
84  // Trivial case, an empty list
85  list.clear();
86  return true;
87  }
88 
89  // Use all storage
90  list.resize(list.capacity());
91 
92  // Start with a few slots, recover current memory where possible
93  List<chunkType> chunks(16);
94  if (list.empty())
95  {
96  chunks[0] = chunkType(new List<T>(chunkSize));
97  }
98  else
99  {
100  chunks[0] = chunkType(new List<T>(std::move(list)));
101  }
102 
103  label nChunks = 1; // Active number of chunks
104  label totalCount = 0; // Total number of elements
105  label localIndex = 0; // Chunk-local index
106 
107  while (!tok.isPunctuation(token::END_LIST))
108  {
109  is.putBack(tok);
110 
111  if (chunks[nChunks-1]->size() <= localIndex)
112  {
113  // Increase number of slots (doubling)
114  if (nChunks >= chunks.size())
115  {
116  chunks.resize(2*chunks.size());
117  }
118 
119  chunks[nChunks] = chunkType(new List<T>(chunkSize));
120  ++nChunks;
121  localIndex = 0;
122  }
123 
124  is >> chunks[nChunks-1]->operator[](localIndex);
125  ++localIndex;
126  ++totalCount;
127 
128  is.fatalCheck
129  (
130  "DynamicList<T>::readBracketList(Istream&) : "
131  "reading entry"
132  );
133 
134  is >> tok;
136  }
137 
138  // Simple case
139  if (nChunks == 1)
140  {
141  list = std::move(*(chunks[0]));
142  list.resize(totalCount);
143  return true;
144  }
145 
146  // Destination
147  list.setCapacity_nocopy(totalCount);
148  list.resize_nocopy(totalCount);
149  auto dest = list.begin();
150 
151  for (label chunki = 0; chunki < nChunks; ++chunki)
152  {
153  List<T> currChunk(std::move(*(chunks[chunki])));
154  chunks[chunki].reset(nullptr);
155 
156  const label localLen = min(currChunk.size(), totalCount);
157 
158  dest = std::move
159  (
160  currChunk.begin(),
161  currChunk.begin(localLen),
162  dest
163  );
164 
165  totalCount -= localLen;
166  }
167  }
168 
169  return true;
170 }
171 
172 
173 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
174 
175 template<class T, int SizeMin>
177 {
178  DynamicList<T, SizeMin>& list = *this;
179 
181 
182  token tok(is);
183 
184  is.fatalCheck("DynamicList<T>::readList(Istream&) : reading first token");
185 
186  if (tok.isCompound())
187  {
188  // Compound: simply transfer contents
189 
190  list.clearStorage(); // Remove old contents
191  list.transfer
192  (
194  );
195  }
196  else if (tok.isLabel())
197  {
198  // Label: could be int(..), int{...} or just a plain '0'
199 
200  const label len = tok.labelToken();
201 
202  // Resize to length required
203  list.resize_nocopy(len);
204 
205  if (is.format() == IOstreamOption::BINARY && is_contiguous<T>::value)
206  {
207  // Binary and contiguous
208 
209  if (len)
210  {
211  Detail::readContiguous<T>
212  (
213  is,
214  list.data_bytes(),
215  list.size_bytes()
216  );
217 
218  is.fatalCheck
219  (
220  "DynamicList<T>::readList(Istream&) : "
221  "reading binary block"
222  );
223  }
224  }
225  else if (std::is_same<char, typename std::remove_cv<T>::type>::value)
226  {
227  // Special treatment for char data (binary I/O only)
228  const auto oldFmt = is.format(IOstreamOption::BINARY);
229 
230  if (len)
231  {
232  // read(...) includes surrounding start/end delimiters
233  is.read(list.data_bytes(), list.size_bytes());
234 
235  is.fatalCheck
236  (
237  "DynamicList<char>::readList(Istream&) : "
238  "reading binary block"
239  );
240  }
241 
242  is.format(oldFmt);
243  }
244  else
245  {
246  // Begin of contents marker
247  const char delimiter = is.readBeginList("List");
248 
249  if (len)
250  {
251  if (delimiter == token::BEGIN_LIST)
252  {
253  auto iter = list.begin();
254  const auto last = list.end();
255 
256  // Contents
257  for (/*nil*/; (iter != last); (void)++iter)
258  {
259  is >> *iter;
260 
261  is.fatalCheck
262  (
263  "DynamicList<T>::readList(Istream&) : "
264  "reading entry"
265  );
266  }
267  }
268  else
269  {
270  // Uniform content (delimiter == token::BEGIN_BLOCK)
271 
272  T elem;
273  is >> elem;
274 
275  is.fatalCheck
276  (
277  "DynamicList<T>::readList(Istream&) : "
278  "reading the single entry"
279  );
280 
281  // Fill with the value
282  UList<T>::operator=(elem);
283  }
284  }
285 
286  // End of contents marker
287  is.readEndList("List");
288  }
289  }
290  else if (tok.isPunctuation(token::BEGIN_LIST))
291  {
292  // "(...)" : read read as bracketed list
293 
294  is.putBack(tok);
295  this->readBracketList(is);
296 
297  // Could also simply be done with emplace_back for each element
298  // but prefer the same mechanism as List::readList to avoid
299  // intermediate resizing
300 
301  // // list.clear(); // Clear addressing, leave storage intact
302  // //
303  // // is >> tok;
304  // // is.fatalCheck(FUNCTION_NAME);
305  // //
306  // // while (!tok.isPunctuation(token::END_LIST))
307  // // {
308  // // is.putBack(tok);
309  // // is >> list.emplace_back();
310  // //
311  // // is.fatalCheck
312  // // (
313  // // "DynamicList<T>::readList(Istream&) : "
314  // // "reading entry"
315  // // );
316  // //
317  // // is >> tok;
318  // // is.fatalCheck(FUNCTION_NAME);
319  // // }
320  }
321  else
322  {
323  list.clear(); // Clear old contents
324 
326  << "incorrect first token, expected <int> or '(', found "
327  << tok.info() << nl
328  << exit(FatalIOError);
329  }
330 
331  return is;
332 }
333 
334 
335 // ************************************************************************* //
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition: tokenI.H:561
type
Types of root.
Definition: Roots.H:52
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
bool isCompound() const noexcept
Token is COMPOUND.
Definition: tokenI.H:803
constexpr DynamicList() noexcept
Default construct, an empty list without allocation.
Definition: DynamicListI.H:131
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
bool empty() const noexcept
True if List is empty (ie, size() is zero)
Definition: UList.H:666
A token holds an item read from Istream.
Definition: token.H:65
void resize(const label len)
Alter addressable list size, allocating new space if required while recovering old content...
Definition: DynamicListI.H:353
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:286
label capacity() const noexcept
Size of the underlying storage.
Definition: DynamicList.H:225
void putBack(const token &tok)
Put back a token (copy). Only a single put back is permitted.
Definition: Istream.C:71
compound & transferCompoundToken(const Istream *is=nullptr)
Return reference to compound and mark internally as released.
Definition: token.C:157
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:391
Istream & readList(Istream &is)
Read from Istream, discarding existing contents.
#define FUNCTION_NAME
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:405
const volScalarField & T
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
label labelToken() const
Return label value.
Definition: tokenI.H:615
void transfer(List< T > &list)
Transfer contents of the argument List into this.
Definition: DynamicListI.H:504
void clearStorage()
Clear the list and delete storage.
Definition: DynamicListI.H:412
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:599
InfoProxy< token > info() const noexcept
Return info proxy, for printing token information to a stream.
Definition: token.H:1078
void resize_nocopy(const label len)
Alter addressable list size, allocating new space if required without necessarily recovering old cont...
Definition: DynamicListI.H:375
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:435
void setCapacity_nocopy(const label len)
Alter the size of the underlying storage, without retaining old content.
Definition: DynamicListI.H:313
streamFormat format() const noexcept
Get the current stream format.
std::streamsize size_bytes() const noexcept
Number of contiguous bytes for the List data.
Definition: UListI.H:293
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...