bitSetIO.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-2019 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 "bitSet.H"
29 #include "IOstreams.H"
30 
31 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
32 
34 {
35  os << *this;
36 }
37 
38 
39 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
40 
42 (
43  Ostream& os,
44  const label shortLen
45 ) const
46 {
47  const bitSet& list = *this;
48  const label len = list.size();
49 
50  // Write list contents depending on data format
51  if (os.format() == IOstream::ASCII)
52  {
53  if (len > 1 && list.uniform())
54  {
55  // Two or more entries, and all entries have identical values.
56  os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK;
57  }
58  else if (!shortLen || len <= shortLen)
59  {
60  // Shorter list, or line-breaks suppressed
61  os << len << token::BEGIN_LIST;
62  for (label i=0; i < len; ++i)
63  {
64  if (i) os << token::SPACE;
65  os << list[i];
66  }
67  os << token::END_LIST;
68  }
69  else
70  {
71  // Longer list
72  os << nl << len << nl << token::BEGIN_LIST << nl;
73  for (label i=0; i < len; ++i)
74  {
75  os << list[i] << nl;
76  }
77  os << token::END_LIST << nl;
78  }
79  }
80  else
81  {
82  // Contents are binary and contiguous
83  os << nl << len << nl;
84 
85  if (len)
86  {
87  // write(...) includes surrounding start/end delimiters
88  os.write
89  (
90  reinterpret_cast<const char*>(list.storage().cdata()),
91  list.byteSize()
92  );
93  }
94  }
95 
96  return os;
97 }
98 
99 
101 (
102  const word& keyword,
103  Ostream& os
104 ) const
105 {
106  if (keyword.size())
107  {
108  os.writeKeyword(keyword);
109  }
110  writeEntry(os);
111  os << token::END_STATEMENT << endl;
112 }
113 
114 
115 // * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * * //
116 
118 {
119  return bitset.writeList(os, 40);
120 }
121 
122 
123 Foam::Ostream& Foam::operator<<
124 (
125  Ostream& os,
126  const InfoProxy<bitSet>& iproxy
127 )
128 {
129  const bitSet& bitset = iproxy.t_;
130 
131  os << "bitSet<" << bitSet::elem_per_block
132  << "> size=" << bitset.size() << "/" << bitset.capacity()
133  << " count=" << bitset.count()
134  << nl;
135 
136  return os;
137 }
138 
139 
140 // ************************************************************************* //
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::InfoProxy
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:47
Foam::bitSet
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:63
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:289
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::bitSet::writeEntry
void writeEntry(Ostream &os) const
Write as a dictionary entry.
Definition: bitSetIO.C:33
Foam::HashSetOps::bitset
bitSet bitset(const labelHashSet &locations)
Transform the on locations to a bitSet.
Definition: HashOps.C:72
bitSet.H
Foam::bitSet::count
unsigned int count(const bool on=true) const
Count number of bits set.
Definition: bitSetI.H:499
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::token::END_STATEMENT
End entry [isseparator].
Definition: token.H:121
Foam::token::END_BLOCK
End block [isseparator].
Definition: token.H:127
Foam::bitSet::writeList
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write bitSet, with line-breaks (ASCII) when length exceeds shortLen.
Definition: bitSetIO.C:42
Foam::PackedList::byteSize
std::streamsize byteSize() const
Definition: PackedListI.H:551
Foam::Ostream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Foam::Ostream::writeKeyword
virtual Ostream & writeKeyword(const keyType &kw)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:57
Foam::token::BEGIN_BLOCK
Begin block [isseparator].
Definition: token.H:126
Foam::PackedList::capacity
label capacity() const
The number of elements that can be stored with reallocating.
Definition: PackedListI.H:391
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::PackedList< 1 >::elem_per_block
static constexpr unsigned elem_per_block
The number of elements stored per data block.
Definition: PackedList.H:147
Foam::token::SPACE
Space [isspace].
Definition: token.H:117
Foam::PackedList::size
label size() const noexcept
Number of entries.
Definition: PackedListI.H:377
Foam::PackedList::storage
const List< unsigned int > & storage() const
Return the underlying storage blocks.
Definition: PackedListI.H:537
Foam::token::END_LIST
End list [isseparator].
Definition: token.H:123
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::token::BEGIN_LIST
Begin list [isseparator].
Definition: token.H:122
Foam::bitSet::uniform
bool uniform() const
True if all entries have identical values, and the set is non-empty.
Definition: bitSetI.H:493