image_container.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #include <rtgui/image_container.h>
  2. typedef unsigned int (*rtgui_hash_func_t) (const void* key);
  3. typedef struct _rtgui_hash_table rtgui_hash_table_t;
  4. typedef rt_bool_t (*rtgui_equal_func_t) (const void* a, const void* b);
  5. typedef void (*rtgui_user_func_t) (const void* value, const void* data);
  6. /*
  7. *Hash tables
  8. */
  9. rtgui_hash_table_t* hash_table_create(rtgui_hash_func_t hash_func, rtgui_equal_func_t key_equal_func);
  10. void hash_table_destroy (rtgui_hash_table_t* hash_table);
  11. void* hash_table_find (rtgui_hash_table_t* hash_table, void* key);
  12. void hash_table_insert (rtgui_hash_table_t* hash_table, void* key, void* value);
  13. rt_bool_t hash_table_remove (rtgui_hash_table_t* hash_table, void* key);
  14. void hash_table_foreach(rtgui_hash_table_t* hash_table, rtgui_user_func_t user_func, void* data);
  15. unsigned int hash_table_get_size (rtgui_hash_table_t* hash_table);
  16. /* Hash Functions
  17. */
  18. unsigned int direct_hash (void* v);
  19. #define HASH_TABLE_MIN_SIZE 11
  20. #define HASH_TABLE_MAX_SIZE 6247
  21. typedef struct _gui_hash_node rtgui_hash_node_t;
  22. struct _gui_hash_node
  23. {
  24. void* key;
  25. void* value;
  26. rtgui_hash_node_t *next;
  27. };
  28. struct _rtgui_hash_table
  29. {
  30. rt_uint16_t size;
  31. rt_uint16_t nnodes;
  32. rtgui_hash_node_t **nodes;
  33. rtgui_hash_func_t hash_func;
  34. rtgui_equal_func_t key_equal_func;
  35. };
  36. static const unsigned int primes[] =
  37. {
  38. 11,
  39. 19,
  40. 37,
  41. 73,
  42. 109,
  43. 163,
  44. 251,
  45. 367,
  46. 557,
  47. 823,
  48. 1237,
  49. 1861,
  50. 2777,
  51. 4177,
  52. 6247,
  53. /*
  54. 9371,
  55. 14057,
  56. 21089,
  57. 31627,
  58. 47431,
  59. 71143,
  60. 106721,
  61. 160073,
  62. 240101,
  63. 360163,
  64. 540217,
  65. 810343,
  66. 1215497,
  67. 1823231,
  68. 2734867,
  69. 4102283,
  70. 6153409,
  71. 9230113,
  72. 13845163,
  73. */
  74. };
  75. static const unsigned int nprimes = sizeof (primes) / sizeof (primes[0]);
  76. static void hash_table_resize (rtgui_hash_table_t *hash_table);
  77. static rtgui_hash_node_t** hash_table_find_node (rtgui_hash_table_t *hash_table, void* key);
  78. static rtgui_hash_node_t* hash_node_create (void* key, void* value);
  79. static void hash_node_destroy (rtgui_hash_node_t *hash_node);
  80. static void hash_nodes_destroy (rtgui_hash_node_t *hash_node);
  81. static unsigned int primes_closest (unsigned int num);
  82. static void hash_table_needresize(rtgui_hash_table_t *hash_table);
  83. rt_inline unsigned int primes_closest (unsigned int num)
  84. {
  85. int i;
  86. for (i = 0; i < nprimes; i++)
  87. if (primes[i] > num)
  88. return primes[i];
  89. return primes[nprimes - 1];
  90. }
  91. /* directly hash */
  92. unsigned int direct_hash (void* v)
  93. {
  94. return (unsigned int)v;
  95. }
  96. rtgui_hash_table_t* hash_table_create(rtgui_hash_func_t hash_func, rtgui_equal_func_t key_equal_func)
  97. {
  98. rtgui_hash_table_t *hash_table;
  99. hash_table = (rtgui_hash_table_t*) rt_malloc (sizeof(rtgui_hash_table_t));
  100. if (hash_table != RT_NULL)
  101. {
  102. hash_table->size = HASH_TABLE_MIN_SIZE;
  103. hash_table->nnodes = 0;
  104. hash_table->hash_func = hash_func ? hash_func : direct_hash;
  105. hash_table->key_equal_func = key_equal_func;
  106. hash_table->nodes = (rtgui_hash_node_t **)rt_malloc ( sizeof(rtgui_hash_node_t*) * hash_table->size);
  107. if (hash_table->nodes == RT_NULL)
  108. {
  109. /* no memory yet */
  110. rt_free(hash_table);
  111. return RT_NULL;
  112. }
  113. rt_memset(hash_table->nodes, 0, sizeof(rtgui_hash_node_t*) * hash_table->size);
  114. }
  115. return hash_table;
  116. }
  117. void hash_table_destroy (rtgui_hash_table_t *hash_table)
  118. {
  119. unsigned int i;
  120. RT_ASSERT(hash_table != RT_NULL);
  121. for (i = 0; i < hash_table->size; i++)
  122. hash_nodes_destroy (hash_table->nodes[i]);
  123. rt_free (hash_table->nodes);
  124. rt_free (hash_table);
  125. }
  126. static rtgui_hash_node_t** hash_table_find_node (rtgui_hash_table_t *hash_table, void* key)
  127. {
  128. rtgui_hash_node_t **node;
  129. node = &hash_table->nodes [(* hash_table->hash_func) (key) % hash_table->size];
  130. if (hash_table->key_equal_func)
  131. while (*node && !(*hash_table->key_equal_func) ((*node)->key, key))
  132. node = &(*node)->next;
  133. else
  134. while (*node && (*node)->key != key)
  135. node = &(*node)->next;
  136. return node;
  137. }
  138. void* hash_table_find (rtgui_hash_table_t* hash_table, void* key)
  139. {
  140. rtgui_hash_node_t *node;
  141. RT_ASSERT(hash_table != RT_NULL);
  142. RT_ASSERT(key != RT_NULL);
  143. node = *hash_table_find_node (hash_table, key);
  144. if (node) return node->value;
  145. else return RT_NULL;
  146. }
  147. void hash_table_insert (rtgui_hash_table_t *hash_table, void* key, void* value)
  148. {
  149. rtgui_hash_node_t **node;
  150. if (hash_table == RT_NULL)return;
  151. node = hash_table_find_node (hash_table, key);
  152. if (*node)
  153. {
  154. (*node)->value = value;
  155. }
  156. else
  157. {
  158. *node = hash_node_create (key, value);
  159. hash_table->nnodes++;
  160. hash_table_needresize (hash_table);
  161. }
  162. }
  163. rt_bool_t hash_table_remove (rtgui_hash_table_t *hash_table, void* key)
  164. {
  165. rtgui_hash_node_t **node, *dest;
  166. if (hash_table == RT_NULL) return RT_FALSE;
  167. node = hash_table_find_node (hash_table, key);
  168. if (*node)
  169. {
  170. dest = *node;
  171. (*node) = dest->next;
  172. hash_node_destroy (dest);
  173. hash_table->nnodes--;
  174. hash_table_needresize (hash_table);
  175. return RT_TRUE;
  176. }
  177. return RT_FALSE;
  178. }
  179. void hash_table_foreach(rtgui_hash_table_t *hash_table, rtgui_user_func_t user_func, void* data)
  180. {
  181. rtgui_hash_node_t *node;
  182. int i;
  183. RT_ASSERT(hash_table != RT_NULL);
  184. RT_ASSERT(user_func != RT_NULL);
  185. for (i = 0; i < hash_table->size; i++)
  186. for (node = hash_table->nodes[i]; node; node = node->next)
  187. (* user_func) (node->value, data);
  188. }
  189. unsigned int hash_table_get_size (rtgui_hash_table_t *hash_table)
  190. {
  191. if ( hash_table ==NULL ) return 0;
  192. return hash_table->nnodes;
  193. }
  194. static void hash_table_needresize(rtgui_hash_table_t *hash_table)
  195. {
  196. if ((hash_table->size >= 3*hash_table->nnodes && hash_table->size > HASH_TABLE_MIN_SIZE) ||
  197. (3 * hash_table->size <= hash_table->nnodes && hash_table->size < HASH_TABLE_MAX_SIZE))
  198. hash_table_resize (hash_table);
  199. }
  200. static void hash_table_resize (rtgui_hash_table_t *hash_table)
  201. {
  202. rtgui_hash_node_t **new_nodes;
  203. rtgui_hash_node_t *node;
  204. rtgui_hash_node_t *next;
  205. unsigned int hash_val;
  206. int new_size;
  207. int i;
  208. i = primes_closest(hash_table->nnodes);
  209. new_size = i > HASH_TABLE_MAX_SIZE ? HASH_TABLE_MAX_SIZE : i < HASH_TABLE_MIN_SIZE ? HASH_TABLE_MIN_SIZE : i ;
  210. new_nodes = (rtgui_hash_node_t **)rt_malloc ( sizeof(rtgui_hash_node_t*) * new_size);
  211. if (new_nodes == RT_NULL) return; /* no memory yet */
  212. rt_memset(new_nodes, 0, sizeof(rtgui_hash_node_t*) * new_size);
  213. for (i = 0; i < hash_table->size; i++)
  214. {
  215. for (node = hash_table->nodes[i]; node; node = next)
  216. {
  217. next = node->next;
  218. hash_val = (* hash_table->hash_func) (node->key) % new_size;
  219. node->next = new_nodes[hash_val];
  220. new_nodes[hash_val] = node;
  221. }
  222. }
  223. rt_free (hash_table->nodes);
  224. hash_table->nodes = new_nodes;
  225. hash_table->size = new_size;
  226. }
  227. static rtgui_hash_node_t* hash_node_create (void* key, void* value)
  228. {
  229. rtgui_hash_node_t *hash_node;
  230. hash_node = (rtgui_hash_node_t*) rt_malloc ( sizeof(rtgui_hash_node_t) );
  231. if (hash_node != RT_NULL)
  232. {
  233. /* set value and key */
  234. hash_node->key = key;
  235. hash_node->value = value;;
  236. hash_node->next = RT_NULL;
  237. }
  238. return hash_node;
  239. }
  240. static void hash_node_destroy (rtgui_hash_node_t *hash_node)
  241. {
  242. rt_free(hash_node);
  243. }
  244. static void hash_nodes_destroy (rtgui_hash_node_t *hash_node)
  245. {
  246. if (hash_node)
  247. {
  248. rtgui_hash_node_t *node = hash_node;
  249. rtgui_hash_node_t *temp;
  250. while (node->next)
  251. {
  252. node->key = NULL;
  253. node->value = NULL;
  254. temp = node;
  255. node = node->next;
  256. rt_free(temp);
  257. }
  258. node->key = NULL;
  259. node->value = NULL;
  260. rt_free(node);
  261. }
  262. }
  263. unsigned int string_hash_func(const void* self)
  264. {
  265. const char *p;
  266. int h=0, g;
  267. for(p = self; *p != '\0'; p += 1)
  268. {
  269. h = ( h << 4 ) + *p;
  270. if ( ( g = h & 0xf0000000 ) )
  271. {
  272. h = h ^ (g >> 24);
  273. h = h ^ g;
  274. }
  275. }
  276. return h ;
  277. }
  278. rt_bool_t string_equal_func(const void* a, const void* b)
  279. {
  280. const char *str1, *str2;
  281. str1 = (const char*)a;
  282. str2 = (const char*)b;
  283. if (strcmp(str1, str2) == 0) return RT_TRUE;
  284. return RT_FALSE;
  285. }
  286. static rtgui_hash_table_t* image_hash_table;
  287. rt_bool_t load_image = RT_FALSE;
  288. void image_container_system_init(rt_bool_t is_load)
  289. {
  290. /* create image hash table */
  291. image_hash_table = hash_table_create(string_hash_func, string_equal_func);
  292. RT_ASSERT(image_hash_table != RT_NULL);
  293. load_image = is_load;
  294. }
  295. rtgui_image_t* image_container_get(const char* filename)
  296. {
  297. /* get image type */
  298. }
  299. rtgui_image_t* image_container_get_memref(const rt_uint8_t* memory, const char* type)
  300. {
  301. }
  302. void image_container_put(rtgui_image_t* image)
  303. {
  304. }