4 Part of the swftools package.
6 Copyright (c) 2001,2002,2003,2004 Matthias Kramm <kramm@quiss.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
32 #define NEW(t,y) t*y = (t*)rfx_calloc(sizeof(t));
34 /* dynamically growing mem section */
35 typedef struct _mem_t {
42 /* fifo buffered growing mem region */
43 typedef struct _ringbuffer_t
49 /* non-nul terminated string */
50 typedef struct _string_t {
55 /* key/value pairs of strings */
56 typedef struct _map_t {
60 /* type information */
61 typedef char (*equals_func)(const void*o1, const void*o2);
62 typedef unsigned int (*hash_func)(const void*o);
63 typedef void* (*dup_func)(const void*o);
64 typedef void (*free_func)(void*o);
66 typedef struct _type_t {
73 extern type_t charptr_type;
74 extern type_t stringstruct_type;
75 extern type_t ptr_type;
77 typedef struct _dictentry {
81 struct _dictentry*next;
84 /* (void*) pointers referenced by strings */
85 typedef struct _dict {
92 /* array of key/value pairs, with fast lookup */
93 typedef struct _array_entry {
98 typedef struct _array {
105 /* array of strings, string<->int mapping,
106 with O(1) for int->string lookup and
107 ~O(n/hashsize) for string->int lookup */
108 typedef struct _stringarray_t
114 typedef struct _heap {
120 int(*compare)(const void *, const void *);
123 /* trie (with rollback) */
124 typedef struct _trielayer {
125 struct _trielayer*row[256];
130 typedef struct _trie {
135 char* strdup_n(const char*str, int size);
136 char* allocprintf(const char*str, ...);
138 float medianf(float*values, int n);
140 unsigned int crc32_add_byte(unsigned int crc32, unsigned char b);
141 unsigned int crc32_add_string(unsigned int crc32, const char*s);
143 void mem_init(mem_t*mem);
144 int mem_put(mem_t*m, void*data, int length);
145 int mem_putstring(mem_t*m, string_t str);
146 int mem_get(mem_t*m, void*data, int length);
147 void mem_clear(mem_t*mem);
148 void mem_destroy(mem_t*mem);
150 void ringbuffer_init(ringbuffer_t*r);
151 void ringbuffer_put(ringbuffer_t*r, void*buf, int size);
152 int ringbuffer_read(ringbuffer_t*r, void*buf, int size);
153 void ringbuffer_clear(ringbuffer_t*r);
155 /* old style functions- should be renamed */
156 string_t string_new(const char*text, int len);
157 string_t string_new2(const char*text);
158 void string_dup(string_t*str, const char*text);
159 void string_dup2(string_t*str, const char*text, int len);
161 char* string_cstr(string_t*str);
162 char* string_escape(string_t*str);
163 string_t* string_new3(const char*text, int len);
164 string_t* string_new4(const char*text);
165 void string_free(string_t*s);
166 unsigned int string_hash(const string_t*str);
167 unsigned int string_hash2(const char*str);
168 unsigned int string_hash3(const char*str, int len);
169 void string_set(string_t*str, const char*text);
170 void string_set2(string_t*str, const char*text, int len);
171 string_t*string_dup3(string_t*s);
172 int string_equals(string_t*str, const char*text);
174 void stringarray_init(stringarray_t*sa, int hashsize);
175 void stringarray_put(stringarray_t*sa, string_t str);
177 char* stringarray_at(stringarray_t*sa, int pos);
178 string_t stringarray_at2(stringarray_t*sa, int pos);
179 int stringarray_find(stringarray_t*sa, string_t*str);
180 void stringarray_clear(stringarray_t*sa);
181 void stringarray_destroy(stringarray_t*sa);
184 dict_t*dict_new2(type_t*type);
185 void dict_init(dict_t*dict, int size);
186 void dict_init2(dict_t*dict, type_t*type, int size);
187 dictentry_t*dict_put(dict_t*h, const void*key, void* data);
188 void dict_put2(dict_t*h, const char*s, void*data);
189 int dict_count(dict_t*h);
190 void dict_dump(dict_t*h, FILE*fi, const char*prefix);
191 dictentry_t* dict_get_slot(dict_t*h, const void*key);
192 char dict_contains(dict_t*h, const void*s);
193 void* dict_lookup(dict_t*h, const void*s);
194 char dict_del(dict_t*h, const void*s);
195 char dict_del2(dict_t*h, const void*key, void*data);
196 dict_t*dict_clone(dict_t*);
198 void dict_foreach_keyvalue(dict_t*h, void (*runFunction)(void*data, const void*key, void*val), void*data);
199 void dict_foreach_value(dict_t*h, void (*runFunction)(void*));
200 void dict_free_all(dict_t*h, char free_keys, void (*free_data_function)(void*));
201 void dict_clear(dict_t*h);
202 void dict_destroy_shallow(dict_t*dict);
203 void dict_destroy(dict_t*dict);
204 #define DICT_ITERATE_DATA(d,t,v) \
205 int v##_i;dictentry_t*v##_e;t v;\
206 for(v##_i=0;v##_i<(d)->hashsize;v##_i++) \
207 for(v##_e=(d)->slots[v##_i]; v##_e && ((v=(t)v##_e->data)||1); v##_e=v##_e->next)
208 #define DICT_ITERATE_KEY(d,t,v) \
209 int v##_i;dictentry_t*v##_e;t v;\
210 for(v##_i=0;v##_i<(d)->hashsize;v##_i++) \
211 for(v##_e=(d)->slots[v##_i];v##_e && ((v=(t)v##_e->key)||1);v##_e=v##_e->next)
212 #define DICT_ITERATE_ITEMS(d,t1,v1,t2,v2) \
213 int v1##_i;dictentry_t*v1##_e;t1 v1;t2 v2; \
214 for(v1##_i=0;v1##_i<(d)->hashsize;v1##_i++) \
215 for(v1##_e=(d)->slots[v1##_i]; v1##_e && (((v1=(t1)v1##_e->key)&&(v2=(t2)v1##_e->data))||1); v1##_e=v1##_e->next)
217 void map_init(map_t*map);
218 void map_put(map_t*map, string_t t1, string_t t2);
219 const char* map_lookup(map_t*map, const char*name);
220 void map_dump(map_t*map, FILE*fi, const char*prefix);
221 void map_clear(map_t*map);
222 void map_destroy(map_t*map);
224 void heap_init(heap_t*h,int elem_size, int(*compare)(const void *, const void *));
225 heap_t* heap_new(int elem_size, int(*compare)(const void *, const void *));
226 heap_t* heap_clone(heap_t*o);
227 void heap_clear(heap_t*h);
228 void heap_destroy(heap_t*h);
229 void heap_put(heap_t*h, void*e);
230 int heap_size(heap_t*h);
231 void* heap_peek(heap_t*h);
232 void* heap_chopmax(heap_t*h);
233 void heap_dump(heap_t*h, FILE*fi);
234 void** heap_flatten(heap_t*h);
237 void trie_put(trie_t*t, unsigned const char*id, void*data);
238 char trie_remove(trie_t*t, unsigned const char*id);
239 void*trie_lookup(trie_t*t, unsigned const char*id);
240 int trie_contains(trie_t*t, unsigned const char*id);
241 void trie_remember(trie_t*t);
242 void trie_rollback(trie_t*t);
243 void trie_dump(trie_t*t);
245 array_t* array_new();
246 array_t* array_new2(type_t*type);
247 void array_free(array_t*array);
248 void*array_getkey(array_t*array, int nr);
249 void*array_getvalue(array_t*array, int nr);
250 int array_append(array_t*array, const void*name, void*data);
251 #define array_contains(a,b) (array_find((a),(b))>=0)
252 int array_find(array_t*array, const void*name);
253 int array_find2(array_t*array, const void*name, void*data);
254 int array_update(array_t*array, const void*name, void*data);
255 int array_append_if_new(array_t*array, const void*name, void*data);
256 #define array_length(a) ((a)->num)
258 #define DECLARE(x) struct _##x;typedef struct _##x x##_t;
259 #define DECLARE_LIST(x) \
260 struct _##x##_list { \
262 struct _##x##_list*next; \
264 typedef struct _##x##_list x##_list_t;
265 int list_length_(void*_list);
266 void*list_clone_(void*_list);
267 void list_append_(void*_list, void*entry);
268 void list_prepend_(void*_list, void*entry);
269 void list_free_(void*_list);
270 void list_deep_free_(void*_list);
271 void list_concat_(void*l1, void*l2);
272 #define list_new() ((void*)0)
273 #define list_append(list, e) {sizeof((list)->next);list_append_(&(list),(e));}
274 #define list_concat(l1, l2) {sizeof((l1)->next);sizeof((l2)->next);list_concat_(&(l1),&(l2));}
275 #define list_prepend(list, e) {sizeof((list)->next);list_prepend_(&(list),(e));}
276 #define list_free(list) {sizeof((list)->next);list_free_(&(list));}
277 #define list_deep_free(list) {sizeof((list)->next);list_deep_free_(&(list));}
278 #define list_clone(list) (sizeof((list)->next),(list?list_clone_(&(list)):0))
279 #define list_length(list) (sizeof((list)->next),list_length_(list))