-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_table.c
129 lines (99 loc) · 2.7 KB
/
hash_table.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hash_table.h"
#define TABLE_SIZE 256
unsigned int hash(const char *key){//Store key in slot number'value'
unsigned long int value = 0;
unsigned int i = 0;
unsigned int key_len = strlen(key);
// Fait plusieurs multiplication
for (;i<key_len;i++){
value = value *37 + key[i];
}
//Assure que la valeur est (0<=value<TABLE_SIZE)
value = value%TABLE_SIZE;
return value;
}
entry_t *ht_pair(const char *key, const char *value){
//allocation de l'entree
entry_t *entry = malloc(sizeof(entry)*1);
entry->key = malloc(strlen(key)+1);
entry->value = malloc(strlen(value)+1);
//copie key et value
strcpy(entry->key, key);
strcpy(entry->value, value);
//next commence avec 0, mais peut-etre modifier plus tard
entry->next = NULL;
return entry;
}
void ht_delete(ht_t *ht){
for(int i=0;i<TABLE_SIZE;i++){
free(ht->entries[i]);
}
free(ht);
}
ht_t *ht_create(void){
//Allocation
ht_t *hashtable = malloc(sizeof(ht_t) *1);
//Allocation des entrees
hashtable->entries = malloc(sizeof(entry_t*)* TABLE_SIZE);
//Mettre tous a zero
int i = 0;
for (;i< TABLE_SIZE; i++){
hashtable->entries[i] = NULL;
}
return hashtable;
}
void ht_set(ht_t *hashtable, const char *key, const char *value){
unsigned int bucket = hash(key);
//Try to look up an entry set
entry_t *entry = hashtable->entries[bucket];
//no entry means bucket is empty, insert immediately
if (entry == NULL){
hashtable->entries[bucket] = ht_pair(key,value);
return;
}
entry_t *prev;
//Passage par chaque entree jusqu'a trouver key ou la fin de sequence
while(entry!= NULL){
if (strcmp(entry->key, key)==0){
free(entry->value);
entry->value = malloc(strlen(value)+1);
strcpy(entry->value, value);
return;
}
prev = entry;
entry = prev->next;
}
//Key pas trouve, alors ajoute key-value pair
prev->next = ht_pair(key,value);
}
int ht_get(ht_t *hashtable, const char *key){
unsigned int slot = hash(key);
//Essaie de trouver slot valide
entry_t * entry = hashtable->entries[slot];
//Pas de slot = pas d'entreen
if (entry == NULL){
printf("Not exisiting value\n");
return 0;
}
while (entry !=NULL){
if (strcmp(entry->key, key) == 0){
return atoi(entry ->value);
}
entry = entry->next;
}
printf("error in hash\n");
return 0;//Pas de key trouve
}
void ht_aff(ht_t *hashtable){
int i = 0;
for (;i< TABLE_SIZE; i++){
if(hashtable->entries[i] == NULL){
continue;
}
printf("key - %s and value - %s\n",hashtable->entries[i]->key,hashtable->entries[i]->value);
}
}