+// ------------------------------- array_t --------------------------------------
+
+array_t* array_new() {
+ array_t*d = malloc(sizeof(array_t));
+ memset(d, 0, sizeof(array_t));
+ d->entry2pos = dict_new();
+ return d;
+}
+array_t* array_new2(type_t*type) {
+ array_t*d = malloc(sizeof(array_t));
+ memset(d, 0, sizeof(array_t));
+ d->entry2pos = dict_new2(type);
+ return d;
+}
+void*array_getkey(array_t*array, int nr) {
+ if(nr > array->num || nr<0) {
+ fprintf(stderr, "error: reference to element %d in array[%d]\n", nr, array->num);
+ return 0;
+ }
+ return array->d[nr].name;
+}
+void*array_getvalue(array_t*array, int nr) {
+ if(nr > array->num || nr<0) {
+ fprintf(stderr, "error: reference to element %d in array[%d]\n", nr, array->num);
+ return 0;
+ }
+ return array->d[nr].data;
+}
+int array_append(array_t*array, const void*name, void*data) {
+ while(array->size <= array->num) {
+ array->size += 64;
+ if(!array->d) {
+ array->d = malloc(sizeof(array_entry_t)*array->size);
+ } else {
+ array->d = realloc(array->d, sizeof(array_entry_t)*array->size);
+ }
+ }
+
+ dictentry_t*e = dict_put(array->entry2pos, name, (void*)(ptroff_t)(array->num+1));
+
+ if(name) {
+ array->d[array->num].name = e->key;
+ } else {
+ array->d[array->num].name = 0;
+ }
+ array->d[array->num].data = (void*)data;
+ return array->num++;
+}
+int array_find(array_t*array, const void*name)
+{
+ int pos = (int)(ptroff_t)dict_lookup(array->entry2pos, name);
+ return pos-1;
+}
+int array_find2(array_t*array, const void*name, void*data)
+{
+ dict_t*h= array->entry2pos;
+ dictentry_t*e = dict_get_slot(array->entry2pos, name);
+
+ while(e) {
+ int index = ((int)(ptroff_t)e->data) - 1;
+ if(h->key_type->equals(e->key, name) && array->d[index].data == data) {
+ return index;
+ }
+ e = e->next;
+ }
+ return -1;
+}
+int array_update(array_t*array, const void*name, void*data) {
+ int pos = array_find(array, name);
+ if(pos>=0) {
+ array->d[pos].data = data;
+ return pos;
+ }
+ return array_append(array, name, data);
+}
+int array_append_if_new(array_t*array, const void*name, void*data) {
+ int pos = array_find(array, name);
+ if(pos>=0)
+ return pos;
+ return array_append(array, name, data);
+}
+void array_free(array_t*array) {
+ dict_destroy(array->entry2pos);
+ if(array->d) {
+ free(array->d);array->d = 0;
+ }
+ free(array);
+}
+
+// ------------------------------- list_t --------------------------------------
+
+struct _commonlist;
+typedef struct _listinfo {
+ int size;
+ struct _commonlist*last;
+} listinfo_t;
+
+typedef struct _commonlist {
+ void*entry;
+ struct _commonlist*next;
+ listinfo_t info[0];
+} commonlist_t;
+
+int list_length_(void*_list)
+{
+ commonlist_t*l = (commonlist_t*)_list;
+ if(!l)
+ return 0;
+ return l->info[0].size;
+}
+void list_concat_(void*_l1, void*_l2)
+{
+ commonlist_t**l1 = (commonlist_t**)_l1;
+ commonlist_t**l2 = (commonlist_t**)_l2;
+
+ if(!*l1) {
+ *l1 = *l2;
+ } else if(*l2) {
+ (*l1)->info[0].last->next = *l2;
+ (*l1)->info[0].last = (*l2)->info[0].last;
+ (*l1)->info[0].size += (*l2)->info[0].size;
+ }
+ *l2 = 0;
+}
+void list_append_(void*_list, void*entry)
+{
+ commonlist_t**list = (commonlist_t**)_list;
+ commonlist_t* n = 0;
+ if(!*list) {
+ n = (commonlist_t*)malloc(sizeof(commonlist_t)+sizeof(listinfo_t));
+ *list = n;
+ (*list)->info[0].size = 0;
+ } else {
+ n = malloc(sizeof(commonlist_t));
+ (*list)->info[0].last->next = n;
+ }
+ n->next = 0;
+ n->entry = entry;
+ (*list)->info[0].last = n;
+ (*list)->info[0].size++;
+}
+/* notice: prepending uses slighly more space than appending */
+void list_prepend_(void*_list, void*entry)
+{
+ commonlist_t**list = (commonlist_t**)_list;
+ commonlist_t* n = (commonlist_t*)malloc(sizeof(commonlist_t)+sizeof(listinfo_t));
+ int size = 0;
+ commonlist_t* last = 0;
+ if(*list) {
+ last = (*list)->info[0].last;
+ size = (*list)->info[0].size;
+ }
+ n->next = *list;
+ n->entry = entry;
+ *list = n;
+ (*list)->info[0].last = last;
+ (*list)->info[0].size = size+1;
+}
+void list_free_(void*_list)
+{
+ commonlist_t**list = (commonlist_t**)_list;
+ commonlist_t*l = *list;
+ while(l) {
+ commonlist_t*next = l->next;
+ free(l);
+ l = next;
+ }
+ *list = 0;
+}
+void list_deep_free_(void*_list)
+{
+ commonlist_t**list = (commonlist_t**)_list;
+ commonlist_t*l = *list;
+ while(l) {
+ commonlist_t*next = l->next;
+ if(l->entry) {
+ free(l->entry);l->entry=0;
+ }
+ free(l);
+ l = next;
+ }
+ *list = 0;
+}
+void*list_clone_(void*_list)
+{
+ commonlist_t*l = *(commonlist_t**)_list;
+
+ void*dest = 0;
+ while(l) {
+ commonlist_t*next = l->next;
+ list_append_(&dest, l->entry);
+ l = next;
+ }
+ return dest;
+
+}
+