-
Notifications
You must be signed in to change notification settings - Fork 16
/
imap.c
187 lines (155 loc) · 4.01 KB
/
imap.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "imap.h"
#include "profile.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
enum imap_status {
IS_NONE,
IS_EXIST,
IS_REMOVE,
};
struct imap_slot {
uint64_t key;
void* value;
enum imap_status status;
struct imap_slot* next;
};
struct imap_context {
struct imap_slot* slots;
size_t size;
size_t count;
struct imap_slot* lastfree;
};
#define DEFAULT_IMAP_SLOT_SIZE 8
struct imap_context *
imap_create() {
struct imap_context* imap = (struct imap_context*)pmalloc(sizeof(*imap));
imap->slots = (struct imap_slot*)pcalloc(DEFAULT_IMAP_SLOT_SIZE, sizeof(struct imap_slot));
imap->size = DEFAULT_IMAP_SLOT_SIZE;
imap->count = 0;
imap->lastfree = imap->slots + imap->size;
return imap;
}
void
imap_free(struct imap_context* imap) {
pfree(imap->slots);
pfree(imap);
}
static inline uint64_t
_imap_hash(struct imap_context* imap, uint64_t key) {
uint64_t hash = key % (uint64_t)(imap->size);
return hash;
}
static void
_imap_rehash(struct imap_context* imap) {
size_t new_sz = DEFAULT_IMAP_SLOT_SIZE;
struct imap_slot* old_slots = imap->slots;
size_t old_count = imap->count;
size_t old_size = imap->size;
while(new_sz <= imap->count) {
new_sz *= 2;
}
struct imap_slot* new_slots = (struct imap_slot*)pcalloc(new_sz, sizeof(struct imap_slot));
imap->lastfree = new_slots + new_sz;
imap->size = new_sz;
imap->slots = new_slots;
imap->count = 0;
size_t i=0;
for(i=0; i<old_size; i++) {
struct imap_slot* p = &(old_slots[i]);
enum imap_status status = p->status;
if(status == IS_EXIST) {
imap_set(imap, p->key, p->value);
}
}
assert(old_count == imap->count);
pfree(old_slots);
}
static struct imap_slot *
_imap_query(struct imap_context* imap, uint64_t key) {
uint64_t hash = _imap_hash(imap, key);
struct imap_slot* p = &(imap->slots[hash]);
if(p->status != IS_NONE) {
while(p) {
if(p->key == key && p->status == IS_EXIST) {
return p;
}
p = p->next;
}
}
return NULL;
}
void *
imap_query(struct imap_context* imap, uint64_t key) {
struct imap_slot* p = _imap_query(imap, key);
if(p) {
return p->value;
}
return NULL;
}
static struct imap_slot *
_imap_getfree(struct imap_context* imap) {
while(imap->lastfree > imap->slots) {
imap->lastfree--;
if(imap->lastfree->status == IS_NONE) {
return imap->lastfree;
}
}
return NULL;
}
void
imap_set(struct imap_context* imap, uint64_t key, void* value) {
assert(value);
uint64_t hash = _imap_hash(imap, key);
struct imap_slot* p = &(imap->slots[hash]);
if(p->status == IS_EXIST) {
struct imap_slot* np = p;
while(np) {
if(np->key == key && np->status == IS_EXIST) {
np->value = value;
return;
}
np = np->next;
}
np = _imap_getfree(imap);
if(np == NULL) {
_imap_rehash(imap);
imap_set(imap, key, value);
return;
}
uint64_t main_hash = _imap_hash(imap, p->key);
np->next = p->next;
p->next = np;
if(main_hash == hash) {
p = np;
}else {
np->key = p->key;
np->value = p->value;
np->status = IS_EXIST;
}
}
imap->count++;
p->status = IS_EXIST;
p->key = key;
p->value = value;
}
void *
imap_remove(struct imap_context* imap, uint64_t key) {
struct imap_slot* p = _imap_query(imap, key);
if(p) {
imap->count--;
p->status = IS_REMOVE;
return p->value;
}
return NULL;
}
void
imap_dump(struct imap_context* imap, observer observer_cb, void* ud) {
size_t i=0;
for(i=0; i<imap->size; i++) {
struct imap_slot* v = &imap->slots[i];
if(v->status == IS_EXIST) {
observer_cb(v->key, v->value, ud);
}
}
}