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
|
#include "ClosedHash.h"
void ClosedHash(int maxSize) {
int hashTable[maxSize]; // I think this is my problem - commented out and Changed to HT[masSize] next line
//int HT[maxSize];
}
ClosedHash::~ClosedHash() {
delete[] ClosedHash;
}
unsigned int ClosedHash::h(int key) const {
unsigned int hash = 0; // The hash value starts at 0
unsigned int keyarr = key; // A copy of the key
// We will treat the key (an integer) as an array of 4
// unsigned characters
unsigned char *keyptr = (unsigned char *) &keyarr;
// Mix each 8-bit character into the hash
for (int i = 0; i < (sizeof(int)); i++) {
// This is the combining step:
hash += keyptr[i];
// This is the mixing step:
hash += (hash << 10);
hash ^= (hash >> 6);
}
// After all the bits of the key have been mixed in,
// ensure that they are properly distributed throughout
// the final hash value:
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
// This last line assumes the you have a data member
// or constant in class ClosedHash called maxSize. This
// is the value M (the number of buckets in the hash). This
// can be a private data element or constant.
return hash % maxSize;
}
// ****
// For comments, see h(), above!
// Also see the comments near the end of h2().
// ****
unsigned int ClosedHash::h2(int key) const {
unsigned int hash = 0;
unsigned int keyarr = key;
unsigned char *keyptr = (unsigned char *) &keyarr;
for (int i = 0; i < sizeof(int); i++) {
hash += keyptr[i];
hash += (hash << 9);
hash ^= (hash >> 5);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
// The following code ensures that the value of h2(k) will be
// odd, which should be coprime with the size of the closed
// hash (32,768 == 2^15).
return (((hash * 2) + 1) % maxSize);
}
// Insert ID into hash table HT
bool insert(int ID, int collisions) {
int home; // Home position for ID
int pos = home = h(getKey(ID)); // Init probe sequence
for (int i = 1; (!(checkHash(EMPTY, HT[pos])) &&
!(checkHash(TOMBSTONE, HT[pos]))); i++) {
// TODO Shouldn't I replace getKey(ID) with just ID?
pos = (home + probeSeq(getKey(ID), i)) % maxSize; // Follow probes | function p is the probe sequence
if (checkHash(ID, HT[pos])) return false; // Duplicate
}
HT[pos] = ID; // Insert ID
return true;
}
bool ClosedHash::find(int searchValue, int &probes) const {
}
// Search for and delete the record with Key K
bool ClosedHash::remove(int delValue) {
int home; // Home position for K
// Initial posit on probe sequence
int pos = home = h(K);
for (int i = 1; !checkKey(K, HT[pos]) &&
!checkHash(EMPTY, HT[pos]); i++)
// Next on probe sequence
pos = (home + probeSeq(K, i)) % M;
if (checkKey(K, HT[pos])) { // Found it
e = HT[pos];
HT[pos] = TOMBSTONE; // Delete it
return true;
} else return false; // K not in hash table
}
bool checkHash(int EMPTY, HT[pos]){
// TODO
}
bool checkKey() {
// TODO
}
bool probeSeq() {
// TODO
}
// TODO fix up the below 4 functions...
int getKey(int ID) {
return ID; //return the key of the requested ID;
}
// Return the number of items currently in the hash.
int count() const {
return count;
}
// Return the current alpha value of the hash.
float alpha() const {
return count / maxSize;
}
// Returns true if the hash is full, otherwise false.
// NB: This is only meaningful in a closed hash table.
bool full() const {
//if hash is full return true else return false;
//
// if hash.size() == maxSize return true; // How do I size of hash??
// else return false; // false == hash is not full
}
|