utarray.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. Copyright (c) 2008-2014, Troy D. Hanson http://troydhanson.github.com/uthash/
  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. */
  22. #ifndef UTARRAY_H
  23. #define UTARRAY_H
  24. #define UTARRAY_VERSION 1.9.9
  25. #ifdef __GNUC__
  26. #define _UNUSED_ __attribute__ ((__unused__))
  27. #else
  28. #define _UNUSED_
  29. #endif
  30. #include <stddef.h> /* size_t */
  31. #include <string.h> /* memset, etc */
  32. #include <stdlib.h> /* exit */
  33. #define oom() exit(-1)
  34. typedef void (ctor_f)(void *dst, const void *src);
  35. typedef void (dtor_f)(void *elt);
  36. typedef void (init_f)(void *elt);
  37. typedef struct {
  38. size_t sz;
  39. init_f *init;
  40. ctor_f *copy;
  41. dtor_f *dtor;
  42. } UT_icd;
  43. typedef struct {
  44. unsigned i,n;/* i: index of next available slot, n: num slots */
  45. UT_icd icd; /* initializer, copy and destructor functions */
  46. char *d; /* n slots of size icd->sz*/
  47. } UT_array;
  48. #define utarray_init(a,_icd) do { \
  49. memset(a,0,sizeof(UT_array)); \
  50. (a)->icd=*_icd; \
  51. } while(0)
  52. #define utarray_done(a) do { \
  53. if ((a)->n) { \
  54. if ((a)->icd.dtor) { \
  55. size_t _ut_i; \
  56. for(_ut_i=0; _ut_i < (a)->i; _ut_i++) { \
  57. (a)->icd.dtor(utarray_eltptr(a,_ut_i)); \
  58. } \
  59. } \
  60. free((a)->d); \
  61. } \
  62. (a)->n=0; \
  63. } while(0)
  64. #define utarray_new(a,_icd) do { \
  65. a=(UT_array*)malloc(sizeof(UT_array)); \
  66. utarray_init(a,_icd); \
  67. } while(0)
  68. #define utarray_free(a) do { \
  69. utarray_done(a); \
  70. free(a); \
  71. } while(0)
  72. #define utarray_reserve(a,by) do { \
  73. if (((a)->i+by) > ((a)->n)) { \
  74. while(((a)->i+by) > ((a)->n)) { (a)->n = ((a)->n ? (2*(a)->n) : 8); } \
  75. if ( ((a)->d=(char*)realloc((a)->d, (a)->n*(a)->icd.sz)) == NULL) oom(); \
  76. } \
  77. } while(0)
  78. #define utarray_push_back(a,p) do { \
  79. utarray_reserve(a,1); \
  80. if ((a)->icd.copy) { (a)->icd.copy( _utarray_eltptr(a,(a)->i++), p); } \
  81. else { memcpy(_utarray_eltptr(a,(a)->i++), p, (a)->icd.sz); }; \
  82. } while(0)
  83. #define utarray_pop_back(a) do { \
  84. if ((a)->icd.dtor) { (a)->icd.dtor( _utarray_eltptr(a,--((a)->i))); } \
  85. else { (a)->i--; } \
  86. } while(0)
  87. #define utarray_extend_back(a) do { \
  88. utarray_reserve(a,1); \
  89. if ((a)->icd.init) { (a)->icd.init(_utarray_eltptr(a,(a)->i)); } \
  90. else { memset(_utarray_eltptr(a,(a)->i),0,(a)->icd.sz); } \
  91. (a)->i++; \
  92. } while(0)
  93. #define utarray_len(a) ((a)->i)
  94. #define utarray_eltptr(a,j) (((j) < (a)->i) ? _utarray_eltptr(a,j) : NULL)
  95. #define _utarray_eltptr(a,j) ((char*)((a)->d + ((a)->icd.sz*(j) )))
  96. #define utarray_insert(a,p,j) do { \
  97. if (j > (a)->i) utarray_resize(a,j); \
  98. utarray_reserve(a,1); \
  99. if ((j) < (a)->i) { \
  100. memmove( _utarray_eltptr(a,(j)+1), _utarray_eltptr(a,j), \
  101. ((a)->i - (j))*((a)->icd.sz)); \
  102. } \
  103. if ((a)->icd.copy) { (a)->icd.copy( _utarray_eltptr(a,j), p); } \
  104. else { memcpy(_utarray_eltptr(a,j), p, (a)->icd.sz); }; \
  105. (a)->i++; \
  106. } while(0)
  107. #define utarray_inserta(a,w,j) do { \
  108. if (utarray_len(w) == 0) break; \
  109. if (j > (a)->i) utarray_resize(a,j); \
  110. utarray_reserve(a,utarray_len(w)); \
  111. if ((j) < (a)->i) { \
  112. memmove(_utarray_eltptr(a,(j)+utarray_len(w)), \
  113. _utarray_eltptr(a,j), \
  114. ((a)->i - (j))*((a)->icd.sz)); \
  115. } \
  116. if ((a)->icd.copy) { \
  117. size_t _ut_i; \
  118. for(_ut_i=0;_ut_i<(w)->i;_ut_i++) { \
  119. (a)->icd.copy(_utarray_eltptr(a,j+_ut_i), _utarray_eltptr(w,_ut_i)); \
  120. } \
  121. } else { \
  122. memcpy(_utarray_eltptr(a,j), _utarray_eltptr(w,0), \
  123. utarray_len(w)*((a)->icd.sz)); \
  124. } \
  125. (a)->i += utarray_len(w); \
  126. } while(0)
  127. #define utarray_resize(dst,num) do { \
  128. size_t _ut_i; \
  129. if (dst->i > (size_t)(num)) { \
  130. if ((dst)->icd.dtor) { \
  131. for(_ut_i=num; _ut_i < dst->i; _ut_i++) { \
  132. (dst)->icd.dtor(utarray_eltptr(dst,_ut_i)); \
  133. } \
  134. } \
  135. } else if (dst->i < (size_t)(num)) { \
  136. utarray_reserve(dst,num-dst->i); \
  137. if ((dst)->icd.init) { \
  138. for(_ut_i=dst->i; _ut_i < num; _ut_i++) { \
  139. (dst)->icd.init(utarray_eltptr(dst,_ut_i)); \
  140. } \
  141. } else { \
  142. memset(_utarray_eltptr(dst,dst->i),0,(dst)->icd.sz*(num-dst->i)); \
  143. } \
  144. } \
  145. dst->i = num; \
  146. } while(0)
  147. #define utarray_concat(dst,src) do { \
  148. utarray_inserta((dst),(src),utarray_len(dst)); \
  149. } while(0)
  150. #define utarray_erase(a,pos,len) do { \
  151. if ((a)->icd.dtor) { \
  152. size_t _ut_i; \
  153. for(_ut_i=0; _ut_i < len; _ut_i++) { \
  154. (a)->icd.dtor(utarray_eltptr((a),pos+_ut_i)); \
  155. } \
  156. } \
  157. if ((a)->i > (pos+len)) { \
  158. memmove( _utarray_eltptr((a),pos), _utarray_eltptr((a),pos+len), \
  159. (((a)->i)-(pos+len))*((a)->icd.sz)); \
  160. } \
  161. (a)->i -= (len); \
  162. } while(0)
  163. #define utarray_renew(a,u) do { \
  164. if (a) utarray_clear(a); \
  165. else utarray_new((a),(u)); \
  166. } while(0)
  167. #define utarray_clear(a) do { \
  168. if ((a)->i > 0) { \
  169. if ((a)->icd.dtor) { \
  170. size_t _ut_i; \
  171. for(_ut_i=0; _ut_i < (a)->i; _ut_i++) { \
  172. (a)->icd.dtor(utarray_eltptr(a,_ut_i)); \
  173. } \
  174. } \
  175. (a)->i = 0; \
  176. } \
  177. } while(0)
  178. #define utarray_sort(a,cmp) do { \
  179. qsort((a)->d, (a)->i, (a)->icd.sz, cmp); \
  180. } while(0)
  181. #define utarray_find(a,v,cmp) bsearch((v),(a)->d,(a)->i,(a)->icd.sz,cmp)
  182. #define utarray_front(a) (((a)->i) ? (_utarray_eltptr(a,0)) : NULL)
  183. #define utarray_next(a,e) (((e)==NULL) ? utarray_front(a) : ((((a)->i) > (utarray_eltidx(a,e)+1)) ? _utarray_eltptr(a,utarray_eltidx(a,e)+1) : NULL))
  184. #define utarray_prev(a,e) (((e)==NULL) ? utarray_back(a) : ((utarray_eltidx(a,e) > 0) ? _utarray_eltptr(a,utarray_eltidx(a,e)-1) : NULL))
  185. #define utarray_back(a) (((a)->i) ? (_utarray_eltptr(a,(a)->i-1)) : NULL)
  186. #define utarray_eltidx(a,e) (((char*)(e) >= (char*)((a)->d)) ? (((char*)(e) - (char*)((a)->d))/(size_t)(a)->icd.sz) : -1)
  187. /* last we pre-define a few icd for common utarrays of ints and strings */
  188. static void utarray_str_cpy(void *dst, const void *src) {
  189. char **_src = (char**)src, **_dst = (char**)dst;
  190. *_dst = (*_src == NULL) ? NULL : strdup(*_src);
  191. }
  192. static void utarray_str_dtor(void *elt) {
  193. char **eltc = (char**)elt;
  194. if (*eltc) free(*eltc);
  195. }
  196. static const UT_icd ut_str_icd _UNUSED_ = {sizeof(char*),NULL,utarray_str_cpy,utarray_str_dtor};
  197. static const UT_icd ut_int_icd _UNUSED_ = {sizeof(int),NULL,NULL,NULL};
  198. static const UT_icd ut_ptr_icd _UNUSED_ = {sizeof(void*),NULL,NULL,NULL};
  199. #endif /* UTARRAY_H */