Source of DictionaryInterface.h


  1: //  Created by Frank M. Carrano and Tim Henry.
  2: //  Copyright (c) 2013 __Pearson Education__. All rights reserved.

  4: /** An interface for the ADT dictionary. (Distinct search keys.)
  5:     Listing 18-1
  6:  @file  DictionaryInterface.h */

  8: #ifndef _DICTIONARY_INTERFACE
  9: #define _DICTIONARY_INTERFACE

 11: #include "NotFoundException.h"

 13: template<class ItemType, class KeyType>
 14: class DictionaryInterface 
 15: {
 16: public:   
 17:    /** Sees whether this dictionary is empty.
 18:     @return True if the dictionary is empty;
 19:        otherwise returns false. */
 20:    virtual bool isEmpty() const = 0;
 21:    
 22:    /** Gets the number of items in this dictionary.
 23:     @return The number of items in the dictionary. */
 24:    virtual int getNumberOfItems() const = 0;
 25:    
 26:    /** Inserts an item into this dictionary according to the item’s search key.
 27:     @pre  The search key of the new item differs from all search
 28:        keys presently in the dictionary.
 29:     @post  If the insertion is successful, newItem is in its
 30:        proper position within the dictionary.
 31:     @param searchKey  The search key associated with the item to be inserted.
 32:     @param newItem  The item to add to the dictionary.
 33:     @return  True if item was successfully added, or false if not. */
 34:    virtual bool add(const KeyType& searchKey, const ItemType& newItem) = 0;
 35:    
 36:    /** Removes an item with the given search key from this dictionary.
 37:     @post  If the item whose search key equals searchKey existed in the dictionary, 
 38:        the item was removed.
 39:     @param searchKey  The search key of the item to be removed.
 40:     @return  True if the item was successfully removed, or false if not. */
 41:    virtual bool remove(const KeyType& searchKey) = 0;
 42:    
 43:    /** Removes all entries from this dictionary. */
 44:    virtual void clear() = 0;
 45:    
 46:    /** Retrieves an item with a given search key from a dictionary.
 47:     @post  If the retrieval is successful, the item is returned.
 48:     @param searchKey  The search key of the item to be retrieved.
 49:     @return  The item associated with the search key.
 50:     @throw  NotFoundException if the item does not exist. */
 51:    virtual ItemType getItem(const KeyType& searchKey) const throw (NotFoundException) = 0;
 52:    
 53:    /** Sees whether this dictionary contains an item with a given
 54:        search key.
 55:     @post  The dictionary is unchanged.
 56:     @param searchKey  The search key of the item to be retrieved.
 57:     @return  True if an item 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 once for each item.
 61:     @post  The given function’s action occurs once for each item in the
 62:        dictionary and possibly alters the item.
 63:     @param visit A client function. */
 64:    virtual void traverse(void visit(ItemType&)) const = 0;
 65: }; // end DictionaryInterface

 67: #endif