2be015f8ef172c1a784b55922e8f4b92d7de2da5
[swftools.git] / lib / q.h
1 /* q.h
2    Header file for q.c.
3
4    Part of the swftools package.
5    
6    Copyright (c) 2001,2002,2003,2004 Matthias Kramm <kramm@quiss.org>
7  
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.
12
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.
17
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 */
21
22 #ifndef __q_h__
23 #define __q_h__
24
25 #include <stdio.h>
26 #include "mem.h"
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 #define NEW(t,y) t*y = (t*)rfx_calloc(sizeof(t));
33
34 /* dynamically growing mem section */
35 typedef struct _mem_t {
36     char*buffer;
37     int len;
38     int pos;
39     int read_pos;
40 } mem_t;
41
42 /* fifo buffered growing mem region */
43 typedef struct _ringbuffer_t
44 {
45     void*internal;
46     int available;
47 } ringbuffer_t;
48
49 /* non-nul terminated string */
50 typedef struct _string_t {
51     const char*str;
52     int len;
53 } string_t;
54
55 /* key/value pairs of strings */
56 typedef struct _map_t {
57     void*internal;
58 } map_t;
59
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);
65
66 typedef struct _type_t {
67     equals_func equals;
68     hash_func hash;
69     dup_func dup;
70     free_func free;
71 } type_t;
72
73 extern type_t charptr_type;
74 extern type_t stringstruct_type;
75 extern type_t ptr_type;
76 extern type_t int_type;
77
78 typedef struct _dictentry {
79     void*key;
80     unsigned int hash;
81     void*data;
82     struct _dictentry*next;
83 } dictentry_t;
84
85 /* (void*) pointers referenced by strings */
86 typedef struct _dict {
87     dictentry_t**slots;
88     type_t*key_type;
89     int hashsize;
90     int num;
91 } dict_t;
92
93 /* array of key/value pairs, with fast lookup */
94 typedef struct _array_entry {
95     void*name;
96     void*data;
97 } array_entry_t;
98
99 typedef struct _array {
100     int num;
101     int size;
102     array_entry_t*d;
103     dict_t*entry2pos;
104 } array_t;
105
106 /* array of strings, string<->int mapping,
107    with O(1) for int->string lookup and
108         ~O(n/hashsize) for string->int lookup */
109 typedef struct _stringarray_t
110 {
111     void*internal;
112 } stringarray_t;
113
114 /* heap */
115 typedef struct _heap {
116     void**elements;
117     char*data;
118     int elem_size;
119     int size;
120     int max_size;
121     int(*compare)(const void *, const void *);
122 } heap_t;
123
124 /* trie (with rollback) */
125 typedef struct _trielayer {
126     struct _trielayer*row[256];
127     unsigned char*rest;
128     void*data;
129 } trielayer_t;
130
131 typedef struct _trie {
132     trielayer_t* start;
133     void*rollback;
134 } trie_t;
135
136 /* move to front list structure */
137 typedef struct _mtf_item {
138     const void*key;
139     int num;
140     struct _mtf_item*next;
141 } mtf_item_t;
142
143 typedef struct _mtf {
144     mtf_item_t*first;
145     type_t*type;
146 } mtf_t;
147
148 char* strdup_n(const char*str, int size);
149 char* allocprintf(const char*str, ...);
150
151 float medianf(float*values, int n);
152
153 unsigned int crc32_add_byte(unsigned int crc32, unsigned char b);
154 unsigned int crc32_add_string(unsigned int crc32, const char*s);
155
156 void mem_init(mem_t*mem);
157 int mem_put(mem_t*m, void*data, int length);
158 int mem_putstring(mem_t*m, string_t str);
159 int mem_get(mem_t*m, void*data, int length);
160 void mem_clear(mem_t*mem);
161 void mem_destroy(mem_t*mem);
162
163 void ringbuffer_init(ringbuffer_t*r);
164 void ringbuffer_put(ringbuffer_t*r, void*buf, int size);
165 int ringbuffer_read(ringbuffer_t*r, void*buf, int size);
166 void ringbuffer_clear(ringbuffer_t*r);
167
168 /* old style functions- should be renamed */
169 string_t string_new(const char*text, int len);
170 string_t string_new2(const char*text);
171 void string_dup(string_t*str, const char*text);
172 void string_dup2(string_t*str, const char*text, int len);
173
174 char* string_cstr(string_t*str);
175 char* string_escape(string_t*str);
176 string_t* string_new3(const char*text, int len);
177 string_t* string_new4(const char*text);
178 void string_free(string_t*s);
179 unsigned int string_hash(const string_t*str);
180 unsigned int string_hash2(const char*str);
181 unsigned int string_hash3(const char*str, int len);
182 void string_set(string_t*str, const char*text);
183 void string_set2(string_t*str, const char*text, int len);
184 string_t*string_dup3(string_t*s);
185 int string_equals(string_t*str, const char*text);
186
187 void stringarray_init(stringarray_t*sa, int hashsize);
188 void stringarray_put(stringarray_t*sa, string_t str);
189
190 char* stringarray_at(stringarray_t*sa, int pos);
191 string_t stringarray_at2(stringarray_t*sa, int pos);
192 int stringarray_find(stringarray_t*sa, string_t*str);
193 void stringarray_clear(stringarray_t*sa);
194 void stringarray_destroy(stringarray_t*sa);
195
196 dict_t*dict_new();
197 dict_t*dict_new2(type_t*type);
198 void dict_init(dict_t*dict, int size);
199 void dict_init2(dict_t*dict, type_t*type, int size);
200 dictentry_t*dict_put(dict_t*h, const void*key, void* data);
201 void dict_put2(dict_t*h, const char*s, void*data);
202 int dict_count(dict_t*h);
203 void dict_dump(dict_t*h, FILE*fi, const char*prefix);
204 dictentry_t* dict_get_slot(dict_t*h, const void*key);
205 char dict_contains(dict_t*h, const void*s);
206 void* dict_lookup(dict_t*h, const void*s);
207 char dict_del(dict_t*h, const void*s);
208 char dict_del2(dict_t*h, const void*key, void*data);
209 dict_t*dict_clone(dict_t*);
210
211 void dict_foreach_keyvalue(dict_t*h, void (*runFunction)(void*data, const void*key, void*val), void*data);
212 void dict_foreach_value(dict_t*h, void (*runFunction)(void*));
213 void dict_free_all(dict_t*h, char free_keys, void (*free_data_function)(void*));
214 void dict_clear(dict_t*h);
215 void dict_destroy_shallow(dict_t*dict);
216 void dict_destroy(dict_t*dict);
217 #define DICT_ITERATE_DATA(d,t,v) \
218     int v##_i;dictentry_t*v##_e;t v;\
219     for(v##_i=0;v##_i<(d)->hashsize;v##_i++) \
220         for(v##_e=(d)->slots[v##_i]; v##_e && ((v=(t)v##_e->data)||1); v##_e=v##_e->next)
221 #define DICT_ITERATE_KEY(d,t,v)  \
222     int v##_i;dictentry_t*v##_e;t v;\
223     for(v##_i=0;v##_i<(d)->hashsize;v##_i++) \
224         for(v##_e=(d)->slots[v##_i];v##_e && ((v=(t)v##_e->key)||1);v##_e=v##_e->next)
225 #define DICT_ITERATE_ITEMS(d,t1,v1,t2,v2) \
226     int v1##_i;dictentry_t*v1##_e;t1 v1;t2 v2; \
227     for(v1##_i=0;v1##_i<(d)->hashsize;v1##_i++) \
228         for(v1##_e=(d)->slots[v1##_i]; v1##_e && (((v1=(t1)v1##_e->key)||1)&&((v2=(t2)v1##_e->data)||1)); v1##_e=v1##_e->next)
229
230 void map_init(map_t*map);
231 void map_put(map_t*map, string_t t1, string_t t2);
232 const char* map_lookup(map_t*map, const char*name);
233 void map_dump(map_t*map, FILE*fi, const char*prefix);
234 void map_clear(map_t*map);
235 void map_destroy(map_t*map);
236
237 void heap_init(heap_t*h,int elem_size, int(*compare)(const void *, const void *));
238 heap_t* heap_new(int elem_size, int(*compare)(const void *, const void *));
239 heap_t* heap_clone(heap_t*o);
240 void heap_clear(heap_t*h);
241 void heap_destroy(heap_t*h);
242 void heap_put(heap_t*h, void*e);
243 int heap_size(heap_t*h);
244 void* heap_peek(heap_t*h);
245 void* heap_chopmax(heap_t*h);
246 void heap_dump(heap_t*h, FILE*fi);
247 void** heap_flatten(heap_t*h);
248
249 trie_t*trie_new();
250 void trie_put(trie_t*t, unsigned const char*id, void*data);
251 char trie_remove(trie_t*t, unsigned const char*id);
252 void*trie_lookup(trie_t*t, unsigned const char*id);
253 int trie_contains(trie_t*t, unsigned const char*id);
254 void trie_remember(trie_t*t);
255 void trie_rollback(trie_t*t);
256 void trie_dump(trie_t*t);
257
258 mtf_t* mtf_new(type_t*type);
259 void mtf_increase(mtf_t*m, const void*key);
260 void mtf_destroy(mtf_t*m);
261
262 array_t* array_new();
263 array_t* array_new2(type_t*type);
264 void array_free(array_t*array);
265 void*array_getkey(array_t*array, int nr);
266 void*array_getvalue(array_t*array, int nr);
267 int array_append(array_t*array, const void*name, void*data);
268 #define array_contains(a,b) (array_find((a),(b))>=0)
269 int array_find(array_t*array, const void*name);
270 int array_find2(array_t*array, const void*name, void*data);
271 int array_update(array_t*array, const void*name, void*data);
272 int array_append_if_new(array_t*array, const void*name, void*data);
273 #define array_length(a) ((a)->num)
274
275 #define DECLARE(x) struct _##x;typedef struct _##x x##_t;
276 #define DECLARE_LIST(x) \
277 struct _##x##_list { \
278     struct _##x* x; \
279     struct _##x##_list*next; \
280 }; \
281 typedef struct _##x##_list x##_list_t;
282 int list_length_(void*_list);
283 void*list_clone_(void*_list);
284 void list_append_(void*_list, void*entry);
285 void list_prepend_(void*_list, void*entry);
286 void list_free_(void*_list);
287 void list_deep_free_(void*_list);
288 void list_concat_(void*l1, void*l2);
289 #define list_new() ((void*)0)
290 #define list_append(list, e) {sizeof((list)->next);list_append_(&(list),(e));}
291 #define list_concat(l1, l2) {sizeof((l1)->next);sizeof((l2)->next);list_concat_(&(l1),&(l2));}
292 #define list_prepend(list, e) {sizeof((list)->next);list_prepend_(&(list),(e));}
293 #define list_free(list) {sizeof((list)->next);list_free_(&(list));}
294 #define list_deep_free(list) {sizeof((list)->next);list_deep_free_(&(list));}
295 #define list_clone(list) (sizeof((list)->next),(list?list_clone_(&(list)):0))
296 #define list_length(list) (sizeof((list)->next),list_length_(list))
297
298 #ifdef __cplusplus
299 }
300 #endif
301
302 #endif //__q_h__