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);
298 (*t)->rest = strdup("");
302 int trie_lookup(trie_t*t, unsigned const char*id)
305 if(t->rest && !strcmp(t->rest, id))
314 char trie_remove(trie_t*t, unsigned const char*id)
317 if(t->rest && !strcmp(t->rest, id)) {
329 // ------------------------------- crc32 --------------------------------------
330 static unsigned int*crc32 = 0;
331 static void crc32_init(void)
336 crc32= (unsigned int*)rfx_alloc(sizeof(unsigned int)*256);
337 for(t=0; t<256; t++) {
340 for (s = 0; s < 8; s++) {
341 c = (0xedb88320L*(c&1)) ^ (c >> 1);
346 // ------------------------------- string_t -----------------------------------
348 void string_set2(string_t*str, const char*text, int len)
353 void string_set(string_t*str, const char*text)
356 str->len = strlen(text);
362 string_t string_new(const char*text, int len)
369 string_t string_new2(const char*text)
373 s.len = strlen(text);
380 string_t* string_new3(const char*text, int len)
383 string_t*s = malloc(sizeof(string_t));
388 string_t*s = malloc(sizeof(string_t)+len+1);
390 s->str = (const char*)(s+1);
391 memcpy((char*)s->str, text, len);
392 ((char*)s->str)[len]=0;
396 string_t* string_new4(const char*text)
398 int l = strlen(text);
399 return string_new3(text, l);
402 void string_free(string_t*s)
407 if((string_t*)(s->str) == s+1) {
411 rfx_free((char*)(s->str));
416 char* string_cstr(string_t*str)
418 return strdup_n(str->str, str->len);
420 char* string_escape(string_t*str)
424 for(t=0;t<str->len;t++) {
430 char*s = malloc(len+1);
432 for(t=0;t<str->len;t++) {
433 if(str->str[t]<0x20) {
435 unsigned char c = str->str[t];
436 *p++ = "0123456789abcdef"[c>>4];
437 *p++ = "0123456789abcdef"[c&0x0f];
443 assert(p == &s[len+1]);
447 unsigned int crc32_add_byte(unsigned int checksum, unsigned char b)
451 return checksum>>8 ^ crc32[(b^checksum)&0xff];
453 unsigned int crc32_add_string(unsigned int checksum, const char*s)
460 checksum = checksum>>8 ^ crc32[(*s^checksum)&0xff];
466 unsigned int string_hash(const string_t*str)
469 unsigned int checksum = 0;
472 for(t=0;t<str->len;t++) {
473 checksum = checksum>>8 ^ crc32[(str->str[t]^checksum)&0xff];
477 unsigned int string_hash2(const char*str)
479 unsigned int checksum = 0;
484 checksum = checksum>>8 ^ crc32[(*p^checksum)&0xff];
489 unsigned int string_hash3(const char*str, int len)
494 return string_hash(&s);
496 void string_dup2(string_t*str, const char*text, int len)
499 str->str = strdup_n(text, len);
501 void string_dup(string_t*str, const char*text)
503 str->len = strlen(text);
504 str->str = strdup(text);
506 int string_equals(string_t*str, const char*text)
508 int l = strlen(text);
509 if(str->len == l && !memcmp(str->str, text, l))
513 int string_equals2(string_t*str, string_t*str2)
515 if(str->len == str2->len && !memcmp(str->str, str2->str, str->len))
520 // ------------------------------- stringarray_t ------------------------------
522 typedef struct _stringlist {
524 struct _stringlist*next;
527 typedef struct _stringarray_internal_t
533 } stringarray_internal_t;
535 void stringarray_init(stringarray_t*sa, int hashsize)
537 stringarray_internal_t*s;
539 sa->internal = (stringarray_internal_t*)rfx_calloc(sizeof(stringarray_internal_t));
540 s = (stringarray_internal_t*)sa->internal;
542 s->hash = rfx_calloc(sizeof(stringlist_t*)*hashsize);
543 s->hashsize = hashsize;
545 void stringarray_put(stringarray_t*sa, string_t str)
547 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
549 int hash = string_hash(&str) % s->hashsize;
551 char*ss = string_cstr(&str);
552 mem_put(&s->pos, &ss, sizeof(char*));
554 stringlist_t*l = rfx_alloc(sizeof(stringlist_t));
556 l->next = s->hash[hash];
561 char* stringarray_at(stringarray_t*sa, int pos)
563 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
565 if(pos<0 || pos>=s->num)
567 p = *(char**)&s->pos.buffer[pos*sizeof(char*)];
572 string_t stringarray_at2(stringarray_t*sa, int pos)
575 s.str = stringarray_at(sa, pos);
576 s.len = s.str?strlen(s.str):0;
579 static stringlist_t* stringlist_del(stringarray_t*sa, stringlist_t*l, int index)
582 stringlist_t*old = l;
584 if(index==l->index) {
586 memset(l, 0, sizeof(stringlist_t));
596 fprintf(stderr, "Internal error: did not find string %d in hash\n", index);
600 void stringarray_del(stringarray_t*sa, int pos)
602 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
603 string_t str = stringarray_at2(sa, pos);
604 int hash = string_hash(&str) % s->hashsize;
605 s->hash[hash] = stringlist_del(sa, s->hash[hash], pos);
606 *(char**)&s->pos.buffer[pos*sizeof(char*)] = 0;
608 int stringarray_find(stringarray_t*sa, string_t* str)
610 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
611 int hash = string_hash(str) % s->hashsize;
613 stringlist_t*l = s->hash[hash];
616 string_t s = stringarray_at2(sa, l->index);
617 if(string_equals2(str, &s)) {
624 void stringarray_clear(stringarray_t*sa)
626 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
629 for(t=0;t<s->hashsize;t++) {
630 stringlist_t*l = s->hash[t];
632 stringlist_t*next = l->next;
633 memset(l, 0, sizeof(stringlist_t));
638 rfx_free(s->hash);s->hash=0;
641 void stringarray_destroy(stringarray_t*sa)
643 stringarray_clear(sa);
647 // ------------------------------- type_t -------------------------------
649 char ptr_equals(const void*o1, const void*o2)
653 unsigned int ptr_hash(const void*o)
655 return string_hash3((const char*)&o, sizeof(o));
657 void* ptr_dup(const void*o)
661 void ptr_free(void*o)
666 char charptr_equals(const void*o1, const void*o2)
670 return !strcmp(o1,o2);
672 unsigned int charptr_hash(const void*o)
676 return string_hash2(o);
678 void* charptr_dup(const void*o)
684 void charptr_free(void*o)
691 char stringstruct_equals(const void*o1, const void*o2)
695 string_t*s1 = (string_t*)o1;
696 string_t*s2 = (string_t*)o2;
697 int l = s1->len<s2->len?s1->len:s2->len;
698 int r = memcmp(s1->str, s2->str, l);
702 return s1->len==s2->len;
704 unsigned int stringstruct_hash(const void*o)
707 return string_hash(o);
709 string_t*string_dup3(string_t*o)
713 string_t*s = malloc(sizeof(string_t));
718 string_t*s = rfx_alloc(sizeof(string_t)+o->len+1);
720 s->str = (const char*)(s+1);
721 memcpy((char*)s->str, o->str, s->len);
722 ((char*)s->str)[s->len]=0;
725 void stringstruct_free(void*o)
738 type_t charptr_type = {
739 equals: charptr_equals,
745 type_t stringstruct_type = {
746 equals: stringstruct_equals,
747 hash: stringstruct_hash,
748 dup: (dup_func)string_dup3,
749 free: stringstruct_free,
752 // ------------------------------- dictionary_t -------------------------------
754 #define INITIAL_SIZE 1
756 static int max(int x, int y) {
762 dict_t*d = rfx_alloc(sizeof(dict_t));
763 dict_init(d, INITIAL_SIZE);
766 dict_t*dict_new2(type_t*t)
768 dict_t*d = rfx_alloc(sizeof(dict_t));
769 dict_init(d, INITIAL_SIZE);
773 void dict_init(dict_t*h, int size)
775 memset(h, 0, sizeof(dict_t));
777 h->slots = h->hashsize?(dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*h->hashsize):0;
779 h->key_type = &charptr_type;
781 void dict_init2(dict_t*h, type_t*t, int size)
783 memset(h, 0, sizeof(dict_t));
785 h->slots = h->hashsize?(dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*h->hashsize):0;
790 dict_t*dict_clone(dict_t*o)
792 dict_t*h = rfx_alloc(sizeof(dict_t));
793 memcpy(h, o, sizeof(dict_t));
794 h->slots = h->hashsize?(dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*h->hashsize):0;
796 for(t=0;t<o->hashsize;t++) {
797 dictentry_t*e = o->slots[t];
799 dictentry_t*n = (dictentry_t*)rfx_alloc(sizeof(dictentry_t));
800 memcpy(n, e, sizeof(dictentry_t));
801 n->key = h->key_type->dup(e->key);
803 n->next = h->slots[t];
811 static void dict_expand(dict_t*h, int newlen)
813 assert(h->hashsize < newlen);
814 dictentry_t**newslots = (dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*newlen);
816 for(t=0;t<h->hashsize;t++) {
817 dictentry_t*e = h->slots[t];
819 dictentry_t*next = e->next;
820 unsigned int newhash = e->hash%newlen;
821 e->next = newslots[newhash];
822 newslots[newhash] = e;
829 h->hashsize = newlen;
832 dictentry_t* dict_put(dict_t*h, const void*key, void* data)
834 unsigned int hash = h->key_type->hash(key);
835 dictentry_t*e = (dictentry_t*)rfx_alloc(sizeof(dictentry_t));
836 unsigned int hash2 = hash % h->hashsize;
838 e->key = h->key_type->dup(key);
839 e->hash = hash; //for resizing
840 e->next = h->slots[hash2];
846 void dict_put2(dict_t*h, const char*s, void*data)
848 assert(h->key_type == &charptr_type);
849 dict_put(h, s, data);
851 void dict_dump(dict_t*h, FILE*fi, const char*prefix)
854 for(t=0;t<h->hashsize;t++) {
855 dictentry_t*e = h->slots[t];
857 if(h->key_type!=&charptr_type) {
858 fprintf(fi, "%s%08x=%08x\n", prefix, e->key, e->data);
860 fprintf(fi, "%s%s=%08x\n", prefix, e->key, e->data);
867 int dict_count(dict_t*h)
872 static inline dictentry_t* dict_do_lookup(dict_t*h, const void*key)
878 unsigned int ohash = h->key_type->hash(key);
879 unsigned int hash = ohash % h->hashsize;
881 /* check first entry for match */
882 dictentry_t*e = h->slots[hash];
883 if(e && h->key_type->equals(e->key, key)) {
889 /* if dict is 2/3 filled, double the size. Do
890 this the first time we have to actually iterate
891 through a slot to find our data */
892 if(e && h->num*3 >= h->hashsize*2) {
893 int newsize = h->hashsize;
894 while(h->num*3 >= newsize*2) {
895 newsize = newsize<15?15:(newsize+1)*2-1;
897 dict_expand(h, newsize);
898 hash = ohash % h->hashsize;
900 if(e && h->key_type->equals(e->key, key)) {
901 // omit move to front
908 /* check subsequent entries for a match */
909 dictentry_t*last = h->slots[hash];
911 if(h->key_type->equals(e->key, key)) {
912 /* move to front- makes a difference of about 10% in most applications */
913 last->next = e->next;
914 e->next = h->slots[hash];
923 void* dict_lookup(dict_t*h, const void*key)
925 dictentry_t*e = dict_do_lookup(h, key);
930 char dict_contains(dict_t*h, const void*key)
932 dictentry_t*e = dict_do_lookup(h, key);
936 char dict_del(dict_t*h, const void*key)
940 unsigned int hash = h->key_type->hash(key) % h->hashsize;
941 dictentry_t*head = h->slots[hash];
942 dictentry_t*e = head, *prev=0;
944 if(h->key_type->equals(e->key, key)) {
945 dictentry_t*next = e->next;
946 rfx_free((void*)e->key);
947 memset(e, 0, sizeof(dictentry_t));
950 h->slots[hash] = next;
964 dictentry_t* dict_get_slot(dict_t*h, const void*key)
968 unsigned int ohash = h->key_type->hash(key);
969 unsigned int hash = ohash % h->hashsize;
970 return h->slots[hash];
973 void dict_foreach_keyvalue(dict_t*h, void (*runFunction)(void*data, const void*key, void*val), void*data)
976 for(t=0;t<h->hashsize;t++) {
977 dictentry_t*e = h->slots[t];
979 dictentry_t*next = e->next;
981 runFunction(data, e->key, e->data);
987 void dict_foreach_value(dict_t*h, void (*runFunction)(void*))
990 for(t=0;t<h->hashsize;t++) {
991 dictentry_t*e = h->slots[t];
993 dictentry_t*next = e->next;
995 runFunction(e->data);
1002 void dict_free_all(dict_t*h, char free_keys, void (*free_data_function)(void*))
1005 for(t=0;t<h->hashsize;t++) {
1006 dictentry_t*e = h->slots[t];
1008 dictentry_t*next = e->next;
1010 h->key_type->free(e->key);
1012 if(free_data_function) {
1013 free_data_function(e->data);
1015 memset(e, 0, sizeof(dictentry_t));
1022 memset(h, 0, sizeof(dict_t));
1025 void dict_clear_shallow(dict_t*h)
1027 dict_free_all(h, 0, 0);
1030 void dict_clear(dict_t*h)
1032 dict_free_all(h, 1, 0);
1035 void dict_destroy_shallow(dict_t*dict)
1037 dict_clear_shallow(dict);
1041 void dict_destroy(dict_t*dict)
1047 // ------------------------------- map_t --------------------------------------
1049 typedef struct _map_internal_t
1054 void map_init(map_t*map)
1057 map->internal = (map_internal_t*)rfx_calloc(sizeof(map_internal_t));
1058 m = (map_internal_t*)map->internal;
1059 dict_init(&m->d, INITIAL_SIZE);
1061 void map_put(map_t*map, string_t t1, string_t t2)
1063 map_internal_t*m = (map_internal_t*)map->internal;
1065 char* s1 = string_cstr(&t1);
1066 dict_put2(&m->d, s1, (void*)string_cstr(&t2));
1069 const char* map_lookup(map_t*map, const char*name)
1071 map_internal_t*m = (map_internal_t*)map->internal;
1072 const char*value = dict_lookup(&m->d, name);
1075 static void freestring(void*data)
1079 static void dumpmapentry(void*data, const void*key, void*value)
1081 FILE*fi = (FILE*)data;
1082 fprintf(fi, "%s=%s\n", key, (char*)value);
1084 void map_dump(map_t*map, FILE*fi, const char*prefix)
1087 map_internal_t*m = (map_internal_t*)map->internal;
1088 dict_foreach_keyvalue(&m->d, dumpmapentry, fi);
1090 void map_clear(map_t*map)
1092 map_internal_t*m = (map_internal_t*)map->internal;
1093 dict_free_all(&m->d, 1, freestring);
1096 void map_destroy(map_t*map)
1102 // ------------------------------- array_t --------------------------------------
1104 array_t* array_new() {
1105 array_t*d = malloc(sizeof(array_t));
1106 memset(d, 0, sizeof(array_t));
1107 d->entry2pos = dict_new();
1110 array_t* array_new2(type_t*type) {
1111 array_t*d = malloc(sizeof(array_t));
1112 memset(d, 0, sizeof(array_t));
1113 d->entry2pos = dict_new2(type);
1116 void*array_getkey(array_t*array, int nr) {
1117 if(nr > array->num || nr<0) {
1118 printf("error: reference to element %d in array[%d]\n", nr, array->num);
1121 return array->d[nr].name;
1123 void*array_getvalue(array_t*array, int nr) {
1124 if(nr > array->num || nr<0) {
1125 printf("error: reference to element %d in array[%d]\n", nr, array->num);
1128 return array->d[nr].data;
1130 int array_append(array_t*array, const void*name, void*data) {
1131 while(array->size <= array->num) {
1134 array->d = malloc(sizeof(array_entry_t)*array->size);
1136 array->d = realloc(array->d, sizeof(array_entry_t)*array->size);
1140 dictentry_t*e = dict_put(array->entry2pos, name, (void*)(ptroff_t)(array->num+1));
1143 array->d[array->num].name = e->key;
1145 array->d[array->num].name = 0;
1147 array->d[array->num].data = (void*)data;
1148 return array->num++;
1150 int array_find(array_t*array, const void*name)
1152 int pos = (int)(ptroff_t)dict_lookup(array->entry2pos, name);
1155 int array_find2(array_t*array, const void*name, void*data)
1157 dict_t*h= array->entry2pos;
1158 dictentry_t*e = dict_get_slot(array->entry2pos, name);
1161 int index = ((int)(ptroff_t)e->data) - 1;
1162 if(h->key_type->equals(e->key, name) && array->d[index].data == data) {
1169 int array_update(array_t*array, const void*name, void*data) {
1170 int pos = array_find(array, name);
1172 array->d[pos].data = data;
1175 return array_append(array, name, data);
1177 int array_append_if_new(array_t*array, const void*name, void*data) {
1178 int pos = array_find(array, name);
1181 return array_append(array, name, data);
1183 void array_free(array_t*array) {
1184 dict_destroy(array->entry2pos);
1186 free(array->d);array->d = 0;
1191 // ------------------------------- list_t --------------------------------------
1194 typedef struct _listinfo {
1196 struct _commonlist*last;
1199 typedef struct _commonlist {
1201 struct _commonlist*next;
1205 int list_length_(void*_list)
1207 commonlist_t*l = (commonlist_t*)_list;
1210 return l->info[0].size;
1212 void list_concat_(void*_l1, void*_l2)
1214 commonlist_t**l1 = (commonlist_t**)_l1;
1215 commonlist_t**l2 = (commonlist_t**)_l2;
1220 (*l1)->info[0].last->next = *l2;
1221 (*l1)->info[0].last = (*l2)->info[0].last;
1222 (*l1)->info[0].size += (*l2)->info[0].size;
1226 void list_append_(void*_list, void*entry)
1228 commonlist_t**list = (commonlist_t**)_list;
1229 commonlist_t* n = 0;
1231 n = (commonlist_t*)malloc(sizeof(commonlist_t)+sizeof(listinfo_t));
1233 (*list)->info[0].size = 0;
1235 n = malloc(sizeof(commonlist_t));
1236 (*list)->info[0].last->next = n;
1240 (*list)->info[0].last = n;
1241 (*list)->info[0].size++;
1243 /* notice: prepending uses slighly more space than appending */
1244 void list_prepend_(void*_list, void*entry)
1246 commonlist_t**list = (commonlist_t**)_list;
1247 commonlist_t* n = (commonlist_t*)malloc(sizeof(commonlist_t)+sizeof(listinfo_t));
1249 commonlist_t* last = 0;
1251 last = (*list)->info[0].last;
1252 size = (*list)->info[0].size;
1257 (*list)->info[0].last = last;
1258 (*list)->info[0].size = size+1;
1260 void list_free_(void*_list)
1262 commonlist_t**list = (commonlist_t**)_list;
1263 commonlist_t*l = *list;
1265 commonlist_t*next = l->next;
1271 void list_deep_free_(void*_list)
1273 commonlist_t**list = (commonlist_t**)_list;
1274 commonlist_t*l = *list;
1276 commonlist_t*next = l->next;
1278 free(l->entry);l->entry=0;
1285 void*list_clone_(void*_list)
1287 commonlist_t*l = *(commonlist_t**)_list;
1291 commonlist_t*next = l->next;
1292 list_append_(&dest, l->entry);