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