//------------------------------------------------------------- // File and Version Information: // $Id: AstSorted2Vector.cc,v 1.1.1.1 2005/03/29 16:49:05 steinke Exp $ // // Description: // Implementation of AstSorted2Vector. May or may not be included // by the header file depending on BABAR_COMP_INST // // Author List: // Roland Martin 5th May 1998 // // Bertram Kopf (RUB) migrated to PandaRoot //------------------------------------------------------------- //----------------------- // This Class's Header -- //----------------------- #include "AstSorted2Vector.h" #include "TError.h" //--------------- // C++ Headers -- //--------------- #include #include #include "PndCollectionUtils.h" using panda::Collection::DeleteObject; //------------------------------- // Collaborating Class Headers -- //------------------------------- #include "AstSortableAssoc.h" //----------------------------------------------------------------------- // Local Macros, Typedefs, Structures, Unions and Forward Declarations -- //----------------------------------------------------------------------- // ---------------------------------------- // -- Public Function Member Definitions -- // ---------------------------------------- //---------------- // Constructors -- //---------------- template AstSorted2Vector::AstSorted2Vector(bool (*lessThan)(const Q &, const Q &)) : _vector(new std::vector* >()), _lessThanFunction(lessThan) { } template AstSorted2Vector::AstSorted2Vector(bool (*lessThan)(const Q &, const Q &), size_t initCapacity) : _vector(new std::vector* >(initCapacity)), _lessThanFunction(lessThan) { } //-------------- // Destructor -- //-------------- template AstSorted2Vector::~AstSorted2Vector() { std::for_each(_vector->begin(), _vector->end(), DeleteObject()); _vector->clear(); delete _vector; } //------------- // Methods -- //------------- //------------- // Operators -- //------------- //------------- // Selectors -- //------------- template bool AstSorted2Vector::contains(const T* theT, const Q* theQ) const{ AstSortableAssoc *theAssoc = new AstSortableAssoc((T*)theT, (Q*)theQ, _lessThanFunction); typename std::vector* >::iterator position = _vector->begin(); while (position != _vector->end()) { if (*theAssoc == **position) { delete theAssoc; return true; } ++position; } delete theAssoc; return false; } template const T* AstSorted2Vector::find( const Q& toFindWith ) const { size_t length = this->entries(), i=length/2; if ( length == 0 ) return 0; int result = -1; bool stillGoing = true; size_t lowerEnd = 0, upperEnd = length-1, counter = 0; while ( stillGoing && counter < 1000 ) { size_t oldI = i; const Q& toTry = this->quality(i); if ( toFindWith == toTry ) { result = i; stillGoing = false; } else { if ( (*_lessThanFunction)( toFindWith, toTry ) ) { upperEnd = i; } else { lowerEnd = i; } i = (size_t)((upperEnd - lowerEnd) / 2. + lowerEnd); if ( i == oldI ) { if ( i == upperEnd ) i--; else i++; } if ( lowerEnd == upperEnd ) stillGoing = false; } counter++; } if ( counter == 1000 ) Fatal("AstSorted2Vector::find( const Q& toFindWith ) const","Didn't find entry after 1000 iterations."); if ( result != -1 ) return &(*this)(result); return 0; } template int AstSorted2Vector::entries() const{ return _vector->size(); } template const T& AstSorted2Vector::operator[](size_t index)const{ return (*_vector)[index]->key(); } template const T& AstSorted2Vector::operator()(size_t index)const{ return (*_vector)[index]->key(); } template const Q& AstSorted2Vector::quality(size_t index)const{ return (*_vector)[index]->quality(); } template const Q& AstSorted2Vector::qualityFast(size_t index)const{ return (*_vector)[index]->quality(); } //------------- // Modifiers -- //------------- template void AstSorted2Vector::insert(T* theT, Q* theQ){ AstSortableAssoc *theAssoc = new AstSortableAssoc(theT, theQ, _lessThanFunction); // Actually this always returns true. Oh well. _vector->push_back(theAssoc); std::sort(_vector->begin(), _vector->end(), panda::Collection::PtrLess()); } template bool AstSorted2Vector::remove(T* theT, Q* theQ){ AstSortableAssoc *theAssoc = new AstSortableAssoc(theT, theQ, _lessThanFunction); typename std::vector* >::iterator position = _vector->begin(); while (position != _vector->end()) { if (*theAssoc == **position) { delete *position; _vector->erase(position); delete *theAssoc; return true; } ++position; } delete theAssoc; return false; } template void AstSorted2Vector::clear(){ std::for_each(_vector->begin(), _vector->end(), DeleteObject()); _vector->clear(); } template void AstSorted2Vector::clearAndDestroy(){ for (int i=0; i< (int) _vector->size();i++){ (*_vector)[i]->deleteMembers(); } std::for_each(_vector->begin(), _vector->end(), DeleteObject()); _vector->clear(); } // ----------------------------------------------- // -- Static Data & Function Member Definitions -- // ----------------------------------------------- // ------------------------------------------- // -- Protected Function Member Definitions -- // ------------------------------------------- // ----------------------------------------- // -- Private Function Member Definitions -- // ----------------------------------------- // ----------------------------------- // -- Internal Function Definitions -- // -----------------------------------