You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
216 lines
3.4 KiB
C++
216 lines
3.4 KiB
C++
|
|
#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 IDT, class T>
|
|
class TSortedTable
|
|
{
|
|
public:
|
|
TSortedTable();
|
|
|
|
int size() const { return vals_.size(); }
|
|
void set( IDT id, T val );
|
|
/*<! If id is set twice, it the old value will
|
|
be replaced by the new one */
|
|
bool get( IDT id, T& val ) const;
|
|
/*!< If id is not found, val is unchanged and
|
|
false is returned. If id is found, val is set
|
|
and true is returned.*/
|
|
|
|
const IDT& id(int pos) const { return ids_[pos]; }
|
|
|
|
bool remove(IDT id);
|
|
void erase() { vals_.erase(); ids_.erase(); }
|
|
|
|
private:
|
|
TTypeSet<T> vals_;
|
|
TSortedList<IDT> ids_;
|
|
};
|
|
|
|
|
|
template <class IDT, class T> inline
|
|
TSortedTable<IDT,T>::TSortedTable()
|
|
: ids_( false )
|
|
{
|
|
|
|
}
|
|
|
|
template <class IDT, class T> inline
|
|
void TSortedTable<IDT,T>::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 <class IDT, class T> inline
|
|
bool TSortedTable<IDT,T>::get( IDT theid, T& v ) const
|
|
{
|
|
int pos = ids_.indexOf( theid );
|
|
|
|
if ( pos==-1 )
|
|
return false;
|
|
|
|
v = vals_[pos];
|
|
return true;
|
|
}
|
|
|
|
|
|
template <class IDT, class T> inline
|
|
bool TSortedTable<IDT,T>::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 T>
|
|
class TSortedPointers
|
|
{
|
|
public:
|
|
TSortedPointers( );
|
|
~TSortedPointers();
|
|
|
|
int size() const { return vals.size(); }
|
|
void set( int id, T* val );
|
|
/*<! If id is set twice, it the old value will
|
|
be replaced by the new one*/
|
|
|
|
const T* get( int id ) const;
|
|
T* get( int id );
|
|
|
|
const T* getByPos( int pos ) const { return vals[pos]; }
|
|
T* getByPos( int pos ) { return vals[pos]; }
|
|
|
|
int id( int pos ) const { return ids[pos]; }
|
|
|
|
bool remove( int id );
|
|
bool removePos( int pos );
|
|
void erase();
|
|
|
|
private:
|
|
TObjectSet<T> vals;
|
|
TSortedList<int> ids;
|
|
};
|
|
|
|
|
|
template <class T> inline
|
|
TSortedPointers<T>::TSortedPointers()
|
|
: ids( false )
|
|
{
|
|
|
|
}
|
|
|
|
|
|
template <class T> inline
|
|
TSortedPointers<T>::~TSortedPointers()
|
|
{ }
|
|
|
|
|
|
template <class T> inline
|
|
void TSortedPointers<T>::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 <class T> inline
|
|
const T* TSortedPointers<T>::get( int id_ ) const
|
|
{
|
|
int pos = ids.indexOf( id_ );
|
|
|
|
if ( pos==-1 )
|
|
return 0;
|
|
|
|
return vals[pos];
|
|
}
|
|
|
|
|
|
template <class T> inline
|
|
T* TSortedPointers<T>::get( int id_ )
|
|
{
|
|
int pos = ids.indexOf( id_ );
|
|
|
|
if ( pos==-1 )
|
|
return 0;
|
|
|
|
return vals[pos];
|
|
}
|
|
|
|
|
|
template <class T> inline
|
|
bool TSortedPointers<T>::remove(int id_)
|
|
{
|
|
int pos = ids.indexOf( id_ );
|
|
|
|
if ( pos==-1 ) return false;
|
|
|
|
vals.remove( pos );
|
|
ids.remove( pos );
|
|
|
|
return true;
|
|
}
|
|
|
|
template <class T> inline
|
|
void TSortedPointers<T>::erase()
|
|
{
|
|
vals.erase();
|
|
ids.erase();
|
|
}
|
|
|
|
|
|
|
|
}//namespace
|
|
|
|
|
|
#endif
|