diff options
author | Nick Mathewson <nickm@torproject.org> | 2015-01-09 11:36:47 -0500 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2015-01-10 15:09:07 -0500 |
commit | c83d83814660b643b705ed7de4aa1fc35e2d20ad (patch) | |
tree | a40e8125e82391e69a16cac1ee73061243d00017 /src/common | |
parent | 33df3e37ffecfed309a1a0f210a96620c0ebb837 (diff) | |
download | tor-c83d83814660b643b705ed7de4aa1fc35e2d20ad.tar.gz tor-c83d83814660b643b705ed7de4aa1fc35e2d20ad.zip |
Implement proposal 227-vote-on-package-fingerprints.txt
This implementation includes tests and a little documentation.
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/container.c | 22 | ||||
-rw-r--r-- | src/common/container.h | 8 |
2 files changed, 24 insertions, 6 deletions
diff --git a/src/common/container.c b/src/common/container.c index 37e28004ae..68e3711c90 100644 --- a/src/common/container.c +++ b/src/common/container.c @@ -518,11 +518,13 @@ smartlist_sort(smartlist_t *sl, int (*compare)(const void **a, const void **b)) /** Given a smartlist <b>sl</b> sorted with the function <b>compare</b>, * return the most frequent member in the list. Break ties in favor of - * later elements. If the list is empty, return NULL. + * later elements. If the list is empty, return NULL. If count_out is + * non-null, set it to the most frequent member. */ void * -smartlist_get_most_frequent(const smartlist_t *sl, - int (*compare)(const void **a, const void **b)) +smartlist_get_most_frequent_(const smartlist_t *sl, + int (*compare)(const void **a, const void **b), + int *count_out) { const void *most_frequent = NULL; int most_frequent_count = 0; @@ -530,8 +532,11 @@ smartlist_get_most_frequent(const smartlist_t *sl, const void *cur = NULL; int i, count=0; - if (!sl->num_used) + if (!sl->num_used) { + if (count_out) + *count_out = 0; return NULL; + } for (i = 0; i < sl->num_used; ++i) { const void *item = sl->list[i]; if (cur && 0 == compare(&cur, &item)) { @@ -549,6 +554,8 @@ smartlist_get_most_frequent(const smartlist_t *sl, most_frequent = cur; most_frequent_count = count; } + if (count_out) + *count_out = most_frequent_count; return (void*)most_frequent; } @@ -728,6 +735,13 @@ smartlist_get_most_frequent_string(smartlist_t *sl) return smartlist_get_most_frequent(sl, compare_string_ptrs_); } +/** Return the most frequent string in the sorted list <b>sl</b> */ +char * +smartlist_get_most_frequent_string_(smartlist_t *sl, int *count_out) +{ + return smartlist_get_most_frequent_(sl, compare_string_ptrs_, count_out); +} + /** Remove duplicate strings from a sorted list, and free them with tor_free(). */ void diff --git a/src/common/container.h b/src/common/container.h index 377cdf5dba..6edc80f75b 100644 --- a/src/common/container.h +++ b/src/common/container.h @@ -94,8 +94,11 @@ void smartlist_del_keeporder(smartlist_t *sl, int idx); void smartlist_insert(smartlist_t *sl, int idx, void *val); void smartlist_sort(smartlist_t *sl, int (*compare)(const void **a, const void **b)); -void *smartlist_get_most_frequent(const smartlist_t *sl, - int (*compare)(const void **a, const void **b)); +void *smartlist_get_most_frequent_(const smartlist_t *sl, + int (*compare)(const void **a, const void **b), + int *count_out); +#define smartlist_get_most_frequent(sl, compare) \ + smartlist_get_most_frequent_((sl), (compare), NULL) void smartlist_uniq(smartlist_t *sl, int (*compare)(const void **a, const void **b), void (*free_fn)(void *elt)); @@ -106,6 +109,7 @@ void smartlist_sort_digests256(smartlist_t *sl); void smartlist_sort_pointers(smartlist_t *sl); char *smartlist_get_most_frequent_string(smartlist_t *sl); +char *smartlist_get_most_frequent_string_(smartlist_t *sl, int *count_out); char *smartlist_get_most_frequent_digest256(smartlist_t *sl); void smartlist_uniq_strings(smartlist_t *sl); |