added type_t structure
[swftools.git] / lib / q.c
1 /* q.c
2
3    Part of the swftools package.
4    
5    Copyright (c) 2001,2002,2003,2004 Matthias Kramm <kramm@quiss.org>
6  
7    This program is rfx_free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the rfx_free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the rfx_free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
20
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <assert.h>
26 #include <memory.h>
27 #include "mem.h"
28 #include "types.h"
29 #include "q.h"
30
31 // ------------------------------- malloc, alloc routines ---------------------
32
33 #ifndef STRNDUP
34 char* strdup_n(const char*str, int size)
35 {
36     char*m = (char*)rfx_alloc(size+1);
37     memcpy(m, str, size);
38     m[size] = 0;
39     return m;
40 }
41 #endif
42 char*qstrdup(const char*string)
43 {
44     return strdup(string);
45 }
46 char*qstrndup(const char*string, int len)
47 {
48     return strdup_n(string, len);
49 }
50
51 // ------------------------------- mem_t --------------------------------------
52
53 void mem_init(mem_t*mem)
54 {
55     memset(mem, 0, sizeof(mem_t));
56 }
57 void mem_clear(mem_t*mem)
58 {
59     rfx_free(mem->buffer);mem->buffer = 0;
60 }
61 void mem_destroy(mem_t*mem)
62 {
63     mem_clear(mem);
64     rfx_free(mem);
65 }
66 static int mem_put_(mem_t*m,const void*data, int length, int null)
67 {
68     int n = m->pos;
69     m->pos += length + (null?1:0);
70     if(m->pos > m->len) { 
71         m->len = (m->pos+63)&~63;
72         m->buffer = m->buffer?(char*)rfx_realloc(m->buffer,m->len):(char*)rfx_alloc(m->len);
73     }
74     assert(n+length <= m->len);
75     memcpy(&m->buffer[n], data, length);
76     if(null)
77         m->buffer[n + length] = 0;
78     return n;
79 }
80 int mem_put(mem_t*m,void*data, int length)
81 {
82     return mem_put_(m, data, length, 0);
83 }
84 int mem_putstring(mem_t*m,string_t str)
85 {
86     return mem_put_(m, str.str, str.len, 1);
87 }
88
89 // ------------------------------- ringbuffer_t -------------------------------
90
91 typedef struct _ringbuffer_internal_t
92 {
93     unsigned char*buffer;
94     int readpos;
95     int writepos;
96     int buffersize;
97 } ringbuffer_internal_t;
98
99 void ringbuffer_init(ringbuffer_t*r)
100 {
101     ringbuffer_internal_t*i = (ringbuffer_internal_t*)rfx_calloc(sizeof(ringbuffer_internal_t)); 
102     memset(r, 0, sizeof(ringbuffer_t));
103     r->internal = i;
104     i->buffer = (unsigned char*)rfx_alloc(1024);
105     i->buffersize = 1024;
106 }
107 int ringbuffer_read(ringbuffer_t*r, void*buf, int len)
108 {
109     unsigned char* data = (unsigned char*)buf;
110     ringbuffer_internal_t*i = (ringbuffer_internal_t*)r->internal;
111     if(r->available < len)
112         len = r->available;
113     if(!len)
114         return 0;
115     if(i->readpos + len > i->buffersize) {
116         int read1 = i->buffersize-i->readpos;
117         memcpy(data, &i->buffer[i->readpos], read1);
118         memcpy(&data[read1], &i->buffer[0], len - read1);
119         i->readpos = len - read1;
120     } else {
121         memcpy(data, &i->buffer[i->readpos], len);
122         i->readpos += len;
123         i->readpos %= i->buffersize;
124     }
125     r->available -= len;
126     return len;
127 }
128 void ringbuffer_put(ringbuffer_t*r, void*buf, int len)
129 {
130     unsigned char* data = (unsigned char*)buf;
131     ringbuffer_internal_t*i = (ringbuffer_internal_t*)r->internal;
132     
133     if(i->buffersize - r->available < len)
134     {
135         unsigned char* buf2;
136         int newbuffersize = i->buffersize;
137         int oldavailable = r->available;
138         newbuffersize*=3;newbuffersize/=2; /*grow at least by 50% each time */
139
140         if(newbuffersize < r->available + len)
141             newbuffersize = r->available + len + 1024;
142
143         buf2 = (unsigned char*)rfx_alloc(newbuffersize);
144         ringbuffer_read(r, buf2, r->available);
145         rfx_free(i->buffer);
146         i->buffer = buf2;
147         i->buffersize = newbuffersize;
148         i->readpos = 0;
149         i->writepos = oldavailable;
150         r->available = oldavailable;
151     }
152     if(i->writepos + len > i->buffersize) {
153         int read1 = i->buffersize-i->writepos;
154         memcpy(&i->buffer[i->writepos], data, read1);
155         memcpy(&i->buffer[0], &data[read1], len - read1);
156         i->writepos = len - read1;
157     } else {
158         memcpy(&i->buffer[i->writepos], data, len);
159         i->writepos += len;
160         i->writepos %= i->buffersize;
161     }
162     r->available += len;
163 }
164 void ringbuffer_clear(ringbuffer_t*r)
165 {
166     ringbuffer_internal_t*i = (ringbuffer_internal_t*)r->internal;
167     rfx_free(i->buffer);i->buffer = 0;
168     rfx_free(i);
169 }
170
171 // ------------------------------- heap_t -------------------------------
172
173 void heap_init(heap_t*h,int n,int elem_size, int(*compare)(const void *, const void *))
174 {
175     memset(h, 0, sizeof(heap_t));
176     h->max_size = n;
177     h->size = 0;
178     h->elem_size = elem_size;
179     h->compare = compare;
180     h->elements = (void**)rfx_calloc(n*sizeof(void*));
181     h->data = (char*)rfx_calloc(h->max_size*h->elem_size);
182 }
183 void heap_clear(heap_t*h)
184 {
185     rfx_free(h->elements);
186     rfx_free(h->data);
187 }
188
189 #define HEAP_NODE_SMALLER(h,node1,node2) ((h)->compare((node1),(node2))>0)
190
191 static void up(heap_t*h, int node)
192 {
193     void*node_p = h->elements[node];
194     int parent = node;
195     do {
196         node = parent;
197         if(!node) break;
198         parent = (node-1)/2;
199         h->elements[node] = h->elements[parent];
200     } while(HEAP_NODE_SMALLER(h,h->elements[parent], node_p));
201
202     h->elements[node] = node_p;
203 }
204 static void down(heap_t*h, int node)
205 {
206     void*node_p = h->elements[node];
207     int child = node;
208     do {
209         node = child;
210
211         /* determine new child's position */
212         child = node<<1|1;
213         if(child >= h->size) 
214             break;
215         if(child+1 < h->size && HEAP_NODE_SMALLER(h,h->elements[child],h->elements[child+1])) // search for bigger child
216             child++;
217
218         h->elements[node] = h->elements[child];
219     } while(HEAP_NODE_SMALLER(h,node_p, h->elements[child]));
220     
221     h->elements[node] = node_p;
222 }
223 void heap_put(heap_t*h, void*e) 
224 {
225     int pos = h->size++;
226     memcpy(&h->data[pos*h->elem_size],e,h->elem_size);
227     h->elements[pos] = &h->data[pos];
228     up(h, pos);
229 }
230 int heap_size(heap_t*h)
231 {
232     return h->size;
233 }
234 void* heap_max(heap_t*h)
235 {
236     return h->elements[0];
237 }
238 void* heap_chopmax(heap_t*h)
239 {
240     void*p = h->elements[0];
241     h->elements[0] = h->elements[--h->size];
242     down(h,0);
243     return p;
244 }
245 void heap_dump(heap_t*h, FILE*fi)
246 {
247     int t;
248     for(t=0;t<h->size;t++) {
249         int s;
250         for(s=0;s<=t;s=(s+1)*2-1) {
251             if(s==t) fprintf(fi,"\n");
252         }
253         //fprintf(fi,"%d ", h->elements[t]->x); //?
254     }
255 }
256 void** heap_flatten(heap_t*h)
257 {
258     void**nodes = (void**)rfx_alloc(h->size*sizeof(void*));
259     void**p = nodes;
260    
261     while(h->size) {
262         /*printf("Heap Size: %d\n", h->size);
263         heap_print(stdout, h);
264         printf("\n");*/
265         *p++ = heap_chopmax(h);
266     }
267     return nodes;
268 }
269
270 // ------------------------------- crc32 --------------------------------------
271 static unsigned int*crc32 = 0;
272 static void crc32_init(void)
273 {
274     int t;
275     if(crc32) 
276         return;
277     crc32= (unsigned int*)rfx_alloc(sizeof(unsigned int)*256);
278     for(t=0; t<256; t++) {
279         unsigned int c = t;
280         int s;
281         for (s = 0; s < 8; s++) {
282           c = (0xedb88320L*(c&1)) ^ (c >> 1);
283         }
284         crc32[t] = c;
285     }
286 }
287 // ------------------------------- string_t -----------------------------------
288
289 void string_set2(string_t*str, const char*text, int len)
290 {
291     str->len = len;
292     str->str = text;
293 }
294 void string_set(string_t*str, const char*text)
295 {
296     if(text) {
297         str->len = strlen(text);
298     } else {
299         str->len = 0;
300     }
301     str->str = text;
302 }
303 string_t string_new(const char*text, int len)
304 {
305     string_t s;
306     s.len = len;
307     s.str = text;
308     return s;
309 }
310 string_t string_new2(const char*text)
311 {
312     string_t s;
313     if(text) {
314         s.len = strlen(text);
315     } else {
316         s.len = 0;
317     }
318     s.str = text;
319     return s;
320 }
321 char* string_cstr(string_t*str)
322 {
323     return strdup_n(str->str, str->len);
324 }
325
326 unsigned int crc32_add_byte(unsigned int checksum, unsigned char b) 
327 {
328     if(!crc32)
329         crc32_init();
330     return checksum>>8 ^ crc32[(b^checksum)&0xff];
331 }
332 unsigned int crc32_add_string(unsigned int checksum, const char*s)
333 {
334     while(*s) {
335         checksum = crc32_add_byte(checksum, *s);
336         s++;
337     }
338     return checksum;
339 }
340
341 unsigned int string_hash(const string_t*str)
342 {
343     int t;
344     unsigned int checksum = 0;
345     if(!crc32)
346         crc32_init();
347     for(t=0;t<str->len;t++) {
348         checksum = checksum>>8 ^ crc32[(str->str[t]^checksum)&0xff];
349     }
350     return checksum;
351 }
352 unsigned int string_hash2(const char*str)
353 {
354     unsigned int checksum = 0;
355     const char*p = str;
356     if(!crc32)
357         crc32_init();
358     while(*p) {
359         checksum = checksum>>8 ^ crc32[(*p^checksum)&0xff];
360         p++;
361     }
362     return checksum;
363 }
364 unsigned int string_hash3(const char*str, int len)
365 {
366     string_t s;
367     s.str = str;
368     s.len = len;
369     return string_hash(&s);
370 }
371 void string_dup2(string_t*str, const char*text, int len)
372 {
373     str->len = len;
374     str->str = strdup_n(text, len);
375 }
376 void string_dup(string_t*str, const char*text)
377 {
378     str->len = strlen(text);
379     str->str = strdup(text);
380 }
381 int string_equals(string_t*str, const char*text)
382 {
383     int l = strlen(text);
384     if(str->len == l && !memcmp(str->str, text, l))
385         return 1;
386     return 0;
387 }
388 int string_equals2(string_t*str, string_t*str2)
389 {
390     if(str->len == str2->len && !memcmp(str->str, str2->str, str->len))
391         return 1;
392     return 0;
393 }
394
395 // ------------------------------- stringarray_t ------------------------------
396
397 typedef struct _stringlist {
398     int index;
399     struct _stringlist*next;
400 } stringlist_t;
401
402 typedef struct _stringarray_internal_t
403 {
404     mem_t pos;
405     stringlist_t**hash;
406     int num;
407     int hashsize;
408 } stringarray_internal_t;
409
410 void stringarray_init(stringarray_t*sa, int hashsize)
411 {
412     stringarray_internal_t*s;
413     int t;
414     sa->internal = (stringarray_internal_t*)rfx_calloc(sizeof(stringarray_internal_t)); 
415     s = (stringarray_internal_t*)sa->internal;
416     mem_init(&s->pos);
417     s->hash = rfx_calloc(sizeof(stringlist_t*)*hashsize);
418     s->hashsize = hashsize;
419 }
420 void stringarray_put(stringarray_t*sa, string_t str)
421 {
422     stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
423     int pos;
424     int hash = string_hash(&str) % s->hashsize;
425
426     char*ss = string_cstr(&str);
427     mem_put(&s->pos, &ss, sizeof(char*));
428
429     stringlist_t*l = rfx_alloc(sizeof(stringlist_t));
430     l->index = s->num;
431     l->next = s->hash[hash];
432     s->hash[hash] = l;
433
434     s->num++;
435 }
436 char* stringarray_at(stringarray_t*sa, int pos)
437 {
438     stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
439     char*p;
440     if(pos<0 || pos>=s->num)
441         return 0;
442     p = *(char**)&s->pos.buffer[pos*sizeof(char*)];
443     if(p<0)
444         return 0;
445     return p;
446 }
447 string_t stringarray_at2(stringarray_t*sa, int pos)
448 {
449     string_t s;
450     s.str = stringarray_at(sa, pos);
451     s.len = s.str?strlen(s.str):0;
452     return s;
453 }
454 static stringlist_t* stringlist_del(stringarray_t*sa, stringlist_t*l, int index)
455 {
456     stringlist_t*ll = l;
457     stringlist_t*old = l;
458     while(l) {
459         if(index==l->index) {
460             old->next = l->next;
461             memset(l, 0, sizeof(stringlist_t));
462             rfx_free(l);
463             if(old==l)
464                 return 0;
465             else
466                 return ll;
467         }
468         old = l;
469         l = l->next;
470     }
471     fprintf(stderr, "Internal error: did not find string %d in hash\n", index);
472     return ll;
473 }
474
475 void stringarray_del(stringarray_t*sa, int pos)
476 {
477     stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
478     string_t str = stringarray_at2(sa, pos);
479     int hash = string_hash(&str) % s->hashsize;
480     s->hash[hash] = stringlist_del(sa, s->hash[hash], pos);
481     *(char**)&s->pos.buffer[pos*sizeof(char*)] = 0;
482 }
483 int stringarray_find(stringarray_t*sa, string_t* str)
484 {
485     stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
486     int hash = string_hash(str) % s->hashsize;
487     int t;
488     stringlist_t*l = s->hash[hash];
489     //TODO: statistics
490     while(l) {
491         string_t s = stringarray_at2(sa, l->index);
492         if(string_equals2(str, &s)) {
493             return l->index;
494         }
495         l = l->next;
496     }
497     return -1;
498 }
499 void stringarray_clear(stringarray_t*sa)
500 {
501     stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
502     mem_clear(&s->pos);
503     int t;
504     for(t=0;t<s->hashsize;t++) {
505         stringlist_t*l = s->hash[t];
506         while(l) {
507             stringlist_t*next = l->next;
508             memset(l, 0, sizeof(stringlist_t));
509             rfx_free(l);
510             l = next;
511         }
512     }
513     rfx_free(s->hash);s->hash=0;
514     rfx_free(s);
515 }
516 void stringarray_destroy(stringarray_t*sa)
517 {
518     stringarray_clear(sa);
519     rfx_free(sa);
520 }
521
522 // ------------------------------- type_t -------------------------------
523
524 char charptr_equals(const void*o1, const void*o2) 
525 {
526     return !strcmp(o1,o2);
527 }
528 unsigned int charptr_hash(const void*o) 
529 {
530     return string_hash2(o);
531 }
532 void* charptr_dup(const void*o) 
533 {
534     return strdup(o);
535 }
536 void charptr_free(void*o) 
537 {
538     rfx_free(o);
539 }
540 char stringstruct_equals(const void*o1, const void*o2) 
541 {
542     string_t*s1 = (string_t*)o1;
543     string_t*s2 = (string_t*)o2;
544     int l = s1->len<s2->len?s1->len:s2->len;
545     int r = memcmp(s1->str, s2->str, l);
546     if(r)
547         return 0;
548     else
549         return s1->len==s2->len;
550 }
551 unsigned int stringstruct_hash(const void*o) 
552 {
553     return string_hash(o);
554 }
555 void*stringstruct_dup(const void*o) 
556 {
557     string_t*s = malloc(sizeof(string_t));
558     string_set2(s, ((string_t*)o)->str, ((string_t*)o)->len);
559     return s;
560 }
561 void stringstruct_free(void*o) 
562 {
563     rfx_free((void*)(((string_t*)o)->str));
564     rfx_free((void*)o);
565 }
566
567
568 type_t charptr_type = {
569     equals: charptr_equals,
570     hash: charptr_hash,
571     dup: charptr_dup,
572     free: charptr_free,
573 };
574
575 type_t stringstruct_type = {
576     equals: stringstruct_equals,
577     hash: stringstruct_hash,
578     dup: stringstruct_dup,
579     free: stringstruct_free,
580 };
581
582 // ------------------------------- dictionary_t -------------------------------
583
584 #define INITIAL_SIZE 1
585
586 static int max(int x, int y) {
587     return x>y?x:y;
588 }
589
590 dict_t*dict_new()
591 {
592     dict_t*d = rfx_alloc(sizeof(dict_t));
593     dict_init(d);
594     return d;
595 }
596 dict_t*dict_new2(type_t*t)
597 {
598     dict_t*d = rfx_alloc(sizeof(dict_t));
599     dict_init(d);
600     d->key_type = t;
601     return d;
602 }
603 void dict_init(dict_t*h) 
604 {
605     memset(h, 0, sizeof(dict_t));
606     h->hashsize = INITIAL_SIZE;
607     h->slots = h->hashsize?(dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*h->hashsize):0;
608     h->num = 0;
609     h->key_type = &charptr_type;
610 }
611
612 static void dict_expand(dict_t*h, int newlen)
613 {
614     assert(h->hashsize < newlen);
615     dictentry_t**newslots = (dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*newlen);
616     int t; 
617     for(t=0;t<h->hashsize;t++) {
618         dictentry_t*e = h->slots[t];
619         while(e) {
620             dictentry_t*next = e->next;
621             unsigned int newhash = e->hash%newlen;
622             e->next = newslots[newhash];
623             newslots[newhash] = e;
624             e = next;
625         }
626     }
627     if(h->slots)
628         rfx_free(h->slots);
629     h->slots = newslots;
630     h->hashsize = newlen;
631 }
632
633 dictentry_t* dict_put(dict_t*h, const void*key, void* data)
634 {
635     unsigned int hash = h->key_type->hash(key);
636     dictentry_t*e = (dictentry_t*)rfx_alloc(sizeof(dictentry_t));
637     unsigned int hash2 = hash % h->hashsize;
638     
639     e->key = h->key_type->dup(key);
640     e->hash = hash; //for resizing
641     e->next = h->slots[hash2];
642     e->data = data;
643     h->slots[hash2] = e;
644     h->num++;
645     return e;
646 }
647 void dict_put2(dict_t*h, const char*s, void*data) 
648 {
649     assert(h->key_type == &charptr_type);
650     dict_put(h, s, data);
651 }
652 void dict_dump(dict_t*h, FILE*fi, const char*prefix)
653 {
654     int t;
655     for(t=0;t<h->hashsize;t++) {
656         dictentry_t*e = h->slots[t];
657         while(e) {
658             if(h->key_type==&charptr_type) {
659                 fprintf(fi, "%s%08x=%08x\n", prefix, e->key, e->data);
660             } else {
661                 fprintf(fi, "%s%s=%08x\n", prefix, e->key, e->data);
662             }
663             e = e->next;
664         }
665     }
666 }
667
668 int dict_count(dict_t*h)
669 {
670     return h->num;
671 }
672
673 void* dict_lookup(dict_t*h, const void*key)
674 {
675     if(!h->num)
676         return 0;
677     
678     unsigned int ohash = h->key_type->hash(key);
679     unsigned int hash = ohash % h->hashsize;
680
681     /* check first entry for match */
682     dictentry_t*e = h->slots[hash];
683     if(e && h->key_type->equals(e->key, key)) {
684         return e->data;
685     } else if(e) {
686         e = e->next;
687     }
688
689     /* if dict is 2/3 filled, double the size. Do
690        this the first time we have to actually iterate
691        through a slot to find our data */
692     if(e && h->num*3 >= h->hashsize*2) {
693         int newsize = h->hashsize;
694         while(h->num*3 >= newsize*2) {
695             newsize = newsize<15?15:(newsize+1)*2-1;
696         }
697         dict_expand(h, newsize);
698         hash = ohash % h->hashsize;
699         e = h->slots[hash];
700     }
701
702     /* check subsequent entries for a match */
703     while(e) {
704         if(h->key_type->equals(e->key, key)) {
705             return e->data;
706         }
707         e = e->next;
708     }
709     return 0;
710 }
711 char dict_del(dict_t*h, const void*key)
712 {
713     if(!h->num)
714         return 0;
715     unsigned int hash = h->key_type->hash(key) % h->hashsize;
716     dictentry_t*head = h->slots[hash];
717     dictentry_t*e = head, *prev=0;
718     while(e) {
719         if(h->key_type->equals(e->key, key)) {
720             dictentry_t*next = e->next;
721             rfx_free((void*)e->key);
722             memset(e, 0, sizeof(dictentry_t));
723             rfx_free(e);
724             if(e == head) {
725                 h->slots[hash] = 0;
726             } else {
727                 assert(prev);
728                 prev->next = next;
729             }
730             h->num--;
731             return 1;
732         }
733         prev = e;
734         e = e->next;
735     }
736     return 0;
737 }
738
739 static dictentry_t* dict_get_slot(dict_t*h, const void*key)
740 {
741     if(!h->num)
742         return 0;
743     unsigned int ohash = h->key_type->hash(key);
744     unsigned int hash = ohash % h->hashsize;
745     return h->slots[hash];
746 }
747
748 void dict_foreach_keyvalue(dict_t*h, void (*runFunction)(void*data, const void*key, void*val), void*data)
749 {
750     int t;
751     for(t=0;t<h->hashsize;t++) {
752         dictentry_t*e = h->slots[t];
753         while(e) {
754             dictentry_t*next = e->next;
755             if(runFunction) {
756                 runFunction(data, e->key, e->data);
757             }
758             e = e->next;
759         }
760     }
761 }
762 void dict_foreach_value(dict_t*h, void (*runFunction)(void*))
763 {
764     int t;
765     for(t=0;t<h->hashsize;t++) {
766         dictentry_t*e = h->slots[t];
767         while(e) {
768             dictentry_t*next = e->next;
769             if(runFunction) {
770                 runFunction(e->data);
771             }
772             e = e->next;
773         }
774     }
775 }
776
777 void dict_free_all(dict_t*h, void (*freeFunction)(void*))
778 {
779     int t;
780     for(t=0;t<h->hashsize;t++) {
781         dictentry_t*e = h->slots[t];
782         while(e) {
783             dictentry_t*next = e->next;
784             h->key_type->free(e->key);
785             if(freeFunction) {
786                 freeFunction(e->data);
787             }
788             memset(e, 0, sizeof(dictentry_t));
789             rfx_free(e);
790             e = next;
791         }
792     }
793     rfx_free(h->slots);
794     memset(h, 0, sizeof(dict_t));
795 }
796
797 void dict_clear(dict_t*h) 
798 {
799     dict_free_all(h, 0);
800 }
801
802 void dict_destroy(dict_t*dict)
803 {
804     dict_clear(dict);
805     rfx_free(dict);
806 }
807
808 // ------------------------------- map_t --------------------------------------
809
810 typedef struct _map_internal_t
811 {
812     dict_t d;
813 } map_internal_t;
814
815 void map_init(map_t*map)
816 {
817     map_internal_t*m;
818     map->internal = (map_internal_t*)rfx_calloc(sizeof(map_internal_t));
819     m = (map_internal_t*)map->internal;
820     dict_init(&m->d);
821 }
822 void map_put(map_t*map, string_t t1, string_t t2)
823 {
824     map_internal_t*m = (map_internal_t*)map->internal;
825     string_t s;
826     char* s1 = string_cstr(&t1);
827     dict_put2(&m->d, s1, (void*)string_cstr(&t2));
828     rfx_free(s1);
829 }
830 const char* map_lookup(map_t*map, const char*name)
831 {
832     map_internal_t*m = (map_internal_t*)map->internal;
833     const char*value = dict_lookup(&m->d, name);
834     return value;
835 }
836 static void freestring(void*data)
837 {
838     rfx_free(data);
839 }
840 static void dumpmapentry(void*data, const void*key, void*value)
841 {
842     FILE*fi = (FILE*)data;
843     fprintf(fi, "%s=%s\n", key, (char*)value);
844 }
845 void map_dump(map_t*map, FILE*fi, const char*prefix)
846 {
847     int t;
848     map_internal_t*m = (map_internal_t*)map->internal;
849     dict_foreach_keyvalue(&m->d, dumpmapentry, fi);
850 }
851 void map_clear(map_t*map)
852 {
853     map_internal_t*m = (map_internal_t*)map->internal;
854     dict_free_all(&m->d, freestring);
855     rfx_free(m);
856 }
857 void map_destroy(map_t*map)
858 {
859     map_clear(map);
860     rfx_free(map);
861 }
862
863 // ------------------------------- array_t --------------------------------------
864
865 array_t* array_new() {
866     array_t*d = malloc(sizeof(array_t));
867     memset(d, 0, sizeof(array_t));
868     d->entry2pos = dict_new();
869     return d;
870 }
871 array_t* array_new2(type_t*type) {
872     array_t*d = malloc(sizeof(array_t));
873     memset(d, 0, sizeof(array_t));
874     d->entry2pos = dict_new2(type);
875     return d;
876 }
877 void*array_getkey(array_t*array, int nr) {
878     if(nr > array->num || nr<0) {
879         printf("error: reference to element %d in array[%d]\n", nr, array->num);
880         *(int*)0 = 0xdead;
881         return 0;
882     }
883     return array->d[nr].name;
884 }
885 void*array_getvalue(array_t*array, int nr) {
886     if(nr > array->num || nr<0) {
887         printf("error: reference to element %d in array[%d]\n", nr, array->num);
888         *(int*)0 = 0xdead;
889         return 0;
890     }
891     return array->d[nr].data;
892 }
893 int array_append(array_t*array, const void*name, void*data) {
894     while(array->size <= array->num) {
895         array->size += 64;
896         if(!array->d) {
897             array->d = malloc(sizeof(array_entry_t)*array->size);
898         } else {
899             array->d = realloc(array->d, sizeof(array_entry_t)*array->size);
900         }
901     }
902
903     dictentry_t*e = dict_put(array->entry2pos, name, (void*)(ptroff_t)(array->num+1));
904
905     if(name) {
906         array->d[array->num].name = e->key;
907     } else {
908         array->d[array->num].name = 0;
909     }
910     array->d[array->num].data = (void*)data;
911     return array->num++;
912 }
913 int array_find(array_t*array, const void*name)
914 {
915     int pos = (int)(ptroff_t)dict_lookup(array->entry2pos, name);
916     return pos-1;
917 }
918 int array_find2(array_t*array, const void*name, void*data)
919 {
920     dict_t*h= array->entry2pos;
921     dictentry_t*e = dict_get_slot(array->entry2pos, name);
922
923     while(e) {
924         int index = ((int)(ptroff_t)e->data) - 1;
925         if(h->key_type->equals(e->key, name) && array->d[index].data == data) {
926             return index;
927         }
928         e = e->next;
929     }
930     return -1;
931 }
932 int array_update(array_t*array, const void*name, void*data) {
933     int pos = array_find(array, name);
934     if(pos>=0) {
935         array->d[pos].data = data;
936         return pos;
937     }
938     return array_append(array, name, data);
939 }
940 int array_append_if_new(array_t*array, const void*name, void*data) {
941     int pos = array_find(array, name);
942     if(pos>=0)
943         return pos;
944     return array_append(array, name, data);
945 }
946 void array_free(array_t*array) {
947     dict_destroy(array->entry2pos);
948     if(array->d) {
949         free(array->d);array->d = 0;
950     }
951     free(array);
952 }
953
954 // ------------------------------- list_t --------------------------------------
955
956 struct _commonlist;
957 typedef struct _listinfo {
958     int size;
959     struct _commonlist*last;
960 } listinfo_t;
961
962 typedef struct _commonlist {
963     void*entry;
964     struct _commonlist*next;
965     listinfo_t info[0];
966 } commonlist_t;
967
968 int list_length(void*_list)
969 {
970     commonlist_t*l = (commonlist_t*)_list;
971     if(!l)
972         return 0;
973     return l->info[0].size;
974 }
975 void list_append_(void*_list, void*entry)
976 {
977     commonlist_t**list = (commonlist_t**)_list;
978     commonlist_t* n = 0;
979     if(!*list) {
980         n = malloc(sizeof(commonlist_t)+sizeof(listinfo_t));
981         *list = n;
982         (*list)->info[0].size = 0;
983     } else {
984         n = malloc(sizeof(commonlist_t));
985         (*list)->info[0].last->next = n;
986     }
987     n->next = 0;
988     n->entry = entry;
989     (*list)->info[0].last = n;
990     (*list)->info[0].size++;
991 }
992 void list_free_(void*_list) 
993 {
994     commonlist_t**list = (commonlist_t**)_list;
995     commonlist_t*l = *list;
996     while(l) {
997         commonlist_t*next = l->next;
998         free(l);
999         l = next;
1000     }
1001     *list = 0;
1002 }
1003 void*list_clone_(void*_list) 
1004 {
1005     commonlist_t*l = *(commonlist_t**)_list;
1006
1007     void*dest = 0;
1008     while(l) {
1009         commonlist_t*next = l->next;
1010         list_append_(&dest, l->entry);
1011         l = next;
1012     }
1013     return dest;
1014
1015 }