1 |
/* RCSid $Id$ */ |
2 |
#ifndef __G3LIST_H |
3 |
#define __G3LIST_H |
4 |
|
5 |
#ifdef __cplusplus |
6 |
extern "C" { |
7 |
#endif |
8 |
|
9 |
#include "gbasic.h" |
10 |
|
11 |
#define G3L_STARTCAP 2 |
12 |
|
13 |
|
14 |
typedef unsigned char* (*create_f)(); |
15 |
typedef void (*free_f)(unsigned char*); |
16 |
typedef int (*copy_f)(unsigned char*,unsigned char*); |
17 |
|
18 |
typedef struct g3List { |
19 |
create_f ocreate; |
20 |
free_f ofree; |
21 |
copy_f ocopy; |
22 |
int cap; |
23 |
int size; |
24 |
unsigned char** items; |
25 |
} g3List; |
26 |
|
27 |
|
28 |
|
29 |
g3List* g3l_create(create_f cf,free_f ff,copy_f cpf); |
30 |
|
31 |
int g3l_append(g3List* nl,unsigned char* el); |
32 |
unsigned char* g3l_get(g3List* nl,int id); |
33 |
|
34 |
int g3l_remove(g3List* nl,int id); |
35 |
unsigned char* g3l_remove_last(g3List* nl); |
36 |
int g3l_get_size(g3List* nl); |
37 |
void g3l_invert(g3List* nl); |
38 |
|
39 |
|
40 |
|
41 |
#define G3L_MKHEADER(pref,type) \ |
42 |
g3List* pref ## _list_create(); \ |
43 |
void pref ## _list_free(g3List* nl); \ |
44 |
void pref ## _list_free_content(g3List* nl); \ |
45 |
type* pref ## _list_append_new(g3List* nl); \ |
46 |
int pref ## _list_append(g3List* nl,type* it); \ |
47 |
type* pref ## _list_get(g3List* nl,int id); \ |
48 |
type* pref ## _list_get_copy(g3List* nl,int id); \ |
49 |
int pref ## _list_copy(g3List* dst,g3List* src); \ |
50 |
int pref ## _list_remove(g3List* nl,int id); \ |
51 |
type* pref ## _list_remove_last(g3List* nl); \ |
52 |
int pref ## _list_get_size(g3List* nl); \ |
53 |
void pref ## _list_clear(g3List* nl); \ |
54 |
int pref ## _list_index(g3List* nl,type*); \ |
55 |
int pref ## _list_replace(g3List* nl,int,type*); |
56 |
|
57 |
|
58 |
|
59 |
#define G3L_MKLIST(pref,type,crf,ff,cpf) \ |
60 |
g3List* pref ## _list_create() { \ |
61 |
return g3l_create((create_f) crf,(free_f) ff,(copy_f) cpf); \ |
62 |
}; \ |
63 |
void pref ## _list_free(g3List* nl) { \ |
64 |
free(nl->items); \ |
65 |
free(nl); \ |
66 |
nl = NULL; \ |
67 |
} \ |
68 |
void pref ## _list_free_content(g3List* nl) { \ |
69 |
int i; \ |
70 |
for(i=0;i<nl->cap;i++)\ |
71 |
nl->ofree((unsigned char*) nl->items[i]);\ |
72 |
free(nl->items); \ |
73 |
free(nl); \ |
74 |
nl = NULL; \ |
75 |
} \ |
76 |
type* pref ## _list_append_new(g3List* nl) { \ |
77 |
type* res; \ |
78 |
res = (type*) nl->ocreate(); \ |
79 |
if (!g3l_append(nl,((unsigned char*) res))) \ |
80 |
return NULL; \ |
81 |
return res; \ |
82 |
} \ |
83 |
int pref ## _list_append(g3List* nl,type* it) { \ |
84 |
return g3l_append(nl,((unsigned char*) it)); \ |
85 |
} \ |
86 |
type* pref ## _list_get(g3List* nl,int id) { \ |
87 |
return ((type*) g3l_get(nl,id)); \ |
88 |
} \ |
89 |
type* pref ## _list_get_copy(g3List* nl,int id) { \ |
90 |
unsigned char* it; \ |
91 |
unsigned char* res; \ |
92 |
if (!(nl->ocopy)) \ |
93 |
return NULL; \ |
94 |
res = nl->ocreate(); \ |
95 |
if (!(it = (unsigned char*) pref ## _list_get(nl,id))) \ |
96 |
return NULL; \ |
97 |
if (!(nl->ocopy(res,it))) { \ |
98 |
fprintf(stderr,"list: copying failed for type %s\n","type"); \ |
99 |
return NULL; \ |
100 |
} \ |
101 |
return ((type*) res); \ |
102 |
} \ |
103 |
int pref ## _list_copy(g3List* dst,g3List* src) {\ |
104 |
int i; \ |
105 |
dst->size = 0; \ |
106 |
for(i=0;i<src->size;i++)\ |
107 |
pref ## _list_append(dst,pref ## _list_get_copy(src,i)); \ |
108 |
return 1; \ |
109 |
}\ |
110 |
int pref ## _list_remove(g3List* nl,int id) { \ |
111 |
return g3l_remove(nl,id); \ |
112 |
}\ |
113 |
type* pref ## _list_remove_last(g3List* nl) { \ |
114 |
return ((type*) g3l_remove_last(nl)); \ |
115 |
} \ |
116 |
int pref ## _list_get_size(g3List* nl) { \ |
117 |
return g3l_get_size(nl); \ |
118 |
} \ |
119 |
void pref ## _list_clear(g3List* nl) { \ |
120 |
nl->size = 0; \ |
121 |
} \ |
122 |
int pref ## _list_index(g3List* nl,type* el) { \ |
123 |
int i; \ |
124 |
for(i=0;i<nl->size;i++)\ |
125 |
if (el == pref ## _list_get(nl,i)) \ |
126 |
return i; \ |
127 |
return -1; \ |
128 |
} \ |
129 |
int pref ## _list_replace(g3List* nl,int id,type* el) { \ |
130 |
if ((id < 0) || (id >= nl->size)) \ |
131 |
return 0; \ |
132 |
nl->items[id] = (unsigned char*) el; \ |
133 |
return 1; \ |
134 |
} |
135 |
|
136 |
|
137 |
|
138 |
|
139 |
#ifdef __cplusplus |
140 |
} |
141 |
#endif |
142 |
#endif |