C
Christopher Benson-Manica
If you liked the functionality of std::map, but found that you
couldn't trust your implementation to handle nonstandard data
structures, how would you code a wrapper? I've produced the
following:
#include <map>
template < class keytype, class recordtype >
class pseudomap
{
private:
map< keytype, unsigned int > maptable;
CustomList<recordtype> recordlist; // custom array-like class
// with methods including
// those used below
public:
void clear() {maptable.clear();recordlist.Clear();}
recordtype& operator[] ( const keytype& key )
{
if( maptable.count(key) ) {
return( *recordlist[maptable[key]] );
}
recordlist.Add( new recordtype() );
maptable[key]=recordlist.Count-1;
return( *recordlist[maptable[key]] );
}
};
I get the feeling that there is a lot to criticize about the code
above (although it seems to work just fine) - so I'm listening
Needless to say not all map functionality is included - but it's good
enough for my current needs...
couldn't trust your implementation to handle nonstandard data
structures, how would you code a wrapper? I've produced the
following:
#include <map>
template < class keytype, class recordtype >
class pseudomap
{
private:
map< keytype, unsigned int > maptable;
CustomList<recordtype> recordlist; // custom array-like class
// with methods including
// those used below
public:
void clear() {maptable.clear();recordlist.Clear();}
recordtype& operator[] ( const keytype& key )
{
if( maptable.count(key) ) {
return( *recordlist[maptable[key]] );
}
recordlist.Add( new recordtype() );
maptable[key]=recordlist.Count-1;
return( *recordlist[maptable[key]] );
}
};
I get the feeling that there is a lot to criticize about the code
above (although it seems to work just fine) - so I'm listening
Needless to say not all map functionality is included - but it's good
enough for my current needs...