Loading...
Searching...
No Matches
PackedListIO.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) 2018-2025 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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\*---------------------------------------------------------------------------*/
28#include "PackedList.H"
29#include "IOstreams.H"
30
31// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
32
33template<unsigned Width>
35{
36 os << *this;
37}
38
39
40// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
41
42template<unsigned Width>
44(
45 Ostream& os,
46 bool debugOutput
47) const
48{
50
51 const label nblocks = debugOutput ? blocks_.size() : num_blocks(size());
52 for (label blocki = 0; blocki < nblocks; ++blocki)
53 {
54 BitOps::print(os, blocks_[blocki], '.') << nl;
55 }
56
58
59 return os;
60}
61
62
63template<unsigned Width>
65{
66 PackedList<Width>& list = *this;
67
68 // Anull list
69 list.clear();
70
72
73 token tok(is);
74
75 is.fatalCheck("PackedList::readList(Istream&) : reading first token");
76
77 if (tok.isLabel())
78 {
79 const label len = tok.labelToken();
80
81 // Set list length to that read
82 list.resize(len);
83
85 {
86 // Binary (always contiguous)
87
88 if (len)
89 {
90 // NOTE: independent of WM_LABEL_SIZE
91 is.read(list.data_bytes(), list.size_bytes());
92
93 is.fatalCheck
94 (
95 "PackedList::readList(Istream&) : "
96 "reading the binary block"
97 );
98 }
99 }
100 else
101 {
102 // Begin of contents marker
103 const char delimiter = is.readBeginList("PackedList");
104
105 if (len)
106 {
107 if (delimiter == token::BEGIN_LIST)
108 {
109 for (label i=0; i<len; ++i)
110 {
111 list.set(i, list.readValue(is));
112
113 is.fatalCheck
114 (
115 "PackedList::readList(Istream&) : "
116 "reading entry"
117 );
118 }
119 }
120 else // token::BEGIN_BLOCK
121 {
122 // Assign for all entries
123 list = list.readValue(is);
124
125 is.fatalCheck
126 (
127 "PackedList::readList(Istream&) : "
128 "reading the single entry"
129 );
130 }
131 }
132
133 // End of contents marker
134 is.readEndList("PackedList");
135 }
136 }
137 else if (tok.isPunctuation(token::BEGIN_LIST))
138 {
139 is >> tok;
141
142 while (!tok.isPunctuation(token::END_LIST))
143 {
144 is.putBack(tok);
145 list.push_back(list.readValue(is));
146
147 is >> tok;
149 }
150 }
151 else if (tok.isPunctuation(token::BEGIN_BLOCK))
152 {
153 is >> tok;
155
156 while (!tok.isPunctuation(token::END_BLOCK))
157 {
158 is.putBack(tok);
159 list.setPair(is);
160
161 is >> tok;
163 }
164 }
165 else
166 {
168 << "incorrect first token, expected <int>, '(' or '{', found "
169 << tok.info() << nl
170 << exit(FatalIOError);
172
173 return is;
174}
175
176
177template<unsigned Width>
179(
180 Ostream& os,
181 label shortLen
182) const
183{
184 const PackedList<Width>& list = *this;
185 const label len = list.size();
186
187 if (shortLen < 0) shortLen = 1; // <- sanity
188
189 if (os.format() == IOstreamOption::BINARY)
190 {
191 // Binary (always contiguous)
192
193 os << nl << len << nl;
194
195 if (len)
196 {
197 // write(...) includes surrounding start/end delimiters
198 os.write(list.cdata_bytes(), list.size_bytes());
199 }
200 }
201 else if (len > 1 && list.uniform())
202 {
203 // Two or more entries, and all entries have identical values.
204 os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK;
205 }
206 else if (!shortLen || len <= shortLen)
207 {
208 // Single-line output
209
210 // Size and start delimiter
211 os << len << token::BEGIN_LIST;
212
213 // Contents
214 for (label i = 0; i < len; ++i)
215 {
216 if (i) os << token::SPACE;
217 os << label(list.get(i));
218 }
219
220 // End delimiter
221 os << token::END_LIST;
222 }
223 else
224 {
225 // Multi-line output
226
227 // Size and start delimiter
228 os << nl << len << nl << token::BEGIN_LIST << nl;
229
230 // Contents
231 if (shortLen <= 1)
232 {
233 // simple multi-line
234 for (label i = 0; i < len; ++i)
235 {
236 os << label(list.get(i)) << nl;
237 }
238 }
239 else
240 {
241 // 'matrix' of values
242 label line = 0;
243
244 for (label i = 0; i < len; ++i, ++line)
245 {
246 if (line == shortLen)
247 {
248 os << nl;
249 line = 0;
250 }
251 else if (line)
252 {
253 os << token::SPACE;
254 }
255 os << label(list.get(i));
256 }
257 if (line) os << nl;
258 }
259
260 // End delimiter
261 os << token::END_LIST << nl;
263
264 return os;
265}
266
267
268template<unsigned Width>
270(
271 const word& keyword,
272 Ostream& os
273) const
274{
275 if (keyword.size())
276 {
277 os.writeKeyword(keyword);
278 }
279 writeEntry(os);
280 os.endEntry();
281}
282
283
284// * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * * //
285
286template<unsigned Width>
288{
289 return list.readList(is);
290}
291
292
293template<unsigned Width>
294Foam::Ostream& Foam::operator<<
295(
296 Ostream& os,
297 const InfoProxy<PackedList<Width>>& iproxy
298)
299{
300 const auto& list = *iproxy;
301
302 os << "PackedList<" << Width
303 << "> size=" << list.size() << "/" << list.capacity()
304 << " (limits: max=" << PackedList<Width>::max_value
305 << ", elem_per_block=" << PackedList<Width>::elem_per_block
306 << ")"
307 << nl;
308
309 return os;
310}
311
312
313// ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
streamFormat format() const noexcept
Get the current stream format.
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition IOstream.C:51
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition Istream.C:192
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition Istream.C:171
void putBack(const token &tok)
Put back a token (copy). Only a single put back is permitted.
Definition Istream.C:71
virtual Istream & read(token &)=0
Return next token from stream.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A dynamic list of packed unsigned integers, with the number of bits per item specified by the <Width>...
Definition PackedList.H:146
static unsigned int readValue(Istream &is)
Read a list entry (allows for specialization).
Definition PackedListI.H:34
char * data_bytes() noexcept
A pointer to the raw storage, reinterpreted as byte data.
block_container blocks_
The blocks of raw data.
Definition PackedList.H:224
void push_back(const unsigned int val)
Append a value at the end of the list.
void resize(const label numElem, const unsigned int val=0u)
Reset addressable list size, does not shrink the allocated size.
Ostream & printBits(Ostream &os, bool debugOutput=false) const
Print bit patterns, optionally with extra debug.
label capacity() const noexcept
Number of elements that can be stored without reallocating.
bool set(const label i, unsigned int val=~0u)
Set value at index i, default value set is the max_value.
Ostream & writeList(Ostream &os, label shortLen=0) const
Write List, with line-breaks in ASCII when length exceeds shortLen.
bool uniform() const
True if all entries have identical values (and list is non-empty).
Definition PackedList.C:85
unsigned int get(const label i) const
Get value at index i or 0 for out-of-range.
constexpr PackedList() noexcept
Default construct, zero-sized and no allocation.
const char * cdata_bytes() const noexcept
A const pointer to the raw storage, reinterpreted as byte data.
void writeEntry(Ostream &os) const
Write as a dictionary entry.
static constexpr label num_blocks(label numElem) noexcept
Calculate the number of blocks required to _address_ the requested number of elements.
Definition PackedList.H:196
void setPair(Istream &is)
Read an index/value pair and set accordingly.
Definition PackedListI.H:51
label size() const noexcept
Number of entries.
Definition PackedList.H:392
void clear()
Clear the list, i.e. set addressable size to zero.
Istream & readList(Istream &is)
Clear list and read from stream.
std::streamsize size_bytes() const noexcept
The number of bytes addressed in the raw storage including any padding.
A line primitive.
Definition line.H:180
A token holds an item read from Istream.
Definition token.H:70
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition tokenI.H:650
@ BEGIN_BLOCK
Begin block [isseparator].
Definition token.H:178
@ END_BLOCK
End block [isseparator].
Definition token.H:179
@ BEGIN_LIST
Begin list [isseparator].
Definition token.H:174
@ END_LIST
End list [isseparator].
Definition token.H:175
@ SPACE
Space [isspace].
Definition token.H:144
bool isLabel() const noexcept
Integral token is convertible to Foam::label.
Definition tokenI.H:843
label labelToken() const
Return integer type as label value or Error.
Definition tokenI.H:869
InfoProxy< token > info() const noexcept
Return info proxy, for printing token information to a stream.
Definition token.H:1253
A class for handling words, derived from Foam::string.
Definition word.H:66
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
Ostream & print(Ostream &os, UIntType value, char off='0', char on='1')
Print 0/1 bits in the (unsigned) integral type.
Definition BitOps.H:320
Istream & operator>>(Istream &, directionInfo &)
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50