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 */
31 // ------------------------------- malloc, alloc routines ---------------------
34 char* strdup_n(const char*str, int size)
36 char*m = (char*)rfx_alloc(size+1);
42 char*qstrdup(const char*string)
44 return strdup(string);
46 char*qstrndup(const char*string, int len)
48 return strdup_n(string, len);
51 // ------------------------------- mem_t --------------------------------------
53 void mem_init(mem_t*mem)
55 memset(mem, 0, sizeof(mem_t));
57 void mem_clear(mem_t*mem)
59 rfx_free(mem->buffer);mem->buffer = 0;
61 void mem_destroy(mem_t*mem)
66 static int mem_put_(mem_t*m,const void*data, int length, int null)
69 m->pos += length + (null?1:0);
71 int v1 = (m->pos+63)&~63;
72 int v2 = m->len + m->len / 2;
74 m->buffer = m->buffer?(char*)rfx_realloc(m->buffer,m->len):(char*)rfx_alloc(m->len);
76 assert(n+length <= m->len);
77 memcpy(&m->buffer[n], data, length);
79 m->buffer[n + length] = 0;
82 int mem_put(mem_t*m,void*data, int length)
84 return mem_put_(m, data, length, 0);
86 int mem_putstring(mem_t*m,string_t str)
88 return mem_put_(m, str.str, str.len, 1);
90 int mem_get(mem_t*m, void*data, int length)
92 if(m->read_pos + length > m->pos) {
93 length = m->pos - m->read_pos;
95 memcpy(data, m->buffer+m->read_pos, length);
96 m->read_pos += length;
100 // ------------------------------- ringbuffer_t -------------------------------
102 typedef struct _ringbuffer_internal_t
104 unsigned char*buffer;
108 } ringbuffer_internal_t;
110 void ringbuffer_init(ringbuffer_t*r)
112 ringbuffer_internal_t*i = (ringbuffer_internal_t*)rfx_calloc(sizeof(ringbuffer_internal_t));
113 memset(r, 0, sizeof(ringbuffer_t));
115 i->buffer = (unsigned char*)rfx_alloc(1024);
116 i->buffersize = 1024;
118 int ringbuffer_read(ringbuffer_t*r, void*buf, int len)
120 unsigned char* data = (unsigned char*)buf;
121 ringbuffer_internal_t*i = (ringbuffer_internal_t*)r->internal;
122 if(r->available < len)
126 if(i->readpos + len > i->buffersize) {
127 int read1 = i->buffersize-i->readpos;
128 memcpy(data, &i->buffer[i->readpos], read1);
129 memcpy(&data[read1], &i->buffer[0], len - read1);
130 i->readpos = len - read1;
132 memcpy(data, &i->buffer[i->readpos], len);
134 i->readpos %= i->buffersize;
139 void ringbuffer_put(ringbuffer_t*r, void*buf, int len)
141 unsigned char* data = (unsigned char*)buf;
142 ringbuffer_internal_t*i = (ringbuffer_internal_t*)r->internal;
144 if(i->buffersize - r->available < len)
147 int newbuffersize = i->buffersize;
148 int oldavailable = r->available;
149 newbuffersize*=3;newbuffersize/=2; /*grow at least by 50% each time */
151 if(newbuffersize < r->available + len)
152 newbuffersize = r->available + len + 1024;
154 buf2 = (unsigned char*)rfx_alloc(newbuffersize);
155 ringbuffer_read(r, buf2, r->available);
158 i->buffersize = newbuffersize;
160 i->writepos = oldavailable;
161 r->available = oldavailable;
163 if(i->writepos + len > i->buffersize) {
164 int read1 = i->buffersize-i->writepos;
165 memcpy(&i->buffer[i->writepos], data, read1);
166 memcpy(&i->buffer[0], &data[read1], len - read1);
167 i->writepos = len - read1;
169 memcpy(&i->buffer[i->writepos], data, len);
171 i->writepos %= i->buffersize;
175 void ringbuffer_clear(ringbuffer_t*r)
177 ringbuffer_internal_t*i = (ringbuffer_internal_t*)r->internal;
178 rfx_free(i->buffer);i->buffer = 0;
182 // ------------------------------- heap_t -------------------------------
184 void heap_init(heap_t*h,int n,int elem_size, int(*compare)(const void *, const void *))
186 memset(h, 0, sizeof(heap_t));
189 h->elem_size = elem_size;
190 h->compare = compare;
191 h->elements = (void**)rfx_calloc(n*sizeof(void*));
192 h->data = (char*)rfx_calloc(h->max_size*h->elem_size);
194 void heap_clear(heap_t*h)
196 rfx_free(h->elements);
200 #define HEAP_NODE_SMALLER(h,node1,node2) ((h)->compare((node1),(node2))>0)
202 static void up(heap_t*h, int node)
204 void*node_p = h->elements[node];
210 h->elements[node] = h->elements[parent];
211 } while(HEAP_NODE_SMALLER(h,h->elements[parent], node_p));
213 h->elements[node] = node_p;
215 static void down(heap_t*h, int node)
217 void*node_p = h->elements[node];
222 /* determine new child's position */
226 if(child+1 < h->size && HEAP_NODE_SMALLER(h,h->elements[child],h->elements[child+1])) // search for bigger child
229 h->elements[node] = h->elements[child];
230 } while(HEAP_NODE_SMALLER(h,node_p, h->elements[child]));
232 h->elements[node] = node_p;
234 void heap_put(heap_t*h, void*e)
237 memcpy(&h->data[pos*h->elem_size],e,h->elem_size);
238 h->elements[pos] = &h->data[pos];
241 int heap_size(heap_t*h)
245 void* heap_max(heap_t*h)
247 return h->elements[0];
249 void* heap_chopmax(heap_t*h)
251 void*p = h->elements[0];
252 h->elements[0] = h->elements[--h->size];
256 void heap_dump(heap_t*h, FILE*fi)
259 for(t=0;t<h->size;t++) {
261 for(s=0;s<=t;s=(s+1)*2-1) {
262 if(s==t) fprintf(fi,"\n");
264 //fprintf(fi,"%d ", h->elements[t]->x); //?
267 void** heap_flatten(heap_t*h)
269 void**nodes = (void**)rfx_alloc(h->size*sizeof(void*));
273 /*printf("Heap Size: %d\n", h->size);
274 heap_print(stdout, h);
276 *p++ = heap_chopmax(h);
281 // ------------------------------- trie --------------------------------------
283 void trie_put(trie_t**t, unsigned const char*id)
286 (*t) = rfx_calloc(sizeof(trie_t));
287 (*t)->rest = (unsigned char*)strdup(id);
290 if((*t)->rest && (*t)->rest[0]) {
291 // shift whatever's currently in here one node down
292 trie_put(&(*t)->row[(*t)->rest[0]], (*t)->rest+1);
296 trie_put(&(*t)->row[id[0]], id+1);
302 int trie_lookup(trie_t*t, unsigned const char*id)
305 if(t->rest && !strcmp(t->rest, id))
315 // ------------------------------- crc32 --------------------------------------
316 static unsigned int*crc32 = 0;
317 static void crc32_init(void)
322 crc32= (unsigned int*)rfx_alloc(sizeof(unsigned int)*256);
323 for(t=0; t<256; t++) {
326 for (s = 0; s < 8; s++) {
327 c = (0xedb88320L*(c&1)) ^ (c >> 1);
332 // ------------------------------- string_t -----------------------------------
334 void string_set2(string_t*str, const char*text, int len)
339 void string_set(string_t*str, const char*text)
342 str->len = strlen(text);
348 string_t string_new(const char*text, int len)
355 string_t string_new2(const char*text)
359 s.len = strlen(text);
366 string_t* string_new3(const char*text, int len)
369 string_t*s = malloc(sizeof(string_t));
374 string_t*s = malloc(sizeof(string_t)+len+1);
376 s->str = (const char*)(s+1);
377 memcpy((char*)s->str, text, len);
378 ((char*)s->str)[len]=0;
382 string_t* string_new4(const char*text)
384 int l = strlen(text);
385 return string_new3(text, l);
388 void string_free(string_t*s)
393 if((string_t*)(s->str) == s+1) {
397 rfx_free((char*)(s->str));
402 char* string_cstr(string_t*str)
404 return strdup_n(str->str, str->len);
406 char* string_escape(string_t*str)
410 for(t=0;t<str->len;t++) {
416 char*s = malloc(len+1);
418 for(t=0;t<str->len;t++) {
419 if(str->str[t]<0x20) {
421 unsigned char c = str->str[t];
422 *p++ = "0123456789abcdef"[c>>4];
423 *p++ = "0123456789abcdef"[c&0x0f];
429 assert(p == &s[len+1]);
433 unsigned int crc32_add_byte(unsigned int checksum, unsigned char b)
437 return checksum>>8 ^ crc32[(b^checksum)&0xff];
439 unsigned int crc32_add_string(unsigned int checksum, const char*s)
446 checksum = checksum>>8 ^ crc32[(*s^checksum)&0xff];
452 unsigned int string_hash(const string_t*str)
455 unsigned int checksum = 0;
458 for(t=0;t<str->len;t++) {
459 checksum = checksum>>8 ^ crc32[(str->str[t]^checksum)&0xff];
463 unsigned int string_hash2(const char*str)
465 unsigned int checksum = 0;
470 checksum = checksum>>8 ^ crc32[(*p^checksum)&0xff];
475 unsigned int string_hash3(const char*str, int len)
480 return string_hash(&s);
482 void string_dup2(string_t*str, const char*text, int len)
485 str->str = strdup_n(text, len);
487 void string_dup(string_t*str, const char*text)
489 str->len = strlen(text);
490 str->str = strdup(text);
492 int string_equals(string_t*str, const char*text)
494 int l = strlen(text);
495 if(str->len == l && !memcmp(str->str, text, l))
499 int string_equals2(string_t*str, string_t*str2)
501 if(str->len == str2->len && !memcmp(str->str, str2->str, str->len))
506 // ------------------------------- stringarray_t ------------------------------
508 typedef struct _stringlist {
510 struct _stringlist*next;
513 typedef struct _stringarray_internal_t
519 } stringarray_internal_t;
521 void stringarray_init(stringarray_t*sa, int hashsize)
523 stringarray_internal_t*s;
525 sa->internal = (stringarray_internal_t*)rfx_calloc(sizeof(stringarray_internal_t));
526 s = (stringarray_internal_t*)sa->internal;
528 s->hash = rfx_calloc(sizeof(stringlist_t*)*hashsize);
529 s->hashsize = hashsize;
531 void stringarray_put(stringarray_t*sa, string_t str)
533 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
535 int hash = string_hash(&str) % s->hashsize;
537 char*ss = string_cstr(&str);
538 mem_put(&s->pos, &ss, sizeof(char*));
540 stringlist_t*l = rfx_alloc(sizeof(stringlist_t));
542 l->next = s->hash[hash];
547 char* stringarray_at(stringarray_t*sa, int pos)
549 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
551 if(pos<0 || pos>=s->num)
553 p = *(char**)&s->pos.buffer[pos*sizeof(char*)];
558 string_t stringarray_at2(stringarray_t*sa, int pos)
561 s.str = stringarray_at(sa, pos);
562 s.len = s.str?strlen(s.str):0;
565 static stringlist_t* stringlist_del(stringarray_t*sa, stringlist_t*l, int index)
568 stringlist_t*old = l;
570 if(index==l->index) {
572 memset(l, 0, sizeof(stringlist_t));
582 fprintf(stderr, "Internal error: did not find string %d in hash\n", index);
586 void stringarray_del(stringarray_t*sa, int pos)
588 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
589 string_t str = stringarray_at2(sa, pos);
590 int hash = string_hash(&str) % s->hashsize;
591 s->hash[hash] = stringlist_del(sa, s->hash[hash], pos);
592 *(char**)&s->pos.buffer[pos*sizeof(char*)] = 0;
594 int stringarray_find(stringarray_t*sa, string_t* str)
596 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
597 int hash = string_hash(str) % s->hashsize;
599 stringlist_t*l = s->hash[hash];
602 string_t s = stringarray_at2(sa, l->index);
603 if(string_equals2(str, &s)) {
610 void stringarray_clear(stringarray_t*sa)
612 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
615 for(t=0;t<s->hashsize;t++) {
616 stringlist_t*l = s->hash[t];
618 stringlist_t*next = l->next;
619 memset(l, 0, sizeof(stringlist_t));
624 rfx_free(s->hash);s->hash=0;
627 void stringarray_destroy(stringarray_t*sa)
629 stringarray_clear(sa);
633 // ------------------------------- type_t -------------------------------
635 char ptr_equals(const void*o1, const void*o2)
639 unsigned int ptr_hash(const void*o)
641 return string_hash3((const char*)&o, sizeof(o));
643 void* ptr_dup(const void*o)
647 void ptr_free(void*o)
652 char charptr_equals(const void*o1, const void*o2)
656 return !strcmp(o1,o2);
658 unsigned int charptr_hash(const void*o)
662 return string_hash2(o);
664 void* charptr_dup(const void*o)
670 void charptr_free(void*o)
677 char stringstruct_equals(const void*o1, const void*o2)
681 string_t*s1 = (string_t*)o1;
682 string_t*s2 = (string_t*)o2;
683 int l = s1->len<s2->len?s1->len:s2->len;
684 int r = memcmp(s1->str, s2->str, l);
688 return s1->len==s2->len;
690 unsigned int stringstruct_hash(const void*o)
693 return string_hash(o);
695 string_t*string_dup3(string_t*o)
699 string_t*s = malloc(sizeof(string_t));
704 string_t*s = rfx_alloc(sizeof(string_t)+o->len+1);
706 s->str = (const char*)(s+1);
707 memcpy((char*)s->str, o->str, s->len);
708 ((char*)s->str)[s->len]=0;
711 void stringstruct_free(void*o)
724 type_t charptr_type = {
725 equals: charptr_equals,
731 type_t stringstruct_type = {
732 equals: stringstruct_equals,
733 hash: stringstruct_hash,
734 dup: (dup_func)string_dup3,
735 free: stringstruct_free,
738 // ------------------------------- dictionary_t -------------------------------
740 #define INITIAL_SIZE 1
742 static int max(int x, int y) {
748 dict_t*d = rfx_alloc(sizeof(dict_t));
749 dict_init(d, INITIAL_SIZE);
752 dict_t*dict_new2(type_t*t)
754 dict_t*d = rfx_alloc(sizeof(dict_t));
755 dict_init(d, INITIAL_SIZE);
759 void dict_init(dict_t*h, int size)
761 memset(h, 0, sizeof(dict_t));
763 h->slots = h->hashsize?(dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*h->hashsize):0;
765 h->key_type = &charptr_type;
767 void dict_init2(dict_t*h, type_t*t, int size)
769 memset(h, 0, sizeof(dict_t));
771 h->slots = h->hashsize?(dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*h->hashsize):0;
776 dict_t*dict_clone(dict_t*o)
778 dict_t*h = rfx_alloc(sizeof(dict_t));
779 memcpy(h, o, sizeof(dict_t));
780 h->slots = h->hashsize?(dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*h->hashsize):0;
782 for(t=0;t<o->hashsize;t++) {
783 dictentry_t*e = o->slots[t];
785 dictentry_t*n = (dictentry_t*)rfx_alloc(sizeof(dictentry_t));
786 memcpy(n, e, sizeof(dictentry_t));
787 n->key = h->key_type->dup(e->key);
789 n->next = h->slots[t];
797 static void dict_expand(dict_t*h, int newlen)
799 assert(h->hashsize < newlen);
800 dictentry_t**newslots = (dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*newlen);
802 for(t=0;t<h->hashsize;t++) {
803 dictentry_t*e = h->slots[t];
805 dictentry_t*next = e->next;
806 unsigned int newhash = e->hash%newlen;
807 e->next = newslots[newhash];
808 newslots[newhash] = e;
815 h->hashsize = newlen;
818 dictentry_t* dict_put(dict_t*h, const void*key, void* data)
820 unsigned int hash = h->key_type->hash(key);
821 dictentry_t*e = (dictentry_t*)rfx_alloc(sizeof(dictentry_t));
822 unsigned int hash2 = hash % h->hashsize;
824 e->key = h->key_type->dup(key);
825 e->hash = hash; //for resizing
826 e->next = h->slots[hash2];
832 void dict_put2(dict_t*h, const char*s, void*data)
834 assert(h->key_type == &charptr_type);
835 dict_put(h, s, data);
837 void dict_dump(dict_t*h, FILE*fi, const char*prefix)
840 for(t=0;t<h->hashsize;t++) {
841 dictentry_t*e = h->slots[t];
843 if(h->key_type!=&charptr_type) {
844 fprintf(fi, "%s%08x=%08x\n", prefix, e->key, e->data);
846 fprintf(fi, "%s%s=%08x\n", prefix, e->key, e->data);
853 int dict_count(dict_t*h)
858 static inline dictentry_t* dict_do_lookup(dict_t*h, const void*key)
864 unsigned int ohash = h->key_type->hash(key);
865 unsigned int hash = ohash % h->hashsize;
867 /* check first entry for match */
868 dictentry_t*e = h->slots[hash];
869 if(e && h->key_type->equals(e->key, key)) {
875 /* if dict is 2/3 filled, double the size. Do
876 this the first time we have to actually iterate
877 through a slot to find our data */
878 if(e && h->num*3 >= h->hashsize*2) {
879 int newsize = h->hashsize;
880 while(h->num*3 >= newsize*2) {
881 newsize = newsize<15?15:(newsize+1)*2-1;
883 dict_expand(h, newsize);
884 hash = ohash % h->hashsize;
886 if(e && h->key_type->equals(e->key, key)) {
887 // omit move to front
894 /* check subsequent entries for a match */
895 dictentry_t*last = h->slots[hash];
897 if(h->key_type->equals(e->key, key)) {
898 /* move to front- makes a difference of about 10% in most applications */
899 last->next = e->next;
900 e->next = h->slots[hash];
909 void* dict_lookup(dict_t*h, const void*key)
911 dictentry_t*e = dict_do_lookup(h, key);
916 char dict_contains(dict_t*h, const void*key)
918 dictentry_t*e = dict_do_lookup(h, key);
922 char dict_del(dict_t*h, const void*key)
926 unsigned int hash = h->key_type->hash(key) % h->hashsize;
927 dictentry_t*head = h->slots[hash];
928 dictentry_t*e = head, *prev=0;
930 if(h->key_type->equals(e->key, key)) {
931 dictentry_t*next = e->next;
932 rfx_free((void*)e->key);
933 memset(e, 0, sizeof(dictentry_t));
936 h->slots[hash] = next;
950 dictentry_t* dict_get_slot(dict_t*h, const void*key)
954 unsigned int ohash = h->key_type->hash(key);
955 unsigned int hash = ohash % h->hashsize;
956 return h->slots[hash];
959 void dict_foreach_keyvalue(dict_t*h, void (*runFunction)(void*data, const void*key, void*val), void*data)
962 for(t=0;t<h->hashsize;t++) {
963 dictentry_t*e = h->slots[t];
965 dictentry_t*next = e->next;
967 runFunction(data, e->key, e->data);
973 void dict_foreach_value(dict_t*h, void (*runFunction)(void*))
976 for(t=0;t<h->hashsize;t++) {
977 dictentry_t*e = h->slots[t];
979 dictentry_t*next = e->next;
981 runFunction(e->data);
988 void dict_free_all(dict_t*h, char free_keys, void (*free_data_function)(void*))
991 for(t=0;t<h->hashsize;t++) {
992 dictentry_t*e = h->slots[t];
994 dictentry_t*next = e->next;
996 h->key_type->free(e->key);
998 if(free_data_function) {
999 free_data_function(e->data);
1001 memset(e, 0, sizeof(dictentry_t));
1008 memset(h, 0, sizeof(dict_t));
1011 void dict_clear_shallow(dict_t*h)
1013 dict_free_all(h, 0, 0);
1016 void dict_clear(dict_t*h)
1018 dict_free_all(h, 1, 0);
1021 void dict_destroy_shallow(dict_t*dict)
1023 dict_clear_shallow(dict);
1027 void dict_destroy(dict_t*dict)
1033 // ------------------------------- map_t --------------------------------------
1035 typedef struct _map_internal_t
1040 void map_init(map_t*map)
1043 map->internal = (map_internal_t*)rfx_calloc(sizeof(map_internal_t));
1044 m = (map_internal_t*)map->internal;
1045 dict_init(&m->d, INITIAL_SIZE);
1047 void map_put(map_t*map, string_t t1, string_t t2)
1049 map_internal_t*m = (map_internal_t*)map->internal;
1051 char* s1 = string_cstr(&t1);
1052 dict_put2(&m->d, s1, (void*)string_cstr(&t2));
1055 const char* map_lookup(map_t*map, const char*name)
1057 map_internal_t*m = (map_internal_t*)map->internal;
1058 const char*value = dict_lookup(&m->d, name);
1061 static void freestring(void*data)
1065 static void dumpmapentry(void*data, const void*key, void*value)
1067 FILE*fi = (FILE*)data;
1068 fprintf(fi, "%s=%s\n", key, (char*)value);
1070 void map_dump(map_t*map, FILE*fi, const char*prefix)
1073 map_internal_t*m = (map_internal_t*)map->internal;
1074 dict_foreach_keyvalue(&m->d, dumpmapentry, fi);
1076 void map_clear(map_t*map)
1078 map_internal_t*m = (map_internal_t*)map->internal;
1079 dict_free_all(&m->d, 1, freestring);
1082 void map_destroy(map_t*map)
1088 // ------------------------------- array_t --------------------------------------
1090 array_t* array_new() {
1091 array_t*d = malloc(sizeof(array_t));
1092 memset(d, 0, sizeof(array_t));
1093 d->entry2pos = dict_new();
1096 array_t* array_new2(type_t*type) {
1097 array_t*d = malloc(sizeof(array_t));
1098 memset(d, 0, sizeof(array_t));
1099 d->entry2pos = dict_new2(type);
1102 void*array_getkey(array_t*array, int nr) {
1103 if(nr > array->num || nr<0) {
1104 printf("error: reference to element %d in array[%d]\n", nr, array->num);
1107 return array->d[nr].name;
1109 void*array_getvalue(array_t*array, int nr) {
1110 if(nr > array->num || nr<0) {
1111 printf("error: reference to element %d in array[%d]\n", nr, array->num);
1114 return array->d[nr].data;
1116 int array_append(array_t*array, const void*name, void*data) {
1117 while(array->size <= array->num) {
1120 array->d = malloc(sizeof(array_entry_t)*array->size);
1122 array->d = realloc(array->d, sizeof(array_entry_t)*array->size);
1126 dictentry_t*e = dict_put(array->entry2pos, name, (void*)(ptroff_t)(array->num+1));
1129 array->d[array->num].name = e->key;
1131 array->d[array->num].name = 0;
1133 array->d[array->num].data = (void*)data;
1134 return array->num++;
1136 int array_find(array_t*array, const void*name)
1138 int pos = (int)(ptroff_t)dict_lookup(array->entry2pos, name);
1141 int array_find2(array_t*array, const void*name, void*data)
1143 dict_t*h= array->entry2pos;
1144 dictentry_t*e = dict_get_slot(array->entry2pos, name);
1147 int index = ((int)(ptroff_t)e->data) - 1;
1148 if(h->key_type->equals(e->key, name) && array->d[index].data == data) {
1155 int array_update(array_t*array, const void*name, void*data) {
1156 int pos = array_find(array, name);
1158 array->d[pos].data = data;
1161 return array_append(array, name, data);
1163 int array_append_if_new(array_t*array, const void*name, void*data) {
1164 int pos = array_find(array, name);
1167 return array_append(array, name, data);
1169 void array_free(array_t*array) {
1170 dict_destroy(array->entry2pos);
1172 free(array->d);array->d = 0;
1177 // ------------------------------- list_t --------------------------------------
1180 typedef struct _listinfo {
1182 struct _commonlist*last;
1185 typedef struct _commonlist {
1187 struct _commonlist*next;
1191 int list_length_(void*_list)
1193 commonlist_t*l = (commonlist_t*)_list;
1196 return l->info[0].size;
1198 void list_concat_(void*_l1, void*_l2)
1200 commonlist_t**l1 = (commonlist_t**)_l1;
1201 commonlist_t**l2 = (commonlist_t**)_l2;
1206 (*l1)->info[0].last->next = *l2;
1207 (*l1)->info[0].last = (*l2)->info[0].last;
1208 (*l1)->info[0].size += (*l2)->info[0].size;
1212 void list_append_(void*_list, void*entry)
1214 commonlist_t**list = (commonlist_t**)_list;
1215 commonlist_t* n = 0;
1217 n = (commonlist_t*)malloc(sizeof(commonlist_t)+sizeof(listinfo_t));
1219 (*list)->info[0].size = 0;
1221 n = malloc(sizeof(commonlist_t));
1222 (*list)->info[0].last->next = n;
1226 (*list)->info[0].last = n;
1227 (*list)->info[0].size++;
1229 /* notice: prepending uses slighly more space than appending */
1230 void list_prepend_(void*_list, void*entry)
1232 commonlist_t**list = (commonlist_t**)_list;
1233 commonlist_t* n = (commonlist_t*)malloc(sizeof(commonlist_t)+sizeof(listinfo_t));
1235 commonlist_t* last = 0;
1237 last = (*list)->info[0].last;
1238 size = (*list)->info[0].size;
1243 (*list)->info[0].last = last;
1244 (*list)->info[0].size = size+1;
1246 void list_free_(void*_list)
1248 commonlist_t**list = (commonlist_t**)_list;
1249 commonlist_t*l = *list;
1251 commonlist_t*next = l->next;
1257 void list_deep_free_(void*_list)
1259 commonlist_t**list = (commonlist_t**)_list;
1260 commonlist_t*l = *list;
1262 commonlist_t*next = l->next;
1264 free(l->entry);l->entry=0;
1271 void*list_clone_(void*_list)
1273 commonlist_t*l = *(commonlist_t**)_list;
1277 commonlist_t*next = l->next;
1278 list_append_(&dest, l->entry);