-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassoc.c
276 lines (209 loc) · 9.17 KB
/
assoc.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/******************************************************************************/
/* */
/* assoc */
/* */
/* a class of associative arrays and Web-oriented functions */
/* */
/* conceived, designed, and implemented by */
/* <a href=ihttps://github.com/Eidonko>[email protected]</a> */
/* */
/* assoc.c */
/* main source file */
/* */
/******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "assoc.h"
int _brickalloc(brick**);
void _brickfree(brick*);
/******************************************************************************/
/* */
/* this function opens a new associative array and returns */
/* aopen a pointer to it. It returns NULL in case of error, !NULL */
/* otherwise. aerror is set to the error or burned accordingly */
/* */
/******************************************************************************/
ASSOC *aopen(int (*acmp)(const void*,const void*)) {
ASSOC *a;
a = (ASSOC*) malloc(sizeof(ASSOC));
if (!a) { strcpy(aerror, "alloc"); return NULL; }
a->current = & a->root;
a->last = a->root.l = a->root.r = a->root.next = NULL;
a->root.i = a->root.o = NULL;
a->p = NULL;
a->acmp = acmp;
*aerror = '\0';
return a;
}
/******************************************************************************/
/* */
/* this function writes a new couple (or substitute an existing */
/* awrite couple) in the associative array that is its first argument. */
/* In the first case a new brick is allocated. aerror is set or */
/* burned accordingly. */
/* */
/******************************************************************************/
int awrite(ASSOC *a, void *i, void *o) {
brick *n, *this;
int cmp;
if (!a) { strcpy(aerror, "not aopen()'d"); return 0; }
n = & a->root;
if (n->i == NULL && n->o == NULL) {
n->i = i; n->o = o;
n->l = n->r = n->next = NULL;
n->status = A_OK;
a->last = n;
# ifdef DEBUG
printf("wrote: (\"%s\", \"%s\")\n", i, o);
# endif
return A_OK;
}
a->p = NULL;
while ( n ) {
if ((cmp = a->acmp(i, n->i)) == 0) {
n->o = o;
n->status = A_OK;
strcpy(aerror, "overwritten");
# ifdef DEBUG
printf("overwrote: (\"%s\", \"%s\")\n", i, o);
# endif
return A_OK;
}
# ifdef DEBUG
printf("comparing \"%s\" with \"%s\"...%s\n",
i, n->i, (cmp<0)?"(l)":"(r)");
# endif
a->p = n;
if (cmp<0) n = n->l ; else n = n->r;
}
/* a final leaf's been reached */
if (_brickalloc(&this)) return A_ALLOC;
this->l = this->r = NULL;
this->i = i; this->o = o;
this->status = A_OK;
# ifdef DEBUG
printf("wrote: (\"%s\", \"%s\")\n", i, o);
# endif
if (cmp<0) a->p->l = this; else a->p->r = this;
a->last->next = this;
this->next = NULL;
a->last = this;
return A_OK;
}
/******************************************************************************/
/* */
/* this function returns the domain value corresponding to next */
/* anext couple in the associative array, regarded as a linked list */
/* ordered after the succession of awrite() calls. As usual for */
/* aerror. Deleted couples are not considered. */
/* */
/******************************************************************************/
void *anext(ASSOC* a) {
void *i;
while (a->current) {
if (a->current->status != A_DELETED) {
i = a->current->i;
a->current = a->current->next;
*aerror = '\0'; /* added! */
return i;
}
if (a->current != NULL)
{
if (a->current->next == NULL)
break;
a->current = a->current->next;
}
}
strcpy(aerror, "no more couples");
return NULL;
}
/******************************************************************************/
/* */
/* this function queries an associative for a value previously */
/* aread coupled with a awrite. It returns NULL if such value does not */
/* exist, or a value otherwise. If the value was previously */
/* deleted with adel(), it is returned and aerror is set to */
/* "deleted"; otherwise it's burned */
/* */
/******************************************************************************/
void *aread(ASSOC *a, void *i) {
brick *n = & a->root;
int cmp;
a->p = NULL;
*aerror = '\0';
while ( n && (cmp = a->acmp(i, n->i)) ) {
a->p = n;
if (cmp<0) n=n->l; else n=n->r;
}
if (n==NULL) return NULL;
a->p = n;
if (n->status == A_DELETED)
strcpy(aerror, "deleted");
return n->o;
}
/******************************************************************************/
/* */
/* resets the pointer to next domain value to be read to the */
/* arewind root of the associative array; aerror is burned. */
/* */
/******************************************************************************/
void arewind(ASSOC *a) {
a->current = & a->root;
*aerror = '\0';
}
int asetpos (ASSOC *a, apos_t *Position) {
if (!a) { strcpy(aerror, "not aopen()'d"); return 0; }
if (!Position) { strcpy(aerror, "invalid apos_t"); return 0; }
a->current = Position;
*aerror = '\0';
return 1;
}
int agetpos (ASSOC *a, apos_t *Position) {
if (!a) { strcpy(aerror, "not aopen()'d"); return 0; }
Position = a->current;
*aerror = '\0';
return 1;
}
/******************************************************************************/
/* */
/* turns the status flag to the A_DELETED value. Memory is not */
/* adel freed---only, from now on anext() will ignore the couple. */
/* Warning: dirty trick used: aread() is executed, then a side */
/* effect is exploited. */
/* */
/******************************************************************************/
void adel(ASSOC *a, void *i) {
aread(a, i);
a->p->status = A_DELETED;
/* aerror is not "touched": it's the same at exit of aread() */
}
/******************************************************************************/
/* */
/* close the associative array and free the memory for its */
/* aclose pointer after having freed all the bricks. */
/* */
/******************************************************************************/
void aclose(ASSOC *a) {
*aerror = '\0';
arewind(a);
for (; a->current; a->current = a->current->next)
_brickfree(a->current);
free(a);
}
/******************************************************************************/
/* */
/* _brick functions for allocating/deallocating bricks */
/* free/alloc */
/* */
/******************************************************************************/
int _brickalloc(brick** b) {
*b = (brick*) malloc(sizeof(brick));
*aerror = '\0';
if (*b==NULL) { strcpy(aerror, "alloc"); return 1; }
return 0;
}
void _brickfree(brick* b) { free(b); }
/******************************************************************************/
/* end of file assoc.c */
/******************************************************************************/