1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2013, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #ifndef TOR_CONTAINER_H
7 #define TOR_CONTAINER_H
11 /** A resizeable list of pointers, with associated helpful functionality.
13 * The members of this struct are exposed only so that macros and inlines can
14 * use them; all access to smartlist internals should go through the functions
15 * and macros defined here.
17 typedef struct smartlist_t
{
19 /** <b>list</b> has enough capacity to store exactly <b>capacity</b> elements
20 * before it needs to be resized. Only the first <b>num_used</b> (\<=
21 * capacity) elements point to valid data.
29 smartlist_t
*smartlist_new(void);
30 void smartlist_free(smartlist_t
*sl
);
31 void smartlist_clear(smartlist_t
*sl
);
32 void smartlist_add(smartlist_t
*sl
, void *element
);
33 void smartlist_add_all(smartlist_t
*sl
, const smartlist_t
*s2
);
34 void smartlist_remove(smartlist_t
*sl
, const void *element
);
35 void *smartlist_pop_last(smartlist_t
*sl
);
36 void smartlist_reverse(smartlist_t
*sl
);
37 void smartlist_string_remove(smartlist_t
*sl
, const char *element
);
38 int smartlist_contains(const smartlist_t
*sl
, const void *element
);
39 int smartlist_contains_string(const smartlist_t
*sl
, const char *element
);
40 int smartlist_string_pos(const smartlist_t
*, const char *elt
);
41 int smartlist_contains_string_case(const smartlist_t
*sl
, const char *element
);
42 int smartlist_contains_int_as_string(const smartlist_t
*sl
, int num
);
43 int smartlist_strings_eq(const smartlist_t
*sl1
, const smartlist_t
*sl2
);
44 int smartlist_contains_digest(const smartlist_t
*sl
, const char *element
);
45 int smartlist_overlap(const smartlist_t
*sl1
, const smartlist_t
*sl2
);
46 void smartlist_intersect(smartlist_t
*sl1
, const smartlist_t
*sl2
);
47 void smartlist_subtract(smartlist_t
*sl1
, const smartlist_t
*sl2
);
49 /* smartlist_choose() is defined in crypto.[ch] */
50 #ifdef DEBUG_SMARTLIST
51 /** Return the number of items in sl.
53 static INLINE
int smartlist_len(const smartlist_t
*sl
);
54 static INLINE
int smartlist_len(const smartlist_t
*sl
) {
56 return (sl
)->num_used
;
58 /** Return the <b>idx</b>th element of sl.
60 static INLINE
void *smartlist_get(const smartlist_t
*sl
, int idx
);
61 static INLINE
void *smartlist_get(const smartlist_t
*sl
, int idx
) {
64 tor_assert(sl
->num_used
> idx
);
67 static INLINE
void smartlist_set(smartlist_t
*sl
, int idx
, void *val
) {
70 tor_assert(sl
->num_used
> idx
);
74 #define smartlist_len(sl) ((sl)->num_used)
75 #define smartlist_get(sl, idx) ((sl)->list[idx])
76 #define smartlist_set(sl, idx, val) ((sl)->list[idx] = (val))
79 /** Exchange the elements at indices <b>idx1</b> and <b>idx2</b> of the
80 * smartlist <b>sl</b>. */
81 static INLINE
void smartlist_swap(smartlist_t
*sl
, int idx1
, int idx2
)
84 void *elt
= smartlist_get(sl
, idx1
);
85 smartlist_set(sl
, idx1
, smartlist_get(sl
, idx2
));
86 smartlist_set(sl
, idx2
, elt
);
90 void smartlist_del(smartlist_t
*sl
, int idx
);
91 void smartlist_del_keeporder(smartlist_t
*sl
, int idx
);
92 void smartlist_insert(smartlist_t
*sl
, int idx
, void *val
);
93 void smartlist_sort(smartlist_t
*sl
,
94 int (*compare
)(const void **a
, const void **b
));
95 void *smartlist_get_most_frequent(const smartlist_t
*sl
,
96 int (*compare
)(const void **a
, const void **b
));
97 void smartlist_uniq(smartlist_t
*sl
,
98 int (*compare
)(const void **a
, const void **b
),
99 void (*free_fn
)(void *elt
));
101 void smartlist_sort_strings(smartlist_t
*sl
);
102 void smartlist_sort_digests(smartlist_t
*sl
);
103 void smartlist_sort_digests256(smartlist_t
*sl
);
105 char *smartlist_get_most_frequent_string(smartlist_t
*sl
);
106 char *smartlist_get_most_frequent_digest256(smartlist_t
*sl
);
108 void smartlist_uniq_strings(smartlist_t
*sl
);
109 void smartlist_uniq_digests(smartlist_t
*sl
);
110 void smartlist_uniq_digests256(smartlist_t
*sl
);
111 void *smartlist_bsearch(smartlist_t
*sl
, const void *key
,
112 int (*compare
)(const void *key
, const void **member
));
113 int smartlist_bsearch_idx(const smartlist_t
*sl
, const void *key
,
114 int (*compare
)(const void *key
, const void **member
),
117 void smartlist_pqueue_add(smartlist_t
*sl
,
118 int (*compare
)(const void *a
, const void *b
),
119 int idx_field_offset
,
121 void *smartlist_pqueue_pop(smartlist_t
*sl
,
122 int (*compare
)(const void *a
, const void *b
),
123 int idx_field_offset
);
124 void smartlist_pqueue_remove(smartlist_t
*sl
,
125 int (*compare
)(const void *a
, const void *b
),
126 int idx_field_offset
,
128 void smartlist_pqueue_assert_ok(smartlist_t
*sl
,
129 int (*compare
)(const void *a
, const void *b
),
130 int idx_field_offset
);
132 #define SPLIT_SKIP_SPACE 0x01
133 #define SPLIT_IGNORE_BLANK 0x02
134 #define SPLIT_STRIP_SPACE 0x04
135 int smartlist_split_string(smartlist_t
*sl
, const char *str
, const char *sep
,
137 char *smartlist_join_strings(smartlist_t
*sl
, const char *join
, int terminate
,
138 size_t *len_out
) ATTR_MALLOC
;
139 char *smartlist_join_strings2(smartlist_t
*sl
, const char *join
,
140 size_t join_len
, int terminate
, size_t *len_out
)
143 /** Iterate over the items in a smartlist <b>sl</b>, in order. For each item,
144 * assign it to a new local variable of type <b>type</b> named <b>var</b>, and
145 * execute the statements inside the loop body. Inside the loop, the loop
146 * index can be accessed as <b>var</b>_sl_idx and the length of the list can
147 * be accessed as <b>var</b>_sl_len.
149 * NOTE: Do not change the length of the list while the loop is in progress,
150 * unless you adjust the _sl_len variable correspondingly. See second example
155 * smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
156 * SMARTLIST_FOREACH_BEGIN(list, char *, cp) {
157 * printf("%d: %s\n", cp_sl_idx, cp);
159 * } SMARTLIST_FOREACH_END(cp);
160 * smartlist_free(list);
163 * Example use (advanced):
165 * SMARTLIST_FOREACH_BEGIN(list, char *, cp) {
166 * if (!strcmp(cp, "junk")) {
168 * SMARTLIST_DEL_CURRENT(list, cp);
170 * } SMARTLIST_FOREACH_END(cp);
173 /* Note: these macros use token pasting, and reach into smartlist internals.
174 * This can make them a little daunting. Here's the approximate unpacking of
175 * the above examples, for entertainment value:
178 * smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
180 * int cp_sl_idx, cp_sl_len = smartlist_len(list);
182 * for (cp_sl_idx = 0; cp_sl_idx < cp_sl_len; ++cp_sl_idx) {
183 * cp = smartlist_get(list, cp_sl_idx);
184 * printf("%d: %s\n", cp_sl_idx, cp);
188 * smartlist_free(list);
193 * int cp_sl_idx, cp_sl_len = smartlist_len(list);
195 * for (cp_sl_idx = 0; cp_sl_idx < cp_sl_len; ++cp_sl_idx) {
196 * cp = smartlist_get(list, cp_sl_idx);
197 * if (!strcmp(cp, "junk")) {
199 * smartlist_del(list, cp_sl_idx);
207 #define SMARTLIST_FOREACH_BEGIN(sl, type, var) \
209 int var ## _sl_idx, var ## _sl_len=(sl)->num_used; \
211 for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \
212 ++var ## _sl_idx) { \
213 var = (sl)->list[var ## _sl_idx];
215 #define SMARTLIST_FOREACH_END(var) \
220 * An alias for SMARTLIST_FOREACH_BEGIN and SMARTLIST_FOREACH_END, using
221 * <b>cmd</b> as the loop body. This wrapper is here for convenience with
224 * By convention, we do not use this for loops which nest, or for loops over
225 * 10 lines or so. Use SMARTLIST_FOREACH_{BEGIN,END} for those.
227 #define SMARTLIST_FOREACH(sl, type, var, cmd) \
228 SMARTLIST_FOREACH_BEGIN(sl,type,var) { \
230 } SMARTLIST_FOREACH_END(var)
232 /** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
233 * with the variable <b>var</b>, remove the current element in a way that
234 * won't confuse the loop. */
235 #define SMARTLIST_DEL_CURRENT(sl, var) \
237 smartlist_del(sl, var ## _sl_idx); \
242 /** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
243 * with the variable <b>var</b>, replace the current element with <b>val</b>.
244 * Does not deallocate the current value of <b>var</b>.
246 #define SMARTLIST_REPLACE_CURRENT(sl, var, val) \
248 smartlist_set(sl, var ## _sl_idx, val); \
251 /* Helper: Given two lists of items, possibly of different types, such that
252 * both lists are sorted on some common field (as determined by a comparison
253 * expression <b>cmpexpr</b>), and such that one list (<b>sl1</b>) has no
254 * duplicates on the common field, loop through the lists in lockstep, and
255 * execute <b>unmatched_var2</b> on items in var2 that do not appear in
258 * WARNING: It isn't safe to add remove elements from either list while the
259 * loop is in progress.
262 * SMARTLIST_FOREACH_JOIN(routerstatus_list, routerstatus_t *, rs,
263 * routerinfo_list, routerinfo_t *, ri,
264 * tor_memcmp(rs->identity_digest, ri->identity_digest, 20),
265 * log_info(LD_GENERAL,"No match for %s", ri->nickname)) {
266 * log_info(LD_GENERAL, "%s matches routerstatus %p", ri->nickname, rs);
267 * } SMARTLIST_FOREACH_JOIN_END(rs, ri);
269 /* The example above unpacks (approximately) to:
270 * int rs_sl_idx = 0, rs_sl_len = smartlist_len(routerstatus_list);
271 * int ri_sl_idx, ri_sl_len = smartlist_len(routerinfo_list);
273 * routerstatus_t *rs;
275 * for (; ri_sl_idx < ri_sl_len; ++ri_sl_idx) {
276 * ri = smartlist_get(routerinfo_list, ri_sl_idx);
277 * while (rs_sl_idx < rs_sl_len) {
278 * rs = smartlist_get(routerstatus_list, rs_sl_idx);
279 * rs_ri_cmp = tor_memcmp(rs->identity_digest, ri->identity_digest, 20);
280 * if (rs_ri_cmp > 0) {
282 * } else if (rs_ri_cmp == 0) {
288 * log_info(LD_GENERAL,"No match for %s", ri->nickname);
291 * log_info(LD_GENERAL,"%s matches with routerstatus %p",ri->nickname,rs);
295 #define SMARTLIST_FOREACH_JOIN(sl1, type1, var1, sl2, type2, var2, \
296 cmpexpr, unmatched_var2) \
298 int var1 ## _sl_idx = 0, var1 ## _sl_len=(sl1)->num_used; \
299 int var2 ## _sl_idx = 0, var2 ## _sl_len=(sl2)->num_used; \
300 int var1 ## _ ## var2 ## _cmp; \
303 for (; var2##_sl_idx < var2##_sl_len; ++var2##_sl_idx) { \
304 var2 = (sl2)->list[var2##_sl_idx]; \
305 while (var1##_sl_idx < var1##_sl_len) { \
306 var1 = (sl1)->list[var1##_sl_idx]; \
307 var1##_##var2##_cmp = (cmpexpr); \
308 if (var1##_##var2##_cmp > 0) { \
310 } else if (var1##_##var2##_cmp == 0) { \
311 goto matched_##var2; \
316 /* Ran out of v1, or no match for var2. */ \
321 #define SMARTLIST_FOREACH_JOIN_END(var1, var2) \
325 #define DECLARE_MAP_FNS(maptype, keytype, prefix) \
326 typedef struct maptype maptype; \
327 typedef struct prefix##entry_t *prefix##iter_t; \
328 maptype* prefix##new(void); \
329 void* prefix##set(maptype *map, keytype key, void *val); \
330 void* prefix##get(const maptype *map, keytype key); \
331 void* prefix##remove(maptype *map, keytype key); \
332 void prefix##free(maptype *map, void (*free_val)(void*)); \
333 int prefix##isempty(const maptype *map); \
334 int prefix##size(const maptype *map); \
335 prefix##iter_t *prefix##iter_init(maptype *map); \
336 prefix##iter_t *prefix##iter_next(maptype *map, prefix##iter_t *iter); \
337 prefix##iter_t *prefix##iter_next_rmv(maptype *map, prefix##iter_t *iter); \
338 void prefix##iter_get(prefix##iter_t *iter, keytype *keyp, void **valp); \
339 int prefix##iter_done(prefix##iter_t *iter); \
340 void prefix##assert_ok(const maptype *map)
342 /* Map from const char * to void *. Implemented with a hash table. */
343 DECLARE_MAP_FNS(strmap_t
, const char *, strmap_
);
344 /* Map from const char[DIGEST_LEN] to void *. Implemented with a hash table. */
345 DECLARE_MAP_FNS(digestmap_t
, const char *, digestmap_
);
347 #undef DECLARE_MAP_FNS
349 /** Iterates over the key-value pairs in a map <b>map</b> in order.
350 * <b>prefix</b> is as for DECLARE_MAP_FNS (i.e., strmap_ or digestmap_).
351 * The map's keys and values are of type keytype and valtype respectively;
352 * each iteration assigns them to keyvar and valvar.
355 * MAP_FOREACH(digestmap_, m, const char *, k, routerinfo_t *, r) {
359 /* Unpacks to, approximately:
361 * digestmap_iter_t *k_iter;
362 * for (k_iter = digestmap_iter_init(m); !digestmap_iter_done(k_iter);
363 * k_iter = digestmap_iter_next(m, k_iter)) {
367 * digestmap_iter_get(k_iter, &k, &r_voidp);
373 #define MAP_FOREACH(prefix, map, keytype, keyvar, valtype, valvar) \
375 prefix##iter_t *keyvar##_iter; \
376 for (keyvar##_iter = prefix##iter_init(map); \
377 !prefix##iter_done(keyvar##_iter); \
378 keyvar##_iter = prefix##iter_next(map, keyvar##_iter)) { \
380 void *valvar##_voidp; \
382 prefix##iter_get(keyvar##_iter, &keyvar, &valvar##_voidp); \
383 valvar = valvar##_voidp;
385 /** As MAP_FOREACH, except allows members to be removed from the map
386 * during the iteration via MAP_DEL_CURRENT. Example use:
389 * MAP_FOREACH(digestmap_, m, const char *, k, routerinfo_t *, r) {
390 * if (is_very_old(r))
391 * MAP_DEL_CURRENT(k);
394 /* Unpacks to, approximately:
396 * digestmap_iter_t *k_iter;
398 * for (k_iter = digestmap_iter_init(m); !digestmap_iter_done(k_iter);
399 * k_iter = k_del ? digestmap_iter_next(m, k_iter)
400 * : digestmap_iter_next_rmv(m, k_iter)) {
405 * digestmap_iter_get(k_iter, &k, &r_voidp);
407 * if (is_very_old(r)) {
413 #define MAP_FOREACH_MODIFY(prefix, map, keytype, keyvar, valtype, valvar) \
415 prefix##iter_t *keyvar##_iter; \
416 int keyvar##_del=0; \
417 for (keyvar##_iter = prefix##iter_init(map); \
418 !prefix##iter_done(keyvar##_iter); \
419 keyvar##_iter = keyvar##_del ? \
420 prefix##iter_next_rmv(map, keyvar##_iter) : \
421 prefix##iter_next(map, keyvar##_iter)) { \
423 void *valvar##_voidp; \
426 prefix##iter_get(keyvar##_iter, &keyvar, &valvar##_voidp); \
427 valvar = valvar##_voidp;
429 /** Used with MAP_FOREACH_MODIFY to remove the currently-iterated-upon
430 * member of the map. */
431 #define MAP_DEL_CURRENT(keyvar) \
436 /** Used to end a MAP_FOREACH() block. */
437 #define MAP_FOREACH_END } STMT_END ;
439 /** As MAP_FOREACH, but does not require declaration of prefix or keytype.
441 * DIGESTMAP_FOREACH(m, k, routerinfo_t *, r) {
443 * } DIGESTMAP_FOREACH_END.
445 #define DIGESTMAP_FOREACH(map, keyvar, valtype, valvar) \
446 MAP_FOREACH(digestmap_, map, const char *, keyvar, valtype, valvar)
448 /** As MAP_FOREACH_MODIFY, but does not require declaration of prefix or
451 * DIGESTMAP_FOREACH_MODIFY(m, k, routerinfo_t *, r) {
452 * if (is_very_old(r))
453 * MAP_DEL_CURRENT(k);
454 * } DIGESTMAP_FOREACH_END.
456 #define DIGESTMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \
457 MAP_FOREACH_MODIFY(digestmap_, map, const char *, keyvar, valtype, valvar)
458 /** Used to end a DIGESTMAP_FOREACH() block. */
459 #define DIGESTMAP_FOREACH_END MAP_FOREACH_END
461 #define STRMAP_FOREACH(map, keyvar, valtype, valvar) \
462 MAP_FOREACH(strmap_, map, const char *, keyvar, valtype, valvar)
463 #define STRMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \
464 MAP_FOREACH_MODIFY(strmap_, map, const char *, keyvar, valtype, valvar)
465 #define STRMAP_FOREACH_END MAP_FOREACH_END
467 void* strmap_set_lc(strmap_t
*map
, const char *key
, void *val
);
468 void* strmap_get_lc(const strmap_t
*map
, const char *key
);
469 void* strmap_remove_lc(strmap_t
*map
, const char *key
);
471 #define DECLARE_TYPED_DIGESTMAP_FNS(prefix, maptype, valtype) \
472 typedef struct maptype maptype; \
473 typedef struct prefix##iter_t prefix##iter_t; \
474 static INLINE maptype* prefix##new(void) \
476 return (maptype*)digestmap_new(); \
478 static INLINE digestmap_t* prefix##to_digestmap(maptype *map) \
480 return (digestmap_t*)map; \
482 static INLINE valtype* prefix##get(maptype *map, const char *key) \
484 return (valtype*)digestmap_get((digestmap_t*)map, key); \
486 static INLINE valtype* prefix##set(maptype *map, const char *key, \
489 return (valtype*)digestmap_set((digestmap_t*)map, key, val); \
491 static INLINE valtype* prefix##remove(maptype *map, const char *key) \
493 return (valtype*)digestmap_remove((digestmap_t*)map, key); \
495 static INLINE void prefix##free(maptype *map, void (*free_val)(void*)) \
497 digestmap_free((digestmap_t*)map, free_val); \
499 static INLINE int prefix##isempty(maptype *map) \
501 return digestmap_isempty((digestmap_t*)map); \
503 static INLINE int prefix##size(maptype *map) \
505 return digestmap_size((digestmap_t*)map); \
507 static INLINE prefix##iter_t *prefix##iter_init(maptype *map) \
509 return (prefix##iter_t*) digestmap_iter_init((digestmap_t*)map); \
511 static INLINE prefix##iter_t *prefix##iter_next(maptype *map, \
512 prefix##iter_t *iter) \
514 return (prefix##iter_t*) digestmap_iter_next( \
515 (digestmap_t*)map, (digestmap_iter_t*)iter); \
517 static INLINE prefix##iter_t *prefix##iter_next_rmv(maptype *map, \
518 prefix##iter_t *iter) \
520 return (prefix##iter_t*) digestmap_iter_next_rmv( \
521 (digestmap_t*)map, (digestmap_iter_t*)iter); \
523 static INLINE void prefix##iter_get(prefix##iter_t *iter, \
528 digestmap_iter_get((digestmap_iter_t*) iter, keyp, &v); \
531 static INLINE int prefix##iter_done(prefix##iter_t *iter) \
533 return digestmap_iter_done((digestmap_iter_t*)iter); \
537 #define BITARRAY_SHIFT 5
538 #elif SIZEOF_INT == 8
539 #define BITARRAY_SHIFT 6
541 #error "int is neither 4 nor 8 bytes. I can't deal with that."
543 #define BITARRAY_MASK ((1u<<BITARRAY_SHIFT)-1)
545 /** A random-access array of one-bit-wide elements. */
546 typedef unsigned int bitarray_t
;
547 /** Create a new bit array that can hold <b>n_bits</b> bits. */
548 static INLINE bitarray_t
*
549 bitarray_init_zero(unsigned int n_bits
)
551 /* round up to the next int. */
552 size_t sz
= (n_bits
+BITARRAY_MASK
) >> BITARRAY_SHIFT
;
553 return tor_malloc_zero(sz
*sizeof(unsigned int));
555 /** Expand <b>ba</b> from holding <b>n_bits_old</b> to <b>n_bits_new</b>,
556 * clearing all new bits. Returns a possibly changed pointer to the
558 static INLINE bitarray_t
*
559 bitarray_expand(bitarray_t
*ba
,
560 unsigned int n_bits_old
, unsigned int n_bits_new
)
562 size_t sz_old
= (n_bits_old
+BITARRAY_MASK
) >> BITARRAY_SHIFT
;
563 size_t sz_new
= (n_bits_new
+BITARRAY_MASK
) >> BITARRAY_SHIFT
;
565 if (sz_new
<= sz_old
)
567 ptr
= tor_realloc(ba
, sz_new
*sizeof(unsigned int));
568 /* This memset does nothing to the older excess bytes. But they were
569 * already set to 0 by bitarry_init_zero. */
570 memset(ptr
+sz_old
*sizeof(unsigned int), 0,
571 (sz_new
-sz_old
)*sizeof(unsigned int));
572 return (bitarray_t
*) ptr
;
574 /** Free the bit array <b>ba</b>. */
576 bitarray_free(bitarray_t
*ba
)
580 /** Set the <b>bit</b>th bit in <b>b</b> to 1. */
582 bitarray_set(bitarray_t
*b
, int bit
)
584 b
[bit
>> BITARRAY_SHIFT
] |= (1u << (bit
& BITARRAY_MASK
));
586 /** Set the <b>bit</b>th bit in <b>b</b> to 0. */
588 bitarray_clear(bitarray_t
*b
, int bit
)
590 b
[bit
>> BITARRAY_SHIFT
] &= ~ (1u << (bit
& BITARRAY_MASK
));
592 /** Return true iff <b>bit</b>th bit in <b>b</b> is nonzero. NOTE: does
593 * not necessarily return 1 on true. */
594 static INLINE
unsigned int
595 bitarray_is_set(bitarray_t
*b
, int bit
)
597 return b
[bit
>> BITARRAY_SHIFT
] & (1u << (bit
& BITARRAY_MASK
));
600 /** A set of digests, implemented as a Bloom filter. */
602 int mask
; /**< One less than the number of bits in <b>ba</b>; always one less
603 * than a power of two. */
604 bitarray_t
*ba
; /**< A bit array to implement the Bloom filter. */
607 #define BIT(n) ((n) & set->mask)
608 /** Add the digest <b>digest</b> to <b>set</b>. */
610 digestset_add(digestset_t
*set
, const char *digest
)
612 const uint32_t *p
= (const uint32_t *)digest
;
613 const uint32_t d1
= p
[0] + (p
[1]>>16);
614 const uint32_t d2
= p
[1] + (p
[2]>>16);
615 const uint32_t d3
= p
[2] + (p
[3]>>16);
616 const uint32_t d4
= p
[3] + (p
[0]>>16);
617 bitarray_set(set
->ba
, BIT(d1
));
618 bitarray_set(set
->ba
, BIT(d2
));
619 bitarray_set(set
->ba
, BIT(d3
));
620 bitarray_set(set
->ba
, BIT(d4
));
623 /** If <b>digest</b> is in <b>set</b>, return nonzero. Otherwise,
624 * <em>probably</em> return zero. */
626 digestset_contains(const digestset_t
*set
, const char *digest
)
628 const uint32_t *p
= (const uint32_t *)digest
;
629 const uint32_t d1
= p
[0] + (p
[1]>>16);
630 const uint32_t d2
= p
[1] + (p
[2]>>16);
631 const uint32_t d3
= p
[2] + (p
[3]>>16);
632 const uint32_t d4
= p
[3] + (p
[0]>>16);
633 return bitarray_is_set(set
->ba
, BIT(d1
)) &&
634 bitarray_is_set(set
->ba
, BIT(d2
)) &&
635 bitarray_is_set(set
->ba
, BIT(d3
)) &&
636 bitarray_is_set(set
->ba
, BIT(d4
));
640 digestset_t
*digestset_new(int max_elements
);
641 void digestset_free(digestset_t
* set
);
643 /* These functions, given an <b>array</b> of <b>n_elements</b>, return the
644 * <b>nth</b> lowest element. <b>nth</b>=0 gives the lowest element;
645 * <b>n_elements</b>-1 gives the highest; and (<b>n_elements</b>-1) / 2 gives
646 * the median. As a side effect, the elements of <b>array</b> are sorted. */
647 int find_nth_int(int *array
, int n_elements
, int nth
);
648 time_t find_nth_time(time_t *array
, int n_elements
, int nth
);
649 double find_nth_double(double *array
, int n_elements
, int nth
);
650 int32_t find_nth_int32(int32_t *array
, int n_elements
, int nth
);
651 uint32_t find_nth_uint32(uint32_t *array
, int n_elements
, int nth
);
652 long find_nth_long(long *array
, int n_elements
, int nth
);
654 median_int(int *array
, int n_elements
)
656 return find_nth_int(array
, n_elements
, (n_elements
-1)/2);
659 median_time(time_t *array
, int n_elements
)
661 return find_nth_time(array
, n_elements
, (n_elements
-1)/2);
664 median_double(double *array
, int n_elements
)
666 return find_nth_double(array
, n_elements
, (n_elements
-1)/2);
668 static INLINE
uint32_t
669 median_uint32(uint32_t *array
, int n_elements
)
671 return find_nth_uint32(array
, n_elements
, (n_elements
-1)/2);
673 static INLINE
int32_t
674 median_int32(int32_t *array
, int n_elements
)
676 return find_nth_int32(array
, n_elements
, (n_elements
-1)/2);