Code Fragment: Hash1



template <typename Key, typename Element, typename HashCmp>
class LinearProbeHashTable {
protected: 						// local types
  // ... (insert HashEntry here)
public: 						// public types
  // ... (insert Position here)
private: 						// member data
  int 		    n, N;				// size and capacity
  EntryPtr	    A;					// array of entries
  HashCmp	    hash;				// the hash comparator
protected: 						// local utilities
  EntryPtr finder(const Key& key) const;		// search utility
  EntryPtr inserter(const Key& key, const Element& e);	// insert utility
public:
  LinearProbeHashTable(int capacity = 100)		// constructor
      : n(0), N(capacity), hash() { A = new HashEntry[N]; }
  int size() const 	    { return n; }		// size of dictionary
  bool isEmpty() const 	    { return (n == 0); }	// empty dictionary?

  Position find(const Key& key) const 			// find a key
    { return Position(finder(key)); }

  void insertItem(const Key& key, const Element& element)
      throw(HashTableFullException) {			// insert (key,element)
    EntryPtr e = inserter(key, element);		// attempt to insert
    if (e == NULL)					// failure
      throw HashTableFullException("Insertion into full hash table");
  }
  void removeElement(const Key& key) 			// remove using key
      throw(NonexistentElementException) {
    EntryPtr e = finder(key);				// look up key
    if (e == NULL)					// not found?
      throw NonexistentElementException("Key not found");
    e->status = FREE;					// mark entry as free
    n--;						// decrease size
  }
  // ... (some functions omitted)
};