When I declare a new Hashtable such as:
String a = null;
HashTable<string> = new HashTable(a, 100);
It tells me HashTable is not a type.
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <iostream>
#include <vector>
#include <list>
using namespace std;
template <typename HashedObj>
class HashTable
{
public:
explicit HashTable( const HashedObj & notFound, int size = 101 );
HashTable( const HashTable & rhs ): item( rhs.item ), theLists( rhs.theLists ) { }
void makeEmpty();
void insert(const HashedObj &x);
void remove(const HashedObj &x);
bool contains(const HashedObj &x)const;
const HashedObj & find( const HashedObj & x ) const;
private:
const HashedObj item;
int size;
vector<list<HashedObj> > theLists;
};
template <typename HashedObj>
int hash(const HashedObj &x, int tablesize);
#endif
//
#include "hashTable.h"
#include <iostream>
#include <list>
#include <string>
#include <math.h>
#include <iterator>
bool checkforlargeprime (int num);
bool checkprime (int nn) {
if (nn > 100) {
return checkforlargeprime(nn);
} else {
int k=2;
while (k < nn) {
int sd = nn%k;
if ( sd == 0) {
return false;
}
k++;
}
}
return true;
}
int getNextPrimeNumber (int num) {
int nam = num+1;
bool das = true;
while ( das == true ) {
if (checkprime(nam))
das = false;
else
nam = nam+1;
}
return nam;
}
bool checkforlargeprime (int num) {
if (num > 100) {
int sss = ((int)(sqrt((double)num)))+1;
int pn = 2;
while (pn < sss) {
if (num%pn == 0) {
return false;
}
pn = getNextPrimeNumber(pn);
}
return true;
} else {
return false;
}
}
template <class HashedObj>
HashTable<HashedObj>::HashTable( const HashedObj & notFound, int size )
: item( notFound ), theLists( getNextPrimeNumber( size ) )
{
}
template <class HashedObj>
void HashTable<HashedObj>::insert( const HashedObj & x )
{
list<HashedObj> &whichList = theLists[hash(x, theLists.size())];
typename std::list<HashedObj>::iterator itr = whichList.find(x);
if( itr.isPastEnd( ) )
whichList.insert( x, whichList.zeroth( ) );
}
template <class HashedObj>
void HashTable<HashedObj>::remove(const HashedObj &x )
{
theLists[hash( x, theLists.size( ))].remove(x);
}
template <class HashedObj>
bool HashTable<HashedObj>::contains(const HashedObj &x )const
{
const list<HashedObj> & whichList = theLists[hash(x)];
return find(whichList.begin(),whichList.end(),x)!=whichList.end();
}
template <class HashedObj>
const HashedObj &HashTable<HashedObj>::find( const HashedObj &x ) const
{
typename std::list<HashedObj>::iterator itr;
itr = theLists[ hash( x, theLists.size( ) ) ].find( x );
if( itr.isPastEnd( ) )
return item;
else
return itr.retrieve( );
}
template <class HashedObj>
void HashTable<HashedObj>::makeEmpty( )
{
for( int i = 0; i < theLists.size( ); i++ )
theLists[ i ].makeEmpty( );
}
int hash( const string &key, int tableSize)
{
int hashVal = 0;
for(int i = 0; i<key.length(); i++)
hashVal = 37*hashVal+key[i];
hashVal %= tableSize;
if(hashVal<0)
hashVal += tableSize;
return hashVal;
}