Commit 8338fbe4 authored by Franjo's avatar Franjo
Browse files

Updated DynList: It compiles with OpenFOAM-4.x

parent 496ff40b
...@@ -29,9 +29,11 @@ License ...@@ -29,9 +29,11 @@ License
// Construct from Istream // Construct from Istream
template<class T, Foam::label staticSize> 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) nextFree_(0)
{ {
FatalErrorIn FatalErrorIn
...@@ -39,11 +41,6 @@ Foam::DynList<T, staticSize>::DynList(Istream& is) ...@@ -39,11 +41,6 @@ Foam::DynList<T, staticSize>::DynList(Istream& is)
"template<class T, Foam::label staticSize>" "template<class T, Foam::label staticSize>"
"\nFoam::DynList<T, staticSize>::DynList(Istream& is)" "\nFoam::DynList<T, staticSize>::DynList(Istream& is)"
) << "Not implemented" << exit(FatalError); ) << "Not implemented" << exit(FatalError);
List<T> helper(is);
nextFree_ = helper.size();
UList<T>::swap(helper);
} }
...@@ -54,7 +51,7 @@ Foam::Ostream& Foam::operator<< ...@@ -54,7 +51,7 @@ Foam::Ostream& Foam::operator<<
const Foam::DynList<T, staticSize>& DL 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; os << helper;
return os; return os;
...@@ -74,9 +71,11 @@ Foam::Istream& Foam::operator>> ...@@ -74,9 +71,11 @@ Foam::Istream& Foam::operator>>
"\nFoam::Istream& Foam::operator>>" "\nFoam::Istream& Foam::operator>>"
"(Foam::Istream& is, Foam::DynList<T, staticSize>& DL)" "(Foam::Istream& is, Foam::DynList<T, staticSize>& DL)"
) << "Not implemented" << exit(FatalError); ) << "Not implemented" << exit(FatalError);
is >> static_cast<List<T>&>(DL); UList<T> helper(DL.dataPtr_, DL.nextFree_);
DL.nextFree_ = DL.List<T>::size(); //is >> static_cast<List<T>&>(DL);
is >> helper;
DL.nextFree_ = helper.size();
return is; return is;
} }
......
...@@ -73,10 +73,14 @@ Istream& operator>> ...@@ -73,10 +73,14 @@ Istream& operator>>
template<class T, label staticSize = 16> template<class T, label staticSize = 16>
class DynList class DynList
:
public UList<T>
{ {
// Private data // Private data
//- pointer to the data
T* dataPtr_;
//- size of the allocated data
label nAllocated_;
//- statically allocated data (used for short lists) //- statically allocated data (used for short lists)
T staticData_[staticSize]; T staticData_[staticSize];
...@@ -84,12 +88,21 @@ class DynList ...@@ -84,12 +88,21 @@ class DynList
label nextFree_; label nextFree_;
// Private member functions // 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 //- allocate list size
inline void allocateSize(const label); inline void allocateSize(const label);
//- check if index is inside the scope (used for debugging only) //- check if index is inside the scope (used for debugging only)
inline void checkIndex(const label) const; inline void checkIndex(const label) const;
//- check if nAllocated_ is greater or equal to nextFree_
inline void checkAllocation() const;
public: public:
// Constructors // Constructors
...@@ -205,6 +218,10 @@ public: ...@@ -205,6 +218,10 @@ public:
template<class ListType> template<class ListType>
inline void operator=(const 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 // IOstream operators
......
...@@ -23,48 +23,66 @@ License ...@@ -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> template<class T, Foam::label staticSize>
inline void Foam::DynList<T, staticSize>::allocateSize(const label s) 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) for(label i=0;i<nextFree_;++i)
newData[i] = this->operator[](i); newData[i] = this->operator[](i);
T* data = UList<T>::begin(); if( nAllocated_ > staticSize )
if( data && (data != staticData_) ) delete [] dataPtr_;
delete [] data;
//UList<T>::reset(newData, s); dataPtr_ = newData;
this->UList<T>::operator=(UList<T>(newData, s)); nAllocated_ = s;
} }
else if( (s > staticSize) && (s < UList<T>::size()) ) else if( s < nAllocated_ )
{ {
T* newData = new T[s]; //- shrinks the list
T* newData = new T[s];
for(label i=0;i<s;++i) for(label i=0;i<s;++i)
newData[i] = this->operator[](i); newData[i] = this->operator[](i);
T* data = UList<T>::begin(); delete [] dataPtr_;
delete [] data;
//UList<T>::reset(newData, s); dataPtr_ = newData;
this->UList<T>::operator=(UList<T>(newData, s)); nAllocated_ = s;
}
} }
else if( (s <= staticSize) && (UList<T>::size() > staticSize) ) else
{ {
for(label i=0;i<s;++i) if( nAllocated_ > staticSize )
staticData_[i] = UList<T>::operator[](i); {
//- delete dynamically allocated data
for(label i=0;i<s;++i)
staticData_[i] = dataPtr_[i];
T* data = UList<T>::begin(); delete [] dataPtr_;
if( data && (data != staticData_) ) }
delete [] data;
//UList<T>::reset(staticData_, staticSize); dataPtr_ = staticData_;
this->UList<T>::operator=(UList<T>(staticData_, staticSize)); nAllocated_ = staticSize;
} }
} }
...@@ -82,60 +100,105 @@ inline void Foam::DynList<T, staticSize>::checkIndex(const label i) const ...@@ -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 * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
//- Construct null //- Construct null
template<class T, Foam::label staticSize> template<class T, Foam::label staticSize>
inline Foam::DynList<T, staticSize>::DynList() inline Foam::DynList<T, staticSize>::DynList()
: :
UList<T>(staticData_, staticSize), dataPtr_(NULL),
nAllocated_(0),
staticData_(),
nextFree_(0) nextFree_(0)
{} {
setSize(0);
# ifdef DEBUG
checkAllocation();
# endif
}
template<class T, Foam::label staticSize> template<class T, Foam::label staticSize>
inline Foam::DynList<T, staticSize>::DynList(const label s) inline Foam::DynList<T, staticSize>::DynList(const label s)
: :
UList<T>(staticData_, staticSize), dataPtr_(NULL),
nAllocated_(0),
staticData_(),
nextFree_(0) nextFree_(0)
{ {
setSize(s); setSize(s);
# ifdef DEBUG
checkAllocation();
# endif
} }
template<class T, Foam::label staticSize> template<class T, Foam::label staticSize>
inline Foam::DynList<T, staticSize>::DynList(const label s, const T& val) inline Foam::DynList<T, staticSize>::DynList(const label s, const T& val)
: :
UList<T>(staticData_, staticSize), dataPtr_(NULL),
nAllocated_(0),
staticData_(),
nextFree_(0) nextFree_(0)
{ {
setSize(s); setSize(s);
for(label i=0;i<s;++i) for(label i=0;i<s;++i)
this->operator[](i) = val; this->operator[](i) = val;
# ifdef DEBUG
checkAllocation();
# endif
} }
template<class T, Foam::label staticSize> template<class T, Foam::label staticSize>
inline Foam::DynList<T, staticSize>::DynList(const UList<T>& ul) inline Foam::DynList<T, staticSize>::DynList(const UList<T>& ul)
: :
UList<T>(staticData_, staticSize), dataPtr_(NULL),
nAllocated_(0),
staticData_(),
nextFree_(0) nextFree_(0)
{ {
setSize(ul.size()); setSize(ul.size());
forAll(ul, i) forAll(ul, i)
this->operator[](i) = ul[i]; this->operator[](i) = ul[i];
# ifdef DEBUG
checkAllocation();
# endif
} }
template<class T, Foam::label staticSize> template<class T, Foam::label staticSize>
template<class ListType> template<class ListType>
inline Foam::DynList<T, staticSize>::DynList(const ListType& l) inline Foam::DynList<T, staticSize>::DynList(const ListType& l)
: :
UList<T>(staticData_, staticSize), dataPtr_(NULL),
nAllocated_(0),
staticData_(),
nextFree_(0) nextFree_(0)
{ {
setSize(l.size()); setSize(l.size());
for(label i=0;i<nextFree_;++i) for(label i=0;i<nextFree_;++i)
this->operator[](i) = l[i]; this->operator[](i) = l[i];
# ifdef DEBUG
checkAllocation();
# endif
} }
//- Copy construct //- Copy construct
...@@ -145,19 +208,24 @@ inline Foam::DynList<T, staticSize>::DynList ...@@ -145,19 +208,24 @@ inline Foam::DynList<T, staticSize>::DynList
const DynList<T, staticSize>& dl const DynList<T, staticSize>& dl
) )
: :
UList<T>(staticData_, staticSize), dataPtr_(NULL),
nAllocated_(0),
staticData_(),
nextFree_(0) nextFree_(0)
{ {
setSize(dl.size()); setSize(dl.size());
for(label i=0;i<nextFree_;++i) for(label i=0;i<nextFree_;++i)
this->operator[](i) = dl[i]; this->operator[](i) = dl[i];
# ifdef DEBUG
checkAllocation();
# endif
} }
template<class T, Foam::label staticSize> template<class T, Foam::label staticSize>
inline Foam::DynList<T, staticSize>::~DynList() inline Foam::DynList<T, staticSize>::~DynList()
{ {
allocateSize(0); allocateSize(0);
//UList<T>::reset(NULL, 0);
} }
...@@ -166,12 +234,20 @@ inline Foam::DynList<T, staticSize>::~DynList() ...@@ -166,12 +234,20 @@ inline Foam::DynList<T, staticSize>::~DynList()
template<class T, Foam::label staticSize> template<class T, Foam::label staticSize>
inline Foam::label Foam::DynList<T, staticSize>::size() const inline Foam::label Foam::DynList<T, staticSize>::size() const
{ {
# ifdef DEBUG
checkAllocation();
# endif
return nextFree_; return nextFree_;
} }
template<class T, Foam::label staticSize> template<class T, Foam::label staticSize>
inline Foam::label Foam::DynList<T, staticSize>::byteSize() const inline Foam::label Foam::DynList<T, staticSize>::byteSize() const
{ {
# ifdef DEBUG
checkAllocation();
# endif
if( !contiguous<T>() ) if( !contiguous<T>() )
{ {
FatalErrorIn("DynList<T>::byteSize()") FatalErrorIn("DynList<T>::byteSize()")
...@@ -181,20 +257,31 @@ inline Foam::label Foam::DynList<T, staticSize>::byteSize() const ...@@ -181,20 +257,31 @@ inline Foam::label Foam::DynList<T, staticSize>::byteSize() const
} }
return nextFree_*sizeof(T); return nextFree_*sizeof(T);
} }
template<class T, Foam::label staticSize> template<class T, Foam::label staticSize>
inline void Foam::DynList<T, staticSize>::setSize(const label s) inline void Foam::DynList<T, staticSize>::setSize(const label s)
{ {
# ifdef DEBUG
checkAllocation();
# endif
allocateSize(s); allocateSize(s);
nextFree_ = s; nextFree_ = s;
# ifdef DEBUG
checkAllocation();
# endif
} }
template<class T, Foam::label staticSize> template<class T, Foam::label staticSize>
inline void Foam::DynList<T, staticSize>::clear() inline void Foam::DynList<T, staticSize>::clear()
{ {
# ifdef DEBUG
checkAllocation();
# endif
nextFree_ = 0; nextFree_ = 0;
} }
...@@ -202,34 +289,62 @@ inline void Foam::DynList<T, staticSize>::clear() ...@@ -202,34 +289,62 @@ inline void Foam::DynList<T, staticSize>::clear()
template<class T, Foam::label staticSize> template<class T, Foam::label staticSize>
void Foam::DynList<T, staticSize>::shrink() void Foam::DynList<T, staticSize>::shrink()
{ {
# ifdef DEBUG
checkAllocation();
# endif
allocateSize(nextFree_); allocateSize(nextFree_);
# ifdef DEBUG
checkAllocation();
# endif
} }
template<class T, Foam::label staticSize> template<class T, Foam::label staticSize>
inline void Foam::DynList<T, staticSize>::append(const T& e) 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); allocateSize(newSize);
} }
UList<T>::operator[](nextFree_++) = e; # ifdef DEBUG
checkAllocation();
# endif
this->operator[](nextFree_++) = e;
} }
template<class T, Foam::label staticSize> template<class T, Foam::label staticSize>
inline void Foam::DynList<T, staticSize>::appendIfNotIn(const T& e) inline void Foam::DynList<T, staticSize>::appendIfNotIn(const T& e)
{ {
# ifdef DEBUG
checkAllocation();
# endif
if( !contains(e) ) if( !contains(e) )
append(e); append(e);
# ifdef DEBUG
checkAllocation();
# endif
} }
template<class T, Foam::label staticSize> template<class T, Foam::label staticSize>
inline bool Foam::DynList<T, staticSize>::contains(const T& e) const inline bool Foam::DynList<T, staticSize>::contains(const T& e) const
{ {
# ifdef DEBUG
checkAllocation();
# endif
for(label i=0;i<nextFree_;++i) for(label i=0;i<nextFree_;++i)
{ {
if( UList<T>::operator[](i) == e ) if( this->operator[](i) == e )
return true; return true;