/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2022 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see .
\*---------------------------------------------------------------------------*/
#include "CompactIOList.H"
#include "IOList.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template
<
template class Container,
template class IOContainer,
template class CompactIOContainer,
class Type
>
bool
Foam::CompactIOListBase::
overflows() const
{
label size = 0;
forAll(*this, i)
{
label oldSize = size;
size += this->operator[](i).size();
if (size < oldSize)
{
return true;
}
}
return false;
}
template
<
template class Container,
template class IOContainer,
template class CompactIOContainer,
class Type
>
void
Foam::CompactIOListBase::
convertToCompact
(
labelList& start,
List& elems
) const
{
start.setSize(this->size() + 1);
start[0] = 0;
for (label i = 1; i < start.size(); i++)
{
const label prev = start[i-1];
start[i] = prev + this->operator[](i-1).size();
if (start[i] < prev)
{
FatalErrorInFunction
<< "Overall number of elements " << start[i]
<< " of CompactIOListBase of size "
<< this->size() << " overflows the representation of a label"
<< endl << "Please recompile with a larger representation"
<< " for label" << exit(FatalError);
}
}
elems.setSize(start[start.size() - 1]);
label elemi = 0;
forAll(*this, i)
{
const Type& subList = this->operator[](i);
forAll(subList, j)
{
elems[elemi++] = subList[j];
}
}
}
template
<
template class Container,
template class IOContainer,
template class CompactIOContainer,
class Type
>
void
Foam::CompactIOListBase::
convertFromCompact
(
const labelList& start,
const List& elems
)
{
this->setSize(start.size() - 1);
forAll(*this, i)
{
Type& subList = this->operator[](i);
label index = start[i];
subList.setSize(start[i+1] - index);
forAll(subList, j)
{
subList[j] = elems[index++];
}
}
}
template
<
template class Container,
template class IOContainer,
template class CompactIOContainer,
class Type
>
void
Foam::CompactIOListBase::
readFromStream(const bool read)
{
Istream& is = readStream(word::null, read);
if (read)
{
if (headerClassName() == IOContainer::typeName)
{
is >> static_cast&>(*this);
close();
}
else if (headerClassName() == CompactIOContainer::typeName)
{
is >> *this;
close();
}
else
{
FatalIOErrorInFunction(is)
<< "unexpected class name " << headerClassName()
<< " expected " << CompactIOContainer::typeName << " or "
<< IOContainer::typeName << endl
<< " while reading object " << name()
<< exit(FatalIOError);
}
}
}
// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
template
<
template class Container,
template class IOContainer,
template class CompactIOContainer,
class Type
>
Foam::CompactIOListBase::
CompactIOListBase(const IOobject& io)
:
regIOobject(io)
{
if
(
io.readOpt() == IOobject::MUST_READ
|| (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
)
{
readFromStream();
}
}
template
<
template class Container,
template class IOContainer,
template class CompactIOContainer,
class Type
>
Foam::CompactIOListBase::
CompactIOListBase
(
const IOobject& io,
const bool read
)
:
regIOobject(io)
{
if (io.readOpt() == IOobject::MUST_READ)
{
readFromStream(read);
}
else if (io.readOpt() == IOobject::READ_IF_PRESENT)
{
bool haveFile = headerOk();
readFromStream(read && haveFile);
}
}
template
<
template class Container,
template class IOContainer,
template class CompactIOContainer,
class Type
>
Foam::CompactIOListBase::
CompactIOListBase
(
const IOobject& io,
const label size
)
:
regIOobject(io)
{
if
(
io.readOpt() == IOobject::MUST_READ
|| (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
)
{
readFromStream();
}
else
{
this->setSize(size);
}
}
template
<
template class Container,
template class IOContainer,
template class CompactIOContainer,
class Type
>
Foam::CompactIOListBase::
CompactIOListBase
(
const IOobject& io,
const Container& l
)
:
regIOobject(io),
Container(l)
{
if
(
io.readOpt() == IOobject::MUST_READ
|| (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
)
{
readFromStream();
}
}
template
<
template class Container,
template class IOContainer,
template class CompactIOContainer,
class Type
>
Foam::CompactIOListBase::
CompactIOListBase
(
const IOobject& io,
Container&& l
)
:
regIOobject(io),
Container(move(l))
{
if
(
io.readOpt() == IOobject::MUST_READ
|| (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
)
{
readFromStream();
}
}
template
<
template class Container,
template class IOContainer,
template class CompactIOContainer,
class Type
>
Foam::CompactIOListBase::
CompactIOListBase
(
CompactIOListBase&& l
)
:
regIOobject(move(l)),
Container(move(l))
{}
// * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
template
<
template class Container,
template class IOContainer,
template class CompactIOContainer,
class Type
>
Foam::CompactIOListBase::
~CompactIOListBase()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template
<
template class Container,
template class IOContainer,
template class CompactIOContainer,
class Type
>
bool
Foam::CompactIOListBase::
writeObject
(
IOstream::streamFormat fmt,
IOstream::versionNumber ver,
IOstream::compressionType cmp,
const bool write
) const
{
if (fmt == IOstream::ASCII)
{
// Change type to be non-compact format type
const word oldTypeName = CompactIOContainer::typeName;
const_cast(CompactIOContainer::typeName) =
IOContainer::typeName;
bool good = regIOobject::writeObject(fmt, ver, cmp, write);
// Change type back
const_cast(CompactIOContainer::typeName) = oldTypeName;
return good;
}
else if (this->overflows())
{
WarningInFunction
<< "Overall number of elements of CompactIOListBase of size "
<< this->size() << " overflows the representation of a label"
<< endl << " Switching to ascii writing" << endl;
// Change type to be non-compact format type
const word oldTypeName = CompactIOContainer::typeName;
const_cast(CompactIOContainer::typeName) =
IOContainer::typeName;
bool good = regIOobject::writeObject(IOstream::ASCII, ver, cmp, write);
// Change type back
const_cast(CompactIOContainer::typeName) = oldTypeName;
return good;
}
else
{
return regIOobject::writeObject(fmt, ver, cmp, write);
}
}
template
<
template class Container,
template class IOContainer,
template class CompactIOContainer,
class Type
>
bool
Foam::CompactIOListBase::
writeData(Ostream& os) const
{
return (os << *this).good();
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template
<
template class Container,
template class IOContainer,
template class CompactIOContainer,
class Type
>
void
Foam::CompactIOListBase::
operator=
(
const CompactIOListBase&
rhs
)
{
Container::operator=(rhs);
}
template
<
template class Container,
template class IOContainer,
template class CompactIOContainer,
class Type
>
void
Foam::CompactIOListBase::
operator=
(
CompactIOListBase&& rhs
)
{
Container::operator=(move(rhs));
}
// * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
template
<
template class Container,
template class IOContainer,
template class CompactIOContainer,
class Type
>
void Foam::writeEntry
(
Ostream& os,
const CompactIOListBase& l
)
{
os << l;
}
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
template
<
template class Container,
template class IOContainer,
template class CompactIOContainer,
class Type
>
Foam::Istream& Foam::operator>>
(
Istream& is,
CompactIOListBase& l
)
{
// ASCII is not compacted
if (is.format() == IOstream::ASCII)
{
is >> static_cast&>(l);
}
else
{
labelList start(is);
List elems(is);
l.convertFromCompact(start, elems);
}
return is;
}
template
<
template class Container,
template class IOContainer,
template class CompactIOContainer,
class Type
>
Foam::Ostream& Foam::operator<<
(
Ostream& os,
const CompactIOListBase& l
)
{
// ASCII is not compacted
if (os.format() == IOstream::ASCII)
{
os << static_cast&>(l);
}
else
{
labelList start;
List elems;
l.convertToCompact(start, elems);
os << start << elems;
}
return os;
}
// ************************************************************************* //