tor  0.4.2.1-alpha-dev
smartlist.h
Go to the documentation of this file.
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 
6 #ifndef TOR_SMARTLIST_H
7 #define TOR_SMARTLIST_H
8 
15 #include <stdarg.h>
16 #include <stddef.h>
17 
21 
22 void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern, ...)
23  CHECK_PRINTF(2, 3);
24 void smartlist_add_vasprintf(struct smartlist_t *sl, const char *pattern,
25  va_list args)
26  CHECK_PRINTF(2, 0);
28 void smartlist_string_remove(smartlist_t *sl, const char *element);
29 int smartlist_contains_string(const smartlist_t *sl, const char *element);
30 int smartlist_pos(const smartlist_t *sl, const void *element);
31 int smartlist_string_pos(const smartlist_t *, const char *elt);
32 int smartlist_contains_string_case(const smartlist_t *sl, const char *element);
33 int smartlist_contains_int_as_string(const smartlist_t *sl, int num);
34 int smartlist_strings_eq(const smartlist_t *sl1, const smartlist_t *sl2);
35 int smartlist_contains_digest(const smartlist_t *sl, const char *element);
36 int smartlist_ints_eq(const smartlist_t *sl1, const smartlist_t *sl2);
37 int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2);
38 void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
39 void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
40 
41 int smartlist_ptrs_eq(const smartlist_t *s1,
42  const smartlist_t *s2);
43 
45  int (*compare)(const void **a, const void **b));
47  int (*compare)(const void **a, const void **b),
48  int *count_out);
49 #define smartlist_get_most_frequent(sl, compare) \
50  smartlist_get_most_frequent_((sl), (compare), NULL)
52  int (*compare)(const void **a, const void **b),
53  void (*free_fn)(void *elt));
54 
59 
62  int *count_out);
64 
68 void *smartlist_bsearch(const smartlist_t *sl, const void *key,
69  int (*compare)(const void *key, const void **member));
70 int smartlist_bsearch_idx(const smartlist_t *sl, const void *key,
71  int (*compare)(const void *key, const void **member),
72  int *found_out);
73 
75  int (*compare)(const void *a, const void *b),
76  ptrdiff_t idx_field_offset,
77  void *item);
79  int (*compare)(const void *a, const void *b),
80  ptrdiff_t idx_field_offset);
82  int (*compare)(const void *a, const void *b),
83  ptrdiff_t idx_field_offset,
84  void *item);
86  int (*compare)(const void *a, const void *b),
87  ptrdiff_t idx_field_offset);
88 
89 char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
90  size_t *len_out) ATTR_MALLOC;
91 char *smartlist_join_strings2(smartlist_t *sl, const char *join,
92  size_t join_len, int terminate, size_t *len_out)
93  ATTR_MALLOC;
94 
95 /* Helper: Given two lists of items, possibly of different types, such that
96  * both lists are sorted on some common field (as determined by a comparison
97  * expression <b>cmpexpr</b>), and such that one list (<b>sl1</b>) has no
98  * duplicates on the common field, loop through the lists in lockstep, and
99  * execute <b>unmatched_var2</b> on items in var2 that do not appear in
100  * var1.
101  *
102  * WARNING: It isn't safe to add remove elements from either list while the
103  * loop is in progress.
104  *
105  * Example use:
106  * SMARTLIST_FOREACH_JOIN(routerstatus_list, routerstatus_t *, rs,
107  * routerinfo_list, routerinfo_t *, ri,
108  * tor_memcmp(rs->identity_digest, ri->identity_digest, 20),
109  * log_info(LD_GENERAL,"No match for %s", ri->nickname)) {
110  * log_info(LD_GENERAL, "%s matches routerstatus %p", ri->nickname, rs);
111  * } SMARTLIST_FOREACH_JOIN_END(rs, ri);
112  **/
113 /* The example above unpacks (approximately) to:
114  * int rs_sl_idx = 0, rs_sl_len = smartlist_len(routerstatus_list);
115  * int ri_sl_idx, ri_sl_len = smartlist_len(routerinfo_list);
116  * int rs_ri_cmp;
117  * routerstatus_t *rs;
118  * routerinfo_t *ri;
119  * for (; ri_sl_idx < ri_sl_len; ++ri_sl_idx) {
120  * ri = smartlist_get(routerinfo_list, ri_sl_idx);
121  * while (rs_sl_idx < rs_sl_len) {
122  * rs = smartlist_get(routerstatus_list, rs_sl_idx);
123  * rs_ri_cmp = tor_memcmp(rs->identity_digest, ri->identity_digest, 20);
124  * if (rs_ri_cmp > 0) {
125  * break;
126  * } else if (rs_ri_cmp == 0) {
127  * goto matched_ri;
128  * } else {
129  * ++rs_sl_idx;
130  * }
131  * }
132  * log_info(LD_GENERAL,"No match for %s", ri->nickname);
133  * continue;
134  * matched_ri: {
135  * log_info(LD_GENERAL,"%s matches with routerstatus %p",ri->nickname,rs);
136  * }
137  * }
138  */
139 #define SMARTLIST_FOREACH_JOIN(sl1, type1, var1, sl2, type2, var2, \
140  cmpexpr, unmatched_var2) \
141  STMT_BEGIN \
142  int var1 ## _sl_idx = 0, var1 ## _sl_len=(sl1)->num_used; \
143  int var2 ## _sl_idx = 0, var2 ## _sl_len=(sl2)->num_used; \
144  int var1 ## _ ## var2 ## _cmp; \
145  type1 var1; \
146  type2 var2; \
147  for (; var2##_sl_idx < var2##_sl_len; ++var2##_sl_idx) { \
148  var2 = (sl2)->list[var2##_sl_idx]; \
149  while (var1##_sl_idx < var1##_sl_len) { \
150  var1 = (sl1)->list[var1##_sl_idx]; \
151  var1##_##var2##_cmp = (cmpexpr); \
152  if (var1##_##var2##_cmp > 0) { \
153  break; \
154  } else if (var1##_##var2##_cmp == 0) { \
155  goto matched_##var2; \
156  } else { \
157  ++var1##_sl_idx; \
158  } \
159  } \
160  /* Ran out of v1, or no match for var2. */ \
161  unmatched_var2; \
162  continue; \
163  matched_##var2: ; \
164 
165 #define SMARTLIST_FOREACH_JOIN_END(var1, var2) \
166  } \
167  STMT_END
168 
169 #endif /* !defined(TOR_SMARTLIST_H) */
void smartlist_add_vasprintf(struct smartlist_t *sl, const char *pattern, va_list args)
Definition: smartlist.c:46
int smartlist_pos(const smartlist_t *sl, const void *element)
Definition: smartlist.c:119
void smartlist_uniq_strings(smartlist_t *sl)
Definition: smartlist.c:574
void smartlist_pqueue_assert_ok(smartlist_t *sl, int(*compare)(const void *a, const void *b), ptrdiff_t idx_field_offset)
Definition: smartlist.c:803
void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2)
Definition: smartlist.c:264
int smartlist_contains_string_case(const smartlist_t *sl, const char *element)
Definition: smartlist.c:133
void smartlist_sort_strings(smartlist_t *sl)
Definition: smartlist.c:549
Header for smartlist_split.c.
int smartlist_contains_digest(const smartlist_t *sl, const char *element)
Definition: smartlist.c:223
char * smartlist_join_strings2(smartlist_t *sl, const char *join, size_t join_len, int terminate, size_t *len_out) ATTR_MALLOC
Definition: smartlist.c:291
int smartlist_ints_eq(const smartlist_t *sl1, const smartlist_t *sl2)
Definition: smartlist.c:176
Top-level declarations for the smartlist_t dynamic array type.
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
Definition: smartlist.c:334
int smartlist_ptrs_eq(const smartlist_t *s1, const smartlist_t *s2)
Definition: smartlist.c:198
Macros for iterating over the elements of a smartlist_t.
int smartlist_bsearch_idx(const smartlist_t *sl, const void *key, int(*compare)(const void *key, const void **member), int *found_out)
Definition: smartlist.c:428
void * smartlist_bsearch(const smartlist_t *sl, const void *key, int(*compare)(const void *key, const void **member))
Definition: smartlist.c:411
const char * smartlist_get_most_frequent_string(smartlist_t *sl)
Definition: smartlist.c:556
int smartlist_strings_eq(const smartlist_t *sl1, const smartlist_t *sl2)
Definition: smartlist.c:157
void smartlist_uniq(smartlist_t *sl, int(*compare)(const void **a, const void **b), void(*free_fn)(void *a))
Definition: smartlist.c:390
void smartlist_string_remove(smartlist_t *sl, const char *element)
Definition: smartlist.c:74
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern,...)
Definition: smartlist.c:36
void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2)
Definition: smartlist.c:249
void void void smartlist_reverse(smartlist_t *sl)
Definition: smartlist.c:59
void smartlist_sort_digests(smartlist_t *sl)
Definition: smartlist.c:824
void smartlist_sort_pointers(smartlist_t *sl)
Definition: smartlist.c:594
const uint8_t * smartlist_get_most_frequent_digest256(smartlist_t *sl)
Definition: smartlist.c:854
void smartlist_uniq_digests256(smartlist_t *sl)
Definition: smartlist.c:863
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out) ATTR_MALLOC
Definition: smartlist.c:279
void smartlist_uniq_digests(smartlist_t *sl)
Definition: smartlist.c:832
const char * smartlist_get_most_frequent_string_(smartlist_t *sl, int *count_out)
Definition: smartlist.c:566
void smartlist_sort_digests256(smartlist_t *sl)
Definition: smartlist.c:846
void smartlist_pqueue_remove(smartlist_t *sl, int(*compare)(const void *a, const void *b), ptrdiff_t idx_field_offset, void *item)
Definition: smartlist.c:779
int smartlist_string_pos(const smartlist_t *, const char *elt)
Definition: smartlist.c:106
void * smartlist_get_most_frequent_(const smartlist_t *sl, int(*compare)(const void **a, const void **b), int *count_out)
Definition: smartlist.c:348
int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2)
Definition: smartlist.c:236
int smartlist_contains_int_as_string(const smartlist_t *sl, int num)
Definition: smartlist.c:147
void smartlist_pqueue_add(smartlist_t *sl, int(*compare)(const void *a, const void *b), ptrdiff_t idx_field_offset, void *item)
Definition: smartlist.c:726
int smartlist_contains_string(const smartlist_t *sl, const char *element)
Definition: smartlist.c:93
void * smartlist_pqueue_pop(smartlist_t *sl, int(*compare)(const void *a, const void *b), ptrdiff_t idx_field_offset)
Definition: smartlist.c:755