Please send questions to st10@humboldt.edu .

/************************************************************/
/* ADAPTED FROM:                                            */
/* http://www.cs.colorado.edu/~main/chapter12/table2.h      */
/*                                                          */
/* Main and Savitch, "Data Structures and Other Objects     */
/*    using C++", 2nd edition, Addison-Wesley, Ch.12, p.587 */
/************************************************************/

//-----------------------------------------------------------
// File: hashtable.h
// Name: Michael Main, Walter Savitch
//       (adapted by Sharon M. Tuttle)
// last modified: 4-20-05
//
// Template Class: hashtable<RecordType> (a hash table of
//                 RecordType instances, where a RecordType
//                 has a member function get_key( ) that
//                 returns an int. The RecordType instances
//                 will be stored and retrieved based on that
//                 int key.)
//
//                 The template parameter RecordType may be
//                 any type with a default constructor, a copy
//                 constructor, and assignment operator, and
//                 a member function get_key( ) that returns an
//                 integer key.
//
// VALUE SEMANTICS for the hashtable<RecordType> template class:
//    Assignments and the copy constructor may be used 
//       with hashtable<RecordType> objects.
//
// DYNAMIC MEMORY USAGE by the hashtable<RecordType> template class:
//    If there is insufficient dynamic memory, then the
//       following functions throw bad_alloc:
//       constructor, the copy constructor, insert,
//       and the assignment operator. [I THINK]
//-----------------------------------------------------------

#ifndef HASHTABLE_H
#define HASHTABLE_H

#include <cstdlib>  // Provides NULL and  size_t
#include "node.h"   // includes the text's linked-list toolkit
                    //    functions
using namespace std;

template <typename RecordType>
class hashtable
{
    public:

        /******************************************************/
        /* TYPEDEFS and MEMBER CONSTANTS for the hashtable    */
        /*    class                                           */
        /******************************************************/

        // hashtable<RecordType>::DEFAULT_TABLE_SIZE is the default
        //    table size of a hashtable that is created by the 
	//    default constructor.
        //
        static const size_t DEFAULT_TABLE_SIZE = 811; 

        /****************************************************/
        /* CONSTRUCTORS and DESTRUCTOR                      */
        /****************************************************/

        // postcondition: creates an empty hashtable instance
        //    of table_size given_table_size.
        //
        hashtable( size_t given_table_size = DEFAULT_TABLE_SIZE );

        // copy constructor
        //
        hashtable(const hashtable<RecordType>& source);

        // destructor
        //
        ~hashtable();

        /*******************************************************/
        /* ACCESSORS and other constant member functions       */
	/*    (observers)                                      */
        /*******************************************************/

        // postconditions: if a record is in the hashtable with
        //    the specified key this_key, then found is set to
        //    true and result is set to a copy of the record
        //    with that key. Otherwise found is false and the
        //    result contains garbage.
        //
        void find(int this_key, bool& found, RecordType& result) const;

        // postcondition: returns true if there is a record
        //    in the hashtable with key of this_key. Othewise,
        //    returns false.
        //
        bool is_present(int this_key) const;

        // postcondition: returns the total number of records
        //    in the hashtable.
        //
        size_t get_size( ) const;

        // postcondition: prints the keys within non-empty
        //    buckets, 1 non-empty bucket per line
        //
        void print_hashtable( ) const;

        /****************************************************/
        /* MODIFIERS and other modifying member functions   */
        /****************************************************/

        // preconditions: entry.get_key( ) >= 0. Also, if 
        //    entry.get_key( ) is not already a key in the
        //    hashtable, then the hashtable has space for another
        //    record (haven't exceeded free store...)
        // postconditions: if the table already had a record
        //    with key equal to entry.get_key( ), then that record
        //    is replaced by entry. Otherwise, entry has been
        //    added as a new record of the hashtable.
        //
        void insert(const RecordType& entry);

        // postconditions: if a record was in the hashtable with
        //    the specified key this_key, then that record has 
        //    been removed; otherwise the hashtable is unchanged.
        //
        void remove (int this_key);
        
        /****************************************************/
        /* OVERLOADED OPERATORS                             */
        /****************************************************/
   
        // postcondition: the hashtable that activated this
        //    will be made to have the same records as source
        //
        void operator =(const hashtable<RecordType>& source);

    private:
        node<RecordType> **data;
        size_t table_size;    // number of buckets in this hash
                              //    table
        size_t total_records; // current number of records in the 
                              //    hashtable

        /****************************************************/
        /* PRIVATE HELPER MEMBER FUNCTIONS                  */
        /****************************************************/

        // postconditions: returns a number in the range
        //    [0, table_size) based on key this_key.
        //
        size_t hash(int this_key) const;
};

#include "hashtable.template" // Include the implementation 
#endif