container.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* Copyright 2003-2004 Roger Dingledine
  2. * Copyright 2004-2007 Roger Dingledine, Nick Mathewson */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. #ifndef __CONTAINER_H
  6. #define __CONTAINER_H
  7. #define CONTAINER_H_ID \
  8. "$Id$"
  9. #include "compat.h"
  10. #include "util.h"
  11. /** A resizeable list of pointers, with associated helpful functionality.
  12. *
  13. * The members of this struct are exposed only so that macros and inlines can
  14. * use them; all access to smartlist internals should go throuch the functions
  15. * and macros defined here.
  16. **/
  17. typedef struct smartlist_t {
  18. /** <b>list</b> has enough capacity to store exactly <b>capacity</b> elements
  19. * before it needs to be resized. Only the first <b>num_used</b> (\<=
  20. * capacity) elements point to valid data.
  21. */
  22. void **list;
  23. int num_used;
  24. int capacity;
  25. } smartlist_t;
  26. smartlist_t *smartlist_create(void);
  27. void smartlist_free(smartlist_t *sl);
  28. void smartlist_set_capacity(smartlist_t *sl, int n);
  29. void smartlist_clear(smartlist_t *sl);
  30. void smartlist_add(smartlist_t *sl, void *element);
  31. void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
  32. void smartlist_remove(smartlist_t *sl, const void *element);
  33. void *smartlist_pop_last(smartlist_t *sl);
  34. void smartlist_reverse(smartlist_t *sl);
  35. void smartlist_string_remove(smartlist_t *sl, const char *element);
  36. int smartlist_isin(const smartlist_t *sl, const void *element) ATTR_PURE;
  37. int smartlist_string_isin(const smartlist_t *sl, const char *element)
  38. ATTR_PURE;
  39. int smartlist_string_isin_case(const smartlist_t *sl, const char *element)
  40. ATTR_PURE;
  41. int smartlist_string_num_isin(const smartlist_t *sl, int num) ATTR_PURE;
  42. int smartlist_digest_isin(const smartlist_t *sl, const char *element)
  43. ATTR_PURE;
  44. int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2)
  45. ATTR_PURE;
  46. void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
  47. void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
  48. /* smartlist_choose() is defined in crypto.[ch] */
  49. #ifdef DEBUG_SMARTLIST
  50. /** Return the number of items in sl.
  51. */
  52. extern INLINE int smartlist_len(const smartlist_t *sl) ATTR_PURE {
  53. tor_assert(sl);
  54. return (sl)->num_used;
  55. }
  56. /** Return the <b>idx</b>th element of sl.
  57. */
  58. extern INLINE void *smartlist_get(const smartlist_t *sl, int idx) ATTR_PURE {
  59. tor_assert(sl);
  60. tor_assert(idx>=0);
  61. tor_assert(sl->num_used < idx);
  62. return sl->list[idx];
  63. }
  64. extern INLINE void smartlist_set(smartlist_t *sl, int idx, void *val) {
  65. tor_assert(sl);
  66. tor_assert(idx>=0);
  67. tor_assert(sl->num_used < idx);
  68. sl->list[idx] = val;
  69. }
  70. #else
  71. #define smartlist_len(sl) ((sl)->num_used)
  72. #define smartlist_get(sl, idx) ((sl)->list[idx])
  73. #define smartlist_set(sl, idx, val) ((sl)->list[idx] = (val))
  74. #endif
  75. void smartlist_del(smartlist_t *sl, int idx);
  76. void smartlist_del_keeporder(smartlist_t *sl, int idx);
  77. void smartlist_insert(smartlist_t *sl, int idx, void *val);
  78. void smartlist_sort(smartlist_t *sl,
  79. int (*compare)(const void **a, const void **b));
  80. void smartlist_uniq(smartlist_t *sl,
  81. int (*compare)(const void **a, const void **b),
  82. void (*free_fn)(void *elt));
  83. void smartlist_sort_strings(smartlist_t *sl);
  84. void smartlist_sort_digests(smartlist_t *sl);
  85. void smartlist_uniq_strings(smartlist_t *sl);
  86. void smartlist_uniq_digests(smartlist_t *sl);
  87. void *smartlist_bsearch(smartlist_t *sl, const void *key,
  88. int (*compare)(const void *key, const void **member))
  89. ATTR_PURE;
  90. void smartlist_pqueue_add(smartlist_t *sl,
  91. int (*compare)(const void *a, const void *b),
  92. void *item);
  93. void *smartlist_pqueue_pop(smartlist_t *sl,
  94. int (*compare)(const void *a, const void *b));
  95. void smartlist_pqueue_assert_ok(smartlist_t *sl,
  96. int (*compare)(const void *a, const void *b));
  97. #define SPLIT_SKIP_SPACE 0x01
  98. #define SPLIT_IGNORE_BLANK 0x02
  99. int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
  100. int flags, int max);
  101. char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
  102. size_t *len_out) ATTR_MALLOC;
  103. char *smartlist_join_strings2(smartlist_t *sl, const char *join,
  104. size_t join_len, int terminate, size_t *len_out)
  105. ATTR_MALLOC;
  106. /** Iterate over the items in a smartlist <b>sl</b>, in order. For each item,
  107. * assign it to a new local variable of type <b>type</b> named <b>var</b>, and
  108. * execute the statement <b>cmd</b>. Inside the loop, the loop index can
  109. * be accessed as <b>var</b>_sl_idx and the length of the list can be accessed
  110. * as <b>var</b>_sl_len.
  111. *
  112. * NOTE: Do not change the length of the list while the loop is in progress,
  113. * unless you adjust the _sl_len variable correspondingly. See second example
  114. * below.
  115. *
  116. * Example use:
  117. * <pre>
  118. * smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
  119. * SMARTLIST_FOREACH(list, char *, cp,
  120. * {
  121. * printf("%d: %s\n", cp_sl_idx, cp);
  122. * tor_free(cp);
  123. * });
  124. * smartlist_free(list);
  125. * </pre>
  126. *
  127. * Example use (advanced):
  128. * <pre>
  129. * SMARTLIST_FOREACH(list, char *, cp,
  130. * {
  131. * if (!strcmp(cp, "junk")) {
  132. * smartlist_del(list, cp_sl_idx);
  133. * tor_free(cp);
  134. * --cp_sl_len; // decrement length of list so we don't run off the end
  135. * --cp_sl_idx; // decrement idx so we consider the item that moved here
  136. * }
  137. * });
  138. * </pre>
  139. */
  140. #define SMARTLIST_FOREACH(sl, type, var, cmd) \
  141. do { \
  142. int var ## _sl_idx, var ## _sl_len=(sl)->num_used; \
  143. type var; \
  144. for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \
  145. ++var ## _sl_idx) { \
  146. var = (sl)->list[var ## _sl_idx]; \
  147. cmd; \
  148. } } while (0)
  149. /** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
  150. * with the variable <b>var</b>, remove the current element in a way that
  151. * won't confuse the loop. */
  152. #define SMARTLIST_DEL_CURRENT(sl, var) \
  153. do { \
  154. smartlist_del(sl, var ## _sl_idx); \
  155. --var ## _sl_idx; \
  156. --var ## _sl_len; \
  157. } while (0);
  158. #define DECLARE_MAP_FNS(maptype, keytype, prefix) \
  159. typedef struct maptype maptype; \
  160. typedef struct prefix##entry_t *prefix##iter_t; \
  161. maptype* prefix##new(void); \
  162. void* prefix##set(maptype *map, keytype key, void *val); \
  163. void* prefix##get(maptype *map, keytype key); \
  164. void* prefix##remove(maptype *map, keytype key); \
  165. void prefix##free(maptype *map, void (*free_val)(void*)); \
  166. int prefix##isempty(maptype *map); \
  167. int prefix##size(maptype *map); \
  168. prefix##iter_t *prefix##iter_init(maptype *map); \
  169. prefix##iter_t *prefix##iter_next(maptype *map, prefix##iter_t *iter); \
  170. prefix##iter_t *prefix##iter_next_rmv(maptype *map, prefix##iter_t *iter); \
  171. void prefix##iter_get(prefix##iter_t *iter, keytype *keyp, void **valp); \
  172. int prefix##iter_done(prefix##iter_t *iter); \
  173. void prefix##assert_ok(maptype *map);
  174. /* Map from const char * to void *. Implemented with a hash table. */
  175. DECLARE_MAP_FNS(strmap_t, const char *, strmap_);
  176. DECLARE_MAP_FNS(digestmap_t, const char *, digestmap_);
  177. void* strmap_set_lc(strmap_t *map, const char *key, void *val);
  178. void* strmap_get_lc(strmap_t *map, const char *key);
  179. void* strmap_remove_lc(strmap_t *map, const char *key);
  180. #endif