-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhashmap.c
71 lines (63 loc) · 1.77 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
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include "hashmap.h"
struct entry map;
void map_init() {
map.rw_lock = malloc(sizeof(pthread_rwlock_t));
pthread_rwlock_init(map.rw_lock, NULL);
}
char *map_get(char *key) {
struct entry *iterator = map.next;
while(iterator != NULL) {
if (pthread_rwlock_rdlock(iterator->rw_lock) == 0) {
if (strcmp(iterator->key, key) == 0) {
pthread_rwlock_unlock(iterator->rw_lock);
return iterator->value;
}
}
pthread_rwlock_unlock(iterator->rw_lock);
iterator = iterator->next;
}
return NULL;
}
int map_set(char *key, char *value) {
struct entry *iterator = ↦
struct entry *newEntry;
while(iterator->next != NULL) {
struct entry *current = iterator->next;
if (strcmp(current->key, key) == 0) {
printf("modifying an existing value\n");
if (pthread_rwlock_wrlock(current->rw_lock) == 0) {
free(current->value);
current->value = malloc(strlen(value));
strcpy(current->value, value);
}
pthread_rwlock_unlock(current->rw_lock);
return 1;
}
iterator = iterator->next;
}
printf("create a new value\n");
if (pthread_rwlock_wrlock(iterator->rw_lock) == 0) { // lock the last element
newEntry = malloc(sizeof(struct entry));
iterator->next = newEntry;
newEntry->key = malloc(strlen(key));
strcpy(newEntry->key, key);
newEntry->value = malloc(strlen(value));
strcpy(newEntry->value, value);
newEntry->next = NULL;
newEntry->rw_lock = malloc(sizeof(pthread_rwlock_t));
pthread_rwlock_init(newEntry->rw_lock, NULL);
}
pthread_rwlock_unlock(iterator->rw_lock);
return 1;
}
void map_print() {
struct entry *iterator = map.next;
while(iterator != NULL) {
printf("key %s, value %s\n",iterator->key, iterator->value);
iterator = iterator->next;
}
}