QuantHash.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * The Python Imaging Library
  3. * $Id$
  4. *
  5. * hash tables used by the image quantizer
  6. *
  7. * history:
  8. * 98-09-10 tjs Contributed
  9. * 98-12-29 fl Added to PIL 1.0b1
  10. *
  11. * Written by Toby J Sargeant <tjs@longford.cs.monash.edu.au>.
  12. *
  13. * Copyright (c) 1998 by Toby J Sargeant
  14. * Copyright (c) 1998 by Secret Labs AB
  15. *
  16. * See the README file for information on usage and redistribution.
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include "QuantHash.h"
  23. typedef struct _HashNode {
  24. struct _HashNode *next;
  25. HashKey_t key;
  26. HashVal_t value;
  27. } HashNode;
  28. struct _HashTable {
  29. HashNode **table;
  30. uint32_t length;
  31. uint32_t count;
  32. HashFunc hashFunc;
  33. HashCmpFunc cmpFunc;
  34. void *userData;
  35. };
  36. #define MIN_LENGTH 11
  37. #define RESIZE_FACTOR 3
  38. static int _hashtable_insert_node(HashTable *,HashNode *,int,int,CollisionFunc);
  39. HashTable *hashtable_new(HashFunc hf,HashCmpFunc cf) {
  40. HashTable *h;
  41. h=malloc(sizeof(HashTable));
  42. if (!h) { return NULL; }
  43. h->hashFunc=hf;
  44. h->cmpFunc=cf;
  45. h->length=MIN_LENGTH;
  46. h->count=0;
  47. h->userData=NULL;
  48. h->table=malloc(sizeof(HashNode *)*h->length);
  49. if (!h->table) { free(h); return NULL; }
  50. memset (h->table,0,sizeof(HashNode *)*h->length);
  51. return h;
  52. }
  53. static uint32_t _findPrime(uint32_t start,int dir) {
  54. static int unit[]={0,1,0,1,0,0,0,1,0,1,0,1,0,1,0,0};
  55. uint32_t t;
  56. while (start>1) {
  57. if (!unit[start&0x0f]) {
  58. start+=dir;
  59. continue;
  60. }
  61. for (t=2;t<sqrt((double)start);t++) {
  62. if (!start%t) break;
  63. }
  64. if (t>=sqrt((double)start)) {
  65. break;
  66. }
  67. start+=dir;
  68. }
  69. return start;
  70. }
  71. static void _hashtable_rehash(HashTable *h,CollisionFunc cf,uint32_t newSize) {
  72. HashNode **oldTable=h->table;
  73. uint32_t i;
  74. HashNode *n,*nn;
  75. uint32_t oldSize;
  76. oldSize=h->length;
  77. h->table=malloc(sizeof(HashNode *)*newSize);
  78. if (!h->table) {
  79. h->table=oldTable;
  80. return;
  81. }
  82. h->length=newSize;
  83. h->count=0;
  84. memset (h->table,0,sizeof(HashNode *)*h->length);
  85. for (i=0;i<oldSize;i++) {
  86. for (n=oldTable[i];n;n=nn) {
  87. nn=n->next;
  88. _hashtable_insert_node(h,n,0,0,cf);
  89. }
  90. }
  91. free(oldTable);
  92. }
  93. static void _hashtable_resize(HashTable *h) {
  94. uint32_t newSize;
  95. uint32_t oldSize;
  96. oldSize=h->length;
  97. newSize=oldSize;
  98. if (h->count*RESIZE_FACTOR<h->length) {
  99. newSize=_findPrime(h->length/2-1,-1);
  100. } else if (h->length*RESIZE_FACTOR<h->count) {
  101. newSize=_findPrime(h->length*2+1,+1);
  102. }
  103. if (newSize<MIN_LENGTH) { newSize=oldSize; }
  104. if (newSize!=oldSize) {
  105. _hashtable_rehash(h,NULL,newSize);
  106. }
  107. }
  108. static int _hashtable_insert_node(HashTable *h,HashNode *node,int resize,int update,CollisionFunc cf) {
  109. uint32_t hash=h->hashFunc(h,node->key)%h->length;
  110. HashNode **n,*nv;
  111. int i;
  112. for (n=&(h->table[hash]);*n;n=&((*n)->next)) {
  113. nv=*n;
  114. i=h->cmpFunc(h,nv->key,node->key);
  115. if (!i) {
  116. if (cf) {
  117. nv->key=node->key;
  118. cf(h,&(nv->key),&(nv->value),node->key,node->value);
  119. free(node);
  120. return 1;
  121. } else {
  122. nv->key=node->key;
  123. nv->value=node->value;
  124. free(node);
  125. return 1;
  126. }
  127. } else if (i>0) {
  128. break;
  129. }
  130. }
  131. if (!update) {
  132. node->next=*n;
  133. *n=node;
  134. h->count++;
  135. if (resize) _hashtable_resize(h);
  136. return 1;
  137. } else {
  138. return 0;
  139. }
  140. }
  141. static int _hashtable_insert(HashTable *h,HashKey_t key,HashVal_t val,int resize,int update) {
  142. HashNode **n,*nv;
  143. HashNode *t;
  144. int i;
  145. uint32_t hash=h->hashFunc(h,key)%h->length;
  146. for (n=&(h->table[hash]);*n;n=&((*n)->next)) {
  147. nv=*n;
  148. i=h->cmpFunc(h,nv->key,key);
  149. if (!i) {
  150. nv->value=val;
  151. return 1;
  152. } else if (i>0) {
  153. break;
  154. }
  155. }
  156. if (!update) {
  157. t=malloc(sizeof(HashNode));
  158. if (!t) return 0;
  159. t->next=*n;
  160. *n=t;
  161. t->key=key;
  162. t->value=val;
  163. h->count++;
  164. if (resize) _hashtable_resize(h);
  165. return 1;
  166. } else {
  167. return 0;
  168. }
  169. }
  170. int hashtable_insert_or_update_computed(HashTable *h,
  171. HashKey_t key,
  172. ComputeFunc newFunc,
  173. ComputeFunc existsFunc) {
  174. HashNode **n,*nv;
  175. HashNode *t;
  176. int i;
  177. uint32_t hash=h->hashFunc(h,key)%h->length;
  178. for (n=&(h->table[hash]);*n;n=&((*n)->next)) {
  179. nv=*n;
  180. i=h->cmpFunc(h,nv->key,key);
  181. if (!i) {
  182. if (existsFunc) {
  183. existsFunc(h,nv->key,&(nv->value));
  184. } else {
  185. return 0;
  186. }
  187. return 1;
  188. } else if (i>0) {
  189. break;
  190. }
  191. }
  192. t=malloc(sizeof(HashNode));
  193. if (!t) return 0;
  194. t->key=key;
  195. t->next=*n;
  196. *n=t;
  197. if (newFunc) {
  198. newFunc(h,t->key,&(t->value));
  199. } else {
  200. free(t);
  201. return 0;
  202. }
  203. h->count++;
  204. _hashtable_resize(h);
  205. return 1;
  206. }
  207. int hashtable_insert(HashTable *h,HashKey_t key,HashVal_t val) {
  208. return _hashtable_insert(h,key,val,1,0);
  209. }
  210. void hashtable_foreach_update(HashTable *h,IteratorUpdateFunc i,void *u) {
  211. HashNode *n;
  212. uint32_t x;
  213. if (h->table) {
  214. for (x=0;x<h->length;x++) {
  215. for (n=h->table[x];n;n=n->next) {
  216. i(h,n->key,&(n->value),u);
  217. }
  218. }
  219. }
  220. }
  221. void hashtable_foreach(HashTable *h,IteratorFunc i,void *u) {
  222. HashNode *n;
  223. uint32_t x;
  224. if (h->table) {
  225. for (x=0;x<h->length;x++) {
  226. for (n=h->table[x];n;n=n->next) {
  227. i(h,n->key,n->value,u);
  228. }
  229. }
  230. }
  231. }
  232. void hashtable_free(HashTable *h) {
  233. HashNode *n,*nn;
  234. uint32_t i;
  235. if (h->table) {
  236. for (i=0;i<h->length;i++) {
  237. for (n=h->table[i];n;n=nn) {
  238. nn=n->next;
  239. free(n);
  240. }
  241. }
  242. free(h->table);
  243. }
  244. free(h);
  245. }
  246. void hashtable_rehash_compute(HashTable *h,CollisionFunc cf) {
  247. _hashtable_rehash(h,cf,h->length);
  248. }
  249. int hashtable_lookup(const HashTable *h,const HashKey_t key,HashVal_t *valp) {
  250. uint32_t hash=h->hashFunc(h,key)%h->length;
  251. HashNode *n;
  252. int i;
  253. for (n=h->table[hash];n;n=n->next) {
  254. i=h->cmpFunc(h,n->key,key);
  255. if (!i) {
  256. *valp=n->value;
  257. return 1;
  258. } else if (i>0) {
  259. break;
  260. }
  261. }
  262. return 0;
  263. }
  264. uint32_t hashtable_get_count(const HashTable *h) {
  265. return h->count;
  266. }
  267. void *hashtable_get_user_data(const HashTable *h) {
  268. return h->userData;
  269. }
  270. void *hashtable_set_user_data(HashTable *h,void *data) {
  271. void *r=h->userData;
  272. h->userData=data;
  273. return r;
  274. }