|
| 1 | +// Assignment 4: Cuckoo Hashing algorithm |
| 2 | +// XX YY ( YOU NEED TO COMPLETE YOUR NAME ) |
| 3 | +// An open addressing method called Cuckoo Hashing |
| 4 | +// INPUT: an input file containing strings of characters, one string per line |
| 5 | +// OUTPUT: a detailed list of where the strings are inserted. |
| 6 | + |
| 7 | +#include <iostream> |
| 8 | +#include <cstring> |
| 9 | +#include <stdio.h> |
| 10 | + |
| 11 | +using namespace std; |
| 12 | + |
| 13 | +// cuckoo tables' size |
| 14 | +const int tablesize = 17; |
| 15 | +// combine the two 1-dimensional table into one 2-dimensional table |
| 16 | +char t[tablesize][2][255]; |
| 17 | + |
| 18 | +// compute the hash functions |
| 19 | +size_t f( char*, size_t ); |
| 20 | + |
| 21 | +// place a string in one of the hash tables |
| 22 | +bool place_in_hash_tables ( char * ); |
| 23 | + |
| 24 | +int main () { |
| 25 | + |
| 26 | + // the strings to be stored in the hash tables |
| 27 | + char s[255]=""; |
| 28 | + char null_st[] =""; |
| 29 | + size_t i; |
| 30 | + size_t len; |
| 31 | + bool placed; |
| 32 | + |
| 33 | + // clear the tables |
| 34 | + for ( i = 0; i < tablesize; i++ ) { |
| 35 | + strcpy ( t[i][0], null_st ); |
| 36 | + strcpy ( t[i][1], null_st ); |
| 37 | + } |
| 38 | + |
| 39 | + char filename[255] = ""; |
| 40 | + |
| 41 | + // display the header |
| 42 | + cout << endl << "CPSC 335-x - Programming Assignment #4: "; |
| 43 | + cout << "Cuckoo Hashing algorithm" << endl; |
| 44 | + |
| 45 | + // read the strings from a file |
| 46 | + cout << "Input the file name (no spaces)!" << endl; |
| 47 | + cin >> filename; |
| 48 | + |
| 49 | + // open the file for reading |
| 50 | + FILE *file = fopen ( filename, "r" ); |
| 51 | + if ( file != NULL ) { |
| 52 | + /* read line by line from the file */ |
| 53 | + while ( fgets ( s, 255, file ) != NULL ) { |
| 54 | + // place null character at the end of the line instead of <return> |
| 55 | + len = strlen ( s ); |
| 56 | + s[ len - 2 ] = '\0'; |
| 57 | + // insert the string in the cuckoo table |
| 58 | + placed = place_in_hash_tables( s ); |
| 59 | + // check whether the placement was successful |
| 60 | + if ( !placed ) { |
| 61 | + cout << "Placement has failed" << endl; |
| 62 | + return -1; |
| 63 | + } |
| 64 | + } |
| 65 | + fclose ( file ); |
| 66 | + } else { |
| 67 | + perror ( filename ); /* why didn't the file open? */ |
| 68 | + } |
| 69 | + return 0; |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +bool place_in_hash_tables ( char *s ) { |
| 74 | + |
| 75 | + bool placed; |
| 76 | + size_t pos; |
| 77 | + int index; |
| 78 | + char temp_s[255]; |
| 79 | + char temp[255]; |
| 80 | + |
| 81 | + strcpy( temp_s, s ); |
| 82 | + |
| 83 | + // use a counter to detect loops |
| 84 | + int counter = 0; |
| 85 | + |
| 86 | + // start with table T1 |
| 87 | + index = 0; |
| 88 | + |
| 89 | + placed = false; |
| 90 | + |
| 91 | + pos = f ( temp_s, index ); |
| 92 | + |
| 93 | + while ( ( !placed ) && ( counter < 2 * tablesize) ) { |
| 94 | + if ( strcmp ( t[pos][index], "" ) == 0 ) { |
| 95 | + // the entry at index <pos> in the <index> hash table is available so store the string <temp_s> there |
| 96 | + cout << "String <" << temp_s << "> will be placed at"; |
| 97 | + cout << " t[" << pos <<"][" << index << "]" << endl; |
| 98 | + strcpy ( t[pos][index], temp_s ); |
| 99 | + placed = true; |
| 100 | + return placed; |
| 101 | + } else { |
| 102 | + // the entry at index <pos> in the <index> hash table is not available so |
| 103 | + // obtain the string stored over there in variable <temp> and store the string <temp_s> there |
| 104 | + // now the string <temp> needs to be placed in the other table |
| 105 | + cout << "String <" << temp_s << "> will be placed at" << " t[" << pos; |
| 106 | + cout <<"][" << index << "]" << " replacing <" << t[pos][index] << ">"; |
| 107 | + cout << endl; |
| 108 | + // YOU NEED TO WRITE THE CODE TO STORE IN temp THE STRING STORED AT |
| 109 | + // t[pos][index] AND STORE IN t[pos][index] THE STRING temp_s |
| 110 | + strcpy ( temp, t[pos][index] ); |
| 111 | + strcpy ( t[pos][index], temp_s ); |
| 112 | + strcpy ( temp_s, temp ); |
| 113 | + // NOW temp_s CONTAINING THE EVICTED STRING NEEDS TO BE STORED |
| 114 | + // IN THE OTHER TABLE |
| 115 | + // WRITE THE CODE TO SET index TO INDICATE THE OTHER TABLE |
| 116 | + // WRITE THE CODE TO CALCULATE IN pos THE HASH VALUE FOR temp_s |
| 117 | + index = (index + 1) % 2; |
| 118 | + pos = f ( temp_s, index ); |
| 119 | + counter++; |
| 120 | + } |
| 121 | + } |
| 122 | + return placed; |
| 123 | +}; |
| 124 | + |
| 125 | +size_t f ( char *s, |
| 126 | + size_t index ) { |
| 127 | + // compute the hash functions |
| 128 | + // s is the string (the key) to which we apply the hash function |
| 129 | + // index indicates which hash function will be used |
| 130 | + // index == 0 means the first hash function |
| 131 | + // index == 1 means the second hash function |
| 132 | + size_t po; |
| 133 | + size_t len; |
| 134 | + int i; |
| 135 | + int val; |
| 136 | + int temp; |
| 137 | + |
| 138 | + po = 1; |
| 139 | + len = strlen ( s ); |
| 140 | + |
| 141 | + if ( index == 0 ) { |
| 142 | + val = s[0]; |
| 143 | + val = val % tablesize; |
| 144 | + if ( val < 0 ) { |
| 145 | + val += tablesize; |
| 146 | + } |
| 147 | + |
| 148 | + if ( len == 1 ) { |
| 149 | + return val; |
| 150 | + } |
| 151 | + |
| 152 | + for ( i = 1; i < len; i++ ) { |
| 153 | + temp = s[i]; |
| 154 | + po *= 31; |
| 155 | + |
| 156 | + po = po % tablesize; |
| 157 | + if ( po < 0 ) { |
| 158 | + po += tablesize; |
| 159 | + } |
| 160 | + |
| 161 | + val += temp * po; |
| 162 | + val = val % tablesize; |
| 163 | + |
| 164 | + if ( val < 0 ) { |
| 165 | + val += tablesize; |
| 166 | + } |
| 167 | + } |
| 168 | + return val; |
| 169 | + } else { |
| 170 | + // YOU NEED TO IMPLEMENT THE STEPS TO CALCULATE THE SECOND |
| 171 | + // HASH FUNCTION |
| 172 | + val = s[len - 1]; |
| 173 | + val = val % tablesize; |
| 174 | + if ( val < 0 ) { |
| 175 | + val += tablesize; |
| 176 | + } |
| 177 | + |
| 178 | + if ( len == 1 ) { |
| 179 | + return val; |
| 180 | + } |
| 181 | + |
| 182 | + for (i = 1; i < len; ++i) { |
| 183 | + temp = s[len - i - 1]; |
| 184 | + po *= 31; |
| 185 | + |
| 186 | + po = po % tablesize; |
| 187 | + if ( po < 0 ) { |
| 188 | + po += tablesize; |
| 189 | + } |
| 190 | + |
| 191 | + val += temp * po; |
| 192 | + val = val % tablesize; |
| 193 | + |
| 194 | + if ( val < 0 ) { |
| 195 | + val += tablesize; |
| 196 | + } |
| 197 | + } |
| 198 | + return val; |
| 199 | + } |
| 200 | +} |
| 201 | + |
0 commit comments