All pastes #1666524 Raw Edit

hash.cpp

public cpp v1 · immutable
#1666524 ·published 2009-11-11 14:18 UTC
rendered paste body
//      hash.cpp////      Copyright 2009 Pedro de la Cruz#include "hash.h"using namespace std;hash::hash() {	buckets = 0;	modulus = 0;	hashArray = NULL;}hash::~hash() {}/** * Prints all linkedlists */void hash::print() {		/* Declare */	int i;	list<record>::iterator it;	/* Print Header */    printf("BEGIN PRINT\n");		/* Print Body */	for(i = 0 ; i < buckets ; i++) {				cout << "BUCKET " <<  i << ": " ;				for( it = hashArray[i].begin() ; it != hashArray[i].end() ; it++ )			cout << it->id << ", ";		cout << endl;	} 		/* Print Footer */    printf("END PRINT\n");}/* * TODO: * 11.10.2009 18:33:51 finish method */void hash::returnBook(int id) {		cout << id << endl;}/** * Make an array of pointers * where each pointer points to a linkedlist */void hash::makeBuckets() {		if( buckets == 0 ) {		cerr << "Error in buckets" << endl;		exit(1);	} else {		hashArray = new list<record>[buckets];	}	}/** * Overloaded */void hash::makeBuckets(int buckets) {		if( buckets <= 0 ) {		cerr << "Error in size of bucket" << endl;		exit(1);	} else {				hashArray = new list<record>[buckets];	}	}/** * Delete the array of linkedlist */void hash::deleteBuckets() {	delete [] hashArray;}/** * Main function that decides what to do */void hash::process(string line) {		/* Print */	if( line[0] == 'P' ) {		print();	}	/* Checkout */	else if( line[0] == 'C' ) {		int id = parseCheckout(line);		processCheckout(id);	}	/* Return */	else if( line[0] == 'R' ) {		record myRecord = parseReturn(line);		int bucketNumber = hashFunction(myRecord);		processReturn(bucketNumber, myRecord);	}}/************** Private ******************//** * The hash function uses the modulus method */int hash::hashFunction(record myRecord) {		if( modulus == 0 ) {		cerr << "Error in hashFunction, with modulus" << endl;		exit(1);	} else {		return myRecord.id % modulus;	}	return NULL;}record hash::parseReturn(string line) {		/* Declare */	string mainPart;	int spaceLoc; 	record myRecord;		/* Generate sub-string */	mainPart = line.substr( 7 , line.size() - 7 );		/* Find id */	spaceLoc = mainPart.find( " " ) ;	myRecord.id = atoi( mainPart.substr( 0 , spaceLoc ).c_str( ) ) ;		/* Find book name */	myRecord.book = mainPart.substr( spaceLoc + 1 , mainPart.size() );		return myRecord;	}int hash::parseCheckout(string line) {		/* Declare */	string mainPart;		/* Generate sub-string */	mainPart = line.substr( 9 , line.size() - 9 );		return atoi( mainPart.c_str() ) ;}void hash::addToHash(int bucketNumber, record myRecord) {		if( hashArray == NULL ) {		cerr << "Can not add to hash when array is not created" << endl;		exit(1);	} else {		hashArray[bucketNumber].push_back(myRecord);	}}/** * Compares two records id's * lower id's are sorted at the beginning */bool hash::compare_records(record uno, record dos) {	if(uno.id < dos.id) 		return false;	else		return true;}int hash::processReturn(int bucketNumber, record myRecord) {		list<record>::iterator it;	for( it = hashArray[bucketNumber].begin() ; it != hashArray[bucketNumber].end() ; it++ )		if( it->id == myRecord.id ) {			cout << "PRESENT IN BUCKET " << bucketNumber <<": ";			cout << "RETURN " << myRecord.id << " " << myRecord.book << endl;			return 0;		}	/* Successful */	if( hashArray[bucketNumber].empty() ) {		addToHash(bucketNumber, myRecord);		cout << "SUCCESSFULL IN BUCKET " << bucketNumber << ": ";		cout << "RETURN " << myRecord.id << " " << myRecord.book << endl;		return 0;	}	/* Collision */	else {		addToHash(bucketNumber, myRecord);		cout << "COLLISION IN BUCKET " << bucketNumber << ": ";		cout << "RETURN " << myRecord.id << " " << myRecord.book << endl;		return 0;	}}int hash::processCheckout(int id) {		/* Declare */	list<record>::iterator it;	int bucket = id % modulus;	int counter = 0;		/* Remove from list if found */	for( it = hashArray[bucket].begin() ; it != hashArray[bucket].end() ; it++ ) {		counter++;		if( it->id == id ) {			hashArray[bucket].erase(it); // does not work, gives garbage as result			cout << "FOUND WITH " << counter << " COMPARISON ";			cout << "(" << it->book << "): CHECKOUT " << it->id << endl;			return 0;		}	}		/* Print NOT found */	cout << "NOT FOUND WITH " << counter << " COMPARISON: ";	cout << "CHECKOUT " << id << endl;		return 0;}/************ Setters/Getters ************/void hash::setBuckets(int buckets) {		if( hashArray == NULL ) {		cerr << "Error: When arrempting to set buckets ";		cerr << "Array not set" << endl;		exit(1);	} else {		this->buckets = buckets ;	}}void hash::setModulus(int modulus) {		if ( hashArray == NULL ) {		cerr << "Error: When arrempting to set modulus ";		cerr << "Array not set" << endl;		exit(1);		} else {		this->modulus = modulus ;	}}