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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/* -----------------------------------------------------------------------------
* This file is part of SWIG, which is licensed as a whole under version 3
* (or any later version) of the GNU General Public License. Some additional
* terms also apply to certain portions of SWIG. The full details of the SWIG
* license and copyrights can be found in the LICENSE and COPYRIGHT files
* included with the SWIG source code as distributed by the SWIG developers
* and at https://www.swig.org/legal.html.
*
* list.c
*
* Implements a simple list object.
* ----------------------------------------------------------------------------- */
#include "dohint.h"
typedef struct List {
int maxitems; /* Max size */
int nitems; /* Num items */
DOH *file;
int line;
DOH **items;
} List;
extern DohObjInfo DohListType;
/* Doubles amount of memory in a list */
static
void more(List *l) {
l->items = (void **) DohRealloc(l->items, l->maxitems * 2 * sizeof(void *));
l->maxitems *= 2;
}
/* -----------------------------------------------------------------------------
* CopyList()
*
* Make a shallow copy of a list.
* ----------------------------------------------------------------------------- */
static DOH *CopyList(DOH *lo) {
List *l, *nl;
int i;
l = (List *) ObjData(lo);
nl = (List *) DohMalloc(sizeof(List));
nl->nitems = l->nitems;
nl->maxitems = l->maxitems;
nl->items = (void **) DohMalloc(l->maxitems * sizeof(void *));
for (i = 0; i < l->nitems; i++) {
nl->items[i] = l->items[i];
Incref(nl->items[i]);
}
nl->file = l->file;
if (nl->file)
Incref(nl->file);
nl->line = l->line;
return DohObjMalloc(&DohListType, nl);
}
/* -----------------------------------------------------------------------------
* DelList()
*
* Delete a list.
* ----------------------------------------------------------------------------- */
static void DelList(DOH *lo) {
List *l = (List *) ObjData(lo);
int i;
for (i = 0; i < l->nitems; i++)
Delete(l->items[i]);
DohFree(l->items);
DohFree(l);
}
/* -----------------------------------------------------------------------------
* List_clear()
*
* Remove all of the list entries, but keep the list object intact.
* ----------------------------------------------------------------------------- */
static void List_clear(DOH *lo) {
List *l = (List *) ObjData(lo);
int i;
for (i = 0; i < l->nitems; i++) {
Delete(l->items[i]);
}
l->nitems = 0;
}
/* -----------------------------------------------------------------------------
* List_insert()
*
* Insert an item into the list. If the item is not a DOH object, it is assumed
* to be a 'char *' and is used to construct an equivalent string object.
* ----------------------------------------------------------------------------- */
static int List_insert(DOH *lo, int pos, DOH *item) {
List *l = (List *) ObjData(lo);
int i;
if (!item)
return -1;
if (!DohCheck(item)) {
item = NewString(item);
Decref(item);
}
if (pos == DOH_END)
pos = l->nitems;
if (pos < 0)
pos = 0;
if (pos > l->nitems)
pos = l->nitems;
if (l->nitems == l->maxitems)
more(l);
for (i = l->nitems; i > pos; i--) {
l->items[i] = l->items[i - 1];
}
l->items[pos] = item;
Incref(item);
l->nitems++;
return 0;
}
/* -----------------------------------------------------------------------------
* List_remove()
*
* Remove an item from a list.
* ----------------------------------------------------------------------------- */
static int List_remove(DOH *lo, int pos) {
List *l = (List *) ObjData(lo);
int i;
if (pos == DOH_END)
pos = l->nitems - 1;
if (pos == DOH_BEGIN)
pos = 0;
assert(!((pos < 0) || (pos >= l->nitems)));
Delete(l->items[pos]);
for (i = pos; i < l->nitems - 1; i++) {
l->items[i] = l->items[i + 1];
}
l->nitems--;
return 0;
}
/* -----------------------------------------------------------------------------
* List_len()
*
* Return the number of elements in the list
* ----------------------------------------------------------------------------- */
static int List_len(DOH *lo) {
List *l = (List *) ObjData(lo);
return l->nitems;
}
/* -----------------------------------------------------------------------------
* List_get()
*
* Get the nth item from the list.
* ----------------------------------------------------------------------------- */
static DOH *List_get(DOH *lo, int n) {
List *l = (List *) ObjData(lo);
if (n == DOH_END)
n = l->nitems - 1;
if (n == DOH_BEGIN)
n = 0;
assert(!((n < 0) || (n >= l->nitems)));
return l->items[n];
}
/* -----------------------------------------------------------------------------
* List_set()
*
* Set the nth item in the list replacing any previous item.
* ----------------------------------------------------------------------------- */
static int List_set(DOH *lo, int n, DOH *val) {
List *l = (List *) ObjData(lo);
if (!val)
return -1;
assert(!((n < 0) || (n >= l->nitems)));
if (!DohCheck(val)) {
val = NewString(val);
Decref(val);
}
Delete(l->items[n]);
l->items[n] = val;
Incref(val);
Delete(val);
return 0;
}
/* -----------------------------------------------------------------------------
* List_first()
*
* Return the first item in the list.
* ----------------------------------------------------------------------------- */
static DohIterator List_first(DOH *lo) {
DohIterator iter;
List *l = (List *) ObjData(lo);
iter.object = lo;
iter._index = 0;
iter._current = 0;
iter.key = 0;
if (l->nitems > 0) {
iter.item = l->items[0];
} else {
iter.item = 0;
}
return iter;
}
/* -----------------------------------------------------------------------------
* List_next()
*
* Return the next item in the list.
* ----------------------------------------------------------------------------- */
static DohIterator List_next(DohIterator iter) {
List *l = (List *) ObjData(iter.object);
iter._index = iter._index + 1;
if (iter._index >= l->nitems) {
iter.item = 0;
iter.key = 0;
} else {
iter.item = l->items[iter._index];
}
return iter;
}
/* -----------------------------------------------------------------------------
* List_str()
*
* Create a string representation of the list.
* ----------------------------------------------------------------------------- */
static DOH *List_str(DOH *lo) {
DOH *s;
int i;
List *l = (List *) ObjData(lo);
s = NewStringEmpty();
if (ObjGetMark(lo)) {
Printf(s, "List(%p)", lo);
return s;
}
ObjSetMark(lo, 1);
Printf(s, "List[ ");
for (i = 0; i < l->nitems; i++) {
Printf(s, "%s", l->items[i]);
if ((i + 1) < l->nitems)
Printf(s, ", ");
}
Printf(s, " ]");
ObjSetMark(lo, 0);
return s;
}
/* -----------------------------------------------------------------------------
* List_dump()
*
* Dump the items to an output stream.
* ----------------------------------------------------------------------------- */
static int List_dump(DOH *lo, DOH *out) {
int nsent = 0;
int i, ret;
List *l = (List *) ObjData(lo);
for (i = 0; i < l->nitems; i++) {
ret = Dump(l->items[i], out);
if (ret < 0)
return -1;
nsent += ret;
}
return nsent;
}
static void List_setfile(DOH *lo, DOH *file) {
DOH *fo;
List *l = (List *) ObjData(lo);
if (!DohCheck(file)) {
fo = NewString(file);
Decref(fo);
} else
fo = file;
Incref(fo);
Delete(l->file);
l->file = fo;
}
static DOH *List_getfile(DOH *lo) {
List *l = (List *) ObjData(lo);
return l->file;
}
static void List_setline(DOH *lo, int line) {
List *l = (List *) ObjData(lo);
l->line = line;
}
static int List_getline(DOH *lo) {
List *l = (List *) ObjData(lo);
return l->line;
}
static DohListMethods ListListMethods = {
List_get,
List_set,
List_remove,
List_insert,
0, /* delslice */
};
DohObjInfo DohListType = {
"List", /* objname */
DelList, /* doh_del */
CopyList, /* doh_copy */
List_clear, /* doh_clear */
List_str, /* doh_str */
0, /* doh_data */
List_dump, /* doh_dump */
List_len, /* doh_len */
0, /* doh_hash */
0, /* doh_cmp */
0, /* doh_equal */
List_first, /* doh_first */
List_next, /* doh_next */
List_setfile, /* doh_setfile */
List_getfile, /* doh_getfile */
List_setline, /* doh_setline */
List_getline, /* doh_getline */
0, /* doh_mapping */
&ListListMethods, /* doh_sequence */
0, /* doh_file */
0, /* doh_string */
0, /* doh_callable */
0, /* doh_position */
};
/* -----------------------------------------------------------------------------
* NewList()
*
* Create a new list.
* ----------------------------------------------------------------------------- */
#define MAXLISTITEMS 8
DOH *DohNewList(void) {
List *l = (List *) DohMalloc(sizeof(List));
l->nitems = 0;
l->maxitems = MAXLISTITEMS;
l->items = (void **) DohCalloc(l->maxitems, sizeof(void *));
l->file = 0;
l->line = 0;
return DohObjMalloc(&DohListType, l);
}
static int (*List_sort_compare_func) (const DOH *, const DOH *);
static int List_qsort_compare(const void *a, const void *b) {
return List_sort_compare_func(*((DOH **) a), *((DOH **) b));
}
/* Sort a list */
void DohSortList(DOH *lo, int (*cmp) (const DOH *, const DOH *)) {
List *l = (List *) ObjData(lo);
if (cmp) {
List_sort_compare_func = cmp;
} else {
List_sort_compare_func = DohCmp;
}
qsort(l->items, l->nitems, sizeof(DOH *), List_qsort_compare);
}
|