From bb2a75f445f408e43730fc55a1a978581ebe9b96 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 1 Dec 2011 16:43:51 +1030 Subject: [PATCH] strset, strmap: invert iterator function meaning. Make a false return abort the iteration, not true. The old way makes sense for search functions (true == I found it), but other kinds of iteration are more common (brute force search is probably dumb). --- ccan/strmap/_info | 4 ++-- ccan/strmap/strmap.c | 2 +- ccan/strmap/strmap.h | 6 +++--- ccan/strmap/test/run-order.c | 6 +++--- ccan/strmap/test/run-prefix.c | 4 ++-- ccan/strset/_info | 2 +- ccan/strset/strset.c | 2 +- ccan/strset/strset.h | 6 +++--- ccan/strset/test/run-hibit.c | 6 +++--- ccan/strset/test/run-iterate-const.c | 2 +- ccan/strset/test/run-order.c | 6 +++--- ccan/strset/test/run-prefix.c | 4 ++-- 12 files changed, 25 insertions(+), 25 deletions(-) diff --git a/ccan/strmap/_info b/ccan/strmap/_info index 9832f80d..82f20d13 100644 --- a/ccan/strmap/_info +++ b/ccan/strmap/_info @@ -21,8 +21,8 @@ * static bool dump(const char *member, size_t value, void *unused) * { * printf("%s at %zu. ", member, value); - * // false means keep going with iteration. - * return false; + * // true means keep going with iteration. + * return true; * } * * int main(int argc, char *argv[]) diff --git a/ccan/strmap/strmap.c b/ccan/strmap/strmap.c index 7f21d193..9fa51d0d 100644 --- a/ccan/strmap/strmap.c +++ b/ccan/strmap/strmap.c @@ -181,7 +181,7 @@ static bool iterate(struct strmap n, return handle(n.u.s, n.v, (void *)data); return iterate(n.u.n->child[0], handle, data) - || iterate(n.u.n->child[1], handle, data); + && iterate(n.u.n->child[1], handle, data); } void strmap_iterate_(const struct strmap *map, diff --git a/ccan/strmap/strmap.h b/ccan/strmap/strmap.h index cf77e949..8fabc359 100644 --- a/ccan/strmap/strmap.h +++ b/ccan/strmap/strmap.h @@ -156,7 +156,7 @@ void strmap_clear_(struct strmap *map); * @handle's prototype should be: * bool @handle(const char *member, type value, typeof(arg) arg) * - * If @handle returns true, the iteration will stop. + * If @handle returns false, the iteration will stop. * You should not alter the map within the @handle function! * * Example: @@ -167,9 +167,9 @@ void strmap_clear_(struct strmap *map); * { * // Only dump out num nodes. * if (*(num--) == 0) - * return true; + * return false; * printf("%s=>%i\n", member, *value); - * return false; + * return true; * } * * static void dump_map(const struct strmap_intp *map) diff --git a/ccan/strmap/test/run-order.c b/ccan/strmap/test/run-order.c index 417a7406..68062eab 100644 --- a/ccan/strmap/test/run-order.c +++ b/ccan/strmap/test/run-order.c @@ -11,7 +11,7 @@ static bool in_order(const char *member, char *value, unsigned int *count) ok1(i == atoi(value)); ok1(*count == i); (*count)++; - return false; + return true; } static bool in_order_by_2(const char *member, char *value, unsigned int *count) @@ -20,7 +20,7 @@ static bool in_order_by_2(const char *member, char *value, unsigned int *count) ok1(i == atoi(value)); ok1(*count == i); (*count) += 2; - return false; + return true; } static bool dump(const char *member, char *value, bool *ok) @@ -28,7 +28,7 @@ static bool dump(const char *member, char *value, bool *ok) diag("%s=>%s", member, value); if (value != member + 1) *ok = false; - return false; + return true; } int main(void) diff --git a/ccan/strmap/test/run-prefix.c b/ccan/strmap/test/run-prefix.c index f6eb1779..9c5156e6 100644 --- a/ccan/strmap/test/run-prefix.c +++ b/ccan/strmap/test/run-prefix.c @@ -12,14 +12,14 @@ static bool in_order(const char *index, char *value, unsigned int *count) ok1(i == atoi(value)); ok1(*count == i); (*count)++; - return false; + return true; } static bool find_empty(const char *index, char *value, char *empty) { if (index == empty) pass("Found empty entry!"); - return false; + return true; } int main(void) diff --git a/ccan/strset/_info b/ccan/strset/_info index a3192325..b5df9dfa 100644 --- a/ccan/strset/_info +++ b/ccan/strset/_info @@ -22,7 +22,7 @@ * static bool dump(const char *member, void *unused) * { * printf("%s ", member); - * return false; + * return true; // Keep going with iteration. * } * * int main(void) diff --git a/ccan/strset/strset.c b/ccan/strset/strset.c index 27d9d456..06b0d7a7 100644 --- a/ccan/strset/strset.c +++ b/ccan/strset/strset.c @@ -237,7 +237,7 @@ static bool iterate(struct strset n, return handle(n.u.n->child[0].u.s, (void *)data); return iterate(n.u.n->child[0], handle, data) - || iterate(n.u.n->child[1], handle, data); + && iterate(n.u.n->child[1], handle, data); } void strset_iterate_(const struct strset *set, diff --git a/ccan/strset/strset.h b/ccan/strset/strset.h index 05e1e358..9d6f1ae3 100644 --- a/ccan/strset/strset.h +++ b/ccan/strset/strset.h @@ -113,16 +113,16 @@ void strset_clear(struct strset *set); * @arg: the argument for the function (types should match). * * You should not alter the set within the @handle function! If it returns - * true, the iteration will stop. + * false, the iteration will stop. * * Example: * static bool dump_some(const char *member, int *num) * { * // Only dump out num nodes. * if (*(num--) == 0) - * return true; + * return false; * printf("%s\n", member); - * return false; + * return true; * } * * static void dump_set(const struct strset *set) diff --git a/ccan/strset/test/run-hibit.c b/ccan/strset/test/run-hibit.c index 83309561..82f4c922 100644 --- a/ccan/strset/test/run-hibit.c +++ b/ccan/strset/test/run-hibit.c @@ -19,7 +19,7 @@ static bool in_order(const char *value, unsigned int *count) encode(template, *count); ok1(streq(value, template)); (*count)++; - return false; + return true; } static bool in_order_by_2(const char *value, unsigned int *count) @@ -28,13 +28,13 @@ static bool in_order_by_2(const char *value, unsigned int *count) encode(template, *count); ok1(streq(value, template)); (*count) += 2; - return false; + return true; } static bool dump(const char *value, void *unused) { diag("%s", value); - return false; + return true; } int main(void) diff --git a/ccan/strset/test/run-iterate-const.c b/ccan/strset/test/run-iterate-const.c index daa27504..9f2b13e2 100644 --- a/ccan/strset/test/run-iterate-const.c +++ b/ccan/strset/test/run-iterate-const.c @@ -9,7 +9,7 @@ static bool find_string(const char *str, const char *cmp) { if (strcmp(str, cmp) == 0) found = true; - return false; + return true; } int main(void) diff --git a/ccan/strset/test/run-order.c b/ccan/strset/test/run-order.c index bc3b2984..910a9f7a 100644 --- a/ccan/strset/test/run-order.c +++ b/ccan/strset/test/run-order.c @@ -10,7 +10,7 @@ static bool in_order(const char *value, unsigned int *count) int i = atoi(value); ok1(*count == i); (*count)++; - return false; + return true; } static bool in_order_by_2(const char *value, unsigned int *count) @@ -18,13 +18,13 @@ static bool in_order_by_2(const char *value, unsigned int *count) int i = atoi(value); ok1(*count == i); (*count) += 2; - return false; + return true; } static bool dump(const char *value, void *unused) { diag("%s", value); - return false; + return true; } int main(void) diff --git a/ccan/strset/test/run-prefix.c b/ccan/strset/test/run-prefix.c index 1be62833..e88f2dd0 100644 --- a/ccan/strset/test/run-prefix.c +++ b/ccan/strset/test/run-prefix.c @@ -11,14 +11,14 @@ static bool in_order(const char *value, unsigned int *count) int i = atoi(value); ok1(*count == i); (*count)++; - return false; + return true; } static bool find_empty(const char *value, char *empty) { if (value == empty) pass("Found empty entry!"); - return false; + return true; } int main(void) -- 2.39.2