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