-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashmap.c
108 lines (98 loc) · 2.91 KB
/
hashmap.c
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
#include "standart.h"
#include "hashmap.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Initializing the minimum size, maximum load and size factor of our Hashmap
#define hashmap_min_size 257
#define max_load 0.5f
#define size_factor 2
// Our Hashmap functions
int doubleHash(char *, HashMap *);
void biggerarray(HashMap *hashMap, int newSize);
// Our Hashmap constructor
HashMap initializeHashMap() {
HashMap a;
a.array = (HashMapEntry *) calloc(hashmap_min_size, sizeof(HashMapEntry));
a.fullness = 0;
a.size = hashmap_min_size;
return a;
}
// Our hashing function
int hash(char *str, int size) {
int hash = 0;
int i = 0;
while (str[i] != 0) {
hash = ((hash << 8) + str[i] - 'a' + 1) % size;
i++;
}
return hash;
}
// In case of a collision in the upper "hash" function, this will be used for hashing
int doubleHash(char *str, HashMap *hashMap) {
int i = 0;
int firstHash = hash(str, (*hashMap).size);
char* key;
while (TRUE) {
key = (*hashMap).array[((firstHash + i) * (i + 1)) % (*hashMap).size].key;
if (key == 0 || (chadstrcmp(key, str)) == 0) {
return ((firstHash + i) * (i + 1)) % (*hashMap).size;
} else {
i++;
}
}
}
// Resizing our Hashmap
void biggerarray(HashMap *hashMap, int newSize) {
HashMapEntry *oldarray = (*hashMap).array;
int oldsize = (*hashMap).size;
(*hashMap).array = (struct HashMapEntry *) calloc(newSize, sizeof(HashMapEntry));
(*hashMap).size = newSize;
for (int i = 0; i < oldsize; i++) {
if (oldarray[i].key == 0) {
continue;
} else {
(*hashMap).array[doubleHash(oldarray[i].key, (hashMap))] = oldarray[i];
}
}
free(oldarray);
}
// Adding a new element to our Hashmap
void add_new_element(HashMap *hashMap, char *key, long long int value) {
hashMap->fullness = hashMap->fullness + 1;
if (hashMap->fullness > max_load * hashMap->size) {
biggerarray(hashMap,hashMap->size*size_factor);
}
HashMapEntry newEntry;
newEntry.key = key;
newEntry.value = value;
hashMap->array[doubleHash(key,hashMap)] = newEntry;
return;
}
// Getting the value of an element inside our Hashmap
long long int getValue(HashMap *hashMap, char* str) {
return (*hashMap).array[doubleHash(str, hashMap)].value;
}
// Function which frees the pre-allocated memories inside the Hashmap
void deconstractor(HashMap *hashMap) {
for(int i = 0; i<(*hashMap).size; i++) {
if ((*hashMap).array[i].key != 0) {
free((*hashMap).array[i].key);
}
}
free((*hashMap).array);
}
// String comparator function
int chadstrcmp(char *str1, char*str2) {
int i = 0;
while(str1[i] !='\0' && str2[i] != '\0') {
if(str1[i] != str2[i]) {
return 1;
}
i++;
}
if (str1[i] =='\0' && str2[i] == '\0') {
return 0;
}
return 1;
}