X-Git-Url: http://git.asbjorn.biz/?p=swftools.git;a=blobdiff_plain;f=lib%2Fq.c;h=a2d7a63a989d06bf98efdfdd8bd578a439c4fe6e;hp=1d50ca3297b84b1a3b6f33cbb38b03f5aeef8f44;hb=2391d7ae5d8a145a250a8b80ab8c93ba74eba030;hpb=71b11cc925e384ecad5b0f48b511bcadc2867f9b diff --git a/lib/q.c b/lib/q.c index 1d50ca3..a2d7a63 100644 --- a/lib/q.c +++ b/lib/q.c @@ -113,6 +113,36 @@ int mem_get(mem_t*m, void*data, int length) return length; } +// ------------------------------- median ------------------------------------- + +float medianf(float*a, int n) +{ + int i,j,l,m; + float x; + int k=n&1?n/2:n/2-1; + l=0; + m=n-1; + while(lrest = (unsigned char*)strdup(id); + (*t)->rest = (unsigned char*)strdup((char*)id); (*t)->data = data; return 0; } @@ -359,7 +389,7 @@ static char _trie_put(trielayer_t**t, unsigned const char*id, void*data) char overwrite = 0; if((*t)->rest) overwrite = 1; - (*t)->rest = strdup(""); + (*t)->rest = (unsigned char*)strdup(""); (*t)->data = data; return overwrite; } @@ -367,7 +397,7 @@ static char _trie_put(trielayer_t**t, unsigned const char*id, void*data) static char _trie_remove(trielayer_t*t, unsigned const char*id) { while(t) { - if(t->rest && !strcmp(t->rest, id)) { + if(t->rest && !strcmp((char*)t->rest, (char*)id)) { free(t->rest); t->rest = 0; return 1; @@ -413,7 +443,7 @@ int trie_contains(trie_t*trie, unsigned const char*id) { trielayer_t*t = trie->start; while(t) { - if(t->rest && !strcmp(t->rest, id)) + if(t->rest && !strcmp((char*)t->rest, (char*)id)) return 1; if(!*id) return 0; @@ -425,7 +455,7 @@ void* trie_lookup(trie_t*trie, unsigned const char*id) { trielayer_t*t = trie->start; while(t) { - if(t->rest && !strcmp(t->rest, id)) + if(t->rest && !strcmp((char*)t->rest, (char*)id)) return t->data; if(!*id) return 0; @@ -478,7 +508,7 @@ void _trie_dump(trielayer_t*t, char*buffer, int pos) } if(t->rest) { buffer[pos]=0; - printf("%s%s %08x\n", buffer, t->rest, t->data); + printf("%s%s %08x\n", buffer, t->rest, (int)t->data); } } @@ -644,14 +674,12 @@ char* string_escape(string_t*str) unsigned int crc32_add_byte(unsigned int checksum, unsigned char b) { - if(!crc32) - crc32_init(); + crc32_init(); return checksum>>8 ^ crc32[(b^checksum)&0xff]; } unsigned int crc32_add_string(unsigned int checksum, const char*s) { - if(!crc32) - crc32_init(); + crc32_init(); if(!s) return checksum; while(*s) { @@ -665,8 +693,7 @@ unsigned int string_hash(const string_t*str) { int t; unsigned int checksum = 0; - if(!crc32) - crc32_init(); + crc32_init(); for(t=0;tlen;t++) { checksum = checksum>>8 ^ crc32[(str->str[t]^checksum)&0xff]; } @@ -676,8 +703,7 @@ unsigned int string_hash2(const char*str) { unsigned int checksum = 0; const char*p = str; - if(!crc32) - crc32_init(); + crc32_init(); while(*p) { checksum = checksum>>8 ^ crc32[(*p^checksum)&0xff]; p++; @@ -1057,9 +1083,9 @@ void dict_dump(dict_t*h, FILE*fi, const char*prefix) dictentry_t*e = h->slots[t]; while(e) { if(h->key_type!=&charptr_type) { - fprintf(fi, "%s%08x=%08x\n", prefix, e->key, e->data); + fprintf(fi, "%s%08x=%08x\n", prefix, (int)e->key, (int)e->data); } else { - fprintf(fi, "%s%s=%08x\n", prefix, e->key, e->data); + fprintf(fi, "%s%s=%08x\n", prefix, (char*)e->key, (int)e->data); } e = e->next; } @@ -1163,6 +1189,34 @@ char dict_del(dict_t*h, const void*key) return 0; } +char dict_del2(dict_t*h, const void*key, void*data) +{ + if(!h->num) + return 0; + unsigned int hash = h->key_type->hash(key) % h->hashsize; + dictentry_t*head = h->slots[hash]; + dictentry_t*e = head, *prev=0; + while(e) { + if(h->key_type->equals(e->key, key) && e->data == data) { + dictentry_t*next = e->next; + h->key_type->free(e->key); + memset(e, 0, sizeof(dictentry_t)); + rfx_free(e); + if(e == head) { + h->slots[hash] = next; + } else { + assert(prev); + prev->next = next; + } + h->num--; + return 1; + } + prev = e; + e = e->next; + } + return 0; +} + dictentry_t* dict_get_slot(dict_t*h, const void*key) { if(!h->num) @@ -1283,7 +1337,7 @@ static void freestring(void*data) static void dumpmapentry(void*data, const void*key, void*value) { FILE*fi = (FILE*)data; - fprintf(fi, "%s=%s\n", key, (char*)value); + fprintf(fi, "%s=%s\n", (char*)key, (char*)value); } void map_dump(map_t*map, FILE*fi, const char*prefix) { @@ -1319,14 +1373,14 @@ array_t* array_new2(type_t*type) { } void*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); + 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) { - printf("error: reference to element %d in array[%d]\n", nr, array->num); + fprintf(stderr, "error: reference to element %d in array[%d]\n", nr, array->num); return 0; } return array->d[nr].data;