/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2019 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 .
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template
inline Foam::DynamicList::DynamicList()
:
capacity_(0)
{}
template
inline Foam::DynamicList::DynamicList
(
const label nElem
)
:
List(nElem),
capacity_(nElem)
{
// We could also enforce SizeInc granularity when (!SizeMult || !SizeDiv)
List::size(0);
}
template
inline Foam::DynamicList::DynamicList
(
const label nElem,
const T& a
)
:
List(nElem, a),
capacity_(nElem)
{}
template
inline Foam::DynamicList::DynamicList
(
const DynamicList& lst
)
:
List(lst),
capacity_(lst.size())
{}
template
inline Foam::DynamicList::DynamicList
(
DynamicList&& lst
)
:
List(move(lst)),
capacity_(lst.capacity_)
{
lst.capacity_ = 0;
}
template
inline Foam::DynamicList::DynamicList
(
const UList& lst
)
:
List(lst),
capacity_(lst.size())
{}
template
inline Foam::DynamicList::DynamicList
(
const UIndirectList& lst
)
:
List(lst),
capacity_(lst.size())
{}
template
inline Foam::DynamicList::DynamicList
(
List&& lst
)
:
List(move(lst)),
capacity_(List::size())
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template
inline Foam::label Foam::DynamicList::capacity()
const
{
return capacity_;
}
template
inline void Foam::DynamicList::setCapacity
(
const label nElem
)
{
label nextFree = List::size();
capacity_ = nElem;
if (nextFree > capacity_)
{
// Truncate addressed sizes too
nextFree = capacity_;
}
// We could also enforce SizeInc granularity when (!SizeMult || !SizeDiv)
List::setSize(capacity_);
List::size(nextFree);
}
template
inline void Foam::DynamicList::reserve
(
const label nElem
)
{
// Allocate more capacity if necessary
if (nElem > capacity_)
{
capacity_ = max
(
nElem,
label(SizeInc + capacity_ * SizeMult / SizeDiv)
);
// Adjust allocated size, leave addressed size untouched
label nextFree = List::size();
List::setSize(capacity_);
List::size(nextFree);
}
}
template
inline void Foam::DynamicList::setSize
(
const label nElem
)
{
// Allocate more capacity if necessary
if (nElem > capacity_)
{
capacity_ = max
(
nElem,
label(SizeInc + capacity_ * SizeMult / SizeDiv)
);
List::setSize(capacity_);
}
// Adjust addressed size
List::size(nElem);
}
template
inline void Foam::DynamicList::setSize
(
const label nElem,
const T& t
)
{
label nextFree = List::size();
setSize(nElem);
// Set new elements to constant value
while (nextFree < nElem)
{
this->operator[](nextFree++) = t;
}
}
template
inline void Foam::DynamicList::resize
(
const label nElem
)
{
this->setSize(nElem);
}
template
inline void Foam::DynamicList::resize
(
const label nElem,
const T& t
)
{
this->setSize(nElem, t);
}
template
inline void Foam::DynamicList::clear()
{
List::size(0);
}
template
inline void Foam::DynamicList::clearStorage()
{
List::clear();
capacity_ = 0;
}
template
inline Foam::DynamicList&
Foam::DynamicList::shrink()
{
label nextFree = List::size();
if (capacity_ > nextFree)
{
// Use the full list when resizing
List::size(capacity_);
// The new size
capacity_ = nextFree;
List::setSize(capacity_);
List::size(nextFree);
}
return *this;
}
template
inline void
Foam::DynamicList::transfer
(
DynamicList& lst
)
{
// Take over storage as-is (without shrink), clear addressing for lst.
capacity_ = lst.capacity_;
lst.capacity_ = 0;
List::transfer(static_cast&>(lst));
}
template
inline void
Foam::DynamicList::transfer(List& lst)
{
// Take over storage, clear addressing for lst.
capacity_ = lst.size();
List::transfer(lst);
}
template
inline Foam::DynamicList&
Foam::DynamicList::append
(
const T& t
)
{
const label elemI = List::size();
setSize(elemI + 1);
this->operator[](elemI) = t;
return *this;
}
template
inline Foam::DynamicList&
Foam::DynamicList::append
(
const UList& lst
)
{
if (this == &lst)
{
FatalErrorInFunction
<< "Attempted appending to self" << abort(FatalError);
}
label nextFree = List::size();
setSize(nextFree + lst.size());
forAll(lst, elemI)
{
this->operator[](nextFree++) = lst[elemI];
}
return *this;
}
template
inline Foam::DynamicList&
Foam::DynamicList::append
(
const UIndirectList& lst
)
{
label nextFree = List::size();
setSize(nextFree + lst.size());
forAll(lst, elemI)
{
this->operator[](nextFree++) = lst[elemI];
}
return *this;
}
template
inline T Foam::DynamicList::remove()
{
const label elemI = List::size() - 1;
if (elemI < 0)
{
FatalErrorInFunction
<< "List is empty" << abort(FatalError);
}
const T& val = List::operator[](elemI);
List::size(elemI);
return val;
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template
inline T& Foam::DynamicList::operator()
(
const label elemI
)
{
if (elemI >= List::size())
{
setSize(elemI + 1);
}
return this->operator[](elemI);
}
template
inline void Foam::DynamicList::operator=
(
const T& t
)
{
UList::operator=(t);
}
template
inline void Foam::DynamicList::operator=
(
const DynamicList& lst
)
{
if (this == &lst)
{
FatalErrorInFunction
<< "Attempted assignment to self" << abort(FatalError);
}
if (capacity_ >= lst.size())
{
// Can copy w/o reallocating, match initial size to avoid reallocation
List::size(lst.size());
List::operator=(lst);
}
else
{
// Make everything available for the copy operation
List::size(capacity_);
List::operator=(lst);
capacity_ = List::size();
}
}
template
inline void Foam::DynamicList::operator=
(
DynamicList&& lst
)
{
if (this == &lst)
{
FatalErrorInFunction
<< "Attempted assignment to self" << abort(FatalError);
}
List::operator=(move(lst));
capacity_ = lst.capacity_;
lst.capacity_ = 0;
}
template
inline void Foam::DynamicList::operator=
(
List&& lst
)
{
if (this == &lst)
{
FatalErrorInFunction
<< "Attempted assignment to self" << abort(FatalError);
}
List::operator=(move(lst));
capacity_ = List::size();
}
template
inline void Foam::DynamicList::operator=
(
const UList& lst
)
{
if (capacity_ >= lst.size())
{
// Can copy w/o reallocating, match initial size to avoid reallocation
List::size(lst.size());
List::operator=(lst);
}
else
{
// Make everything available for the copy operation
List::size(capacity_);
List::operator=(lst);
capacity_ = List::size();
}
}
template
inline void Foam::DynamicList::operator=
(
const UIndirectList& lst
)
{
if (capacity_ >= lst.size())
{
// Can copy w/o reallocating, match initial size to avoid reallocation
List::size(lst.size());
List::operator=(lst);
}
else
{
// Make everything available for the copy operation
List::size(capacity_);
List::operator=(lst);
capacity_ = List::size();
}
}
// * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
template
typename Foam::UList::iterator
Foam::DynamicList::erase
(
typename UList::iterator curIter
)
{
typename Foam::UList::iterator iter = curIter;
typename Foam::UList::iterator nextIter = curIter;
if (iter != this->end())
{
++iter;
while (iter != this->end())
{
*nextIter++ = *iter++;
}
this->setSize(this->size() - 1);
}
return curIter;
}
// ************************************************************************* //