-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmap.c
170 lines (156 loc) · 4.04 KB
/
hmap.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "hmap.h"
#include "common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
bool is_empty(void *data, unsigned long size)
{
register unsigned long i;
for (i=0; i<size; i++)
{
if (((unsigned char *)data)[i] != HMAP_EMPTY_BLOCK)
return FALSE;
}
return TRUE;
}
bool is_deleted(void *data, unsigned long size)
{
register unsigned long i;
for (i=0; i<size; i++)
{
if (((unsigned char *) data)[i] != HMAP_DELETED_BLOCK)
return FALSE;
}
return TRUE;
}
bool is_reserved(void *data, unsigned long size)
{
register unsigned long i;
if (((unsigned char *) data)[0] != HMAP_EMPTY_BLOCK &&
((unsigned char *) data)[0] != HMAP_DELETED_BLOCK)
return FALSE;
for (i=0; i<size; i++)
{
if (((unsigned char *) data)[i] != ((unsigned char *) data)[0])
return FALSE;
}
return TRUE;
}
struct hmap *hmap_init(unsigned long map_size, unsigned long data_size,
unsigned long key_size, unsigned long (*hash)(const void *))
{
struct hmap *map;
map = malloc(sizeof(struct hmap));
if (map == NULL)
return NULL;
map->keys = malloc(map_size*key_size);
if (map->keys == NULL)
{
free(map);
return NULL;
}
map->data = malloc(map_size*data_size);
if (map->data == NULL)
{
free(map->keys);
free(map);
return NULL;
}
memset(map->keys, HMAP_EMPTY_BLOCK, map_size*key_size);
map->map_size = map_size;
map->key_size = key_size;
map->data_size = data_size;
map->hash = hash;
return map;
}
void hmap_free(struct hmap *map)
{
free(map->keys);
free(map->data);
free(map);
}
int hmap_insert(struct hmap *map, void *key, void *data)
{
unsigned long i=0, index=map->hash(key);
unsigned char *aux_key=map->keys, *aux_data=map->data;
if (is_reserved(key, map->key_size))/*Makes sure to dont add reserved keys*/
return FAILURE;
while (!is_reserved(aux_key+((index+i)%map->map_size)*(map->key_size),
map->key_size))
/*While haven't found a empty or deleted key*/
{
if (i==map->map_size) /*If already passed through the whole map*/
return FAILURE;
i++;
}
memcpy(aux_key+((index+i)%map->map_size)*(map->key_size), key,
map->key_size); /*Write key and data*/
memcpy(aux_data+((index+i)%map->map_size)*(map->data_size), data,
map->data_size);
return SUCCESS;
}
int hmap_remove(struct hmap *map, void *key, void *data)
{
unsigned long i=0, index=map->hash(key);
unsigned char *aux_key=map->keys, *aux_data=map->data;
while (!is_empty(aux_key+((index+i)%map->map_size)*(map->key_size),
map->key_size)&&i!=map->map_size)
{
if (memcmp(aux_key+((index+i)%map->map_size)*(map->key_size), key,
map->key_size) == 0)
{
memcpy(data, aux_data+((index+i)%map->map_size)*(map->data_size),
map->data_size);
memset(aux_key+((index+i)%map->map_size)*(map->key_size), 0xff,
map->key_size);
return SUCCESS;
}
i++;
}
return FAILURE;
}
int hmap_search(struct hmap *map, void *key, void *data)
{
unsigned long i=0, index=map->hash(key);
unsigned char *aux_key=map->keys, *aux_data=map->data;
while (!is_empty(aux_key+((index+i)%map->map_size)*(map->key_size),
map->key_size)&&i!=map->map_size)
{
if (memcmp(aux_key+((index+i)%map->map_size)*(map->key_size), key,
map->key_size) == 0)
{
memcpy(data, aux_data+((index+i)%map->map_size)*(map->data_size),
map->data_size);
return SUCCESS;
}
i++;
}
return FAILURE;
}
void hmap_print(struct hmap *map)
{
unsigned long i, j;
unsigned char *aux_key=map->keys, *aux_data=map->data;
printf("Struct hmap at %p\n", (void *) map);
printf("map_size=%lu key_size=%lu data_size=%lu\n", map->map_size,
map->key_size, map->data_size);
for (i=0; i<map->map_size; i++)
{
if (is_empty(aux_key+i*map->key_size,
map->key_size))
printf("Block %lu: empty\n", i);
else if (is_deleted(aux_key+i*map->key_size,
map->key_size))
printf("Block %lu: deleted\n", i);
else
{
printf("Block %lu: key = ", i);
for (j=0; j<map->key_size; j++)
printf("%u ", aux_key[i*map->key_size+j]);
printf("data = ");
for (j=0; j<map->key_size; j++)
printf("%u ", aux_data[i*map->data_size+j]);
printf("\n");
}
}
}