X-Git-Url: http://git.asbjorn.biz/?p=swftools.git;a=blobdiff_plain;f=lib%2Fq.h;h=2ea7fe1faa923df5207a4d3f0ae777862cab46b6;hp=9e50000bf9e27fde4593f47c22c6d045d7960093;hb=2ddfa640af28b592ecf4295f0b1b7b43c923f707;hpb=434d5040b814eae784d080c695fe8c3d370a166b diff --git a/lib/q.h b/lib/q.h index 9e50000..2ea7fe1 100644 --- a/lib/q.h +++ b/lib/q.h @@ -23,6 +23,7 @@ #define __q_h__ #include +#include "mem.h" #ifdef __cplusplus extern "C" { @@ -72,6 +73,7 @@ typedef struct _type_t { extern type_t charptr_type; extern type_t stringstruct_type; extern type_t ptr_type; +extern type_t int_type; typedef struct _dictentry { void*key; @@ -110,8 +112,7 @@ typedef struct _stringarray_t } stringarray_t; /* heap */ -typedef struct _heap -{ +typedef struct _heap { void**elements; char*data; int elem_size; @@ -120,13 +121,34 @@ typedef struct _heap int(*compare)(const void *, const void *); } heap_t; -typedef struct _trie { - struct _trie*row[256]; +/* trie (with rollback) */ +typedef struct _trielayer { + struct _trielayer*row[256]; unsigned char*rest; + void*data; +} trielayer_t; + +typedef struct _trie { + trielayer_t* start; + void*rollback; } trie_t; +/* move to front list structure */ +typedef struct _mtf_item { + const void*key; + int num; + struct _mtf_item*next; +} mtf_item_t; + +typedef struct _mtf { + mtf_item_t*first; + type_t*type; +} mtf_t; char* strdup_n(const char*str, int size); +char* allocprintf(const char*str, ...); + +float medianf(float*values, int n); unsigned int crc32_add_byte(unsigned int crc32, unsigned char b); unsigned int crc32_add_string(unsigned int crc32, const char*s); @@ -183,6 +205,7 @@ dictentry_t* dict_get_slot(dict_t*h, const void*key); char dict_contains(dict_t*h, const void*s); void* dict_lookup(dict_t*h, const void*s); char dict_del(dict_t*h, const void*s); +char dict_del2(dict_t*h, const void*key, void*data); dict_t*dict_clone(dict_t*); void dict_foreach_keyvalue(dict_t*h, void (*runFunction)(void*data, const void*key, void*val), void*data); @@ -191,9 +214,18 @@ void dict_free_all(dict_t*h, char free_keys, void (*free_data_function)(void*)); void dict_clear(dict_t*h); void dict_destroy_shallow(dict_t*dict); void dict_destroy(dict_t*dict); -#define DICT_ITERATE_DATA(d,t,v) int v##_i;dictentry_t*v##_e;t v;for(v##_i=0;v##_i<(d)->hashsize;v##_i++) for(v##_e=(d)->slots[v##_i]; v##_e && ((v=(t)v##_e->data)||1); v##_e=v##_e->next) -#define DICT_ITERATE_KEY(d,t,v) int v##_i;dictentry_t*v##_e;t v;for(v##_i=0;v##_i<(d)->hashsize;v##_i++) for(v##_e=(d)->slots[v##_i];v##_e && ((v=(t)v##_e->key)||1);v##_e=v##_e->next) -#define DICT_ITERATE_ITEMS(d,t1,v1,t2,v2) int v1##_i;dictentry_t*v1##_e;t1 v1;t2 v2;for(v1##_i=0;v1##_i<(d)->hashsize;v1##_i++) for(v1##_e=(d)->slots[v1##_i]; v1##_e && (((v1=(t1)v1##_e->key)&&(v2=(t2)v1##_e->data))||1); v1##_e=v1##_e->next) +#define DICT_ITERATE_DATA(d,t,v) \ + int v##_i;dictentry_t*v##_e;t v;\ + for(v##_i=0;v##_i<(d)->hashsize;v##_i++) \ + for(v##_e=(d)->slots[v##_i]; v##_e && ((v=(t)v##_e->data)||1); v##_e=v##_e->next) +#define DICT_ITERATE_KEY(d,t,v) \ + int v##_i;dictentry_t*v##_e;t v;\ + for(v##_i=0;v##_i<(d)->hashsize;v##_i++) \ + for(v##_e=(d)->slots[v##_i];v##_e && ((v=(t)v##_e->key)||1);v##_e=v##_e->next) +#define DICT_ITERATE_ITEMS(d,t1,v1,t2,v2) \ + int v1##_i;dictentry_t*v1##_e;t1 v1;t2 v2; \ + for(v1##_i=0;v1##_i<(d)->hashsize;v1##_i++) \ + for(v1##_e=(d)->slots[v1##_i]; v1##_e && (((v1=(t1)v1##_e->key)&&(v2=(t2)v1##_e->data))||1); v1##_e=v1##_e->next) void map_init(map_t*map); void map_put(map_t*map, string_t t1, string_t t2); @@ -202,17 +234,30 @@ void map_dump(map_t*map, FILE*fi, const char*prefix); void map_clear(map_t*map); void map_destroy(map_t*map); -void heap_init(heap_t*h,int n,int elem_size, int(*compare)(const void *, const void *)); +void heap_init(heap_t*h,int elem_size, int(*compare)(const void *, const void *)); +heap_t* heap_new(int elem_size, int(*compare)(const void *, const void *)); +heap_t* heap_clone(heap_t*o); void heap_clear(heap_t*h); +void heap_destroy(heap_t*h); void heap_put(heap_t*h, void*e); int heap_size(heap_t*h); -void* heap_max(heap_t*h); +void* heap_peek(heap_t*h); void* heap_chopmax(heap_t*h); void heap_dump(heap_t*h, FILE*fi); void** heap_flatten(heap_t*h); -void trie_put(trie_t**t, unsigned const char*id); -int trie_lookup(trie_t*t, unsigned const char*id); +trie_t*trie_new(); +void trie_put(trie_t*t, unsigned const char*id, void*data); +char trie_remove(trie_t*t, unsigned const char*id); +void*trie_lookup(trie_t*t, unsigned const char*id); +int trie_contains(trie_t*t, unsigned const char*id); +void trie_remember(trie_t*t); +void trie_rollback(trie_t*t); +void trie_dump(trie_t*t); + +mtf_t* mtf_new(type_t*type); +void mtf_increase(mtf_t*m, const void*key); +void mtf_destroy(mtf_t*m); array_t* array_new(); array_t* array_new2(type_t*type); @@ -247,7 +292,7 @@ void list_concat_(void*l1, void*l2); #define list_prepend(list, e) {sizeof((list)->next);list_prepend_(&(list),(e));} #define list_free(list) {sizeof((list)->next);list_free_(&(list));} #define list_deep_free(list) {sizeof((list)->next);list_deep_free_(&(list));} -#define list_clone(list) (sizeof((list)->next),list_clone_(&(list))) +#define list_clone(list) (sizeof((list)->next),(list?list_clone_(&(list)):0)) #define list_length(list) (sizeof((list)->next),list_length_(list)) #ifdef __cplusplus