#ifndef sortedtable_h #define sortedtable_h //________________________________________________________________________ // //Contents: Array sorting //________________________________________________________________________ #include "SortedList.h" /*!\brief A TSortedTable keeps track of ids and their corresponding values. Each id can only be present once. */ namespace NSet { template class TSortedTable { public: TSortedTable(); int size() const { return vals_.size(); } void set( IDT id, T val ); /* vals_; TSortedList ids_; }; template inline TSortedTable::TSortedTable() : ids_( false ) { } template inline void TSortedTable::set( IDT theid, T val ) { int newpos = ids_.indexOf( theid ); if ( newpos==-1 ) { ids_ += theid; newpos = ids_.indexOf( theid ); vals_.insert( newpos, val ); } vals_[newpos] = val; } template inline bool TSortedTable::get( IDT theid, T& v ) const { int pos = ids_.indexOf( theid ); if ( pos==-1 ) return false; v = vals_[pos]; return true; } template inline bool TSortedTable::remove(IDT theid) { int pos = ids_.indexOf( theid ); if ( pos==-1 ) return false; vals_.remove( pos ); ids_.remove( pos ); return true; } /*!\brief A TSortedPointers keeps track of ids and their corresponding pointer. Each id can only be present once. */ template class TSortedPointers { public: TSortedPointers( ); ~TSortedPointers(); int size() const { return vals.size(); } void set( int id, T* val ); /* vals; TSortedList ids; }; template inline TSortedPointers::TSortedPointers() : ids( false ) { } template inline TSortedPointers::~TSortedPointers() { } template inline void TSortedPointers::set( int id_, T* val ) { int newpos = ids.indexOf( id_ ); if ( newpos==-1 ) { ids += id_; newpos = ids.indexOf( id_ ); vals.insertAt( val, newpos ); } vals.replace( newpos, val ); } template inline const T* TSortedPointers::get( int id_ ) const { int pos = ids.indexOf( id_ ); if ( pos==-1 ) return 0; return vals[pos]; } template inline T* TSortedPointers::get( int id_ ) { int pos = ids.indexOf( id_ ); if ( pos==-1 ) return 0; return vals[pos]; } template inline bool TSortedPointers::remove(int id_) { int pos = ids.indexOf( id_ ); if ( pos==-1 ) return false; vals.remove( pos ); ids.remove( pos ); return true; } template inline void TSortedPointers::erase() { vals.erase(); ids.erase(); } }//namespace #endif