// DICT.H - Dictionary Class Template #include #include "range.h" class DictionaryFull { public: friend ostream & operator <<(ostream & os, const DictionaryFull & F); }; ostream & operator <<(ostream & os, const DictionaryFull & F) { os << "Dictionary is full."; return os; } template class DuplicateKey { public: DuplicateKey( TKey theKey ) { m_key = theKey; } friend ostream & operator <<(ostream & os, const DuplicateKey & F) { os << "Attempt to add duplicate key."; return os; }; private: TKey m_key; }; /*// If I define the function here, I get a warning. That is why // I have defined this function within the class. template ostream & operator <<(ostream & os, const DuplicateKey & F) { os << "Attempt to add duplicate key."; return os; }; */ template class Dictionary { public: Dictionary( int initialSize ); bool Add( const TKey & tk, const TVal & tv ); // Add a new key and value to the dictionary. // Return true if successful, false otherwise. int Find( const TKey & tk ); // If a key matching is found, return its index // position; if not found, return -1. TVal At(int index) const; // Retrieve a copy of the value at position index vector FindVal( const TVal & searchFor ); // Return a vector containing all keys that // correspond to a particular value. If no // matches are found, return an empty vector. int size() const // Return the number of entries { return m_vKeys.size(); } friend ostream & operator <<( ostream & os, const Dictionary & D ) { for(unsigned i = 0; i < D.size(); i++) cout << D.m_vKeys[i] << " -> " << D.m_vValues[i] << '\n'; return os; } private: vector m_vKeys; // vector of dictionary m_vKeys vector m_vValues; // vector of dictionary m_vValues }; /* // If I define the function here, I get a warning. That is why // I have defined this function within the class. template ostream & operator <<( ostream & os, const Dictionary & D ) { for(unsigned i = 0; i < D.size(); i++) cout << D.m_vKeys[i] << " -> " << D.m_vValues[i] << '\n'; return os; } */ template Dictionary::Dictionary( int initialSize ) { m_vKeys.reserve( initialSize ); m_vValues.reserve( initialSize ); } template bool Dictionary::Add( const TKey & tk, const TVal & tv ) // Add a new key and associated value to the // dictionary. Return true if successful. If an attempt // is made to add a duplicate key, throw a DuplicateKey // exception. { if( Find(tk) == -1 ) // not already in dictionary? { m_vKeys.push_back(tk); // add new key m_vValues.push_back(tv); // add associated value return true; // success! } // must have been a duplicate key throw DuplicateKey(tk); return false; } template int Dictionary::Find( const TKey & tk ) { for(int i = 0; i < m_vKeys.size(); i++) { if( tk == m_vKeys[i] ) return i; } return -1; // not found } template TVal Dictionary::At(int index) const { return m_vValues[index]; } /* template ostream & operator <<( ostream & os, const Dictionary & D ) { for(unsigned i = 0; i < D.size(); i++) cout << D.m_vKeys[i] << " -> " << D.m_vValues[i] << '\n'; return os; } */