tor  0.4.2.0-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 
20 
21 void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern, ...)
22  CHECK_PRINTF(2, 3);
23 void smartlist_add_vasprintf(struct smartlist_t *sl, const char *pattern,
24  va_list args)
25  CHECK_PRINTF(2, 0);
27 void smartlist_string_remove(smartlist_t *sl, const char *element);
28 int smartlist_contains_string(const smartlist_t *sl, const char *element);
29 int smartlist_pos(const smartlist_t *sl, const void *element);
30 int smartlist_string_pos(const smartlist_t *, const char *elt);
31 int smartlist_contains_string_case(const smartlist_t *sl, const char *element);
32 int smartlist_contains_int_as_string(const smartlist_t *sl, int num);
33 int smartlist_strings_eq(const smartlist_t *sl1, const smartlist_t *sl2);
34 int smartlist_contains_digest(const smartlist_t *sl, const char *element);
35 int smartlist_ints_eq(const smartlist_t *sl1, const smartlist_t *sl2);
36 int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2);
37 void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
38 void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
39 
40 int smartlist_ptrs_eq(const smartlist_t *s1,
41  const smartlist_t *s2);
42 
44  int (*compare)(const void **a, const void **b));
46  int (*compare)(const void **a, const void **b),
47  int *count_out);
48 #define smartlist_get_most_frequent(sl, compare) \
49  smartlist_get_most_frequent_((sl), (compare), NULL)
51  int (*compare)(const void **a, const void **b),
52  void (*free_fn)(void *elt));
53 
58 
61  int *count_out);
63 
67 void *smartlist_bsearch(const smartlist_t *sl, const void *key,
68  int (*compare)(const void *key, const void **member));
69 int smartlist_bsearch_idx(const smartlist_t *sl, const void *key,
70  int (*compare)(const void *key, const void **member),
71  int *found_out);
72 
74  int (*compare)(const void *a, const void *b),
75  int idx_field_offset,
76  void *item);
78  int (*compare)(const void *a, const void *b),
79  int idx_field_offset);
81  int (*compare)(const void *a, const void *b),
82  int idx_field_offset,
83  void *item);
85  int (*compare)(const void *a, const void *b),
86  int idx_field_offset);
87 
88 char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
89  size_t *len_out) ATTR_MALLOC;
90 char *smartlist_join_strings2(smartlist_t *sl, const char *join,
91  size_t join_len, int terminate, size_t *len_out)
92  ATTR_MALLOC;
93 
94 /* Helper: Given two lists of items, possibly of different types, such that
95  * both lists are sorted on some common field (as determined by a comparison
96  * expression <b>cmpexpr</b>), and such that one list (<b>sl1</b>) has no
97  * duplicates on the common field, loop through the lists in lockstep, and
98  * execute <b>unmatched_var2</b> on items in var2 that do not appear in
99  * var1.
100  *
101  * WARNING: It isn't safe to add remove elements from either list while the
102  * loop is in progress.
103  *
104  * Example use:
105  * SMARTLIST_FOREACH_JOIN(routerstatus_list, routerstatus_t *, rs,
106  * routerinfo_list, routerinfo_t *, ri,
107  * tor_memcmp(rs->identity_digest, ri->identity_digest, 20),
108  * log_info(LD_GENERAL,"No match for %s", ri->nickname)) {
109  * log_info(LD_GENERAL, "%s matches routerstatus %p", ri->nickname, rs);
110  * } SMARTLIST_FOREACH_JOIN_END(rs, ri);
111  **/
112 /* The example above unpacks (approximately) to:
113  * int rs_sl_idx = 0, rs_sl_len = smartlist_len(routerstatus_list);
114  * int ri_sl_idx, ri_sl_len = smartlist_len(routerinfo_list);
115  * int rs_ri_cmp;
116  * routerstatus_t *rs;
117  * routerinfo_t *ri;
118  * for (; ri_sl_idx < ri_sl_len; ++ri_sl_idx) {
119  * ri = smartlist_get(routerinfo_list, ri_sl_idx);
120  * while (rs_sl_idx < rs_sl_len) {
121  * rs = smartlist_get(routerstatus_list, rs_sl_idx);
122  * rs_ri_cmp = tor_memcmp(rs->identity_digest, ri->identity_digest, 20);
123  * if (rs_ri_cmp > 0) {
124  * break;
125  * } else if (rs_ri_cmp == 0) {
126  * goto matched_ri;
127  * } else {
128  * ++rs_sl_idx;
129  * }
130  * }
131  * log_info(LD_GENERAL,"No match for %s", ri->nickname);
132  * continue;
133  * matched_ri: {
134  * log_info(LD_GENERAL,"%s matches with routerstatus %p",ri->nickname,rs);
135  * }
136  * }
137  */
138 #define SMARTLIST_FOREACH_JOIN(sl1, type1, var1, sl2, type2, var2, \
139  cmpexpr, unmatched_var2) \
140  STMT_BEGIN \
141  int var1 ## _sl_idx = 0, var1 ## _sl_len=(sl1)->num_used; \
142  int var2 ## _sl_idx = 0, var2 ## _sl_len=(sl2)->num_used; \
143  int var1 ## _ ## var2 ## _cmp; \
144  type1 var1; \
145  type2 var2; \
146  for (; var2##_sl_idx < var2##_sl_len; ++var2##_sl_idx) { \
147  var2 = (sl2)->list[var2##_sl_idx]; \
148  while (var1##_sl_idx < var1##_sl_len) { \
149  var1 = (sl1)->list[var1##_sl_idx]; \
150  var1##_##var2##_cmp = (cmpexpr); \
151  if (var1##_##var2##_cmp > 0) { \
152  break; \
153  } else if (var1##_##var2##_cmp == 0) { \
154  goto matched_##var2; \
155  } else { \
156  ++var1##_sl_idx; \
157  } \
158  } \
159  /* Ran out of v1, or no match for var2. */ \
160  unmatched_var2; \
161  continue; \
162  matched_##var2: ; \
163 
164 #define SMARTLIST_FOREACH_JOIN_END(var1, var2) \
165  } \
166  STMT_END
167 
168 #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_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
void smartlist_pqueue_remove(smartlist_t *sl, int(*compare)(const void *a, const void *b), int idx_field_offset, void *item)
Definition: smartlist.c:779
void smartlist_pqueue_assert_ok(smartlist_t *sl, int(*compare)(const void *a, const void *b), int idx_field_offset)
Definition: smartlist.c:803
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_pqueue_add(smartlist_t *sl, int(*compare)(const void *a, const void *b), int idx_field_offset, void *item)
Definition: smartlist.c:726
void * smartlist_pqueue_pop(smartlist_t *sl, int(*compare)(const void *a, const void *b), int idx_field_offset)
Definition: smartlist.c:755
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
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
int smartlist_contains_string(const smartlist_t *sl, const char *element)
Definition: smartlist.c:93