-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.c
More file actions
79 lines (68 loc) · 1.7 KB
/
HashTable.c
File metadata and controls
79 lines (68 loc) · 1.7 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
/*
*Batch Number 6
*Abhinav Bhatia (2011A7PS371P)
*Mukul Bhutani (2011A7PS343P)
*/
#include "HashTable.h"
#include "parserDef.h"
#include <stdlib.h>
HashTable hashTable_createNew(int hashTableSize, int(*comparator) (void*, void*), int(*hashFunction) (void* value, int hashTableSize))
{
HashTable ht = { 0 };
ht.size = 0;
ht.comparator = comparator;
ht.hashFunction = hashFunction;
ht.hashTableSize = hashTableSize;
ht.table = (LinkList*)malloc(hashTableSize*sizeof(LinkList));
int i;
for (i = 0; i < hashTableSize; i++)
{
ht.table[i] = linkList_createNew(comparator);
}
return ht;
}
int hashTable_find(HashTable ht, void* value)
{
int hash = ht.hashFunction(value, ht.hashTableSize);
if (linkList_find(ht.table[hash], value)) return hash;
return -1;
}
//int symbolTable_find(HashTable ht, Symbol s)
//{
//
// int hash = ht.hashFunction(s.symbolName, ht.hashTableSize);
// if (linkList_find(ht.table[hash], &s)) return hash;
// return -1;
//}
HashTable hashTable_remove(HashTable ht, void* value)
{
int i = hashTable_find(ht, value);
if (i < 0) return ht;
ht.table[i] = linkList_remove(ht.table[i], value);
ht.size--;
return ht;
}
HashTable hashTable_add(HashTable ht, void* value)
{
int hash = ht.hashFunction(value, ht.hashTableSize);
ht.table[hash] = linkList_add(ht.table[hash], value);
ht.size++;
return ht;
}
HashTable hashTable_clear(HashTable ht)
{
int i;
for (i = 0; i < ht.hashTableSize; i++)
{
int reduce = ht.table[i].size;
ht.table[i] = linkList_clear(ht.table[i]);
ht.size -= reduce;
}
return ht;
}
void* hashTable_getElement(HashTable ht, void* key)
{
int index = hashTable_find(ht, key);
if (index < 0) return NULL;
return linkList_find(ht.table[index], key)->element;
}