#ifndef DICTIONARY_DOT_H #define DICTIONARY_DOT_H // The Dictionary class is just a wrapper for the STL "map" class. // The interface here is small and understandable, hence the value. #include #include #include "util.h" class NoSuchKeyException : public Exception { public: string what() const { return "Dictionary: no such key"; } }; template class Dictionary { map d; public: void put( const K& key, const V& value ); // destructive put void remove( const K& key ); // pre: contains V at( const K& key ) const; // pre: contains bool contains( const K& key ) const; bool is_empty() const { return d.empty(); } vector keys() const; }; template void Dictionary::put( const K& key, const V& value ) { d[key] = value; } template void Dictionary::remove( const K& key ) { if( !contains(key) ) throw NoSuchKeyException(); d.erase(key); } template V Dictionary::at( const K& key ) const { if( !contains(key) ) throw NoSuchKeyException(); return d.find(key)->second; } template bool Dictionary::contains( const K& key ) const { return d.count(key) == 1; } template vector Dictionary::keys() const { vector temp; for( map::const_iterator i=d.begin(); i!=d.end(); ++i ) temp.push_back( i->first ); return temp; } #endif