-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestHashTable.c
More file actions
103 lines (81 loc) · 2.25 KB
/
testHashTable.c
File metadata and controls
103 lines (81 loc) · 2.25 KB
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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "hashtable.h"
#include "object.h"
void destroyString (ObjectType * objectType, void * value) {
//
}
int compareString (ObjectType * objectType, void * value1, void * value2) {
return strcmp((char*)value1, (char*)value2);
}
char * stringToString(ObjectType * objectType, void * value) {
char* str = (char*)value;
int size = getStringSize(str);
char * temp = malloc(sizeof(char) * (size + 1));
stringInsert(temp, str, 0);
temp[size] = '\0';
return temp;
}
static ObjectType * STRING_TYPE;
#define TABLE_SIZE 211
#define EOS '\0'
/* -----------------------------------------------------------------------------
* hashpjw
* Peter J. Weinberger's hash function
* Source: Aho, Sethi, and Ullman, "Compilers", Addison-Wesley, 1986 (page 436).
*/
int hashpjw( void* value )
{
char *s = (char*)value;
char *p;
unsigned h = 0, g;
for ( p = s; *p != EOS; p++ )
{
h = (h << 4) + (*p);
if ( g = h & 0xf0000000 )
{
h = h ^ ( g >> 24 );
h = h ^ g;
}
}
return h % TABLE_SIZE;
}
int main() {
STRING_TYPE = objectTypeInit(stringToString, compareString, destroyString);
HashTable * table = hashTableInit(TABLE_SIZE, hashpjw, STRING_TYPE, STRING_TYPE);
hashTablePrint(table);
printf("\n");
printf("Size: %d\n", hashTableSize(table));
hashTablePut(table, "One", "Another");
hashTablePrint(table);
printf("\n");
printf("Size: %d\n", hashTableSize(table));
hashTablePut(table, "two", "Joke");
hashTablePrint(table);
printf("\n");
printf("Size: %d\n", hashTableSize(table));
hashTablePut(table, "Three", "John");
hashTablePrint(table);
printf("\n");
printf("Size: %d\n", hashTableSize(table));
hashTablePut(table, "two", "John");
hashTablePrint(table);
printf("\n");
printf("Size: %d\n", hashTableSize(table));
hashTableRemove(table, "Three");
hashTablePrint(table);
printf("\n");
printf("Size: %d\n", hashTableSize(table));
hashTableRemove(table, "Three");
hashTablePrint(table);
printf("\n");
printf("Size: %d\n", hashTableSize(table));
char* element = hashTableGet(table, "One");
printf("One is: %s\n", element);
hashTableClear(table);
hashTablePrint(table);
printf("\n");
printf("Size: %d\n", hashTableSize(table));
hashTableDestroy(table);
}