1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
unsigned int hashFunctionInt(const void *void_key) {
unsigned int *temp = static_cast<unsigned int *>(const_cast<void *>(void_key));
unsigned int key = *temp;
//TODO: Complete this. You need to hash the int variable key and return a value between 0 and 999.
return 0;
}
unsigned int hashFunctionString(const void *void_key) {
string *temp = static_cast<string *>(const_cast<void *>(void_key));
string key = *temp;
//TODO: Complete this. You need to hash the int variable key and return a value between 0 and 999.
return 0;
}
//***********************************
//The hash table class
//***********************************
template <typename T, typename U>
class hashTable : public manageMemory {
public:
hashTable() {hashFunction = &hashFunctionInt; linkedListArray = new modifiedLinkedList<T, U>[NUMBER_OF_LINKED_LISTS];}
hashTable(unsigned int(*hashFunction)(const void *));
hashTable(const hashTable& obj);
~hashTable();
hashTable& operator=( hashTable tmp );
void add(const T& key, const U& value);
bool exists(const T& key) const;
void remove(const T& key);
U item(const T& key);
U& operator[](const T& key);
//friended so the checkTest function can have access to private data members of this class.
friend void testSimpleIntHash();
friend void testHashOfObjects();
protected:
modifiedLinkedList<T, U> *linkedListArray; //The array of linked lists
unsigned int hash(const T& key) const;
private:
int getWorstBucket() const;
int getTotalCount() const;
unsigned int(*hashFunction)(const void*);
static const int NUMBER_OF_LINKED_LISTS = 1000;
};
template <typename T, typename U>
hashTable<T,U>::hashTable(unsigned int (* hashFunction)(const void*)) {
this->hashFunction = hashFunction;
//TODO: Initialize an array of 1000 linked lists using the
//new keyword, storing the array starting address in the
//pointer linkedListArray
linkedListArray = NULL;
}
//copy constructor
template <typename T, typename U>
hashTable<T,U>::hashTable(const hashTable& obj) {
this->hashFunction = obj.hashFunction;
if (obj.linkedListArray == NULL) {
this->linkedListArray = NULL;
} else {
this->linkedListArray = new modifiedLinkedList<T, U>[NUMBER_OF_LINKED_LISTS];
for (int i = 0; i < NUMBER_OF_LINKED_LISTS; i++) {
this->linkedListArray[i] = obj.linkedListArray[i];
}
}
}
template <typename T, typename U>
hashTable<T,U>& hashTable<T,U>::operator=( hashTable<T,U> tmp ) {
this->hashFunction = tmp.hashFunction;
delete [] (this->linkedListArray);
if (tmp.linkedListArray == NULL) {
this->linkedListArray = NULL;
} else {
this->linkedListArray = new modifiedLinkedList<T, U>[NUMBER_OF_LINKED_LISTS];
for (int i = 0; i < NUMBER_OF_LINKED_LISTS; i++) {
this->linkedListArray[i] = tmp.linkedListArray[i];
}
}
return *this;
}
template <typename T, typename U>
hashTable<T,U>::~hashTable() {
if (linkedListArray != NULL) {
delete[] linkedListArray;
}
}
template <typename T, typename U>
void hashTable<T, U>::add(const T& key, const U& value) {
//TODO:
//hash the key hash(key)
//Get the returned index
//use that index in your array of linked lists
int hashIndex;
hashIndex = hash(key);
}
template <typename T, typename U>
bool hashTable<T, U>::exists(const T& key) const {
//TODO:
//hash the key hash(key)
//Get the returned index
//use that index in your array of linked lists
return false;
}
template <typename T, typename U>
U hashTable<T, U>::item(const T& key) {
//TODO:
//hash the key hash(key)
//Get the returned index
//use that index in your array of linked lists
int hashIndex;
hashIndex = hash(key);
//to get it to compile
U temp;
return temp;
}
template <typename T, typename U>
U& hashTable<T, U>::operator[](const T& key) {
//TODO:
//hash the key hash(key)
//Get the returned index
//use that index in your array of linked lists
//These two lines are to just get it to compile, you don't want them.
//(this is also causing the memory leak test to fail)
U* temp = new U;
return *temp;
}
template <typename T, typename U>
void hashTable<T, U>::remove(const T& key) {
//TODO:
//hash the key hash(key)
//Get the returned index
//use that index in your array of linked lists
}
template <typename T, typename U>
unsigned int hashTable<T, U>::hash(const T& key) const {
//A helpful method which does all the function pointer work
//for you so it knows which hash function to call.
return (*hashFunction)(&key);
}
template <typename T, typename U>
int hashTable<T, U>::getWorstBucket() const{
int count;
int highest = 0;
if (linkedListArray != NULL) {
for (int i = 0; i < 1000; i++) {
count = linkedListArray[i].getCount();
if (count > highest) {
highest = count;
}
}
}
return highest;
}
template <typename T, typename U>
int hashTable<T, U>::getTotalCount() const{
int count;
int sum = 0;
if (linkedListArray != NULL) {
for (int i = 0; i < 1000; i++) {
count = linkedListArray[i].getCount();
sum += count;
}
}
return sum;
}
|