Skip to content

Instantly share code, notes, and snippets.

/HashTable.cpp Secret

Created March 8, 2015 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/611f6619eb7e03a393a4 to your computer and use it in GitHub Desktop.
Save anonymous/611f6619eb7e03a393a4 to your computer and use it in GitHub Desktop.
#include "HashTable.h"
HashTable::HashTable( int tableLength )
{
if (tableLength <= 0) tableLength = TABLE_SIZE;
array = new LinkedList[ tableLength ];
length = tableLength;
}
int HashTable::hash( string const itemKey )
{
unsigned int sum = 0;
for (int i = 0; i < itemKey.length(); i++) {
sum = (sum * 32) + itemKey[i];
}
return sum = sum % TABLE_SIZE;
}
void HashTable::insertItem( Item * newItem )
{
int index = hash( newItem -> key);
array[ index ].insertItem( newItem );
}
bool HashTable::removeItem( string const itemKey )
{
//string local_var = itemKey;
int index = hash( itemKey );
return array[ index ].removeItem( itemKey );
}
Item * HashTable::getItemByKey( string const itemKey )
{
int index = hash( itemKey );
return array[index].getItem( itemKey );
}
void HashTable::printTable()
{
for (int i = 0; i < length; i++)
{
cout << "Slot[" << i << "]:";
array[i].printList();
}
}
int HashTable::getLength()
{
return length;
}
int HashTable::getNumberOfItems()
{
int itemCount = 0;
for (int i = 0; i < length; i++)
itemCount += array[i].getLength();
return itemCount;
}
HashTable::~HashTable()
{
if (array) {
delete [] array;
}
}
std::ostream& operator<< ( std::ostream& out,
HashTable &passed_in_hashTahbe_object )
{
passed_in_hashTahbe_object.printTable();
return out;
}
#ifndef HashTable_h
#define HashTable_h
#include "LinkedList.h"
#include <string.h>
#include <stdio.h>
const int TABLE_SIZE = 3;
class HashTable
{
private:
LinkedList * array;
int length;
int hash( string itemKey );
public:
HashTable( int tableLength = 3 );
void insertItem( Item * newItem );
bool removeItem( string const itemKey );
Item * getItemByKey( string const itemKey );
void printTable();
int getLength();
int getNumberOfItems();
~HashTable();
friend std::ostream& operator<< ( std::ostream& out,
HashTable &passed_in_hashTahbe_object );
};
#endif
#include "LinkedList.h"
LinkedList::LinkedList()
{
head = new Item;
head -> next = NULL;
length = 0;
}
void LinkedList::insertItem( Item * pi_newItem )
{
if (!head -> next)
{
head -> next = pi_newItem;
length++;
return;
}
Item * p = head;
Item * q = head;
while (q)
{
p = q;
q = p -> next;
}
p -> next = pi_newItem;
pi_newItem -> next = NULL;
length++;
}
bool LinkedList::removeItem( string const itemKey )
{
if (!head -> next) return false;
Item * p = head;
Item * q = head;
while (q)
{
if (q->key == itemKey)
{
p -> next = q -> next;
delete q;
length--;
return true;
}
p = q;
q = p -> next;
}
return false;
}
string const LinkedList::getItemKey(Item* ri_Item){
Player *local = getCurrPlayer(ri_Item);
return local->getuserName();
}
Item * LinkedList::getItem( string pi_itemkey ) const
{
Item * p = head;
Item * q = head;
while (q)
{
p = q;
if ((p != head) && (p -> key == pi_itemkey))
return p;
q = p -> next;
}
return NULL;
}
void LinkedList::printList()
{
if (length == 0)
{
cout << "\n EMPTY \n";
return;
}
Item * p = head;
Item * q = head;
cout << "\n " ;
while (q)
{
p = q;
if (p != head)
{
cout << p -> key << " ["<<getPlayerLevel(*p) <<"]";
if (p -> next) cout << "\n ";
else cout << " ";
}
q = p -> next;
}
cout << "\n";
}
int LinkedList::getPlayerLevel(Item& ri_Item){
return ri_Item.a_player.getLevel();
}
int LinkedList::getLength()
{
return length;
}
Player* LinkedList::getCurrPlayer(Item* ri_Item){
Player *temp;
temp = &ri_Item->a_player;
return temp;
}
LinkedList::~LinkedList()
{
Item * p = head;
Item * q = head;
while (q)
{
p = q;
q = p -> next;
if (q) delete p;
}
}
#ifndef LinkedList_h
#define LinkedList_h
#include <iostream>
#include <string>
#include "Player.h"
using namespace std;
struct Item
{
string key;
Player a_player;
Item * next;
};
class LinkedList
{
private:
Item * head;
int length;
public:
LinkedList();
void insertItem( Item * newItem );
bool removeItem( string const itemKey ) ;
Item * getItem( string itemKey ) const;
Player * getCurrPlayer(Item* ri_Item);
string const getItemKey(Item* ri_Item);
int getPlayerLevel(Item& ri_Item);
void printList();
int getLength();
~LinkedList();
};
#endif
#include <iostream>
#include "Player.h"
#include "HashTable.h"
#include "PlayerDB.h"
#ifdef _WIN32
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
using namespace std;
void Test()
{
Player* outPlayer = NULL;
// Test 1: Make sure that an empty player database doesn't fail
PlayerDB pdb(cout);
pdb.PrintDiagnostics();
// Test 2: Try to fetch a non-user
outPlayer = pdb.FetchPlayer("sleak");
if (outPlayer != NULL) {
cout << "Fetched a player when we shouldn't have..." << endl;
}
// Test 3: Add some players, and a duplicate
pdb.AddPlayer(Player("Brutus",MALE));
pdb.AddPlayer(Player("Brutus",MALE));
pdb.AddPlayer(Player("Socrates",MALE));
pdb.AddPlayer(Player("Sappho",FEMALE));
pdb.PrintDiagnostics();
// Test 4: Removing players
pdb.RemovePlayer("Socrates");
pdb.PrintDiagnostics();
// Test 5: Fetch a player and make sure that it is the correct one
outPlayer = pdb.FetchPlayer("Sappho");
if (outPlayer != NULL) {
outPlayer->LevelUp();
}
pdb.PrintDiagnostics(); // Note that the level for Sappho should be "1" now
// Test 6: Try some degenerate cases
pdb.AddPlayer(Player("a long player name to make sure our hash function will work with long names.",FEMALE));
pdb.PrintDiagnostics();
// Test 7: Add a bunch more players
pdb.AddPlayer(Player("Hypatia",FEMALE));
pdb.AddPlayer(Player("Corinna",FEMALE));
pdb.AddPlayer(Player("Euripides",MALE));
pdb.AddPlayer(Player("Sophocles",MALE));
pdb.AddPlayer(Player("Aristophanes",MALE));
pdb.PrintDiagnostics();
// Test 8: Removing stuff in various positions in the linked list
pdb.RemovePlayer("Hypatia");
pdb.RemovePlayer("Brutus");
pdb.PrintDiagnostics();
}
int main()
{
Test();
#ifdef _WIN32
if (_CrtDumpMemoryLeaks()) {
cout << "Memory leaks!" << endl;
}
#endif
return(0);
}
#include "Player.h"
#include <iostream>
#include <iomanip>
using namespace std;
#pragma warning(disable:4996) // allow call to strcpy
/*************************
private:
char* userName;
int playerLevel;
gender playerGender;
*************************/
Player::Player() : userName ( NULL ), playerGender ( UNKNOWN ),
playerLevel ( 0 )
{
}
Player::Player ( char * pi_userName, gender ci_gender ) :
userName ( NULL ),
playerGender ( UNKNOWN ),
playerLevel ( 0 )
{
setuserName ( pi_userName );
setGender ( ci_gender );
}
Player::Player ( char * pi_userName, gender ci_gender, int ci_level ) :
userName ( NULL ),
playerGender ( UNKNOWN ),
playerLevel ( ci_level )
{
setuserName ( pi_userName );
setGender ( ci_gender );
}
Player::Player ( const Player& ri_Player ) : userName ( NULL ),
playerGender ( UNKNOWN )
{
setuserName ( ri_Player.userName );
setGender ( ri_Player.playerGender );
setLevel ( ri_Player.playerLevel );
}
const Player& Player::operator= ( const Player& ri_Player )
{
//if it is a self copy, don't do anything
if ( this == &ri_Player )
return *this;
//make current object *this a copy of the passed in student
else
{
setuserName ( ri_Player.userName );
setGender ( ri_Player.playerGender );
setLevel ( ri_Player.playerLevel );
return *this;
}
}
Player::~Player()
{
// if(userName)
// delete[] userName;
}
void Player::LevelUp()
{
this->playerLevel++;
}
void Player::setLevel ( int ci_level )
{
playerLevel = ci_level;
}
void Player::incrementLevel()
{
playerLevel++;
}
int Player::getLevel ( void ) const
{
return playerLevel;
}
void Player::setGender ( gender ci_playerGender )
{
playerGender = ci_playerGender;
}
gender Player::getGender ( void ) const
{
return playerGender;
}
void Player::setuserName ( char * pi_userName )
{
if ( this->userName )
delete [] this->userName;
//set new name
this->userName = new char[strlen ( pi_userName ) + 1];
strcpy ( this->userName, pi_userName );
}
char * Player::getuserName() const
{
return userName;
}
//
// Player.h
// lab3e
//
// Created by admini on 3/4/15.
// Copyright (c) 2015 admini. All rights reserved.
//
#ifndef __lab3e__Player__
#define __lab3e__Player__
#include <iostream>
using namespace std;
enum gender {MALE, FEMALE, UNKNOWN};
class Player
{
public:
Player();
Player ( char * pi_userName, gender pi_playerGender );
Player ( char * pi_userName, gender pi_playerGender, int pi_level );
Player ( const Player&
pi_Player ); //copy constructor: make current object a copy of "student"
~Player(); //destructor: release the dynamically allocated memory
char * getuserName ( void ) const;
int getLevel ( void ) const;
gender getGender ( void ) const;
void setuserName ( char * pi_userName );
void setGender ( gender ci_playerGender );
void setLevel ( int ci_level );
void incrementLevel();
void LevelUp();
const Player& operator= ( const Player&
pi_Player ); //overloading assignment operator
friend ostream& operator<< ( ostream& out, const Player& ri_Player );
private:
char *userName;
int playerLevel;
gender playerGender;
};
bool operator< ( const Player& d1, const Player& d2 );
bool operator== ( const Player& d1, const Player& d2 );
#endif /* defined(__lab3e__Player__) */
//
// PlayerDB.cpp
// lab3e
//
// Created by admini on 3/4/15.
// Copyright (c) 2015 admini. All rights reserved.
//
#include "PlayerDB.h"
PlayerDB::PlayerDB() : playerDB_Out ( &cout )
{
}
PlayerDB::PlayerDB ( ostream& out ) : playerDB_Out ( &out )
{
}
bool PlayerDB::AddPlayer ( const Player &ri_player )
{
bool player_added = false;
string local_userName = ri_player.getuserName(); // so we only do one lookup
*playerDB_Out << "Attempting to add player \"" << local_userName <<
"\" to the database -- ";
if ( !player_database.getItemByKey ( local_userName.c_str() ) )
{
Item * newItem = new Item {ri_player.getuserName(), ri_player , NULL};
player_database.insertItem ( newItem );
*playerDB_Out << "Success!." << endl;
player_added = true;
}
else
{
*playerDB_Out << "Failed." << endl;
}
return player_added;
}
bool PlayerDB::RemovePlayer ( string const name )
{
bool rval = false;
*playerDB_Out << "Removeing player \"" << name << "\" -- ";
if ( player_database.removeItem ( name ) )
{
*playerDB_Out << "Success!" << endl;
rval = true;
}
else
{
*playerDB_Out << "Failed." << endl;
}
return rval;
}
Player *PlayerDB::FetchPlayer ( string const userName )
{
Item *local_Item;
Player *local_Player;
local_Item = player_database.getItemByKey ( userName );
*playerDB_Out << "Fetching player \"" << userName << "\" -- ";
if ( local_Item != NULL )
{
*playerDB_Out << "Success!" << endl;
local_Player = &local_Item->a_player;
}
else
{
*playerDB_Out << "Failed." << endl;
local_Player = nullptr;
}
return local_Player;
}
void PlayerDB::PrintDiagnostics()
{
unsigned int hashTableCap = player_database.getLength();
unsigned int hashTableEntries = player_database.getNumberOfItems();
*playerDB_Out << "====================" << endl;
*playerDB_Out << "Hash Table Diagnostics" << endl << endl;
*playerDB_Out << "Table Size: " << hashTableCap << endl;
*playerDB_Out << "Number of Entries: " << hashTableEntries << endl;
*playerDB_Out << player_database;
*playerDB_Out << "====================" << endl;
}
//
// PlayerDB.h
// lab3e
//
// Created by admini on 3/4/15.
// Copyright (c) 2015 admini. All rights reserved.
//
#ifndef __lab3e__PlayerDB__
#define __lab3e__PlayerDB__
#include <iostream>
#include "HashTable.h"
using namespace::std;
class PlayerDB : Player
{
public:
PlayerDB();
PlayerDB ( ostream& out );
bool AddPlayer ( const Player &a_passed_in_player );
bool RemovePlayer ( string const name );
Player *FetchPlayer ( string userName );
void PrintDiagnostics();
private:
HashTable player_database;
ostream* playerDB_Out;
};
#endif /* defined(__lab3e__PlayerDB__) */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment