smartlist.h 7.7 KB

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