Commit baea3d76 authored by mattijs's avatar mattijs
Browse files

CompactListList to use offsets sized one beyond nRows

parent fe17dd2d
......@@ -32,6 +32,8 @@ Description
#include "CompactListList.H"
#include "IOstreams.H"
#include "OStringStream.H"
#include "IStringStream.H"
using namespace Foam;
......@@ -40,7 +42,29 @@ using namespace Foam;
int main(int argc, char *argv[])
{
CompactListList<label> cll1;
{
// null construct
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);
lll[0].setSize(3, 0);
......@@ -60,14 +84,21 @@ int main(int argc, char *argv[])
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 as List<List<label > > " << List<List<label > >(cll2) << endl;
Info<< "cll2 as List<List<label > > " << cll2()
<< endl;
cll2.setSize(3);
Info<< "cll2 = " << cll2 << endl;
cll2.setSize(0);
Info<< "cll2 = " << cll2 << endl;
List<label> rowSizes(5);
rowSizes[0] = 2;
......@@ -87,6 +118,27 @@ int main(int argc, char *argv[])
Info<< "cll3 = " << cll3 << 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;
}
......
......@@ -31,13 +31,15 @@ License
template<class T>
Foam::CompactListList<T>::CompactListList(const List<List<T> >& ll)
:
offsets_(ll.size())
size_(ll.size()),
offsets_(ll.size()+1)
{
label sumSize = 0;
offsets_[0] = 0;
forAll(ll, i)
{
sumSize += ll[i].size();
offsets_[i] = sumSize;
offsets_[i+1] = sumSize;
}
m_.setSize(sumSize);
......@@ -61,13 +63,15 @@ Foam::CompactListList<T>::CompactListList
const UList<label>& rowSizes
)
:
offsets_(rowSizes.size())
size_(rowSizes.size()),
offsets_(rowSizes.size()+1)
{
label sumSize = 0;
offsets_[0] = 0;
forAll(rowSizes, i)
{
sumSize += rowSizes[i];
offsets_[i] = sumSize;
offsets_[i+1] = sumSize;
}
m_.setSize(sumSize);
......@@ -81,13 +85,15 @@ Foam::CompactListList<T>::CompactListList
const T& t
)
:
offsets_(rowSizes.size())
size_(rowSizes.size()),
offsets_(rowSizes.size()+1)
{
label sumSize = 0;
offsets_[0] = 0;
forAll(rowSizes, i)
{
sumSize += rowSizes[i];
offsets_[i] = sumSize;
offsets_[i+1] = sumSize;
}
m_.setSize(sumSize, t);
......@@ -111,6 +117,7 @@ Foam::CompactListList<T>::CompactListList
bool reUse
)
:
size_(lst.size()),
offsets_(lst.offsets_, reUse),
m_(lst.m_, reUse)
{}
......@@ -125,12 +132,13 @@ void Foam::CompactListList<T>::setSize(const label nRows)
{
clear();
}
if (nRows < offsets_.size())
if (nRows < size())
{
offsets_.setSize(nRows);
m_.setSize(offsets_[nRows - 1]);
size_ = nRows;
offsets_.setSize(nRows+1);
m_.setSize(offsets_[nRows]);
}
else if (nRows > offsets_.size())
else if (nRows > size())
{
FatalErrorIn("CompactListList<T>::setSize(const label nRows)")
<< "Cannot be used to extend the list from " << offsets_.size()
......@@ -148,7 +156,8 @@ void Foam::CompactListList<T>::setSize
const label nData
)
{
offsets_.setSize(nRows);
size_ = nRows;
offsets_.setSize(nRows+1);
m_.setSize(nData);
}
......@@ -161,7 +170,8 @@ void Foam::CompactListList<T>::setSize
const T& t
)
{
offsets_.setSize(nRows);
size_ = nRows;
offsets_.setSize(nRows+1);
m_.setSize(nData, t);
}
......@@ -169,13 +179,15 @@ void Foam::CompactListList<T>::setSize
template<class T>
void Foam::CompactListList<T>::setSize(const UList<label>& rowSizes)
{
offsets_.setSize(rowSizes.size());
size_ = rowSizes.size();
offsets_.setSize(rowSizes.size()+1);
label sumSize = 0;
offsets_[0] = 0;
forAll(rowSizes, i)
{
sumSize += rowSizes[i];
offsets_[i] = sumSize;
offsets_[i+1] = sumSize;
}
m_.setSize(sumSize);
......@@ -185,13 +197,14 @@ void Foam::CompactListList<T>::setSize(const UList<label>& rowSizes)
template<class T>
Foam::labelList Foam::CompactListList<T>::sizes() const
{
labelList rowSizes(offsets_.size());
labelList rowSizes(size());
label prevOffset = 0;
forAll(offsets_, i)
if (rowSizes.size() > 0)
{
rowSizes[i] = offsets_[i]-prevOffset;
prevOffset = offsets_[i];
forAll(rowSizes, i)
{
rowSizes[i] = offsets_[i+1] - offsets_[i];
}
}
return rowSizes;
}
......@@ -200,6 +213,7 @@ Foam::labelList Foam::CompactListList<T>::sizes() const
template<class T>
void Foam::CompactListList<T>::clear()
{
size_ = 0;
offsets_.clear();
m_.clear();
}
......@@ -208,6 +222,7 @@ void Foam::CompactListList<T>::clear()
template<class T>
void Foam::CompactListList<T>::transfer(CompactListList<T>& a)
{
size_ = a.size_;
offsets_.transfer(a.offsets_);
m_.transfer(a.m_);
}
......@@ -218,21 +233,11 @@ void Foam::CompactListList<T>::transfer(CompactListList<T>& a)
template<class T>
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(offsets_, i)
forAll(ll, i)
{
List<T>& lst = ll[i];
lst.setSize(offsets_[i] - offsetPrev);
forAll(lst, j)
{
lst[j] = m_[offsetPrev + j];
}
offsetPrev = offsets_[i];
ll[i] = operator[](i);
}
return ll;
......
......@@ -29,15 +29,17 @@ Description
A packed storage unstructured matrix of objects of type \<T\>
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.
- offset[i] gives the index of first element of row i + 1
- offset[i] - offset[i-1] is the number of elements in row i
and for i = 0, offset[i-1] = 0.
- offset[i] gives the index of first element of row i
- offset[i+1] - offset[i] is the number of elements in row i
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
CompactListList.C
CompactListListI.H
......@@ -72,6 +74,8 @@ class CompactListList
{
// Private data
label size_;
//- Offset table
List<label> offsets_;
......@@ -92,7 +96,7 @@ public:
inline CompactListList();
//- 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)
// and number of data.
......@@ -103,13 +107,13 @@ public:
inline CompactListList(const label nRows, const label nData, const T&);
//- Construct given list of row-sizes.
CompactListList(const UList<label>& rowSizes);
explicit CompactListList(const UList<label>& rowSizes);
//- Construct given list of row-sizes
CompactListList(const UList<label>& rowSizes, const T&);
//- 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.
CompactListList(CompactListList<T>&, bool reUse);
......@@ -131,7 +135,7 @@ public:
//- Return true if the number of rows is zero
inline bool empty() const;
//- Return the offset table
//- Return the offset table (= size()+1)
inline const List<label>& offsets() const;
//- Return non-const access to the offset table
......
......@@ -24,11 +24,15 @@ License
\*---------------------------------------------------------------------------*/
#include "ListOps.H"
#include "SubList.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class T>
inline Foam::CompactListList<T>::CompactListList()
:
size_(0)
{}
......@@ -39,7 +43,8 @@ inline Foam::CompactListList<T>::CompactListList
const label nData
)
:
offsets_(nRows, 0),
size_(nRows),
offsets_(nRows+1, 0),
m_(nData)
{}
......@@ -52,7 +57,8 @@ inline Foam::CompactListList<T>::CompactListList
const T& t
)
:
offsets_(nRows, 0),
size_(nRows),
offsets_(nRows+1, 0),
m_(nData, t)
{}
......@@ -77,14 +83,14 @@ inline const Foam::CompactListList<T>& Foam::CompactListList<T>::null()
template<class T>
inline Foam::label Foam::CompactListList<T>::size() const
{
return offsets_.size();
return size_;
}
template<class T>
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
{
if (i == 0)
{
return j;
}
else
{
return offsets_[i-1] + j;
}
return offsets_[i] + j;
}
......@@ -146,15 +145,7 @@ inline Foam::label Foam::CompactListList<T>::whichRow(const label i) const
<< abort(FatalError);
}
forAll(offsets_, rowI)
{
if (i < offsets_[rowI])
{
return rowI;
}
}
return -1;
return findLower(offsets_, i+1);
}
......@@ -221,14 +212,9 @@ inline Foam::UList<T> Foam::CompactListList<T>::operator[]
const label i
)
{
if (i == 0)
{
return UList<T>(m_.begin(), offsets_[i]);
}
else
{
return UList<T>(&m_[offsets_[i-1]], offsets_[i] - offsets_[i-1]);
}
//return SubList<T>(m_, offsets_[i+1]-start, start);
label start = offsets_[i];
return UList<T>(&m_[start], offsets_[i+1] - start);
}
......@@ -238,14 +224,9 @@ inline const Foam::UList<T> Foam::CompactListList<T>::operator[]
const label i
) const
{
if (i == 0)
{
return UList<T>(m_.begin(), offsets_[i]);
}
else
{
return UList<T>(&m_[offsets_[i-1]], offsets_[i] - offsets_[i-1]);
}
label start = offsets_[i];
//return SubList<T>(m_, offsets_[i+1]-start, start);
return UList<T>(const_cast<T*>(&m_[start]), offsets_[i+1] - start);
}
......
......@@ -42,6 +42,15 @@ template<class T>
Foam::Istream& Foam::operator>>(Istream& is, CompactListList<T>& lst)
{
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;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment