X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=lib%2Fq.c;h=5ade500188a8a3d8763d6e0a080c6ca5d744aa03;hb=c27172590efa648cdb4ba244fdca07b9d64c4c01;hp=fa28bda05d100e6f7b78afbec624e9d88c4cb222;hpb=210871fb25129505f89062549560543181f9f2f2;p=swftools.git diff --git a/lib/q.c b/lib/q.c index fa28bda..5ade500 100644 --- a/lib/q.c +++ b/lib/q.c @@ -25,6 +25,7 @@ #include #include #include "mem.h" +#include "types.h" #include "q.h" // ------------------------------- malloc, alloc routines --------------------- @@ -292,7 +293,11 @@ void string_set2(string_t*str, const char*text, int len) } void string_set(string_t*str, const char*text) { - str->len = strlen(text); + if(text) { + str->len = strlen(text); + } else { + str->len = 0; + } str->str = text; } string_t string_new(const char*text, int len) @@ -305,7 +310,11 @@ string_t string_new(const char*text, int len) string_t string_new2(const char*text) { string_t s; - s.len = strlen(text); + if(text) { + s.len = strlen(text); + } else { + s.len = 0; + } s.str = text; return s; } @@ -497,7 +506,11 @@ void stringarray_destroy(stringarray_t*sa) // ------------------------------- dictionary_t ------------------------------- -#define INITIAL_SIZE 63 +#define INITIAL_SIZE 1 + +static int max(int x, int y) { + return x>y?x:y; +} dict_t*dict_new() { @@ -534,6 +547,7 @@ static void dict_expand(dict_t*h, int newlen) } void dict_put(dict_t*h, string_t s, void* data) { + /* TODO: should we look for duplicates here? */ unsigned int hash = string_hash(&s); dictentry_t*e = (dictentry_t*)rfx_alloc(sizeof(dictentry_t)); @@ -573,32 +587,58 @@ void dict_dump(dict_t*h, FILE*fi, const char*prefix) } } } + int dict_count(dict_t*h) { return h->num; } -void* dict_lookup2(dict_t*h, const char*s, int len) +void* dict_lookup4(dict_t*h, const char*s, int len, const void*data) { if(!h->num) return 0; - - if(h->num*3 >= h->hashsize*2) { - /* if dict is 2/3 filled, double the size */ - dict_expand(h, (h->hashsize+1)*2-1); - } - + unsigned int ohash = string_hash2(s); unsigned int hash = ohash % h->hashsize; + /* check first entry for match */ dictentry_t*e = h->slots[hash]; + if(e && e->len == len && !memcmp(e->s, s, len)) { + return e->data; + } else if(e) { + e = e->next; + } + + /* if dict is 2/3 filled, double the size. Do + this the first time we have to actually iterate + through a slot to find our data */ + if(e && h->num*3 >= h->hashsize*2) { + int newsize = h->hashsize; + while(h->num*3 >= newsize*2) { + newsize = newsize<15?15:(newsize+1)*2-1; + } + dict_expand(h, newsize); + hash = ohash % h->hashsize; + e = h->slots[hash]; + } + + /* check subsequent entries for a match */ while(e) { - if(e->len == len && !memcmp(e->s, s, len)) { + if(e->len == len && !memcmp(e->s, s, len) && (!data || data==e->data)) { return e->data; } e = e->next; } return 0; } +void* dict_lookup3(dict_t*h, const char*s, const void*data) +{ + int l = strlen(s); + return dict_lookup4(h, s, l, data); +} +void* dict_lookup2(dict_t*h, const char*s, int len) +{ + return dict_lookup4(h, s, len, 0); +} void* dict_lookup(dict_t*h, const char*s) { int l = strlen(s); @@ -632,6 +672,16 @@ char dict_del(dict_t*h, const char*s) } return 0; } + +static dictentry_t* dict_get_all(dict_t*h, const char*s) +{ + if(!h->num) + return 0; + unsigned int ohash = string_hash2(s); + unsigned int hash = ohash % h->hashsize; + return h->slots[hash]; +} + void dict_foreach_keyvalue(dict_t*h, void (*runFunction)(void*data, const char*key, void*val), void*data) { int t; @@ -662,6 +712,7 @@ void dict_foreach_value(dict_t*h, void (*runFunction)(void*)) } } } + void dict_free_all(dict_t*h, void (*freeFunction)(void*)) { int t; @@ -681,10 +732,12 @@ void dict_free_all(dict_t*h, void (*freeFunction)(void*)) rfx_free(h->slots); memset(h, 0, sizeof(dict_t)); } + void dict_clear(dict_t*h) { dict_free_all(h, 0); } + void dict_destroy(dict_t*dict) { dict_clear(dict); @@ -746,3 +799,140 @@ void map_destroy(map_t*map) rfx_free(map); } +// ------------------------------- 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; +} +void array_free(array_t*array) { + dict_destroy(array->entry2pos); + array->entry2pos; + if(array->d) { + free(array->d);array->d = 0; + } + free(array); +} +const char*array_getkey(array_t*array, int nr) { + if(nr > array->num || nr<0) { + printf("error: reference to element %d in array[%d]\n", nr, array->num); + *(int*)0 = 0; + return 0; + } + return array->d[nr].name; +} +char*array_getvalue(array_t*array, int nr) { + if(nr > array->num || nr<0) { + printf("error: reference to element %d in array[%d]\n", nr, array->num); + *(int*)0 = 0; + return 0; + } + return array->d[nr].data; +} +int array_append(array_t*array, const char*name, const 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); + } + } + if(name) { + array->d[array->num].name = strdup(name); + } else { + array->d[array->num].name = 0; + } + array->d[array->num].data = (void*)data; + dict_put2(array->entry2pos, name, (void*)(ptroff_t)(array->num+1)); + return array->num++; +} +int array_find(array_t*array, const char*name) +{ + if(!name) + name = ""; + int pos = (int)(ptroff_t)dict_lookup(array->entry2pos, name); + return pos-1; +} +int array_find2(array_t*array, const char*name, void*data) +{ + if(!name) + name = ""; + int len= strlen(name); + + dictentry_t*e = dict_get_all(array->entry2pos, name); + + while(e) { + int index = ((int)(ptroff_t)e->data) - 1; + if(e->len == len && !memcmp(e->s, name, len) && array->d[index].data == data) { + return index; + } + e = e->next; + } + return -1; +} +int array_update(array_t*array, const char*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 char*name, void*data) { + int pos = array_find(array, name); + if(pos>=0) + return pos; + return array_append(array, name, data); +} + +// ------------------------------- 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_append_(void*_list, void*entry) +{ + commonlist_t**list = (commonlist_t**)_list; + commonlist_t* n = 0; + if(!*list) { + n = malloc(sizeof(commonlist_t)+sizeof(listinfo_t)); + *list = n; + } 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++; +} +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; +}