//------------------------------------------------------------------- // File and Version Information: // $Id: AstSTLMapIndex.cc,v 1.2 2006/06/30 11:24:22 steinke Exp $ // // Description: // Given a HepAList or std::vector, provides a fast // map between the objects in the vector and the // index within the vector. // // Author List: // Matthias Steinke // // Bertram Kopf (RUB) migrated to PandaRoot //------------------------------------------------------------------- //----------------------- // This Class's Header -- //----------------------- #include "AstSTLMapIndex.h" //--------------- // C++ Headers -- //--------------- #include #include #include "PndCollectionUtils.h" using panda::Collection::PtrLess; //------------------------------- // Collaborating Class Headers -- //------------------------------- #include "AstIndexedObject.h" //----------------------------------------------------------------------- // Local Macros, Typedefs, Structures, Unions and Forward Declarations -- //----------------------------------------------------------------------- // ---------------------------------------- // -- Public Function Member Definitions -- // ---------------------------------------- //---------------- // Constructors -- //---------------- template AstSTLMapIndex::AstSTLMapIndex(const std::vector &list, unsigned (*hFun)(const T&)){ _map = new std::map*, PtrLess >; _hashFun = hFun; int index; int listLength = list.size(); for (index = 0; index < listLength; index++){ T* theObject = list[index]; AstIndexedObject *indexedObject = new AstIndexedObject(index, theObject); _map->insert(typename std::map*, PtrLess>::value_type(theObject, indexedObject)); } } //-------------- // Destructor -- //-------------- template AstSTLMapIndex::~AstSTLMapIndex() { typename std::map*, PtrLess>::iterator killer = _map->begin(); while (killer != _map->end()) { delete killer->second; ++killer; } delete _map; } //------------- // Methods -- //------------- template bool AstSTLMapIndex::contains(const T* theT) const{ return (_map->find(theT) != _map->end()); } template bool AstSTLMapIndex::index(const T* theT, int &index) const{ typename std::map*, PtrLess>::iterator position; position = _map->find((T*)theT); if (position != _map->end()) { AstIndexedObject *foundIndex = position->second; index = foundIndex->getInt(); return true; } else return false; } template bool AstSTLMapIndex::append(T* theT) { if (_map->find(theT) != _map->end()) return false; AstIndexedObject *newIndex = new AstIndexedObject(_map->size(),theT); _map->insert(typename std::map*, PtrLess>::value_type(theT, newIndex)); return true; } template T* AstSTLMapIndex::remove(const T*theT){ typename std::map*, PtrLess>::iterator position; position = _map->find((T*)theT); if (position == _map->end()) return 0; AstIndexedObject *foundIndex = position->second; int index = foundIndex->getInt(); T* foundT = foundIndex->getT(); std::vector*> temp; typename std::map*, PtrLess>::iterator findMore = _map->begin(); AstIndexedObject *nextIndex; while (findMore != _map->end()){ nextIndex = findMore->second; if (nextIndex->getInt() > index) temp.push_back(nextIndex); ++findMore; } _map->erase(_map->find((T*)theT)); delete foundIndex; int i; for (i=0;igetT(); int findIndex = temp[i]->getInt(); delete _map->find(findThis)->second; // Note that at this point the object at temp[i] is // no longer functional! AstIndexedObject *newIndex = new AstIndexedObject(findIndex-1,findThis); (*_map)[findThis] = newIndex; } // Ouf! Done... return foundT; } template int AstSTLMapIndex::members() const{ return _map->size(); } template void AstSTLMapIndex::clear(){ typename std::map*, PtrLess>::iterator killer = _map->begin(); while (killer != _map->end()) { delete killer->second; ++killer; } _map->clear(); } template void AstSTLMapIndex::clearAndDestroy(){ clear(); } //------------- // Operators -- //------------- //------------- // Selectors -- //------------- // ----------------------------------------------- // -- Static Data & Function Member Definitions -- // ----------------------------------------------- template unsigned AstSTLMapIndex::hashFunction(const T& p) { return ((unsigned long) &p >> 5) & 0x03ff; }