smartlist.h 7.9 KB

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