#pragma once #include #include using namespace std; /* * * hashtable.h * * hash() * perl hashing function * * class hashtable * A =SIMULATION= of a hash table using templates * (c) 2006 Charles A. Morris * */ unsigned long hash( string key ); template struct record { unsigned long hashsum; string key; T value; }; template class hashtable { public: hashtable(); ~hashtable(); bool empty() const; bool insert( const string key, const T record ); bool retreive( const string key, T & target ) const; void display() const; private: vector keys; vector < record > data; //cheating // record * data; //the memory where the records are stored long recordSize; //the size of the records int numRecords; //the amount of records currently stored }; template hashtable::hashtable() { recordSize = sizeof(record); // data = new record[]; numRecords = 0; } template hashtable::~hashtable() { // delete [] data; } template bool hashtable::empty() const { // return data.empty(); return false; } //returns true if collision, false if OK template bool hashtable::insert( const string key, const T value ) { const unsigned long sum = hash(key); //simulate collisions for( int i = 0; i < data.size(); i++ ) { if( data.at(i).hashsum == sum ) { data.at(i).value = value; return true; } } //simulate collisions // for( int i = 0; i < numRecords; i++ ) // { // record * temprecord; // temprecord = *(data+(i*recordSize)); // // // if( temprecord->hashsum == sum ) // { // temprecord->value = value; // return true; // } // } /* instead of using a vector here, you would seek to the location in memory calculated by long location = sum + sizeof(T); then memcpy() it directly there. */ // memcpy( data+sum, value, recordSize ); //the vector cheating way record mystruct; mystruct.key = key; mystruct.hashsum = sum; mystruct.value = value; data.push_back(mystruct); return false; } template bool hashtable::retreive( const string key, T & target ) const { unsigned long sum = hash(key); return false; } template void hashtable::display() const { cout<<" /-------------------------------\\"<