Commit cf488912 authored by Mark Olesen's avatar Mark Olesen
Browse files

added xfer<...> transfer() method to various containers

- this should provide a slightly more naturally means to using transfer
  constructors, for example
          labelList list2(list1.transfer());
      vs. labelList list2(xferMove(list1));

- returns a plain list where appropriate (eg, DynamicList, SortableList)
  for example
          labelList list2(dynList1.transfer());
      vs. labelList list2(xferMoveTo<labelList>(dynList1));
parent 5e90a0dd
......@@ -145,6 +145,14 @@ int main(int argc, char *argv[])
Info<< "<dlC>" << dlC << "</dlC>" << nl << "sizes: "
<< " " << dlC.size() << "/" << dlC.capacity() << endl;
List<label> lstB(dlC.transfer());
Info<< "Transferred to normal list via the transfer() method" << endl;
Info<< "<lstB>" << lstB << "</lstB>" << nl << "sizes: "
<< " " << lstB.size() << endl;
Info<< "<dlC>" << dlC << "</dlC>" << nl << "sizes: "
<< " " << dlC.size() << "/" << dlC.capacity() << endl;
return 0;
}
......
......@@ -23,7 +23,7 @@ License
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Application
Description
\*---------------------------------------------------------------------------*/
......@@ -54,6 +54,11 @@ int main(int argc, char *argv[])
list2.setSize(10, vector(1, 2, 3));
Info<< list2 << endl;
List<vector> list3(list2.transfer());
Info<< "Transferred via the transfer() method" << endl;
Info<< list2 << endl;
Info<< list3 << endl;
return 0;
}
......
......@@ -104,6 +104,13 @@ int main(int argc, char *argv[])
Info<<"list1: " << list1 << endl;
PtrList<Scalar> list3(list1.transfer());
Info<< "Transferred via the transfer() method" << endl;
Info<<"list1: " << list1 << endl;
Info<<"list2: " << list2 << endl;
Info<<"list3: " << list3 << endl;
Info<< nl << "Done." << endl;
return 0;
}
......
......@@ -26,15 +26,10 @@ License
#include "CompactListList.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
template<class T>
CompactListList<T>::CompactListList(const List<List<T> >& ll)
Foam::CompactListList<T>::CompactListList(const List<List<T> >& ll)
:
offsets_(ll.size())
{
......@@ -61,7 +56,10 @@ CompactListList<T>::CompactListList(const List<List<T> >& ll)
template<class T>
CompactListList<T>::CompactListList(const UList<label>& rowSizes)
Foam::CompactListList<T>::CompactListList
(
const UList<label>& rowSizes
)
:
offsets_(rowSizes.size())
{
......@@ -77,7 +75,11 @@ CompactListList<T>::CompactListList(const UList<label>& rowSizes)
template<class T>
CompactListList<T>::CompactListList(const UList<label>& rowSizes, const T& t)
Foam::CompactListList<T>::CompactListList
(
const UList<label>& rowSizes,
const T& t
)
:
offsets_(rowSizes.size())
{
......@@ -93,23 +95,31 @@ CompactListList<T>::CompactListList(const UList<label>& rowSizes, const T& t)
template<class T>
CompactListList<T>::CompactListList(const xfer<CompactListList<T> >& lst)
Foam::CompactListList<T>::CompactListList
(
const xfer<CompactListList<T> >& lst
)
{
transfer(lst());
}
template<class T>
CompactListList<T>::CompactListList(CompactListList<T>& cll, bool reUse)
Foam::CompactListList<T>::CompactListList
(
CompactListList<T>& lst,
bool reUse
)
:
offsets_(cll.offsets_, reUse),
m_(cll.m_, reUse)
offsets_(lst.offsets_, reUse),
m_(lst.m_, reUse)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>
const CompactListList<T>& CompactListList<T>::null()
const Foam::CompactListList<T>& Foam::CompactListList<T>::null()
{
CompactListList<T>* nullPtr = reinterpret_cast<CompactListList<T>*>(NULL);
return *nullPtr;
......@@ -117,7 +127,7 @@ const CompactListList<T>& CompactListList<T>::null()
template<class T>
void CompactListList<T>::setSize(const label nRows)
void Foam::CompactListList<T>::setSize(const label nRows)
{
if (nRows == 0)
{
......@@ -140,14 +150,14 @@ void CompactListList<T>::setSize(const label nRows)
template<class T>
void CompactListList<T>::setSize(const label nRows, const label nData)
void Foam::CompactListList<T>::setSize(const label nRows, const label nData)
{
offsets_.setSize(nRows);
m_.setSize(nData);
}
template<class T>
void CompactListList<T>::setSize
void Foam::CompactListList<T>::setSize
(
const label nRows,
const label nData,
......@@ -159,7 +169,7 @@ void CompactListList<T>::setSize
}
template<class T>
labelList CompactListList<T>::sizes() const
Foam::labelList Foam::CompactListList<T>::sizes() const
{
labelList rowSizes(offsets_.size());
......@@ -173,7 +183,7 @@ labelList CompactListList<T>::sizes() const
}
template<class T>
void CompactListList<T>::setSize(const UList<label>& rowSizes)
void Foam::CompactListList<T>::setSize(const UList<label>& rowSizes)
{
offsets_.setSize(rowSizes.size());
......@@ -188,7 +198,7 @@ void CompactListList<T>::setSize(const UList<label>& rowSizes)
}
template<class T>
void CompactListList<T>::clear()
void Foam::CompactListList<T>::clear()
{
offsets_.clear();
m_.clear();
......@@ -196,7 +206,7 @@ void CompactListList<T>::clear()
template<class T>
void CompactListList<T>::transfer(CompactListList<T>& a)
void Foam::CompactListList<T>::transfer(CompactListList<T>& a)
{
offsets_.transfer(a.offsets_);
m_.transfer(a.m_);
......@@ -206,34 +216,29 @@ void CompactListList<T>::transfer(CompactListList<T>& a)
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class T>
List<List<T> > CompactListList<T>::operator()() const
Foam::List<Foam::List<T> > Foam::CompactListList<T>::operator()() const
{
List<List<T> > llt(offsets_.size());
List<List<T> > ll(offsets_.size());
label offsetPrev = 0;
forAll(offsets_, i)
{
List<T>& llti = llt[i];
List<T>& lst = ll[i];
llti.setSize(offsets_[i] - offsetPrev);
lst.setSize(offsets_[i] - offsetPrev);
forAll(llti, j)
forAll(lst, j)
{
llti[j] = m_[offsetPrev + j];
lst[j] = m_[offsetPrev + j];
}
offsetPrev = offsets_[i];
}
return llt;
return ll;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //
#include "CompactListListIO.C"
......
......@@ -164,6 +164,8 @@ public:
// into this CompactListList and annull the argument list.
void transfer(CompactListList<T>&);
//- Transfer the contents to the xfer container
inline xfer<CompactListList<T> > transfer();
// Other
......
......@@ -24,20 +24,20 @@ License
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class T>
inline CompactListList<T>::CompactListList()
inline Foam::CompactListList<T>::CompactListList()
{}
template<class T>
inline CompactListList<T>::CompactListList(const label nRows, const label nData)
inline Foam::CompactListList<T>::CompactListList
(
const label nRows,
const label nData
)
:
offsets_(nRows, 0),
m_(nData)
......@@ -45,7 +45,7 @@ inline CompactListList<T>::CompactListList(const label nRows, const label nData)
template<class T>
inline CompactListList<T>::CompactListList
inline Foam::CompactListList<T>::CompactListList
(
const label nRows,
const label nData,
......@@ -58,7 +58,8 @@ inline CompactListList<T>::CompactListList
template<class T>
inline autoPtr<CompactListList<T> > CompactListList<T>::clone() const
inline Foam::autoPtr<Foam::CompactListList<T> >
Foam::CompactListList<T>::clone() const
{
return autoPtr<CompactListList<T> >(new CompactListList<T>(*this));
}
......@@ -67,38 +68,46 @@ inline autoPtr<CompactListList<T> > CompactListList<T>::clone() const
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>
inline label CompactListList<T>::size() const
inline Foam::label Foam::CompactListList<T>::size() const
{
return offsets_.size();
}
template<class T>
inline const List<label>& CompactListList<T>::offsets() const
inline const Foam::List<Foam::label>& Foam::CompactListList<T>::offsets() const
{
return offsets_;
}
template<class T>
inline List<label>& CompactListList<T>::offsets()
inline Foam::List<label>& Foam::CompactListList<T>::offsets()
{
return offsets_;
}
template<class T>
inline const List<T>& CompactListList<T>::m() const
inline const Foam::List<T>& Foam::CompactListList<T>::m() const
{
return m_;
}
template<class T>
inline List<T>& CompactListList<T>::m()
inline Foam::List<T>& Foam::CompactListList<T>::m()
{
return m_;
}
template<class T>
inline label CompactListList<T>::index(const label i, const label j) const
inline Foam::label Foam::CompactListList<T>::index
(
const label i,
const label j
) const
{
if (i == 0)
{
......@@ -110,8 +119,9 @@ inline label CompactListList<T>::index(const label i, const label j) const
}
}
template<class T>
inline label CompactListList<T>::whichRow(const label i) const
inline Foam::label Foam::CompactListList<T>::whichRow(const label i) const
{
if (i < 0 || i >= m_.size())
{
......@@ -133,17 +143,35 @@ inline label CompactListList<T>::whichRow(const label i) const
return -1;
}
template<class T>
inline label CompactListList<T>::whichColumn(const label row, const label i)
const
inline Foam::label Foam::CompactListList<T>::whichColumn
(
const label row,
const label i
) const
{
return i - index(row, 0);
}
template<class T>
inline Foam::xfer<Foam::CompactListList<T> >
Foam::CompactListList<T>::transfer()
{
Foam::xfer<CompactListList<T> > xf;
xf().transfer(*this);
return xf;
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class T>
inline UList<T> CompactListList<T>::operator[](const label i)
inline Foam::UList<T> Foam::CompactListList<T>::operator[]
(
const label i
)
{
if (i == 0)
{
......@@ -155,8 +183,12 @@ inline UList<T> CompactListList<T>::operator[](const label i)
}
}
template<class T>
inline const UList<T> CompactListList<T>::operator[](const label i) const
inline const Foam::UList<T> Foam::CompactListList<T>::operator[]
(
const label i
) const
{
if (i == 0)
{
......@@ -170,13 +202,18 @@ inline const UList<T> CompactListList<T>::operator[](const label i) const
template<class T>
inline T& CompactListList<T>::operator()(const label i, const label j)
inline T& Foam::CompactListList<T>::operator()
(
const label i,
const label j
)
{
return m_[index(i, j)];
}
template<class T>
inline const T& CompactListList<T>::operator()
inline const T& Foam::CompactListList<T>::operator()
(
const label i,
const label j
......@@ -187,14 +224,10 @@ inline const T& CompactListList<T>::operator()
template<class T>
inline void CompactListList<T>::operator=(const T& t)
inline void Foam::CompactListList<T>::operator=(const T& t)
{
m_ = t;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* //
......@@ -27,15 +27,10 @@ License
#include "CompactListList.H"
#include "Istream.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
template<class T>
CompactListList<T>::CompactListList(Istream& is)
Foam::CompactListList<T>::CompactListList(Istream& is)
{
operator>>(is, *this);
}
......@@ -44,23 +39,19 @@ CompactListList<T>::CompactListList(Istream& is)
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
template<class T>
Istream& operator>>(Istream& is, CompactListList<T>& cll)
Foam::Istream& Foam::operator>>(Istream& is, CompactListList<T>& lst)
{
is >> cll.offsets_ >> cll.m_;
is >> lst.offsets_ >> lst.m_;
return is;
}
template<class T>
Ostream& operator<<(Ostream& os, const CompactListList<T>& cll)
Foam::Ostream& Foam::operator<<(Ostream& os, const CompactListList<T>& lst)
{
os << cll.offsets_ << cll.m_;
os << lst.offsets_ << lst.m_;
return os;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* //
......@@ -153,6 +153,8 @@ public:
//- Transfer contents of the argument DynamicList into this DynamicList
inline void transfer(DynamicList<T, SizeInc, SizeMult, SizeDiv>&);
//- Transfer the contents to the xfer container as a plain List
inline xfer<List<T> > transfer();
// Member Operators
......
......@@ -211,6 +211,16 @@ Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::transfer
}
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline Foam::xfer<Foam::List<T> >
Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::transfer()
{
Foam::xfer<List<T> > xf;
xf().transfer(*this);
return xf;
}
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append
(
......
......@@ -178,6 +178,9 @@ public:
// and annull the argument list.
void transfer(SortableList<T>&);
//- Transfer the contents to the xfer container
inline xfer<List<T> > transfer();
//- Return subscript-checked element of UList.
inline T& newElmt(const label);
......
......@@ -65,6 +65,16 @@ inline Foam::label Foam::List<T>::size() const
return UList<T>::size_;
}
template<class T>
inline Foam::xfer<Foam::List<T> > Foam::List<T>::transfer()
{
Foam::xfer<List<T> > xf;
xf().transfer(*this);
return xf;
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class T>
......
......@@ -101,7 +101,6 @@ void Foam::PackedList<nBits>::transfer(PackedList<nBits>& lst)
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
// Assignment.
template<int nBits>
void Foam::PackedList<nBits>::operator=(const PackedList<nBits>& lst)
{
......
......@@ -46,7 +46,6 @@ SourceFiles
namespace Foam
{
/*---------------------------------------------------------------------------*\
Class PackedListName Declaration
\*---------------------------------------------------------------------------*/
......@@ -158,6 +157,9 @@ public:
// and annull the argument list.
void transfer(PackedList<nBits>&);
//- Transfer the contents to the xfer container
inline xfer<PackedList<nBits> > transfer();
// Access
......@@ -195,10 +197,9 @@ public:
// Ostream operator
// // Write PackedList to Ostream.
// friend Ostream& operator<< <nBits> (Ostream&, const PackedList<nBits>&);
}
;
// // Write PackedList to Ostream.
// friend Ostream& operator<< <nBits> (Ostream&, const PackedList<nBits>&);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -32,12 +32,9 @@ License
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// Calculate underlying list size
template<int nBits>
inline label PackedList<nBits>::intSize(const label sz)
inline Foam::label Foam::PackedList<nBits>::intSize(const label sz)
{
const label nElemsPerLabel = sizeof(unsigned int)*8/nBits;
......@@ -47,7 +44,7 @@ inline label PackedList<nBits>::intSize(const label sz)
// Convert index into index in integer array
template<int nBits>
inline label PackedList<nBits>::intIndex(const label i)
inline Foam::label Foam::PackedList<nBits>::intIndex(const label <