diff --git a/meshLibrary/utilities/containers/DynList/DynList.C b/meshLibrary/utilities/containers/DynList/DynList.C index 2bf3a2a47721d4c00e563d9bae97a2954668165e..2e2350e14e88dce8e99024c3d54436f39f9cd7b5 100644 --- a/meshLibrary/utilities/containers/DynList/DynList.C +++ b/meshLibrary/utilities/containers/DynList/DynList.C @@ -29,9 +29,11 @@ License // Construct from Istream template -Foam::DynList::DynList(Istream& is) +Foam::DynList::DynList(Istream&) : - UList(), + dataPtr_(NULL), + nAllocated_(0), + staticData_(), nextFree_(0) { FatalErrorIn @@ -39,11 +41,6 @@ Foam::DynList::DynList(Istream& is) "template" "\nFoam::DynList::DynList(Istream& is)" ) << "Not implemented" << exit(FatalError); - - List helper(is); - - nextFree_ = helper.size(); - UList::swap(helper); } @@ -54,7 +51,7 @@ Foam::Ostream& Foam::operator<< const Foam::DynList& DL ) { - UList helper(const_cast(DL.begin()), DL.nextFree_); + UList 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& DL)" ) << "Not implemented" << exit(FatalError); - - is >> static_cast&>(DL); - DL.nextFree_ = DL.List::size(); + + UList helper(DL.dataPtr_, DL.nextFree_); + //is >> static_cast&>(DL); + is >> helper; + DL.nextFree_ = helper.size(); return is; } diff --git a/meshLibrary/utilities/containers/DynList/DynList.H b/meshLibrary/utilities/containers/DynList/DynList.H index 1b550836b83f1ef36956c1eb601cb9732fbad098..8cf6d46afa574089c6dd67bec53061b38c1b5614 100644 --- a/meshLibrary/utilities/containers/DynList/DynList.H +++ b/meshLibrary/utilities/containers/DynList/DynList.H @@ -73,10 +73,14 @@ Istream& operator>> template class DynList -: - public UList { // 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 inline void operator=(const ListType&); + //- Compare the list with the another one + inline bool operator==(const DynList&) const; + inline bool operator!=(const DynList&) const; + // IOstream operators diff --git a/meshLibrary/utilities/containers/DynList/DynListI.H b/meshLibrary/utilities/containers/DynList/DynListI.H index 5e1e2ae9f2fa35ed5e577b97a12d225ae897448d..3f9e25127aab1ae0938b7cbde125992fd13fa23d 100644 --- a/meshLibrary/utilities/containers/DynList/DynListI.H +++ b/meshLibrary/utilities/containers/DynList/DynListI.H @@ -23,48 +23,66 @@ License \*---------------------------------------------------------------------------*/ +template +inline T* Foam::DynList::data() +{ + return dataPtr_; +} + +template +inline const T* Foam::DynList::data() const +{ + return dataPtr_; +} template inline void Foam::DynList::allocateSize(const label s) { - if( s > UList::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;ioperator[](i); + for(label i=0;ioperator[](i); - T* data = UList::begin(); - if( data && (data != staticData_) ) - delete [] data; + if( nAllocated_ > staticSize ) + delete [] dataPtr_; - //UList::reset(newData, s); - this->UList::operator=(UList(newData, s)); - } - else if( (s > staticSize) && (s < UList::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;ioperator[](i); + for(label i=0;ioperator[](i); - T* data = UList::begin(); - delete [] data; + delete [] dataPtr_; - //UList::reset(newData, s); - this->UList::operator=(UList(newData, s)); + dataPtr_ = newData; + nAllocated_ = s; + } } - else if( (s <= staticSize) && (UList::size() > staticSize) ) + else { - for(label i=0;i::operator[](i); + if( nAllocated_ > staticSize ) + { + //- delete dynamically allocated data + for(label i=0;i::begin(); - if( data && (data != staticData_) ) - delete [] data; + delete [] dataPtr_; + } - //UList::reset(staticData_, staticSize); - this->UList::operator=(UList(staticData_, staticSize)); + dataPtr_ = staticData_; + nAllocated_ = staticSize; } } @@ -82,60 +100,105 @@ inline void Foam::DynList::checkIndex(const label i) const } } +template +inline void Foam::DynList::checkAllocation() const +{ + if( nextFree_ > nAllocated_ ) + FatalErrorIn + ( + "template " + "inline void Foam::DynList::" + "checkAllocation() const" + ) << "nextFree_ is out of scope 0 " << " and " << nAllocated_ + << abort(FatalError); +} + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // //- Construct null template inline Foam::DynList::DynList() : - UList(staticData_, staticSize), + dataPtr_(NULL), + nAllocated_(0), + staticData_(), nextFree_(0) -{} +{ + setSize(0); + + # ifdef DEBUG + checkAllocation(); + # endif +} template inline Foam::DynList::DynList(const label s) : - UList(staticData_, staticSize), + dataPtr_(NULL), + nAllocated_(0), + staticData_(), nextFree_(0) { setSize(s); + + # ifdef DEBUG + checkAllocation(); + # endif } template inline Foam::DynList::DynList(const label s, const T& val) : - UList(staticData_, staticSize), + dataPtr_(NULL), + nAllocated_(0), + staticData_(), nextFree_(0) { setSize(s); for(label i=0;ioperator[](i) = val; + + # ifdef DEBUG + checkAllocation(); + # endif } template inline Foam::DynList::DynList(const UList& ul) : - UList(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 template inline Foam::DynList::DynList(const ListType& l) : - UList(staticData_, staticSize), + dataPtr_(NULL), + nAllocated_(0), + staticData_(), nextFree_(0) { setSize(l.size()); for(label i=0;ioperator[](i) = l[i]; + + # ifdef DEBUG + checkAllocation(); + # endif } //- Copy construct @@ -145,19 +208,24 @@ inline Foam::DynList::DynList const DynList& dl ) : - UList(staticData_, staticSize), + dataPtr_(NULL), + nAllocated_(0), + staticData_(), nextFree_(0) { setSize(dl.size()); for(label i=0;ioperator[](i) = dl[i]; + + # ifdef DEBUG + checkAllocation(); + # endif } template inline Foam::DynList::~DynList() { allocateSize(0); - //UList::reset(NULL, 0); } @@ -166,12 +234,20 @@ inline Foam::DynList::~DynList() template inline Foam::label Foam::DynList::size() const { + # ifdef DEBUG + checkAllocation(); + # endif + return nextFree_; } template inline Foam::label Foam::DynList::byteSize() const { + # ifdef DEBUG + checkAllocation(); + # endif + if( !contiguous() ) { FatalErrorIn("DynList::byteSize()") @@ -181,20 +257,31 @@ inline Foam::label Foam::DynList::byteSize() const } return nextFree_*sizeof(T); - } template inline void Foam::DynList::setSize(const label s) { + # ifdef DEBUG + checkAllocation(); + # endif + allocateSize(s); nextFree_ = s; + + # ifdef DEBUG + checkAllocation(); + # endif } template inline void Foam::DynList::clear() { + # ifdef DEBUG + checkAllocation(); + # endif + nextFree_ = 0; } @@ -202,34 +289,62 @@ inline void Foam::DynList::clear() template void Foam::DynList::shrink() { + # ifdef DEBUG + checkAllocation(); + # endif + allocateSize(nextFree_); + + # ifdef DEBUG + checkAllocation(); + # endif } template inline void Foam::DynList::append(const T& e) { - if( nextFree_ >= UList::size() ) + # ifdef DEBUG + checkAllocation(); + # endif + + if( nextFree_ >= nAllocated_ ) { - const label newSize = 2*UList::size()+2; + const label newSize = 2*nAllocated_+2; allocateSize(newSize); } - UList::operator[](nextFree_++) = e; + # ifdef DEBUG + checkAllocation(); + # endif + + this->operator[](nextFree_++) = e; } template inline void Foam::DynList::appendIfNotIn(const T& e) { + # ifdef DEBUG + checkAllocation(); + # endif + if( !contains(e) ) append(e); + + # ifdef DEBUG + checkAllocation(); + # endif } template inline bool Foam::DynList::contains(const T& e) const { + # ifdef DEBUG + checkAllocation(); + # endif + for(label i=0;i::operator[](i) == e ) + if( this->operator[](i) == e ) return true; } @@ -242,9 +357,13 @@ inline Foam::label Foam::DynList::containsAtPosition const T& e ) const { + # ifdef DEBUG + checkAllocation(); + # endif + for(label i=0;i::operator[](i) == e ) + if( this->operator[](i) == e ) return i; } @@ -254,12 +373,20 @@ inline Foam::label Foam::DynList::containsAtPosition template inline const T& Foam::DynList::lastElement() const { + # ifdef DEBUG + checkAllocation(); + # endif + return this->operator[](nextFree_-1); } template inline T Foam::DynList::removeLastElement() { + # ifdef DEBUG + checkAllocation(); + # endif + if( nextFree_ == 0 ) { FatalErrorIn @@ -268,13 +395,18 @@ inline T Foam::DynList::removeLastElement() ) << "List is empty" << abort(FatalError); } - T el = UList::operator[](--nextFree_); + T el = this->operator[](nextFree_-1); + --nextFree_; return el; } template inline T Foam::DynList::removeElement(const label i) { + # ifdef DEBUG + checkAllocation(); + # endif + if( nextFree_ == 0 ) { FatalErrorIn @@ -287,12 +419,20 @@ inline T Foam::DynList::removeElement(const label i) this->operator[](i) = this->operator[](nextFree_-1); --nextFree_; + # ifdef DEBUG + checkAllocation(); + # endif + return el; } template inline T& Foam::DynList::newElmt(const label i) { + # ifdef DEBUG + checkAllocation(); + # endif + return this->operator()(i); } @@ -301,13 +441,21 @@ inline T& Foam::DynList::newElmt(const label i) template inline T& Foam::DynList::operator()(const label i) { + # ifdef DEBUG + checkAllocation(); + # endif + nextFree_ = Foam::max(nextFree_, i + 1); - if( nextFree_ >= UList::size() ) + if( nextFree_ >= nAllocated_ ) { allocateSize(2 * nextFree_+1); } + # ifdef DEBUG + checkAllocation(); + # endif + return this->operator[](i); } @@ -315,20 +463,22 @@ template inline const T& Foam::DynList::operator[](const label i) const { # ifdef FULLDEBUG + checkAllocation(); checkIndex(i); # endif - return UList::operator[](i); + return dataPtr_[i]; } template inline T& Foam::DynList::operator[](const label i) { # ifdef FULLDEBUG + checkAllocation(); checkIndex(i); # endif - return UList::operator[](i); + return dataPtr_[i]; } template @@ -374,7 +524,12 @@ inline const T& Foam::DynList::rcElement template inline void Foam::DynList::operator=(const T& t) { - UList::operator=(t); + # ifdef DEBUG + checkAllocation(); + # endif + + for(label i=0;i @@ -383,9 +538,17 @@ inline void Foam::DynList::operator= const DynList& dl ) { + # ifdef DEBUG + checkAllocation(); + # endif + allocateSize(dl.size()); nextFree_ = dl.size(); + # ifdef DEBUG + checkAllocation(); + # endif + for(label i=0;ioperator[](i) = dl[i]; } @@ -394,12 +557,45 @@ template template inline void Foam::DynList::operator=(const ListType& l) { + # ifdef DEBUG + checkAllocation(); + # endif + allocateSize(l.size()); nextFree_ = l.size(); + # ifdef DEBUG + checkAllocation(); + # endif + for(label i=0;ioperator[](i) = l[i]; } +template +inline bool Foam::DynList::operator== +( + const DynList& DL +) const +{ + if( nextFree_ != DL.nextFree_ ) + return false; + + forAll(DL, i) + if( this->operator[](i) != DL[i] ) + return false; + + return true; +} + +template +inline bool Foam::DynList::operator!= +( + const DynList& DL +) const +{ + return !operator==(DL); +} + // ************************************************************************* //