First let me just say that I'm a student seeking help on an assignment, but I am not asking for anyone to just hand me the answer. I am asking for help on understanding how to solve this problem. I'm stumped, and I don't quite understand it. I've done a ton of reading, searching, asking, and racking my brain, but I need someone to help me. Any input what so ever would greatly be appreciated. Thanks!
My assignment instructions:
Implement a generic Map that supports the insert and lookup operations. The implementation will store a hash table of pairs (key, definition). You will lookup a definition by providing a key. The following code provides the Map specification (minus some details).
template <typename HashedObj, typename Object>
class Pair
{
HashedObj key;
Object def;
// Appropriate Constructors, etc.
};
template <typename HashedObj, typename Object>
class Dictionary
{
public:
Dictionary( void ) { }
void insert( const HashedObj & key, const Object & definition );
const Object & lookup( const HashedObj & key ) const;
bool isEmpty() const;;
void makeEmpty();
private:
HashTable<Pair<HashedObj,Object> > items;
};
I'm having a hard time understanding how to implement this. Here is what I have so far, and I'm not even sure if I'm on the right track or not.
My work thus far:
#include <iostream>
#include <string>
#include <vector>
#include <conio.h>
using namespace std;
template <typename HashedObj>
class HashTable
{
public:
explicit HashTable( int size = 101 )
{
makeEmpty( );
}
bool contains( const HashedObj & x ) const
{
return isActive( findPos( x ) );
}
void makeEmpty( )
{
currentSize = 0;
for( int i = 0; i < array.size( ); i++ )
{
array[ i ].info = EMPTY;
}
}
bool insert( const HashedObj & x )
{
// insert x as active
int currentPos = findPos( x );
if( isActive( currentPos ) )
{
return false;
}
array[ currentPos ] = HashEntry( x, ACTIVE );
// rehash
if(++currentSize > array.size( ) / 2 )
{
rehash( );
}
return true;
}
bool remove( const HashedObj & x )
{
int currentPos = findPos( x );
if( !isActive( currentPos ) )
{
return false;
}
array[ currentPos ].info = DELETED;
return true;
}
enum EntryType { ACTIVE, EMPTY, DELETED };
private:
struct HashEntry
{
HashedObj element;
EntryType info;
HashEntry( const HashedObj & e = HashedObj( ), EntryType i = EMPTY ): element( e ), info( i ) { }
};
vector<HashEntry> array;
int currentSize;
bool isActive( int currentPos ) const
{
return array[ currentPos ].info == ACTIVE;
}
int findPos( const HashedObj & x ) const
{
int offset = 1;
int currentPos = myhash( x );
while( array[ currentPos ].info != EMPTY && array[ currentPos ].element != x )
{
currentPos += offset; // Compute ith probe
offset += 2;
if( currentPos >= array.size( ) )
{
currentPos -= array.size( );
}
}
return currentPos;
}
void rehash( )
{
vector<list<HashedObj> > oldLists = theLists;
// create new double-sized, empty table
theLists.resize( nextPrime( 2 * theLists.size( ) ) );
for( int j = 0; j < theLists.size( ); j++ )
{
theLists[ j ].clear( );
}
// copy table over
currentSize = 0;
for( int i = 0; i < oldLists.size( ); j++ )
{
list<HashedObj>::iterator itr = oldLists[ i ].begin( );
while( itr != oldLists[ i ].end( ) )
{
insert( *itr++ );
}
}
}
int myhash( const HashedObj & x ) const
{
int hashVal = hash( x );
hashVal %= theLists.size( );
if( hashVal< 0 )
{
hashVal += theLists.size( );
}
return hashVal;
}
};
template <typename HashedObj, typename Object>
class Pair
{
HashedObj key;
Object def;
public:
Pair() {}
~Pair() {}
};
template <typename HashedObj, typename Object>
class Dictionary
{
public:
Dictionary( void ) { }
~Dictionary( ) { }
void insert( const HashedObj & key, const Object & definition )
{
// ????
}
const Object & lookup( const HashedObj & key ) const
{
// ??????
}
bool isEmpty() const
{
//
}
void makeEmpty()
{
//
}
private:
HashTable<Pair<HashedObj,Object> > items;
};
// our pause function that allows us to view the results before closing the app
void pause()
{
printf("\n----------------------------------------------");
printf("\nPress any key to exit...");
_getch(); // waits for user to press any key
}
int main (void)
{
Dictionary <std::string, std::string> dict;
dict.insert("ex1", "example");
dict.lookup("ex1");
pause();
return (0);
}
Any help or guidance on this would be greatly appreciated. Thanks!!!!!!