CompactListList to use offsets sized one beyond nRows

This commit is contained in:
mattijs
2009-11-03 18:04:24 +00:00
parent fe17dd2d1f
commit baea3d76a8
5 changed files with 131 additions and 80 deletions

View File

@ -32,6 +32,8 @@ Description
#include "CompactListList.H" #include "CompactListList.H"
#include "IOstreams.H" #include "IOstreams.H"
#include "OStringStream.H"
#include "IStringStream.H"
using namespace Foam; using namespace Foam;
@ -40,7 +42,29 @@ using namespace Foam;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
{
// null construct
CompactListList<label> cll1; CompactListList<label> cll1;
Info<< "cll1:" << cll1 << endl;
// Resize and assign row by row
labelList row0(2, 0);
labelList row1(3, 1);
labelList rowSizes(2);
rowSizes[0] = row0.size();
rowSizes[1] = row1.size();
cll1.resize(rowSizes);
cll1[0].assign(row0); //note: operator= will not work since UList
cll1[1].assign(row1);
Info<< "cll1:" << cll1 << endl;
forAll(cll1.m(), i)
{
Info<< "i:" << i << " whichRow:" << cll1.whichRow(i) << endl;
}
}
List<List<label> > lll(5); List<List<label> > lll(5);
lll[0].setSize(3, 0); lll[0].setSize(3, 0);
@ -60,14 +84,21 @@ int main(int argc, char *argv[])
Info<< endl; Info<< endl;
Info<< "cll2(2, 3) = " << cll2(2, 3) << nl << endl;
cll2(2, 3) = 999;
Info<< "cll2(2, 3) = " << cll2(2, 3) << nl << endl; Info<< "cll2(2, 3) = " << cll2(2, 3) << nl << endl;
Info<< "cll2 as List<List<label > > " << List<List<label > >(cll2) << endl; Info<< "cll2 as List<List<label > > " << cll2()
<< endl;
cll2.setSize(3); cll2.setSize(3);
Info<< "cll2 = " << cll2 << endl; Info<< "cll2 = " << cll2 << endl;
cll2.setSize(0);
Info<< "cll2 = " << cll2 << endl;
List<label> rowSizes(5); List<label> rowSizes(5);
rowSizes[0] = 2; rowSizes[0] = 2;
@ -87,6 +118,27 @@ int main(int argc, char *argv[])
Info<< "cll3 = " << cll3 << endl; Info<< "cll3 = " << cll3 << endl;
Info<< "cll4 = " << cll4 << endl; Info<< "cll4 = " << cll4 << endl;
{
// IO
OStringStream ostr;
ostr << cll4;
IStringStream istr(ostr.str());
CompactListList<label> cll5(istr);
Info<< "cll5 = " << cll5 << endl;
}
{
// IO
cll4.clear();
OStringStream ostr;
ostr << cll4;
IStringStream istr(ostr.str());
CompactListList<label> cll5(istr);
Info<< "cll5 = " << cll5 << endl;
}
return 0; return 0;
} }

View File

@ -31,13 +31,15 @@ License
template<class T> template<class T>
Foam::CompactListList<T>::CompactListList(const List<List<T> >& ll) Foam::CompactListList<T>::CompactListList(const List<List<T> >& ll)
: :
offsets_(ll.size()) size_(ll.size()),
offsets_(ll.size()+1)
{ {
label sumSize = 0; label sumSize = 0;
offsets_[0] = 0;
forAll(ll, i) forAll(ll, i)
{ {
sumSize += ll[i].size(); sumSize += ll[i].size();
offsets_[i] = sumSize; offsets_[i+1] = sumSize;
} }
m_.setSize(sumSize); m_.setSize(sumSize);
@ -61,13 +63,15 @@ Foam::CompactListList<T>::CompactListList
const UList<label>& rowSizes const UList<label>& rowSizes
) )
: :
offsets_(rowSizes.size()) size_(rowSizes.size()),
offsets_(rowSizes.size()+1)
{ {
label sumSize = 0; label sumSize = 0;
offsets_[0] = 0;
forAll(rowSizes, i) forAll(rowSizes, i)
{ {
sumSize += rowSizes[i]; sumSize += rowSizes[i];
offsets_[i] = sumSize; offsets_[i+1] = sumSize;
} }
m_.setSize(sumSize); m_.setSize(sumSize);
@ -81,13 +85,15 @@ Foam::CompactListList<T>::CompactListList
const T& t const T& t
) )
: :
offsets_(rowSizes.size()) size_(rowSizes.size()),
offsets_(rowSizes.size()+1)
{ {
label sumSize = 0; label sumSize = 0;
offsets_[0] = 0;
forAll(rowSizes, i) forAll(rowSizes, i)
{ {
sumSize += rowSizes[i]; sumSize += rowSizes[i];
offsets_[i] = sumSize; offsets_[i+1] = sumSize;
} }
m_.setSize(sumSize, t); m_.setSize(sumSize, t);
@ -111,6 +117,7 @@ Foam::CompactListList<T>::CompactListList
bool reUse bool reUse
) )
: :
size_(lst.size()),
offsets_(lst.offsets_, reUse), offsets_(lst.offsets_, reUse),
m_(lst.m_, reUse) m_(lst.m_, reUse)
{} {}
@ -125,12 +132,13 @@ void Foam::CompactListList<T>::setSize(const label nRows)
{ {
clear(); clear();
} }
if (nRows < offsets_.size()) if (nRows < size())
{ {
offsets_.setSize(nRows); size_ = nRows;
m_.setSize(offsets_[nRows - 1]); offsets_.setSize(nRows+1);
m_.setSize(offsets_[nRows]);
} }
else if (nRows > offsets_.size()) else if (nRows > size())
{ {
FatalErrorIn("CompactListList<T>::setSize(const label nRows)") FatalErrorIn("CompactListList<T>::setSize(const label nRows)")
<< "Cannot be used to extend the list from " << offsets_.size() << "Cannot be used to extend the list from " << offsets_.size()
@ -148,7 +156,8 @@ void Foam::CompactListList<T>::setSize
const label nData const label nData
) )
{ {
offsets_.setSize(nRows); size_ = nRows;
offsets_.setSize(nRows+1);
m_.setSize(nData); m_.setSize(nData);
} }
@ -161,7 +170,8 @@ void Foam::CompactListList<T>::setSize
const T& t const T& t
) )
{ {
offsets_.setSize(nRows); size_ = nRows;
offsets_.setSize(nRows+1);
m_.setSize(nData, t); m_.setSize(nData, t);
} }
@ -169,13 +179,15 @@ void Foam::CompactListList<T>::setSize
template<class T> template<class T>
void Foam::CompactListList<T>::setSize(const UList<label>& rowSizes) void Foam::CompactListList<T>::setSize(const UList<label>& rowSizes)
{ {
offsets_.setSize(rowSizes.size()); size_ = rowSizes.size();
offsets_.setSize(rowSizes.size()+1);
label sumSize = 0; label sumSize = 0;
offsets_[0] = 0;
forAll(rowSizes, i) forAll(rowSizes, i)
{ {
sumSize += rowSizes[i]; sumSize += rowSizes[i];
offsets_[i] = sumSize; offsets_[i+1] = sumSize;
} }
m_.setSize(sumSize); m_.setSize(sumSize);
@ -185,13 +197,14 @@ void Foam::CompactListList<T>::setSize(const UList<label>& rowSizes)
template<class T> template<class T>
Foam::labelList Foam::CompactListList<T>::sizes() const Foam::labelList Foam::CompactListList<T>::sizes() const
{ {
labelList rowSizes(offsets_.size()); labelList rowSizes(size());
label prevOffset = 0; if (rowSizes.size() > 0)
forAll(offsets_, i)
{ {
rowSizes[i] = offsets_[i]-prevOffset; forAll(rowSizes, i)
prevOffset = offsets_[i]; {
rowSizes[i] = offsets_[i+1] - offsets_[i];
}
} }
return rowSizes; return rowSizes;
} }
@ -200,6 +213,7 @@ Foam::labelList Foam::CompactListList<T>::sizes() const
template<class T> template<class T>
void Foam::CompactListList<T>::clear() void Foam::CompactListList<T>::clear()
{ {
size_ = 0;
offsets_.clear(); offsets_.clear();
m_.clear(); m_.clear();
} }
@ -208,6 +222,7 @@ void Foam::CompactListList<T>::clear()
template<class T> template<class T>
void Foam::CompactListList<T>::transfer(CompactListList<T>& a) void Foam::CompactListList<T>::transfer(CompactListList<T>& a)
{ {
size_ = a.size_;
offsets_.transfer(a.offsets_); offsets_.transfer(a.offsets_);
m_.transfer(a.m_); m_.transfer(a.m_);
} }
@ -218,21 +233,11 @@ void Foam::CompactListList<T>::transfer(CompactListList<T>& a)
template<class T> template<class T>
Foam::List<Foam::List<T> > Foam::CompactListList<T>::operator()() const Foam::List<Foam::List<T> > Foam::CompactListList<T>::operator()() const
{ {
List<List<T> > ll(offsets_.size()); List<List<T> > ll(size());
label offsetPrev = 0; forAll(ll, i)
forAll(offsets_, i)
{ {
List<T>& lst = ll[i]; ll[i] = operator[](i);
lst.setSize(offsets_[i] - offsetPrev);
forAll(lst, j)
{
lst[j] = m_[offsetPrev + j];
}
offsetPrev = offsets_[i];
} }
return ll; return ll;

View File

@ -29,15 +29,17 @@ Description
A packed storage unstructured matrix of objects of type \<T\> A packed storage unstructured matrix of objects of type \<T\>
using an offset table for access. using an offset table for access.
The offset table is the size of the number of rows whose elements are the The offset table is the size of the number of rows+1
whose elements are the
accumulated sizes of the rows, i.e. accumulated sizes of the rows, i.e.
- offset[i] gives the index of first element of row i + 1 - offset[i] gives the index of first element of row i
- offset[i] - offset[i-1] is the number of elements in row i - offset[i+1] - offset[i] is the number of elements in row i
and for i = 0, offset[i-1] = 0.
Storage is allocated on free-store during construction. Storage is allocated on free-store during construction.
As a special case a null-contructed CompactListList has an empty
offsets_ (instead of size 1).
SourceFiles SourceFiles
CompactListList.C CompactListList.C
CompactListListI.H CompactListListI.H
@ -72,6 +74,8 @@ class CompactListList
{ {
// Private data // Private data
label size_;
//- Offset table //- Offset table
List<label> offsets_; List<label> offsets_;
@ -92,7 +96,7 @@ public:
inline CompactListList(); inline CompactListList();
//- Construct by converting given List<List<T> > //- Construct by converting given List<List<T> >
CompactListList(const List<List<T> >&); explicit CompactListList(const List<List<T> >&);
//- Construct given size of offset table (number of rows) //- Construct given size of offset table (number of rows)
// and number of data. // and number of data.
@ -103,13 +107,13 @@ public:
inline CompactListList(const label nRows, const label nData, const T&); inline CompactListList(const label nRows, const label nData, const T&);
//- Construct given list of row-sizes. //- Construct given list of row-sizes.
CompactListList(const UList<label>& rowSizes); explicit CompactListList(const UList<label>& rowSizes);
//- Construct given list of row-sizes //- Construct given list of row-sizes
CompactListList(const UList<label>& rowSizes, const T&); CompactListList(const UList<label>& rowSizes, const T&);
//- Construct by transferring the parameter contents //- Construct by transferring the parameter contents
CompactListList(const Xfer<CompactListList<T> >&); explicit CompactListList(const Xfer<CompactListList<T> >&);
//- Construct as copy or re-use as specified. //- Construct as copy or re-use as specified.
CompactListList(CompactListList<T>&, bool reUse); CompactListList(CompactListList<T>&, bool reUse);
@ -131,7 +135,7 @@ public:
//- Return true if the number of rows is zero //- Return true if the number of rows is zero
inline bool empty() const; inline bool empty() const;
//- Return the offset table //- Return the offset table (= size()+1)
inline const List<label>& offsets() const; inline const List<label>& offsets() const;
//- Return non-const access to the offset table //- Return non-const access to the offset table

View File

@ -24,11 +24,15 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "ListOps.H"
#include "SubList.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class T> template<class T>
inline Foam::CompactListList<T>::CompactListList() inline Foam::CompactListList<T>::CompactListList()
:
size_(0)
{} {}
@ -39,7 +43,8 @@ inline Foam::CompactListList<T>::CompactListList
const label nData const label nData
) )
: :
offsets_(nRows, 0), size_(nRows),
offsets_(nRows+1, 0),
m_(nData) m_(nData)
{} {}
@ -52,7 +57,8 @@ inline Foam::CompactListList<T>::CompactListList
const T& t const T& t
) )
: :
offsets_(nRows, 0), size_(nRows),
offsets_(nRows+1, 0),
m_(nData, t) m_(nData, t)
{} {}
@ -77,14 +83,14 @@ inline const Foam::CompactListList<T>& Foam::CompactListList<T>::null()
template<class T> template<class T>
inline Foam::label Foam::CompactListList<T>::size() const inline Foam::label Foam::CompactListList<T>::size() const
{ {
return offsets_.size(); return size_;
} }
template<class T> template<class T>
inline bool Foam::CompactListList<T>::empty() const inline bool Foam::CompactListList<T>::empty() const
{ {
return offsets_.empty(); return !size_;
} }
@ -123,14 +129,7 @@ inline Foam::label Foam::CompactListList<T>::index
const label j const label j
) const ) const
{ {
if (i == 0) return offsets_[i] + j;
{
return j;
}
else
{
return offsets_[i-1] + j;
}
} }
@ -146,15 +145,7 @@ inline Foam::label Foam::CompactListList<T>::whichRow(const label i) const
<< abort(FatalError); << abort(FatalError);
} }
forAll(offsets_, rowI) return findLower(offsets_, i+1);
{
if (i < offsets_[rowI])
{
return rowI;
}
}
return -1;
} }
@ -221,14 +212,9 @@ inline Foam::UList<T> Foam::CompactListList<T>::operator[]
const label i const label i
) )
{ {
if (i == 0) //return SubList<T>(m_, offsets_[i+1]-start, start);
{ label start = offsets_[i];
return UList<T>(m_.begin(), offsets_[i]); return UList<T>(&m_[start], offsets_[i+1] - start);
}
else
{
return UList<T>(&m_[offsets_[i-1]], offsets_[i] - offsets_[i-1]);
}
} }
@ -238,14 +224,9 @@ inline const Foam::UList<T> Foam::CompactListList<T>::operator[]
const label i const label i
) const ) const
{ {
if (i == 0) label start = offsets_[i];
{ //return SubList<T>(m_, offsets_[i+1]-start, start);
return UList<T>(m_.begin(), offsets_[i]); return UList<T>(const_cast<T*>(&m_[start]), offsets_[i+1] - start);
}
else
{
return UList<T>(&m_[offsets_[i-1]], offsets_[i] - offsets_[i-1]);
}
} }

View File

@ -42,6 +42,15 @@ template<class T>
Foam::Istream& Foam::operator>>(Istream& is, CompactListList<T>& lst) Foam::Istream& Foam::operator>>(Istream& is, CompactListList<T>& lst)
{ {
is >> lst.offsets_ >> lst.m_; is >> lst.offsets_ >> lst.m_;
// Note: empty list gets output as two empty lists
if (lst.offsets_.size() == 0)
{
lst.size_ = 0;
}
else
{
lst.size_ = lst.offsets_.size()-1;
}
return is; return is;
} }