smartlist.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #ifndef TOR_SMARTLIST_H
  6. #define TOR_SMARTLIST_H
  7. /**
  8. * \file smartlist.h
  9. *
  10. * \brief Header for smartlist.c
  11. **/
  12. #include <stdarg.h>
  13. #include "lib/smartlist_core/smartlist_core.h"
  14. #include "lib/smartlist_core/smartlist_foreach.h"
  15. #include "lib/smartlist_core/smartlist_split.h"
  16. void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern, ...)
  17. CHECK_PRINTF(2, 3);
  18. void smartlist_add_vasprintf(struct smartlist_t *sl, const char *pattern,
  19. va_list args)
  20. CHECK_PRINTF(2, 0);
  21. void smartlist_reverse(smartlist_t *sl);
  22. void smartlist_string_remove(smartlist_t *sl, const char *element);
  23. int smartlist_contains_string(const smartlist_t *sl, const char *element);
  24. int smartlist_pos(const smartlist_t *sl, const void *element);
  25. int smartlist_string_pos(const smartlist_t *, const char *elt);
  26. int smartlist_contains_string_case(const smartlist_t *sl, const char *element);
  27. int smartlist_contains_int_as_string(const smartlist_t *sl, int num);
  28. int smartlist_strings_eq(const smartlist_t *sl1, const smartlist_t *sl2);
  29. int smartlist_contains_digest(const smartlist_t *sl, const char *element);
  30. int smartlist_ints_eq(const smartlist_t *sl1, const smartlist_t *sl2);
  31. int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2);
  32. void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
  33. void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
  34. int smartlist_ptrs_eq(const smartlist_t *s1,
  35. const smartlist_t *s2);
  36. void smartlist_sort(smartlist_t *sl,
  37. int (*compare)(const void **a, const void **b));
  38. void *smartlist_get_most_frequent_(const smartlist_t *sl,
  39. int (*compare)(const void **a, const void **b),
  40. int *count_out);
  41. #define smartlist_get_most_frequent(sl, compare) \
  42. smartlist_get_most_frequent_((sl), (compare), NULL)
  43. void smartlist_uniq(smartlist_t *sl,
  44. int (*compare)(const void **a, const void **b),
  45. void (*free_fn)(void *elt));
  46. void smartlist_sort_strings(smartlist_t *sl);
  47. void smartlist_sort_digests(smartlist_t *sl);
  48. void smartlist_sort_digests256(smartlist_t *sl);
  49. void smartlist_sort_pointers(smartlist_t *sl);
  50. const char *smartlist_get_most_frequent_string(smartlist_t *sl);
  51. const char *smartlist_get_most_frequent_string_(smartlist_t *sl,
  52. int *count_out);
  53. const uint8_t *smartlist_get_most_frequent_digest256(smartlist_t *sl);
  54. void smartlist_uniq_strings(smartlist_t *sl);
  55. void smartlist_uniq_digests(smartlist_t *sl);
  56. void smartlist_uniq_digests256(smartlist_t *sl);
  57. void *smartlist_bsearch(const smartlist_t *sl, const void *key,
  58. int (*compare)(const void *key, const void **member));
  59. int smartlist_bsearch_idx(const smartlist_t *sl, const void *key,
  60. int (*compare)(const void *key, const void **member),
  61. int *found_out);
  62. void smartlist_pqueue_add(smartlist_t *sl,
  63. int (*compare)(const void *a, const void *b),
  64. int idx_field_offset,
  65. void *item);
  66. void *smartlist_pqueue_pop(smartlist_t *sl,
  67. int (*compare)(const void *a, const void *b),
  68. int idx_field_offset);
  69. void smartlist_pqueue_remove(smartlist_t *sl,
  70. int (*compare)(const void *a, const void *b),
  71. int idx_field_offset,
  72. void *item);
  73. void smartlist_pqueue_assert_ok(smartlist_t *sl,
  74. int (*compare)(const void *a, const void *b),
  75. int idx_field_offset);
  76. char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
  77. size_t *len_out) ATTR_MALLOC;
  78. char *smartlist_join_strings2(smartlist_t *sl, const char *join,
  79. size_t join_len, int terminate, size_t *len_out)
  80. ATTR_MALLOC;
  81. /* Helper: Given two lists of items, possibly of different types, such that
  82. * both lists are sorted on some common field (as determined by a comparison
  83. * expression <b>cmpexpr</b>), and such that one list (<b>sl1</b>) has no
  84. * duplicates on the common field, loop through the lists in lockstep, and
  85. * execute <b>unmatched_var2</b> on items in var2 that do not appear in
  86. * var1.
  87. *
  88. * WARNING: It isn't safe to add remove elements from either list while the
  89. * loop is in progress.
  90. *
  91. * Example use:
  92. * SMARTLIST_FOREACH_JOIN(routerstatus_list, routerstatus_t *, rs,
  93. * routerinfo_list, routerinfo_t *, ri,
  94. * tor_memcmp(rs->identity_digest, ri->identity_digest, 20),
  95. * log_info(LD_GENERAL,"No match for %s", ri->nickname)) {
  96. * log_info(LD_GENERAL, "%s matches routerstatus %p", ri->nickname, rs);
  97. * } SMARTLIST_FOREACH_JOIN_END(rs, ri);
  98. **/
  99. /* The example above unpacks (approximately) to:
  100. * int rs_sl_idx = 0, rs_sl_len = smartlist_len(routerstatus_list);
  101. * int ri_sl_idx, ri_sl_len = smartlist_len(routerinfo_list);
  102. * int rs_ri_cmp;
  103. * routerstatus_t *rs;
  104. * routerinfo_t *ri;
  105. * for (; ri_sl_idx < ri_sl_len; ++ri_sl_idx) {
  106. * ri = smartlist_get(routerinfo_list, ri_sl_idx);
  107. * while (rs_sl_idx < rs_sl_len) {
  108. * rs = smartlist_get(routerstatus_list, rs_sl_idx);
  109. * rs_ri_cmp = tor_memcmp(rs->identity_digest, ri->identity_digest, 20);
  110. * if (rs_ri_cmp > 0) {
  111. * break;
  112. * } else if (rs_ri_cmp == 0) {
  113. * goto matched_ri;
  114. * } else {
  115. * ++rs_sl_idx;
  116. * }
  117. * }
  118. * log_info(LD_GENERAL,"No match for %s", ri->nickname);
  119. * continue;
  120. * matched_ri: {
  121. * log_info(LD_GENERAL,"%s matches with routerstatus %p",ri->nickname,rs);
  122. * }
  123. * }
  124. */
  125. #define SMARTLIST_FOREACH_JOIN(sl1, type1, var1, sl2, type2, var2, \
  126. cmpexpr, unmatched_var2) \
  127. STMT_BEGIN \
  128. int var1 ## _sl_idx = 0, var1 ## _sl_len=(sl1)->num_used; \
  129. int var2 ## _sl_idx = 0, var2 ## _sl_len=(sl2)->num_used; \
  130. int var1 ## _ ## var2 ## _cmp; \
  131. type1 var1; \
  132. type2 var2; \
  133. for (; var2##_sl_idx < var2##_sl_len; ++var2##_sl_idx) { \
  134. var2 = (sl2)->list[var2##_sl_idx]; \
  135. while (var1##_sl_idx < var1##_sl_len) { \
  136. var1 = (sl1)->list[var1##_sl_idx]; \
  137. var1##_##var2##_cmp = (cmpexpr); \
  138. if (var1##_##var2##_cmp > 0) { \
  139. break; \
  140. } else if (var1##_##var2##_cmp == 0) { \
  141. goto matched_##var2; \
  142. } else { \
  143. ++var1##_sl_idx; \
  144. } \
  145. } \
  146. /* Ran out of v1, or no match for var2. */ \
  147. unmatched_var2; \
  148. continue; \
  149. matched_##var2: ; \
  150. #define SMARTLIST_FOREACH_JOIN_END(var1, var2) \
  151. } \
  152. STMT_END
  153. #endif /* !defined(TOR_CONTAINER_H) */