/src/lib/fcitx-utils/utarray.h

https://bitbucket.org/rve/fcitx · C Header · 362 lines · 295 code · 40 blank · 27 comment · 83 complexity · b8d6a90ae61af04a873e47a7afa13a4e MD5 · raw file

  1. /*
  2. Copyright (c) 2008-2010, Troy D. Hanson http://uthash.sourceforge.net
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  9. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  10. TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  11. PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  12. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  13. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  14. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  15. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  16. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  17. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  18. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19. */
  20. /* a dynamic array implementation using macros
  21. * see http://uthash.sourceforge.net/utarray
  22. */
  23. #ifndef UTARRAY_H
  24. #define UTARRAY_H
  25. #define UTARRAY_VERSION 1.9
  26. #ifdef __GNUC__
  27. #define _UNUSED_ __attribute__ ((__unused__))
  28. #else
  29. #define _UNUSED_
  30. #endif
  31. #include <stddef.h> /* size_t */
  32. #include <string.h> /* memset, etc */
  33. #include <stdlib.h> /* exit */
  34. #define oom() exit(-1)
  35. typedef void (ctor_f)(void *dst, const void *src);
  36. typedef void (dtor_f)(void *elt);
  37. typedef void (init_f)(void *elt);
  38. typedef struct {
  39. size_t sz;
  40. init_f *init;
  41. ctor_f *copy;
  42. dtor_f *dtor;
  43. } UT_icd;
  44. typedef struct {
  45. unsigned i, n; /* i: index of next available slot, n: num slots */
  46. const UT_icd *icd; /* initializer, copy and destructor functions */
  47. char *d; /* n slots of size icd->sz*/
  48. } UT_array;
  49. #define utarray_init(a, _icd) do { \
  50. memset(a, 0, sizeof(UT_array)); \
  51. (a)->icd = _icd; \
  52. } while(0)
  53. #define utarray_done(a) do { \
  54. if ((a)->n) { \
  55. if ((a)->icd->dtor) { \
  56. size_t _ut_i; \
  57. for(_ut_i = 0;_ut_i < (a)->i;_ut_i++) { \
  58. (a)->icd->dtor(utarray_eltptr(a, _ut_i)); \
  59. } \
  60. } \
  61. free((a)->d); \
  62. } \
  63. (a)->n = 0; \
  64. } while(0)
  65. #define utarray_steal(a, p) do { \
  66. if (!(a)->n) { \
  67. p = NULL; \
  68. break; \
  69. } \
  70. p = (a)->d; \
  71. (a)->d = NULL; \
  72. (a)->n = (a)->i = 0; \
  73. } while(0)
  74. #define utarray_new(a, _icd) do { \
  75. a = (UT_array*)malloc(sizeof(UT_array)); \
  76. utarray_init(a, _icd); \
  77. } while(0)
  78. #define utarray_free(a) do { \
  79. utarray_done(a); \
  80. free(a); \
  81. } while(0)
  82. #define utarray_reserve(a, by) do { \
  83. if (((a)->i + by) > ((a)->n)) { \
  84. while(((a)->i + by) > ((a)->n)) { \
  85. (a)->n = ((a)->n ? (2 * (a)->n) : 8); \
  86. } \
  87. if (!((a)->d = (char*)realloc((a)->d, (a)->n * (a)->icd->sz))) { \
  88. oom(); \
  89. } \
  90. } \
  91. } while(0)
  92. #define utarray_push_back(a, p) do { \
  93. utarray_reserve(a, 1); \
  94. if ((a)->icd->copy) { \
  95. (a)->icd->copy(_utarray_eltptr(a, (a)->i++), p); \
  96. } else { \
  97. memcpy(_utarray_eltptr(a, (a)->i++), p, (a)->icd->sz); \
  98. }; \
  99. } while(0)
  100. #define utarray_pop_back(a) do { \
  101. if ((a)->icd->dtor) { \
  102. (a)->icd->dtor(_utarray_eltptr(a, --((a)->i))); \
  103. } else { \
  104. (a)->i--; \
  105. } \
  106. } while(0)
  107. #define utarray_extend_back(a) do { \
  108. utarray_reserve(a, 1); \
  109. if ((a)->icd->init) { \
  110. (a)->icd->init(_utarray_eltptr(a, (a)->i)); \
  111. } else { \
  112. memset(_utarray_eltptr(a, (a)->i), 0, (a)->icd->sz); \
  113. } \
  114. (a)->i++; \
  115. } while(0)
  116. #define utarray_len(a) ((a)->i)
  117. #define utarray_eltptr(a, j) (((j) < (a)->i) ? _utarray_eltptr(a, j) : NULL)
  118. #define _utarray_eltptr(a, j) ((char*)((a)->d + ((a)->icd->sz * (j))))
  119. #define utarray_push_front(a, p) do { \
  120. utarray_reserve(a, 1); \
  121. if (0 < (a)->i) { \
  122. memmove(_utarray_eltptr(a, 1), _utarray_eltptr(a, 0), \
  123. ((a)->i) * ((a)->icd->sz)); \
  124. } \
  125. if ((a)->icd->copy) { \
  126. (a)->icd->copy(_utarray_eltptr(a, 0), p); \
  127. } else { \
  128. memcpy(_utarray_eltptr(a, 0), p, (a)->icd->sz); \
  129. }; \
  130. (a)->i++; \
  131. } while(0)
  132. #define utarray_insert(a, p, j) do { \
  133. utarray_reserve(a, 1); \
  134. if (j > (a)->i) \
  135. break; \
  136. if ((j) < (a)->i) { \
  137. memmove(_utarray_eltptr(a, (j) + 1), _utarray_eltptr(a, j), \
  138. ((a)->i - (j)) * ((a)->icd->sz)); \
  139. } \
  140. if ((a)->icd->copy) { \
  141. (a)->icd->copy(_utarray_eltptr(a, j), p); \
  142. } else { \
  143. memcpy(_utarray_eltptr(a, j), p, (a)->icd->sz); \
  144. }; \
  145. (a)->i++; \
  146. } while(0)
  147. #define utarray_move(a, f, t) do { \
  148. if (f >= (a)->i) \
  149. break; \
  150. if (t >= (a)->i) \
  151. break; \
  152. if (f == t) \
  153. break; \
  154. char *_temp = malloc((a)->icd->sz); \
  155. if (f > t) { \
  156. memcpy(_temp, _utarray_eltptr(a, f), (a)->icd->sz); \
  157. memmove(_utarray_eltptr(a, t + 1), _utarray_eltptr(a, t), \
  158. (a)->icd->sz * (f - (t))); \
  159. memcpy(_utarray_eltptr(a, t), _temp, (a)->icd->sz); \
  160. } else { \
  161. memcpy(_temp, _utarray_eltptr(a, f), (a)->icd->sz); \
  162. memmove(_utarray_eltptr(a, f), _utarray_eltptr(a, f + 1), \
  163. (a)->icd->sz * (t - (f))); \
  164. memcpy(_utarray_eltptr(a, t), _temp, (a)->icd->sz); \
  165. } \
  166. free(_temp); \
  167. } while(0)
  168. #define utarray_inserta(a, w, j) do { \
  169. if (utarray_len(w) == 0) \
  170. break; \
  171. if (j > (a)->i) \
  172. break; \
  173. utarray_reserve(a, utarray_len(w)); \
  174. if ((j) < (a)->i) { \
  175. memmove(_utarray_eltptr(a, (j) + utarray_len(w)), \
  176. _utarray_eltptr(a, j), \
  177. ((a)->i - (j)) * ((a)->icd->sz)); \
  178. } \
  179. if ((a)->icd->copy) { \
  180. size_t _ut_i; \
  181. for(_ut_i = 0;_ut_i < (w)->i;_ut_i++) { \
  182. (a)->icd->copy(_utarray_eltptr(a, j + _ut_i), \
  183. _utarray_eltptr(w, _ut_i)); \
  184. } \
  185. } else { \
  186. memcpy(_utarray_eltptr(a, j), _utarray_eltptr(w, 0), \
  187. utarray_len(w) * ((a)->icd->sz)); \
  188. } \
  189. (a)->i += utarray_len(w); \
  190. } while(0)
  191. #define utarray_resize(dst, num) do { \
  192. size_t _ut_i; \
  193. if ((dst)->i > (size_t)(num)) { \
  194. if ((dst)->icd->dtor) { \
  195. for (_ut_i = num;_ut_i < (dst)->i;_ut_i++) { \
  196. (dst)->icd->dtor(utarray_eltptr(dst, _ut_i)); \
  197. } \
  198. } \
  199. } else if ((dst)->i < (size_t)(num)) { \
  200. utarray_reserve(dst, num - (dst)->i); \
  201. if ((dst)->icd->init) { \
  202. for (_ut_i = (dst)->i;_ut_i < num;_ut_i++) { \
  203. (dst)->icd->init(utarray_eltptr(dst, _ut_i)); \
  204. } \
  205. } else { \
  206. memset(_utarray_eltptr(dst, (dst)->i), 0, \
  207. (dst)->icd->sz * (num - (dst)->i)); \
  208. } \
  209. } \
  210. (dst)->i = num; \
  211. } while(0)
  212. #define utarray_concat(dst, src) do { \
  213. utarray_inserta(dst, src, utarray_len(dst)); \
  214. } while(0)
  215. #define utarray_erase(a, pos, len) do { \
  216. if ((a)->icd->dtor) { \
  217. size_t _ut_i; \
  218. for(_ut_i = 0;_ut_i < len;_ut_i++) { \
  219. (a)->icd->dtor(utarray_eltptr(a, pos + _ut_i)); \
  220. } \
  221. } \
  222. if ((a)->i > (pos + len)) { \
  223. memmove(_utarray_eltptr(a, pos), _utarray_eltptr(a, pos + len), \
  224. (((a)->i) - (pos + len)) * ((a)->icd->sz)); \
  225. } \
  226. (a)->i -= (len); \
  227. } while(0)
  228. #define utarray_remove_quick(a, pos) do { \
  229. if ((a)->i - 1 != (pos)) \
  230. memcpy(_utarray_eltptr(a, pos), _utarray_eltptr(a, (a)->i - 1), \
  231. (a)->icd->sz); \
  232. (a)->i--; \
  233. } while(0)
  234. #define utarray_clear(a) do { \
  235. if ((a)->i > 0) { \
  236. if ((a)->icd->dtor) { \
  237. size_t _ut_i; \
  238. for(_ut_i = 0;_ut_i < (a)->i;_ut_i++) { \
  239. (a)->icd->dtor(utarray_eltptr(a, _ut_i)); \
  240. } \
  241. } \
  242. (a)->i = 0; \
  243. } \
  244. } while(0)
  245. #define utarray_sort(a, cmp) do { \
  246. qsort((a)->d, (a)->i, (a)->icd->sz, cmp); \
  247. } while(0)
  248. #define utarray_sort_range(a, cmp, from, to) do { \
  249. if ((from) >= (to) || (from) >= (a)->i || (to) > (a)->i) \
  250. break; \
  251. qsort(_utarray_eltptr(a, from), (to) - (from), (a)->icd->sz, cmp); \
  252. } while(0)
  253. void fcitx_qsort_r(void *base_, size_t nmemb, size_t size,
  254. int (*compar)(const void *, const void *, void *),
  255. void *thunk);
  256. void fcitx_msort_r(void *base_, size_t nmemb, size_t size,
  257. int (*compar)(const void *, const void *, void *),
  258. void *thunk);
  259. #define utarray_sort_r(a, cmp, arg) do { \
  260. fcitx_qsort_r((a)->d, (a)->i, (a)->icd->sz, cmp, arg); \
  261. } while(0)
  262. #define utarray_msort_r(a, cmp, arg) do { \
  263. fcitx_msort_r((a)->d, (a)->i, (a)->icd->sz, cmp, arg); \
  264. } while(0)
  265. #define utarray_front(a) (((a)->i) ? (_utarray_eltptr(a, 0)) : NULL)
  266. #define utarray_next(a, e) (((e) == NULL) ? utarray_front(a) : \
  267. ((((a)->i) > (utarray_eltidx(a, e) + 1)) ? \
  268. _utarray_eltptr(a, utarray_eltidx(a, e) + 1) : \
  269. NULL))
  270. #define utarray_back(a) (((a)->i) ? (_utarray_eltptr(a, (a)->i - 1)) : NULL)
  271. #define utarray_eltidx(a, e) (((char*)(e) >= (char*)((a)->d)) ? \
  272. (((char*)(e) - (char*)((a)->d)) / (a)->icd->sz) : \
  273. -1)
  274. /* last we pre-define a few icd for common utarrays of ints and strings */
  275. static void utarray_str_cpy(void *dst, const void *src)
  276. {
  277. char **_src = (char**)src, **_dst = (char**)dst;
  278. *_dst = (*_src == NULL) ? NULL : strdup(*_src);
  279. }
  280. static void utarray_str_dtor(void *elt)
  281. {
  282. char **eltc = (char**)elt;
  283. if (*eltc)
  284. free(*eltc);
  285. }
  286. static const UT_icd ut_str_icd _UNUSED_ = {
  287. sizeof(char*), NULL, utarray_str_cpy, utarray_str_dtor
  288. };
  289. static const UT_icd ut_int_icd _UNUSED_ = {
  290. sizeof(int), NULL, NULL, NULL
  291. };
  292. #define utarray_custom_bsearch(key, a, acc, cmp) \
  293. fcitx_utils_custom_bsearch(key, (a)->d, (a)->i, (a)->icd->sz, acc, cmp)
  294. #define utarray_foreach(key, array, type) \
  295. type *key; \
  296. for (key = (type*)utarray_front(array);key; \
  297. key = (type*)utarray_next((array), key))
  298. static inline UT_array*
  299. utarray_clone(UT_array *from)
  300. {
  301. UT_array *to;
  302. utarray_new(to, from->icd);
  303. if (utarray_len(from) == 0)
  304. return to;
  305. utarray_reserve(to, utarray_len(from));
  306. if (to->icd->copy) {
  307. size_t i;
  308. for(i = 0;i < from->i;i++) {
  309. to->icd->copy(_utarray_eltptr(to, i), _utarray_eltptr(from, i));
  310. }
  311. } else {
  312. memcpy(_utarray_eltptr(to, 0), _utarray_eltptr(from, 0),
  313. utarray_len(from) * (to->icd->sz));
  314. }
  315. to->i = utarray_len(from);
  316. return to;
  317. }
  318. #endif /* UTARRAY_H */
  319. // kate: indent-mode cstyle; space-indent on; indent-width 0;