3 Part of the swftools package.
5 Copyright (c) 2001,2002,2003,2004 Matthias Kramm <kramm@quiss.org>
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.
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.
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 */
32 // ------------------------------- malloc, alloc routines ---------------------
35 char* strdup_n(const char*str, int size)
37 char*m = (char*)rfx_alloc(size+1);
43 char*qstrdup(const char*string)
45 return strdup(string);
47 char*qstrndup(const char*string, int len)
49 return strdup_n(string, len);
51 char* allocprintf(const char*format, ...)
54 va_start(arglist1, format);
56 int l = vsnprintf(&dummy, 1, format, arglist1);
60 va_start(arglist2, format);
61 char*buf = malloc(l+1);
62 vsnprintf(buf, l+1, format, arglist2);
67 // ------------------------------- mem_t --------------------------------------
69 void mem_init(mem_t*mem)
71 memset(mem, 0, sizeof(mem_t));
73 void mem_clear(mem_t*mem)
75 rfx_free(mem->buffer);mem->buffer = 0;
77 void mem_destroy(mem_t*mem)
82 static int mem_put_(mem_t*m,const void*data, int length, int null)
85 m->pos += length + (null?1:0);
87 int v1 = (m->pos+63)&~63;
88 int v2 = m->len + m->len / 2;
90 m->buffer = m->buffer?(char*)rfx_realloc(m->buffer,m->len):(char*)rfx_alloc(m->len);
92 assert(n+length <= m->len);
93 memcpy(&m->buffer[n], data, length);
95 m->buffer[n + length] = 0;
98 int mem_put(mem_t*m,void*data, int length)
100 return mem_put_(m, data, length, 0);
102 int mem_putstring(mem_t*m,string_t str)
104 return mem_put_(m, str.str, str.len, 1);
106 int mem_get(mem_t*m, void*data, int length)
108 if(m->read_pos + length > m->pos) {
109 length = m->pos - m->read_pos;
111 memcpy(data, m->buffer+m->read_pos, length);
112 m->read_pos += length;
116 // ------------------------------- median -------------------------------------
118 float medianf(float*a, int n)
146 // ------------------------------- ringbuffer_t -------------------------------
148 typedef struct _ringbuffer_internal_t
150 unsigned char*buffer;
154 } ringbuffer_internal_t;
156 void ringbuffer_init(ringbuffer_t*r)
158 ringbuffer_internal_t*i = (ringbuffer_internal_t*)rfx_calloc(sizeof(ringbuffer_internal_t));
159 memset(r, 0, sizeof(ringbuffer_t));
161 i->buffer = (unsigned char*)rfx_alloc(1024);
162 i->buffersize = 1024;
164 int ringbuffer_read(ringbuffer_t*r, void*buf, int len)
166 unsigned char* data = (unsigned char*)buf;
167 ringbuffer_internal_t*i = (ringbuffer_internal_t*)r->internal;
168 if(r->available < len)
172 if(i->readpos + len > i->buffersize) {
173 int read1 = i->buffersize-i->readpos;
174 memcpy(data, &i->buffer[i->readpos], read1);
175 memcpy(&data[read1], &i->buffer[0], len - read1);
176 i->readpos = len - read1;
178 memcpy(data, &i->buffer[i->readpos], len);
180 i->readpos %= i->buffersize;
185 void ringbuffer_put(ringbuffer_t*r, void*buf, int len)
187 unsigned char* data = (unsigned char*)buf;
188 ringbuffer_internal_t*i = (ringbuffer_internal_t*)r->internal;
190 if(i->buffersize - r->available < len)
193 int newbuffersize = i->buffersize;
194 int oldavailable = r->available;
195 newbuffersize*=3;newbuffersize/=2; /*grow at least by 50% each time */
197 if(newbuffersize < r->available + len)
198 newbuffersize = r->available + len + 1024;
200 buf2 = (unsigned char*)rfx_alloc(newbuffersize);
201 ringbuffer_read(r, buf2, r->available);
204 i->buffersize = newbuffersize;
206 i->writepos = oldavailable;
207 r->available = oldavailable;
209 if(i->writepos + len > i->buffersize) {
210 int read1 = i->buffersize-i->writepos;
211 memcpy(&i->buffer[i->writepos], data, read1);
212 memcpy(&i->buffer[0], &data[read1], len - read1);
213 i->writepos = len - read1;
215 memcpy(&i->buffer[i->writepos], data, len);
217 i->writepos %= i->buffersize;
221 void ringbuffer_clear(ringbuffer_t*r)
223 ringbuffer_internal_t*i = (ringbuffer_internal_t*)r->internal;
224 rfx_free(i->buffer);i->buffer = 0;
228 // ------------------------------- heap_t -------------------------------
230 void heap_init(heap_t*h,int elem_size, int(*compare)(const void *, const void *))
232 memset(h, 0, sizeof(heap_t));
234 h->elem_size = elem_size;
235 h->compare = compare;
239 heap_t* heap_new(int elem_size, int(*compare)(const void *, const void *))
241 heap_t*h = malloc(sizeof(heap_t));
242 heap_init(h, elem_size, compare);
245 heap_t* heap_clone(heap_t*o)
247 heap_t*h = malloc(sizeof(heap_t));
248 memcpy(h, o, sizeof(heap_t));
249 h->elements = rfx_alloc(sizeof(void*)*h->size);
251 for(t=0;t<h->size;t++) {
252 h->elements[t] = rfx_alloc(h->elem_size);
253 memcpy(h->elements[t], o->elements[t], h->elem_size);
257 void heap_clear(heap_t*h)
260 for(t=0;t<h->size;t++) {
261 rfx_free(h->elements[t]);
264 rfx_free(h->elements);
266 void heap_destroy(heap_t*h)
272 #define HEAP_NODE_LARGER(h,node1,node2) ((h)->compare((node1),(node2))>0)
273 #define HEAP_NODE_SMALLER(h,node1,node2) ((h)->compare((node1),(node2))<0)
275 static void up(heap_t*h, int node)
277 void*node_p = h->elements[node];
284 h->elements[node] = h->elements[parent];
285 } while(HEAP_NODE_SMALLER(h, h->elements[parent], node_p));
286 h->elements[node] = node_p;
288 static void down(heap_t*h, int node)
290 void*node_p = h->elements[node];
295 /* determine new child's position */
299 if(child+1 < h->size && HEAP_NODE_SMALLER(h,h->elements[child],h->elements[child+1])) // search for bigger child
302 h->elements[node] = h->elements[child];
303 } while(HEAP_NODE_SMALLER(h,node_p, h->elements[child]));
305 h->elements[node] = node_p;
307 void heap_put(heap_t*h, void*e)
310 void*data = rfx_alloc(h->elem_size);
311 memcpy(data,e,h->elem_size);
313 if(pos>=h->max_size) {
314 h->max_size = h->max_size<15?15:(h->max_size+1)*2-1;
315 h->elements = (void**)rfx_realloc(h->elements, h->max_size*sizeof(void*));
316 assert(pos<h->max_size);
319 h->elements[pos] = data;
322 int heap_size(heap_t*h)
326 void* heap_peek(heap_t*h)
330 return h->elements[0];
332 void* heap_chopmax(heap_t*h)
336 void*p = h->elements[0];
337 h->elements[0] = h->elements[--h->size];
341 void heap_dump(heap_t*h, FILE*fi)
344 for(t=0;t<h->size;t++) {
346 for(s=0;s<=t;s=(s+1)*2-1) {
347 if(s==t) fprintf(fi,"\n");
349 //fprintf(fi,"%d ", h->elements[t]->x); //?
352 void** heap_flatten(heap_t*h)
354 void**nodes = (void**)rfx_alloc((h->size+1)*sizeof(void*));
358 /*printf("Heap Size: %d\n", h->size);
359 heap_print(stdout, h);
361 *p++ = heap_chopmax(h);
367 // ------------------------------- trie --------------------------------------
371 return (trie_t*)rfx_calloc(sizeof(trie_t));
373 static char _trie_put(trielayer_t**t, unsigned const char*id, void*data)
376 (*t) = rfx_calloc(sizeof(trielayer_t));
377 (*t)->rest = (unsigned char*)strdup((char*)id);
381 if((*t)->rest && (*t)->rest[0]) {
382 // make room: shift whatever's currently in here one node down
383 _trie_put(&(*t)->row[(*t)->rest[0]], (*t)->rest+1, (*t)->data);
387 return _trie_put(&(*t)->row[id[0]], id+1, data);
392 (*t)->rest = (unsigned char*)strdup("");
397 static char _trie_remove(trielayer_t*t, unsigned const char*id)
400 if(t->rest && !strcmp((char*)t->rest, (char*)id)) {
412 static void trie_rollback_removes(trie_t*t, unsigned const char*id, void*data);
413 static void trie_rollback_adds(trie_t*t, unsigned const char*id, void*data);
415 void trie_put(trie_t*t, unsigned const char*id, void*data)
418 _trie_put(&t->start, id, data);
420 char contains = trie_contains(t, id);
421 void*olddata = contains?trie_lookup(t, id):0;
422 _trie_put(&t->start, id, data);
424 trie_rollback_adds(t, id, olddata);
426 trie_rollback_removes(t, id, data);
429 char trie_remove(trie_t*t, unsigned const char*id)
432 return _trie_remove(t->start, id);
434 void*olddata = trie_lookup(t, id);
435 char exists = _trie_remove(t->start, id);
437 trie_rollback_adds(t, id, olddata);
442 int trie_contains(trie_t*trie, unsigned const char*id)
444 trielayer_t*t = trie->start;
446 if(t->rest && !strcmp((char*)t->rest, (char*)id))
454 void* trie_lookup(trie_t*trie, unsigned const char*id)
456 trielayer_t*t = trie->start;
458 if(t->rest && !strcmp((char*)t->rest, (char*)id))
467 typedef struct _triememory {
468 const unsigned char*key;
471 struct _triememory*next;
474 typedef struct _trierollback {
476 struct _trierollback*prev;
479 static void trie_rollback_adds(trie_t*t, unsigned const char*id, void*data)
481 trierollback_t*rollback = (trierollback_t*)t->rollback;
482 triememory_t*m = (triememory_t*)rfx_calloc(sizeof(triememory_t));
486 m->next = rollback->ops;
489 static void trie_rollback_removes(trie_t*t, unsigned const char*id, void*data)
491 trierollback_t*rollback = (trierollback_t*)t->rollback;
492 triememory_t*m = (triememory_t*)rfx_calloc(sizeof(triememory_t));
496 m->next = rollback->ops;
500 void _trie_dump(trielayer_t*t, char*buffer, int pos)
506 _trie_dump(t->row[i], buffer, pos+1);
511 printf("%s%s %08x\n", buffer, t->rest, (int)t->data);
515 void trie_dump(trie_t*t)
518 _trie_dump(t->start, buffer, 0);
522 void trie_remember(trie_t*t)
524 trierollback_t*old = (trierollback_t*)t->rollback;
525 t->rollback = (trierollback_t*)rfx_calloc(sizeof(trierollback_t));
526 ((trierollback_t*)t->rollback)->prev = old;
529 void trie_rollback(trie_t*t)
531 trierollback_t*rollback = (trierollback_t*)t->rollback;
533 fprintf(stderr, "Internal error: can't roll back this trie any further\n");
536 t->rollback = ((trierollback_t*)t->rollback)->prev;
538 triememory_t*op = rollback->ops;
540 triememory_t*next = op->next;
542 if(!_trie_remove(t->start, op->key)) {
543 fprintf(stderr, "Internal error: can't delete key %s in trie during rollback\n", op->key);
546 if(_trie_put(&t->start, op->key, op->data)) {
547 fprintf(stderr, "Internal error: overwrote key %s in trie during rollback\n", op->key);
556 // ------------------------------- crc32 --------------------------------------
557 static unsigned int crc32[256];
558 static char crc32_initialized=0;
559 static void crc32_init(void)
562 if(crc32_initialized)
564 crc32_initialized = 1;
565 for(t=0; t<256; t++) {
568 for (s = 0; s < 8; s++) {
569 c = (0xedb88320L*(c&1)) ^ (c >> 1);
574 // ------------------------------- string_t -----------------------------------
576 void string_set2(string_t*str, const char*text, int len)
581 void string_set(string_t*str, const char*text)
584 str->len = strlen(text);
590 string_t string_new(const char*text, int len)
597 string_t string_new2(const char*text)
601 s.len = strlen(text);
608 string_t* string_new3(const char*text, int len)
611 string_t*s = malloc(sizeof(string_t));
616 string_t*s = malloc(sizeof(string_t)+len+1);
618 s->str = (const char*)(s+1);
619 memcpy((char*)s->str, text, len);
620 ((char*)s->str)[len]=0;
624 string_t* string_new4(const char*text)
626 int l = strlen(text);
627 return string_new3(text, l);
630 void string_free(string_t*s)
635 if((string_t*)(s->str) == s+1) {
639 rfx_free((char*)(s->str));
644 char* string_cstr(string_t*str)
646 return strdup_n(str->str, str->len);
648 char* string_escape(string_t*str)
652 for(t=0;t<str->len;t++) {
658 char*s = malloc(len+1);
660 for(t=0;t<str->len;t++) {
661 if(str->str[t]<0x20) {
663 unsigned char c = str->str[t];
664 *p++ = "0123456789abcdef"[c>>4];
665 *p++ = "0123456789abcdef"[c&0x0f];
671 assert(p == &s[len+1]);
675 unsigned int crc32_add_byte(unsigned int checksum, unsigned char b)
678 return checksum>>8 ^ crc32[(b^checksum)&0xff];
680 unsigned int crc32_add_string(unsigned int checksum, const char*s)
686 checksum = checksum>>8 ^ crc32[(*s^checksum)&0xff];
692 unsigned int string_hash(const string_t*str)
695 unsigned int checksum = 0;
697 for(t=0;t<str->len;t++) {
698 checksum = checksum>>8 ^ crc32[(str->str[t]^checksum)&0xff];
702 unsigned int string_hash2(const char*str)
704 unsigned int checksum = 0;
708 checksum = checksum>>8 ^ crc32[(*p^checksum)&0xff];
713 unsigned int string_hash3(const char*str, int len)
718 return string_hash(&s);
720 void string_dup2(string_t*str, const char*text, int len)
723 str->str = strdup_n(text, len);
725 void string_dup(string_t*str, const char*text)
727 str->len = strlen(text);
728 str->str = strdup(text);
730 int string_equals(string_t*str, const char*text)
732 int l = strlen(text);
733 if(str->len == l && !memcmp(str->str, text, l))
737 int string_equals2(string_t*str, string_t*str2)
739 if(str->len == str2->len && !memcmp(str->str, str2->str, str->len))
744 // ------------------------------- stringarray_t ------------------------------
746 typedef struct _stringlist {
748 struct _stringlist*next;
751 typedef struct _stringarray_internal_t
757 } stringarray_internal_t;
759 void stringarray_init(stringarray_t*sa, int hashsize)
761 stringarray_internal_t*s;
763 sa->internal = (stringarray_internal_t*)rfx_calloc(sizeof(stringarray_internal_t));
764 s = (stringarray_internal_t*)sa->internal;
766 s->hash = rfx_calloc(sizeof(stringlist_t*)*hashsize);
767 s->hashsize = hashsize;
769 void stringarray_put(stringarray_t*sa, string_t str)
771 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
773 int hash = string_hash(&str) % s->hashsize;
775 char*ss = string_cstr(&str);
776 mem_put(&s->pos, &ss, sizeof(char*));
778 stringlist_t*l = rfx_alloc(sizeof(stringlist_t));
780 l->next = s->hash[hash];
785 char* stringarray_at(stringarray_t*sa, int pos)
787 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
789 if(pos<0 || pos>=s->num)
791 p = *(char**)&s->pos.buffer[pos*sizeof(char*)];
796 string_t stringarray_at2(stringarray_t*sa, int pos)
799 s.str = stringarray_at(sa, pos);
800 s.len = s.str?strlen(s.str):0;
803 static stringlist_t* stringlist_del(stringarray_t*sa, stringlist_t*l, int index)
806 stringlist_t*old = l;
808 if(index==l->index) {
810 memset(l, 0, sizeof(stringlist_t));
820 fprintf(stderr, "Internal error: did not find string %d in hash\n", index);
824 void stringarray_del(stringarray_t*sa, int pos)
826 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
827 string_t str = stringarray_at2(sa, pos);
828 int hash = string_hash(&str) % s->hashsize;
829 s->hash[hash] = stringlist_del(sa, s->hash[hash], pos);
830 *(char**)&s->pos.buffer[pos*sizeof(char*)] = 0;
832 int stringarray_find(stringarray_t*sa, string_t* str)
834 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
835 int hash = string_hash(str) % s->hashsize;
837 stringlist_t*l = s->hash[hash];
840 string_t s = stringarray_at2(sa, l->index);
841 if(string_equals2(str, &s)) {
848 void stringarray_clear(stringarray_t*sa)
850 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
853 for(t=0;t<s->hashsize;t++) {
854 stringlist_t*l = s->hash[t];
856 stringlist_t*next = l->next;
857 memset(l, 0, sizeof(stringlist_t));
862 rfx_free(s->hash);s->hash=0;
865 void stringarray_destroy(stringarray_t*sa)
867 stringarray_clear(sa);
871 // ------------------------------- type_t -------------------------------
873 char ptr_equals(const void*o1, const void*o2)
877 unsigned int ptr_hash(const void*o)
879 return string_hash3((const char*)&o, sizeof(o));
881 void* ptr_dup(const void*o)
885 void ptr_free(void*o)
890 char int_equals(const void*o1, const void*o2)
894 unsigned int int_hash(const void*o)
896 return string_hash3((const char*)&o, sizeof(o));
898 void* int_dup(const void*o)
902 void int_free(void*o)
907 char charptr_equals(const void*o1, const void*o2)
911 return !strcmp(o1,o2);
913 unsigned int charptr_hash(const void*o)
917 return string_hash2(o);
919 void* charptr_dup(const void*o)
925 void charptr_free(void*o)
932 char stringstruct_equals(const void*o1, const void*o2)
936 string_t*s1 = (string_t*)o1;
937 string_t*s2 = (string_t*)o2;
938 int l = s1->len<s2->len?s1->len:s2->len;
939 int r = memcmp(s1->str, s2->str, l);
943 return s1->len==s2->len;
945 unsigned int stringstruct_hash(const void*o)
948 return string_hash(o);
950 string_t*string_dup3(string_t*o)
954 string_t*s = malloc(sizeof(string_t));
959 string_t*s = rfx_alloc(sizeof(string_t)+o->len+1);
961 s->str = (const char*)(s+1);
962 memcpy((char*)s->str, o->str, s->len);
963 ((char*)s->str)[s->len]=0;
966 void stringstruct_free(void*o)
986 type_t charptr_type = {
987 equals: charptr_equals,
993 type_t stringstruct_type = {
994 equals: stringstruct_equals,
995 hash: stringstruct_hash,
996 dup: (dup_func)string_dup3,
997 free: stringstruct_free,
1000 // ------------------------------- dictionary_t -------------------------------
1002 #define INITIAL_SIZE 1
1004 static int max(int x, int y) {
1010 dict_t*d = rfx_alloc(sizeof(dict_t));
1011 dict_init(d, INITIAL_SIZE);
1014 dict_t*dict_new2(type_t*t)
1016 dict_t*d = rfx_alloc(sizeof(dict_t));
1017 dict_init(d, INITIAL_SIZE);
1021 void dict_init(dict_t*h, int size)
1023 memset(h, 0, sizeof(dict_t));
1025 h->slots = h->hashsize?(dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*h->hashsize):0;
1027 h->key_type = &charptr_type;
1029 void dict_init2(dict_t*h, type_t*t, int size)
1031 memset(h, 0, sizeof(dict_t));
1033 h->slots = h->hashsize?(dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*h->hashsize):0;
1038 dict_t*dict_clone(dict_t*o)
1040 dict_t*h = rfx_alloc(sizeof(dict_t));
1041 memcpy(h, o, sizeof(dict_t));
1042 h->slots = h->hashsize?(dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*h->hashsize):0;
1044 for(t=0;t<o->hashsize;t++) {
1045 dictentry_t*e = o->slots[t];
1047 dictentry_t*n = (dictentry_t*)rfx_alloc(sizeof(dictentry_t));
1048 memcpy(n, e, sizeof(dictentry_t));
1049 n->key = h->key_type->dup(e->key);
1051 n->next = h->slots[t];
1059 static void dict_expand(dict_t*h, int newlen)
1061 assert(h->hashsize < newlen);
1062 dictentry_t**newslots = (dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*newlen);
1064 for(t=0;t<h->hashsize;t++) {
1065 dictentry_t*e = h->slots[t];
1067 dictentry_t*next = e->next;
1068 unsigned int newhash = e->hash%newlen;
1069 e->next = newslots[newhash];
1070 newslots[newhash] = e;
1076 h->slots = newslots;
1077 h->hashsize = newlen;
1080 dictentry_t* dict_put(dict_t*h, const void*key, void* data)
1082 unsigned int hash = h->key_type->hash(key);
1083 dictentry_t*e = (dictentry_t*)rfx_alloc(sizeof(dictentry_t));
1088 unsigned int hash2 = hash % h->hashsize;
1090 e->key = h->key_type->dup(key);
1091 e->hash = hash; //for resizing
1092 e->next = h->slots[hash2];
1094 h->slots[hash2] = e;
1098 void dict_put2(dict_t*h, const char*s, void*data)
1100 assert(h->key_type == &charptr_type);
1101 dict_put(h, s, data);
1103 void dict_dump(dict_t*h, FILE*fi, const char*prefix)
1106 for(t=0;t<h->hashsize;t++) {
1107 dictentry_t*e = h->slots[t];
1109 if(h->key_type!=&charptr_type) {
1110 fprintf(fi, "%s%08x=%08x\n", prefix, (int)e->key, (int)e->data);
1112 fprintf(fi, "%s%s=%08x\n", prefix, (char*)e->key, (int)e->data);
1119 int dict_count(dict_t*h)
1124 static inline dictentry_t* dict_do_lookup(dict_t*h, const void*key)
1130 unsigned int ohash = h->key_type->hash(key);
1131 unsigned int hash = ohash % h->hashsize;
1133 /* check first entry for match */
1134 dictentry_t*e = h->slots[hash];
1135 if(e && h->key_type->equals(e->key, key)) {
1141 /* if dict is 2/3 filled, double the size. Do
1142 this the first time we have to actually iterate
1143 through a slot to find our data */
1144 if(e && h->num*3 >= h->hashsize*2) {
1145 int newsize = h->hashsize;
1146 while(h->num*3 >= newsize*2) {
1147 newsize = newsize<15?15:(newsize+1)*2-1;
1149 dict_expand(h, newsize);
1150 hash = ohash % h->hashsize;
1152 if(e && h->key_type->equals(e->key, key)) {
1153 // omit move to front
1160 /* check subsequent entries for a match */
1161 dictentry_t*last = h->slots[hash];
1163 if(h->key_type->equals(e->key, key)) {
1164 /* move to front- makes a difference of about 10% in most applications */
1165 last->next = e->next;
1166 e->next = h->slots[hash];
1175 void* dict_lookup(dict_t*h, const void*key)
1177 dictentry_t*e = dict_do_lookup(h, key);
1182 char dict_contains(dict_t*h, const void*key)
1184 dictentry_t*e = dict_do_lookup(h, key);
1188 char dict_del(dict_t*h, const void*key)
1192 unsigned int hash = h->key_type->hash(key) % h->hashsize;
1193 dictentry_t*head = h->slots[hash];
1194 dictentry_t*e = head, *prev=0;
1196 if(h->key_type->equals(e->key, key)) {
1197 dictentry_t*next = e->next;
1198 h->key_type->free(e->key);
1199 memset(e, 0, sizeof(dictentry_t));
1202 h->slots[hash] = next;
1216 char dict_del2(dict_t*h, const void*key, void*data)
1220 unsigned int hash = h->key_type->hash(key) % h->hashsize;
1221 dictentry_t*head = h->slots[hash];
1222 dictentry_t*e = head, *prev=0;
1224 if(h->key_type->equals(e->key, key) && e->data == data) {
1225 dictentry_t*next = e->next;
1226 h->key_type->free(e->key);
1227 memset(e, 0, sizeof(dictentry_t));
1230 h->slots[hash] = next;
1244 dictentry_t* dict_get_slot(dict_t*h, const void*key)
1248 unsigned int ohash = h->key_type->hash(key);
1249 unsigned int hash = ohash % h->hashsize;
1250 return h->slots[hash];
1253 void dict_foreach_keyvalue(dict_t*h, void (*runFunction)(void*data, const void*key, void*val), void*data)
1256 for(t=0;t<h->hashsize;t++) {
1257 dictentry_t*e = h->slots[t];
1259 dictentry_t*next = e->next;
1261 runFunction(data, e->key, e->data);
1267 void dict_foreach_value(dict_t*h, void (*runFunction)(void*))
1270 for(t=0;t<h->hashsize;t++) {
1271 dictentry_t*e = h->slots[t];
1273 dictentry_t*next = e->next;
1275 runFunction(e->data);
1282 void dict_free_all(dict_t*h, char free_keys, void (*free_data_function)(void*))
1285 for(t=0;t<h->hashsize;t++) {
1286 dictentry_t*e = h->slots[t];
1288 dictentry_t*next = e->next;
1290 h->key_type->free(e->key);
1292 if(free_data_function) {
1293 free_data_function(e->data);
1295 memset(e, 0, sizeof(dictentry_t));
1302 memset(h, 0, sizeof(dict_t));
1305 void dict_clear_shallow(dict_t*h)
1307 dict_free_all(h, 0, 0);
1310 void dict_clear(dict_t*h)
1312 dict_free_all(h, 1, 0);
1315 void dict_destroy_shallow(dict_t*dict)
1317 dict_clear_shallow(dict);
1321 void dict_destroy(dict_t*dict)
1329 // ------------------------------- mtf_t --------------------------------------
1330 mtf_t* mtf_new(type_t*type)
1336 void mtf_increase(mtf_t*m, const void*key)
1338 mtf_item_t*item = m->first;
1339 mtf_item_t*last = 0;
1341 if(m->type->equals(item->key, key)) {
1343 if(item->num>m->first->num) {
1344 if(last) last->next = item->next;
1345 else m->first = item->next;
1346 item->next = m->first;
1355 if(last) last->next = n;
1360 void mtf_destroy(mtf_t*m)
1363 mtf_item_t*item = m->first;
1366 mtf_item_t*next = item->next;
1374 // ------------------------------- map_t --------------------------------------
1376 typedef struct _map_internal_t
1381 void map_init(map_t*map)
1384 map->internal = (map_internal_t*)rfx_calloc(sizeof(map_internal_t));
1385 m = (map_internal_t*)map->internal;
1386 dict_init(&m->d, INITIAL_SIZE);
1388 void map_put(map_t*map, string_t t1, string_t t2)
1390 map_internal_t*m = (map_internal_t*)map->internal;
1392 char* s1 = string_cstr(&t1);
1393 dict_put2(&m->d, s1, (void*)string_cstr(&t2));
1396 const char* map_lookup(map_t*map, const char*name)
1398 map_internal_t*m = (map_internal_t*)map->internal;
1399 const char*value = dict_lookup(&m->d, name);
1402 static void freestring(void*data)
1406 static void dumpmapentry(void*data, const void*key, void*value)
1408 FILE*fi = (FILE*)data;
1409 fprintf(fi, "%s=%s\n", (char*)key, (char*)value);
1411 void map_dump(map_t*map, FILE*fi, const char*prefix)
1414 map_internal_t*m = (map_internal_t*)map->internal;
1415 dict_foreach_keyvalue(&m->d, dumpmapentry, fi);
1417 void map_clear(map_t*map)
1419 map_internal_t*m = (map_internal_t*)map->internal;
1420 dict_free_all(&m->d, 1, freestring);
1423 void map_destroy(map_t*map)
1429 // ------------------------------- array_t --------------------------------------
1431 array_t* array_new() {
1432 array_t*d = malloc(sizeof(array_t));
1433 memset(d, 0, sizeof(array_t));
1434 d->entry2pos = dict_new();
1437 array_t* array_new2(type_t*type) {
1438 array_t*d = malloc(sizeof(array_t));
1439 memset(d, 0, sizeof(array_t));
1440 d->entry2pos = dict_new2(type);
1443 void*array_getkey(array_t*array, int nr) {
1444 if(nr > array->num || nr<0) {
1445 fprintf(stderr, "error: reference to element %d in array[%d]\n", nr, array->num);
1448 return array->d[nr].name;
1450 void*array_getvalue(array_t*array, int nr) {
1451 if(nr > array->num || nr<0) {
1452 fprintf(stderr, "error: reference to element %d in array[%d]\n", nr, array->num);
1455 return array->d[nr].data;
1457 int array_append(array_t*array, const void*name, void*data) {
1458 while(array->size <= array->num) {
1461 array->d = malloc(sizeof(array_entry_t)*array->size);
1463 array->d = realloc(array->d, sizeof(array_entry_t)*array->size);
1467 dictentry_t*e = dict_put(array->entry2pos, name, (void*)(ptroff_t)(array->num+1));
1470 array->d[array->num].name = e->key;
1472 array->d[array->num].name = 0;
1474 array->d[array->num].data = (void*)data;
1475 return array->num++;
1477 int array_find(array_t*array, const void*name)
1479 int pos = (int)(ptroff_t)dict_lookup(array->entry2pos, name);
1482 int array_find2(array_t*array, const void*name, void*data)
1484 dict_t*h= array->entry2pos;
1485 dictentry_t*e = dict_get_slot(array->entry2pos, name);
1488 int index = ((int)(ptroff_t)e->data) - 1;
1489 if(h->key_type->equals(e->key, name) && array->d[index].data == data) {
1496 int array_update(array_t*array, const void*name, void*data) {
1497 int pos = array_find(array, name);
1499 array->d[pos].data = data;
1502 return array_append(array, name, data);
1504 int array_append_if_new(array_t*array, const void*name, void*data) {
1505 int pos = array_find(array, name);
1508 return array_append(array, name, data);
1510 void array_free(array_t*array) {
1511 dict_destroy(array->entry2pos);
1513 free(array->d);array->d = 0;
1518 // ------------------------------- list_t --------------------------------------
1521 typedef struct _listinfo {
1523 struct _commonlist*last;
1526 typedef struct _commonlist {
1528 struct _commonlist*next;
1532 int list_length_(void*_list)
1534 commonlist_t*l = (commonlist_t*)_list;
1537 return l->info[0].size;
1539 void list_concat_(void*_l1, void*_l2)
1541 commonlist_t**l1 = (commonlist_t**)_l1;
1542 commonlist_t**l2 = (commonlist_t**)_l2;
1547 (*l1)->info[0].last->next = *l2;
1548 (*l1)->info[0].last = (*l2)->info[0].last;
1549 (*l1)->info[0].size += (*l2)->info[0].size;
1553 void list_append_(void*_list, void*entry)
1555 commonlist_t**list = (commonlist_t**)_list;
1556 commonlist_t* n = 0;
1558 n = (commonlist_t*)malloc(sizeof(commonlist_t)+sizeof(listinfo_t));
1560 (*list)->info[0].size = 0;
1562 n = malloc(sizeof(commonlist_t));
1563 (*list)->info[0].last->next = n;
1567 (*list)->info[0].last = n;
1568 (*list)->info[0].size++;
1570 /* notice: prepending uses slighly more space than appending */
1571 void list_prepend_(void*_list, void*entry)
1573 commonlist_t**list = (commonlist_t**)_list;
1574 commonlist_t* n = (commonlist_t*)malloc(sizeof(commonlist_t)+sizeof(listinfo_t));
1576 commonlist_t* last = 0;
1578 last = (*list)->info[0].last;
1579 size = (*list)->info[0].size;
1584 (*list)->info[0].last = last;
1585 (*list)->info[0].size = size+1;
1587 void list_free_(void*_list)
1589 commonlist_t**list = (commonlist_t**)_list;
1590 commonlist_t*l = *list;
1592 commonlist_t*next = l->next;
1598 void list_deep_free_(void*_list)
1600 commonlist_t**list = (commonlist_t**)_list;
1601 commonlist_t*l = *list;
1603 commonlist_t*next = l->next;
1605 free(l->entry);l->entry=0;
1612 void*list_clone_(void*_list)
1614 commonlist_t*l = *(commonlist_t**)_list;
1618 commonlist_t*next = l->next;
1619 list_append_(&dest, l->entry);