smartlist.h 7.7 KB

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