optimized dict implementation
[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
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 /* dynamically growing mem section */
32 typedef struct _mem_t {
33     char*buffer;
34     int len;
35     int pos;
36 } mem_t;
37
38 /* fifo buffered growing mem region */
39 typedef struct _ringbuffer_t
40 {
41     void*internal;
42     int available;
43 } ringbuffer_t;
44
45 /* non-nul terminated string */
46 typedef struct _string_t {
47     const char*str;
48     int len;
49 } string_t;
50
51 /* key/value pairs of strings */
52 typedef struct _map_t {
53     void*internal;
54 } map_t;
55
56 /* (void*)s referenced by strings */
57 typedef struct _dictentry {
58     const char*s;
59     void*data;
60     struct _dictentry*next;
61 } dictentry_t;
62
63 typedef struct _dict {
64     dictentry_t**slots;
65     int hashsize;
66     int num;
67 } dict_t;
68
69 /* array of strings */
70 typedef struct _stringarray_t
71 {
72     void*internal;
73 } stringarray_t;
74
75 /* heap */
76 typedef struct _heap
77 {
78     void**elements;
79     char*data;
80     int elem_size;
81     int size;
82     int max_size;
83     int(*compare)(const void *, const void *);
84 } heap_t;
85
86 void mem_init(mem_t*mem);
87 int mem_put(mem_t*m, void*data, int length);
88 int mem_putstring(mem_t*m, string_t str);
89 void mem_clear(mem_t*mem);
90 void mem_destroy(mem_t*mem);
91
92 void ringbuffer_init(ringbuffer_t*r);
93 void ringbuffer_put(ringbuffer_t*r, void*buf, int size);
94 int ringbuffer_read(ringbuffer_t*r, void*buf, int size);
95 void ringbuffer_clear(ringbuffer_t*r);
96
97 string_t string_new(const char*text, int len);
98 string_t string_new2(const char*text);
99 unsigned int string_hash(string_t*str);
100 unsigned int string_hash2(const char*str);
101 void string_set(string_t*str, const char*text);
102 void string_set2(string_t*str, const char*text, int len);
103 void string_dup(string_t*str, const char*text);
104 void string_dup2(string_t*str, const char*text, int len);
105 int string_equals(string_t*str, const char*text);
106
107 void stringarray_init(stringarray_t*sa, int hashsize);
108 void stringarray_put(stringarray_t*sa, string_t str);
109 char* stringarray_at(stringarray_t*sa, int pos);
110 string_t stringarray_at2(stringarray_t*sa, int pos);
111 int stringarray_find(stringarray_t*sa, string_t*str);
112 void stringarray_clear(stringarray_t*sa);
113 void stringarray_destroy(stringarray_t*sa);
114
115 void map_init(map_t*map);
116 void map_put(map_t*map, string_t t1, string_t t2);
117 const char* map_lookup(map_t*map, const char*name);
118 void map_dump(map_t*map, FILE*fi, const char*prefix);
119 void map_clear(map_t*map);
120 void map_destroy(map_t*map);
121
122 dict_t*dict_new();
123 void dict_init(dict_t*dict);
124 void dict_put(dict_t*dict, string_t t1, void* t2);
125 void dict_put2(dict_t*dict, const char* t1, void* t2);
126 stringarray_t* dict_index(dict_t*dict);
127 int dict_count(dict_t* dict);
128 void* dict_lookup(dict_t*dict, const char*name);
129 void dict_dump(dict_t*dict, FILE*fi, const char*prefix);
130 char dict_del(dict_t*dict, const char* name);
131 void dict_foreach_value(dict_t*h, void (*runFunction)(void*));
132 void dict_free_all(dict_t*h, void (*freeFunction)(void*));
133 void dict_clear(dict_t*dict);
134 void dict_destroy(dict_t*dict);
135
136 void heap_init(heap_t*h,int n,int elem_size, int(*compare)(const void *, const void *));
137 void heap_clear(heap_t*h);
138 void heap_put(heap_t*h, void*e);
139 int heap_size(heap_t*h);
140 void* heap_max(heap_t*h);
141 void* heap_chopmax(heap_t*h);
142 void heap_dump(heap_t*h, FILE*fi);
143 void** heap_flatten(heap_t*h);
144
145 char* strdup_n(const char*str, int size);
146
147 #ifdef __cplusplus
148 }
149 #endif
150
151 #endif //__q_h__