Source of Listing18-1.h


  1: //  Created by Frank M. Carrano and Timothy M. Henry.
  2: //  Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.

  4: // Listing 18-1

  6: /** An interface for the ADT dictionary. (Distinct search keys.)
  7:  @file  DictionaryInterface.h */

  9: #ifndef DICTIONARY_INTERFACE_
 10: #define DICTIONARY_INTERFACE_

 12: #include "NotFoundException.h"

 14: template<class KeyType, class ValueType>
 15: class DictionaryInterface 
 16: {
 17: public:   
 18:    /** Sees whether this dictionary is empty.
 19:     @return  True if the dictionary is empty;
 20:        otherwise returns false. */
 21:    virtual bool isEmpty() const = 0;
 22:    
 23:    /** Gets the number of entries in this dictionary.
 24:     @return  The number of entries in the dictionary. */
 25:    virtual int getNumberOfEntries() const = 0;
 26:    
 27:    /** Adds a new search key and associated value to this dictionary.
 28:     @pre  The new search key differs from all search keys presently
 29:        in the dictionary.
 30:     @post  If the addition is successful, the new key-value pair
 31:        proper position within the dictionary.
 32:     @param searchKey  The search key associated with the value to
 33:     @param newValue  The value to be added.
 34:     @return  True if the entry was successfully added, or false if not. */
 35:     virtual bool add(const KeyType& searchKey, const ValueType& newValue) = 0;
 36:     
 37:     /** Removes a key-value pair from this dictionary.
 38:     @post  If the entry whose search key equals searchKey existed in the
 39:        dictionary, the entry was removed.
 40:     @param searchKey  The search key of the entry to be removed.
 41:     @return  True if the entry was successfully removed, or false if not. */
 42:     virtual bool remove(const KeyType& searchKey) = 0;
 43:    
 44:     /** Removes all entries from this dictionary. */
 45:    virtual void clear() = 0;
 46:    
 47:    /** Retrieves the value in this dictionary whose search key is given
 48:     @post  If the retrieval is successful, the value is returned.
 49:     @param searchKey  The search key of the value to be retrieved.
 50:     @return  The value associated with the search key.
 51:     @throw  NotFoundException if the key-value pair does not exist. */
 52:     virtual ValueType getValue(const KeyType& searchKey) const
 53:                                throw (NotFoundException) = 0;
 54:     /** Sees whether this dictionary contains an entry with a given search key.
 55:     @post  The dictionary is unchanged.
 56:     @param searchKey  The given search key.
 57:     @return  True if an entry with the given search key exists in the dictionary. */
 58:    virtual bool contains(const KeyType& searchKey) const = 0;
 59:    
 60:    /** Traverses this dictionary and calls a given client function
 61:        for each entry.
 62:     @post  The given function's action occurs once for each entry in the
 63:        dictionary and possibly alters the entry.
 64:     @param visit  A client function. */
 65:    virtual void traverse(void visit(ValueType&)) const = 0;
 66:    
 67:    /** Destroys this dictionary and frees its assigned memory. */
 68:    virtual ~DictionaryInterface(){ }
 69: }; // end DictionaryInterface

 71: #endif