identList.c
4.64 KB
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
#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <expValue.h>
#include <bbtree.h>
#include <ident.h>
#include <identList.h>
struct identList
{
s_bbTree * idIdx;
s_bbTree * idHash;
};
/*
* statische Funktionen (interner gebrauch)
*/
static
int
idHashCmp (void * _a, void * _b)
{
s_ident * a = (s_ident *) _a;
s_ident * b = (s_ident *) _b;
return strcmp (identGetKey (a), identGetKey (b));
}
static
int
idIdxCmp (void * _a, void * _b)
{
s_ident * a = (s_ident *) _a;
s_ident * b = (s_ident *) _b;
return identGetIdx (a) - identGetIdx (b);
}
/*
* nicht statische Funktionen (Interface)
*/
/*
* Contructors / Destructors for identList
*/
s_identList *
identListNew (void)
{
s_identList * ret = (s_identList *) malloc (sizeof (s_identList));
ret->idIdx = bbTreeNew (idIdxCmp);
ret->idHash = bbTreeNew (idHashCmp);
return ret;
}
void
identListFree (s_identList * l)
{
s_ident ** idArray,
** _run;
for (_run = idArray = (s_ident **) identListToArray (l);
*_run != NULL;
_run++)
{
identDequeue (*_run);
if (! identIsQueued (*_run))
identFree (*_run);
}
free (idArray);
bbTreeFree (l->idIdx);
bbTreeFree (l->idHash);
free (l);
}
/*
* insertions or deletion into a identList
*/
s_ident *
identListPutVal (s_identList * l, s_ident * val)
{
s_ident * oldVal;
int idx = identGetIdx (val);
char * key = identGetKey (val);
identEnqueue (val);
if ((idx < 0 && key[0] == '\0') || idx != -1 && key[0] != '\0')
/* calling error: either key or idx must be valid. But not both. */
return NULL;
if (idx >= 0)
oldVal = (s_ident *) bbTreeInsert (l->idIdx, val);
else
oldVal = (s_ident *) bbTreeInsert (l->idHash, val);
if (oldVal != NULL)
/*
* these are few lines with a lot behind them. The not obvious question
* here is: What happens if oldval points to the same address than val?
* well, this could only happen if val was was formally queued, because
* else oldVal would be NULL. Knowing this makes clear that the queue
* state is not changed at all, because first it was increased above with
* identEnqueue and the it will be decreased in this if block again. But as
* it was formally queued it will not be freed (Good)!
*/
{
identDequeue (oldVal);
if (! identIsQueued (oldVal))
identFree (oldVal);
}
return val;
}
s_ident *
identListPutExpByIdx (s_identList * l, int idx, s_expVal * val)
{
return identListPutVal (l, identExpNew (idx, NULL, val));
}
s_ident *
identListPutIdlByIdx (s_identList * l, int idx, s_identList * val)
{
return identListPutVal (l, identIdlNew (idx, NULL, val));
}
s_ident *
identListPutExpByKey (s_identList * l, const char * key, s_expVal * val)
{
return identListPutVal (l, identExpNew (-1, key, val));
}
s_ident *
identListPutIdlByKey (s_identList * l, const char * key, s_identList * val)
{
return identListPutVal (l, identIdlNew (-1, key, val));
}
void
identListRemoveByIdx (s_identList * l, int idx)
{
s_ident * seek = (s_ident *) identNew (idx, NULL);
s_ident * val = bbTreeRemove (l->idIdx, seek);
#ifdef DEBUG2
printf ("[!DEBUG!] seek (remove) identNew: %d/%s\n", idx, "");
#endif
identFree (seek);
if (val != NULL)
{
identDequeue (val);
if (! identIsQueued (val))
identFree (val);
}
}
void
identListRemoveByKey (s_identList * l, const char * key)
{
s_ident * seek = (s_ident *) identNew (-1, key);
s_ident * val = bbTreeRemove (l->idIdx, seek);
#ifdef DEBUG2
printf ("[!DEBUG!] seek (remove) identNew: %d/%s\n", -1, key);
#endif
identFree (seek);
if (val != NULL)
{
identDequeue (val);
if (! identIsQueued (val))
identFree (val);
}
}
/*
* seeking in identList
*/
s_ident *
identListSeekIdx (s_identList * l, int idx)
{
s_ident * seek = (s_ident *) identNew (idx, NULL);
s_ident * val = (s_ident *) bbTreeSeek (l->idIdx, seek);
#ifdef DEBUG2
printf ("[!DEBUG!] seek identNew: %d/%s\n", idx, "");
#endif
identFree (seek);
return val;
}
s_ident *
identListSeekKey (s_identList * l, const char * key)
{
s_ident * seek = (s_ident *) identNew (-1, key);
s_ident * val = (s_ident *) bbTreeSeek (l->idHash, seek);
#ifdef DEBUG2
printf ("[!DEBUG!] seek identNew: %d/%s\n", -1, key);
#endif
identFree (seek);
return val;
}
/*
* identList to other DataStructures
*/
s_ident **
identListToArray (s_identList * l)
{
int idIdxLen = bbTreeSize (l->idIdx);
int idHashLen = bbTreeSize (l->idHash);
int retLen = idIdxLen + idHashLen + 1;
s_ident ** ret = (s_ident **) malloc (sizeof (s_ident *) * retLen);
memset (ret, 0, sizeof (s_ident **) * retLen);
bbTreeInOrder (l->idIdx, (void **) ret);
bbTreeInOrder (l->idHash, (void **) &(ret[idIdxLen]));
return ret;
}