container.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Copyright 2003-2004 Roger Dingledine
  2. * Copyright 2004-2006 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. typedef struct smartlist_t {
  13. /** <b>list</b> has enough capacity to store exactly <b>capacity</b> elements
  14. * before it needs to be resized. Only the first <b>num_used</b> (\<=
  15. * capacity) elements point to valid data.
  16. */
  17. void **list;
  18. int num_used;
  19. int capacity;
  20. } smartlist_t;
  21. smartlist_t *smartlist_create(void);
  22. void smartlist_free(smartlist_t *sl);
  23. void smartlist_set_capacity(smartlist_t *sl, int n);
  24. void smartlist_clear(smartlist_t *sl);
  25. void smartlist_add(smartlist_t *sl, void *element);
  26. void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
  27. void smartlist_remove(smartlist_t *sl, const void *element);
  28. void *smartlist_pop_last(smartlist_t *sl);
  29. void smartlist_reverse(smartlist_t *sl);
  30. void smartlist_string_remove(smartlist_t *sl, const char *element);
  31. int smartlist_isin(const smartlist_t *sl, const void *element) ATTR_PURE;
  32. int smartlist_string_isin(const smartlist_t *sl, const char *element)
  33. ATTR_PURE;
  34. int smartlist_string_num_isin(const smartlist_t *sl, int num) ATTR_PURE;
  35. int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2)
  36. ATTR_PURE;
  37. void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
  38. void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
  39. /* smartlist_choose() is defined in crypto.[ch] */
  40. #ifdef DEBUG_SMARTLIST
  41. /** Return the number of items in sl.
  42. */
  43. extern INLINE int smartlist_len(const smartlist_t *sl) ATTR_PURE {
  44. tor_assert(sl);
  45. return (sl)->num_used;
  46. }
  47. /** Return the <b>idx</b>th element of sl.
  48. */
  49. extern INLINE void *smartlist_get(const smartlist_t *sl, int idx) ATTR_PURE {
  50. tor_assert(sl);
  51. tor_assert(idx>=0);
  52. tor_assert(sl->num_used < idx);
  53. return sl->list[idx];
  54. }
  55. extern INLINE void smartlist_set(smartlist_t *sl, int idx, void *val) {
  56. tor_assert(sl);
  57. tor_assert(idx>=0);
  58. tor_assert(sl->num_used < idx);
  59. sl->list[idx] = val;
  60. }
  61. #else
  62. #define smartlist_len(sl) ((sl)->num_used)
  63. #define smartlist_get(sl, idx) ((sl)->list[idx])
  64. #define smartlist_set(sl, idx, val) ((sl)->list[idx] = (val))
  65. #endif
  66. void smartlist_del(smartlist_t *sl, int idx);
  67. void smartlist_del_keeporder(smartlist_t *sl, int idx);
  68. void smartlist_insert(smartlist_t *sl, int idx, void *val);
  69. void smartlist_sort(smartlist_t *sl,
  70. int (*compare)(const void **a, const void **b));
  71. void smartlist_uniq(smartlist_t *sl,
  72. int (*compare)(const void **a, const void **b),
  73. void (*free_fn)(void *elt));
  74. void smartlist_sort_strings(smartlist_t *sl);
  75. void smartlist_sort_digests(smartlist_t *sl);
  76. void smartlist_uniq_strings(smartlist_t *sl);
  77. void smartlist_uniq_digests(smartlist_t *sl);
  78. void *smartlist_bsearch(smartlist_t *sl, const void *key,
  79. int (*compare)(const void *key, const void **member))
  80. ATTR_PURE;
  81. void smartlist_pqueue_add(smartlist_t *sl,
  82. int (*compare)(const void *a, const void *b),
  83. void *item);
  84. void *smartlist_pqueue_pop(smartlist_t *sl,
  85. int (*compare)(const void *a, const void *b));
  86. void smartlist_pqueue_assert_ok(smartlist_t *sl,
  87. int (*compare)(const void *a, const void *b));
  88. #define SPLIT_SKIP_SPACE 0x01
  89. #define SPLIT_IGNORE_BLANK 0x02
  90. int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
  91. int flags, int max);
  92. char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
  93. size_t *len_out) ATTR_MALLOC;
  94. char *smartlist_join_strings2(smartlist_t *sl, const char *join,
  95. size_t join_len, int terminate, size_t *len_out)
  96. ATTR_MALLOC;
  97. /** Iterate over the items in a smartlist <b>sl</b>, in order. For each item,
  98. * assign it to a new local variable of type <b>type</b> named <b>var</b>, and
  99. * execute the statement <b>cmd</b>. Inside the loop, the loop index can
  100. * be accessed as <b>var</b>_sl_idx.
  101. *
  102. * Example use:
  103. * <pre>
  104. * smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
  105. * SMARTLIST_FOREACH(list, char *, cp,
  106. * {
  107. * printf("%d: %s\n", cp_sl_idx, cp);
  108. * tor_free(cp);
  109. * });
  110. * smarlitst_free(list);
  111. * </pre>
  112. */
  113. #define SMARTLIST_FOREACH(sl, type, var, cmd) \
  114. do { \
  115. int var ## _sl_idx, var ## _sl_len=(sl)->num_used; \
  116. type var; \
  117. for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \
  118. ++var ## _sl_idx) { \
  119. var = (sl)->list[var ## _sl_idx]; \
  120. cmd; \
  121. } } while (0)
  122. #define DECLARE_MAP_FNS(maptype, keytype, prefix) \
  123. typedef struct maptype maptype; \
  124. typedef struct prefix##entry_t *prefix##iter_t; \
  125. maptype* prefix##new(void); \
  126. void* prefix##set(maptype *map, keytype key, void *val); \
  127. void* prefix##get(maptype *map, keytype key); \
  128. void* prefix##remove(maptype *map, keytype key); \
  129. void prefix##free(maptype *map, void (*free_val)(void*)); \
  130. int prefix##isempty(maptype *map); \
  131. int prefix##size(maptype *map); \
  132. prefix##iter_t *prefix##iter_init(maptype *map); \
  133. prefix##iter_t *prefix##iter_next(maptype *map, prefix##iter_t *iter); \
  134. prefix##iter_t *prefix##iter_next_rmv(maptype *map, prefix##iter_t *iter); \
  135. void prefix##iter_get(prefix##iter_t *iter, keytype *keyp, void **valp); \
  136. int prefix##iter_done(prefix##iter_t *iter); \
  137. void prefix##assert_ok(maptype *map);
  138. /* Map from const char * to void *. Implemented with a hash table. */
  139. DECLARE_MAP_FNS(strmap_t, const char *, strmap_);
  140. DECLARE_MAP_FNS(digestmap_t, const char *, digestmap_);
  141. void* strmap_set_lc(strmap_t *map, const char *key, void *val);
  142. void* strmap_get_lc(strmap_t *map, const char *key);
  143. void* strmap_remove_lc(strmap_t *map, const char *key);
  144. #endif