Commit be20a8a4 authored by Franjo Juretic's avatar Franjo Juretic

DynList does not inherit UList any more

parent a6640eb0
......@@ -29,9 +29,11 @@ License
// Construct from Istream
template<class T, Foam::label staticSize>
Foam::DynList<T, staticSize>::DynList(Istream& is)
Foam::DynList<T, staticSize>::DynList(Istream&)
:
UList<T>(),
dataPtr_(NULL),
nAllocated_(0),
staticData_(),
nextFree_(0)
{
FatalErrorIn
......@@ -39,11 +41,6 @@ Foam::DynList<T, staticSize>::DynList(Istream& is)
"template<class T, Foam::label staticSize>"
"\nFoam::DynList<T, staticSize>::DynList(Istream& is)"
) << "Not implemented" << exit(FatalError);
List<T> helper(is);
nextFree_ = helper.size();
UList<T>::swap(helper);
}
......@@ -54,7 +51,7 @@ Foam::Ostream& Foam::operator<<
const Foam::DynList<T, staticSize>& DL
)
{
UList<T> helper(const_cast<T*>(DL.begin()), DL.nextFree_);
UList<T> helper(DL.dataPtr_, DL.nextFree_);
os << helper;
return os;
......@@ -74,9 +71,11 @@ Foam::Istream& Foam::operator>>
"\nFoam::Istream& Foam::operator>>"
"(Foam::Istream& is, Foam::DynList<T, staticSize>& DL)"
) << "Not implemented" << exit(FatalError);
is >> static_cast<List<T>&>(DL);
DL.nextFree_ = DL.List<T>::size();
UList<T> helper(DL.dataPtr_, DL.nextFree_);
//is >> static_cast<List<T>&>(DL);
is >> helper;
DL.nextFree_ = helper.size();
return is;
}
......
......@@ -73,10 +73,14 @@ Istream& operator>>
template<class T, label staticSize = 16>
class DynList
:
public UList<T>
{
// Private data
//- pointer to the data
T* dataPtr_;
//- size of the allocated data
label nAllocated_;
//- statically allocated data (used for short lists)
T staticData_[staticSize];
......@@ -84,12 +88,21 @@ class DynList
label nextFree_;
// Private member functions
//- access to the data pointer
inline T* data();
//- const access to the data pointer
inline const T* data() const;
//- allocate list size
inline void allocateSize(const label);
//- check if index is inside the scope (used for debugging only)
inline void checkIndex(const label) const;
//- check if nAllocated_ is greater or equal to nextFree_
inline void checkAllocation() const;
public:
// Constructors
......@@ -205,6 +218,10 @@ public:
template<class ListType>
inline void operator=(const ListType&);
//- Compare the list with the another one
inline bool operator==(const DynList<T, staticSize>&) const;
inline bool operator!=(const DynList<T, staticSize>&) const;
// IOstream operators
......
......@@ -23,48 +23,66 @@ License
\*---------------------------------------------------------------------------*/
template<class T, Foam::label staticSize>
inline T* Foam::DynList<T, staticSize>::data()
{
return dataPtr_;
}
template<class T, Foam::label staticSize>
inline const T* Foam::DynList<T, staticSize>::data() const
{
return dataPtr_;
}
template<class T, Foam::label staticSize>
inline void Foam::DynList<T, staticSize>::allocateSize(const label s)
{
if( s > UList<T>::size() )
checkAllocation();
if( s > staticSize )
{
T* newData = new T[s];
if( s > nAllocated_ )
{
//- allocates enough space for the elements
T* newData = new T[s];
for(label i=0;i<nextFree_;++i)
newData[i] = this->operator[](i);
for(label i=0;i<nextFree_;++i)
newData[i] = this->operator[](i);
T* data = UList<T>::begin();
if( data && (data != staticData_) )
delete [] data;
if( nAllocated_ > staticSize )
delete [] dataPtr_;
//UList<T>::reset(newData, s);
this->UList<T>::operator=(UList<T>(newData, s));
}
else if( (s > staticSize) && (s < UList<T>::size()) )
{
T* newData = new T[s];
dataPtr_ = newData;
nAllocated_ = s;
}
else if( s < nAllocated_ )
{
//- shrinks the list
T* newData = new T[s];
for(label i=0;i<s;++i)
newData[i] = this->operator[](i);
for(label i=0;i<s;++i)
newData[i] = this->operator[](i);
T* data = UList<T>::begin();
delete [] data;
delete [] dataPtr_;
//UList<T>::reset(newData, s);
this->UList<T>::operator=(UList<T>(newData, s));
dataPtr_ = newData;
nAllocated_ = s;
}
}
else if( (s <= staticSize) && (UList<T>::size() > staticSize) )
else
{
for(label i=0;i<s;++i)
staticData_[i] = UList<T>::operator[](i);
if( nAllocated_ > staticSize )
{
//- delete dynamically allocated data
for(label i=0;i<s;++i)
staticData_[i] = dataPtr_[i];
T* data = UList<T>::begin();
if( data && (data != staticData_) )
delete [] data;
delete [] dataPtr_;
}
//UList<T>::reset(staticData_, staticSize);
this->UList<T>::operator=(UList<T>(staticData_, staticSize));
dataPtr_ = staticData_;
nAllocated_ = staticSize;
}
}
......@@ -82,60 +100,105 @@ inline void Foam::DynList<T, staticSize>::checkIndex(const label i) const
}
}
template<class T, Foam::label staticSize>
inline void Foam::DynList<T, staticSize>::checkAllocation() const
{
if( nextFree_ > nAllocated_ )
FatalErrorIn
(
"template<class T, Foam::label staticSize> "
"inline void Foam::DynList<T, staticSize>::"
"checkAllocation() const"
) << "nextFree_ is out of scope 0 " << " and " << nAllocated_
<< abort(FatalError);
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
//- Construct null
template<class T, Foam::label staticSize>
inline Foam::DynList<T, staticSize>::DynList()
:
UList<T>(staticData_, staticSize),
dataPtr_(NULL),
nAllocated_(0),
staticData_(),
nextFree_(0)
{}
{
setSize(0);
# ifdef DEBUG
checkAllocation();
# endif
}
template<class T, Foam::label staticSize>
inline Foam::DynList<T, staticSize>::DynList(const label s)
:
UList<T>(staticData_, staticSize),
dataPtr_(NULL),
nAllocated_(0),
staticData_(),
nextFree_(0)
{
setSize(s);
# ifdef DEBUG
checkAllocation();
# endif
}
template<class T, Foam::label staticSize>
inline Foam::DynList<T, staticSize>::DynList(const label s, const T& val)
:
UList<T>(staticData_, staticSize),
dataPtr_(NULL),
nAllocated_(0),
staticData_(),
nextFree_(0)
{
setSize(s);
for(label i=0;i<s;++i)
this->operator[](i) = val;
# ifdef DEBUG
checkAllocation();
# endif
}
template<class T, Foam::label staticSize>
inline Foam::DynList<T, staticSize>::DynList(const UList<T>& ul)
:
UList<T>(staticData_, staticSize),
dataPtr_(NULL),
nAllocated_(0),
staticData_(),
nextFree_(0)
{
setSize(ul.size());
forAll(ul, i)
this->operator[](i) = ul[i];
# ifdef DEBUG
checkAllocation();
# endif
}
template<class T, Foam::label staticSize>
template<class ListType>
inline Foam::DynList<T, staticSize>::DynList(const ListType& l)
:
UList<T>(staticData_, staticSize),
dataPtr_(NULL),
nAllocated_(0),
staticData_(),
nextFree_(0)
{
setSize(l.size());
for(label i=0;i<nextFree_;++i)
this->operator[](i) = l[i];
# ifdef DEBUG
checkAllocation();
# endif
}
//- Copy construct
......@@ -145,19 +208,24 @@ inline Foam::DynList<T, staticSize>::DynList
const DynList<T, staticSize>& dl
)
:
UList<T>(staticData_, staticSize),
dataPtr_(NULL),
nAllocated_(0),
staticData_(),
nextFree_(0)
{
setSize(dl.size());
for(label i=0;i<nextFree_;++i)
this->operator[](i) = dl[i];
# ifdef DEBUG
checkAllocation();
# endif
}
template<class T, Foam::label staticSize>
inline Foam::DynList<T, staticSize>::~DynList()
{
allocateSize(0);
//UList<T>::reset(NULL, 0);
}
......@@ -166,12 +234,20 @@ inline Foam::DynList<T, staticSize>::~DynList()
template<class T, Foam::label staticSize>
inline Foam::label Foam::DynList<T, staticSize>::size() const
{
# ifdef DEBUG
checkAllocation();
# endif
return nextFree_;
}
template<class T, Foam::label staticSize>
inline Foam::label Foam::DynList<T, staticSize>::byteSize() const
{
# ifdef DEBUG
checkAllocation();
# endif
if( !contiguous<T>() )
{
FatalErrorIn("DynList<T>::byteSize()")
......@@ -181,20 +257,31 @@ inline Foam::label Foam::DynList<T, staticSize>::byteSize() const
}
return nextFree_*sizeof(T);
}
template<class T, Foam::label staticSize>
inline void Foam::DynList<T, staticSize>::setSize(const label s)
{
# ifdef DEBUG
checkAllocation();
# endif
allocateSize(s);
nextFree_ = s;
# ifdef DEBUG
checkAllocation();
# endif
}
template<class T, Foam::label staticSize>
inline void Foam::DynList<T, staticSize>::clear()
{
# ifdef DEBUG
checkAllocation();
# endif
nextFree_ = 0;
}
......@@ -202,34 +289,62 @@ inline void Foam::DynList<T, staticSize>::clear()
template<class T, Foam::label staticSize>
void Foam::DynList<T, staticSize>::shrink()
{
# ifdef DEBUG
checkAllocation();
# endif
allocateSize(nextFree_);
# ifdef DEBUG
checkAllocation();
# endif
}
template<class T, Foam::label staticSize>
inline void Foam::DynList<T, staticSize>::append(const T& e)
{
if( nextFree_ >= UList<T>::size() )
# ifdef DEBUG
checkAllocation();
# endif
if( nextFree_ >= nAllocated_ )
{
const label newSize = 2*UList<T>::size()+2;
const label newSize = 2*nAllocated_+2;
allocateSize(newSize);
}
UList<T>::operator[](nextFree_++) = e;
# ifdef DEBUG
checkAllocation();
# endif
this->operator[](nextFree_++) = e;
}
template<class T, Foam::label staticSize>
inline void Foam::DynList<T, staticSize>::appendIfNotIn(const T& e)
{
# ifdef DEBUG
checkAllocation();
# endif
if( !contains(e) )
append(e);
# ifdef DEBUG
checkAllocation();
# endif
}
template<class T, Foam::label staticSize>
inline bool Foam::DynList<T, staticSize>::contains(const T& e) const
{
# ifdef DEBUG
checkAllocation();
# endif
for(label i=0;i<nextFree_;++i)
{
if( UList<T>::operator[](i) == e )
if( this->operator[](i) == e )
return true;
}
......@@ -242,9 +357,13 @@ inline Foam::label Foam::DynList<T, staticSize>::containsAtPosition
const T& e
) const
{
# ifdef DEBUG
checkAllocation();
# endif
for(label i=0;i<nextFree_;++i)
{
if( UList<T>::operator[](i) == e )
if( this->operator[](i) == e )
return i;
}
......@@ -254,12 +373,20 @@ inline Foam::label Foam::DynList<T, staticSize>::containsAtPosition
template<class T, Foam::label staticSize>
inline const T& Foam::DynList<T, staticSize>::lastElement() const
{
# ifdef DEBUG
checkAllocation();
# endif
return this->operator[](nextFree_-1);
}
template<class T, Foam::label staticSize>
inline T Foam::DynList<T, staticSize>::removeLastElement()
{
# ifdef DEBUG
checkAllocation();
# endif
if( nextFree_ == 0 )
{
FatalErrorIn
......@@ -268,13 +395,18 @@ inline T Foam::DynList<T, staticSize>::removeLastElement()
) << "List is empty" << abort(FatalError);
}
T el = UList<T>::operator[](--nextFree_);
T el = this->operator[](nextFree_-1);
--nextFree_;
return el;
}
template<class T, Foam::label staticSize>
inline T Foam::DynList<T, staticSize>::removeElement(const label i)
{
# ifdef DEBUG
checkAllocation();
# endif
if( nextFree_ == 0 )
{
FatalErrorIn
......@@ -287,12 +419,20 @@ inline T Foam::DynList<T, staticSize>::removeElement(const label i)
this->operator[](i) = this->operator[](nextFree_-1);
--nextFree_;
# ifdef DEBUG
checkAllocation();
# endif
return el;
}
template<class T, Foam::label staticSize>
inline T& Foam::DynList<T, staticSize>::newElmt(const label i)
{
# ifdef DEBUG
checkAllocation();
# endif
return this->operator()(i);
}
......@@ -301,13 +441,21 @@ inline T& Foam::DynList<T, staticSize>::newElmt(const label i)
template<class T, Foam::label staticSize>
inline T& Foam::DynList<T, staticSize>::operator()(const label i)
{
# ifdef DEBUG
checkAllocation();
# endif
nextFree_ = Foam::max(nextFree_, i + 1);
if( nextFree_ >= UList<T>::size() )
if( nextFree_ >= nAllocated_ )
{
allocateSize(2 * nextFree_+1);
}
# ifdef DEBUG
checkAllocation();
# endif
return this->operator[](i);
}
......@@ -315,20 +463,22 @@ template<class T, Foam::label staticSize>
inline const T& Foam::DynList<T, staticSize>::operator[](const label i) const
{
# ifdef FULLDEBUG
checkAllocation();
checkIndex(i);
# endif
return UList<T>::operator[](i);
return dataPtr_[i];
}
template<class T, Foam::label staticSize>
inline T& Foam::DynList<T, staticSize>::operator[](const label i)
{
# ifdef FULLDEBUG
checkAllocation();
checkIndex(i);
# endif
return UList<T>::operator[](i);
return dataPtr_[i];
}
template<class T, Foam::label staticSize>
......@@ -374,7 +524,12 @@ inline const T& Foam::DynList<T, staticSize>::rcElement
template<class T, Foam::label staticSize>
inline void Foam::DynList<T, staticSize>::operator=(const T& t)
{
UList<T>::operator=(t);
# ifdef DEBUG
checkAllocation();
# endif
for(label i=0;i<nextFree_;++i)
operator[](i) = t;
}
template<class T, Foam::label staticSize>
......@@ -383,9 +538,17 @@ inline void Foam::DynList<T, staticSize>::operator=
const DynList<T, staticSize>& dl
)
{
# ifdef DEBUG
checkAllocation();
# endif
allocateSize(dl.size());
nextFree_ = dl.size();
# ifdef DEBUG
checkAllocation();
# endif
for(label i=0;i<nextFree_;++i)
this->operator[](i) = dl[i];
}
......@@ -394,12 +557,45 @@ template<class T, Foam::label staticSize>
template<class ListType>
inline void Foam::DynList<T, staticSize>::operator=(const ListType& l)
{
# ifdef DEBUG
checkAllocation();
# endif
allocateSize(l.size());
nextFree_ = l.size();
# ifdef DEBUG
checkAllocation();
# endif
for(label i=0;i<nextFree_;++i)
this->operator[](i) = l[i];
}
template<class T, Foam::label staticSize>
inline bool Foam::DynList<T, staticSize>::operator==
(
const DynList<T, staticSize>& DL
) const
{
if( nextFree_ != DL.nextFree_ )
return false;
forAll(DL, i)
if( this->operator[](i) != DL[i] )