Commit bb2a75f4 authored by Rusty Russell's avatar Rusty Russell

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).
parent 7c69053b
...@@ -21,8 +21,8 @@ ...@@ -21,8 +21,8 @@
* static bool dump(const char *member, size_t value, void *unused) * static bool dump(const char *member, size_t value, void *unused)
* { * {
* printf("%s at %zu. ", member, value); * printf("%s at %zu. ", member, value);
* // false means keep going with iteration. * // true means keep going with iteration.
* return false; * return true;
* } * }
* *
* int main(int argc, char *argv[]) * int main(int argc, char *argv[])
......
...@@ -181,7 +181,7 @@ static bool iterate(struct strmap n, ...@@ -181,7 +181,7 @@ static bool iterate(struct strmap n,
return handle(n.u.s, n.v, (void *)data); return handle(n.u.s, n.v, (void *)data);
return iterate(n.u.n->child[0], handle, 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, void strmap_iterate_(const struct strmap *map,
......
...@@ -156,7 +156,7 @@ void strmap_clear_(struct strmap *map); ...@@ -156,7 +156,7 @@ void strmap_clear_(struct strmap *map);
* @handle's prototype should be: * @handle's prototype should be:
* bool @handle(const char *member, type value, typeof(arg) arg) * 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! * You should not alter the map within the @handle function!
* *
* Example: * Example:
...@@ -167,9 +167,9 @@ void strmap_clear_(struct strmap *map); ...@@ -167,9 +167,9 @@ void strmap_clear_(struct strmap *map);
* { * {
* // Only dump out num nodes. * // Only dump out num nodes.
* if (*(num--) == 0) * if (*(num--) == 0)
* return true; * return false;
* printf("%s=>%i\n", member, *value); * printf("%s=>%i\n", member, *value);
* return false; * return true;
* } * }
* *
* static void dump_map(const struct strmap_intp *map) * static void dump_map(const struct strmap_intp *map)
......
...@@ -11,7 +11,7 @@ static bool in_order(const char *member, char *value, unsigned int *count) ...@@ -11,7 +11,7 @@ static bool in_order(const char *member, char *value, unsigned int *count)
ok1(i == atoi(value)); ok1(i == atoi(value));
ok1(*count == i); ok1(*count == i);
(*count)++; (*count)++;
return false; return true;
} }
static bool in_order_by_2(const char *member, char *value, unsigned int *count) 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) ...@@ -20,7 +20,7 @@ static bool in_order_by_2(const char *member, char *value, unsigned int *count)
ok1(i == atoi(value)); ok1(i == atoi(value));
ok1(*count == i); ok1(*count == i);
(*count) += 2; (*count) += 2;
return false; return true;
} }
static bool dump(const char *member, char *value, bool *ok) static bool dump(const char *member, char *value, bool *ok)
...@@ -28,7 +28,7 @@ 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); diag("%s=>%s", member, value);
if (value != member + 1) if (value != member + 1)
*ok = false; *ok = false;
return false; return true;
} }
int main(void) int main(void)
......
...@@ -12,14 +12,14 @@ static bool in_order(const char *index, char *value, unsigned int *count) ...@@ -12,14 +12,14 @@ static bool in_order(const char *index, char *value, unsigned int *count)
ok1(i == atoi(value)); ok1(i == atoi(value));
ok1(*count == i); ok1(*count == i);
(*count)++; (*count)++;
return false; return true;
} }
static bool find_empty(const char *index, char *value, char *empty) static bool find_empty(const char *index, char *value, char *empty)
{ {
if (index == empty) if (index == empty)
pass("Found empty entry!"); pass("Found empty entry!");
return false; return true;
} }
int main(void) int main(void)
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
* static bool dump(const char *member, void *unused) * static bool dump(const char *member, void *unused)
* { * {
* printf("%s ", member); * printf("%s ", member);
* return false; * return true; // Keep going with iteration.
* } * }
* *
* int main(void) * int main(void)
......
...@@ -237,7 +237,7 @@ static bool iterate(struct strset n, ...@@ -237,7 +237,7 @@ static bool iterate(struct strset n,
return handle(n.u.n->child[0].u.s, (void *)data); return handle(n.u.n->child[0].u.s, (void *)data);
return iterate(n.u.n->child[0], handle, 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, void strset_iterate_(const struct strset *set,
......
...@@ -113,16 +113,16 @@ void strset_clear(struct strset *set); ...@@ -113,16 +113,16 @@ void strset_clear(struct strset *set);
* @arg: the argument for the function (types should match). * @arg: the argument for the function (types should match).
* *
* You should not alter the set within the @handle function! If it returns * You should not alter the set within the @handle function! If it returns
* true, the iteration will stop. * false, the iteration will stop.
* *
* Example: * Example:
* static bool dump_some(const char *member, int *num) * static bool dump_some(const char *member, int *num)
* { * {
* // Only dump out num nodes. * // Only dump out num nodes.
* if (*(num--) == 0) * if (*(num--) == 0)
* return true; * return false;
* printf("%s\n", member); * printf("%s\n", member);
* return false; * return true;
* } * }
* *
* static void dump_set(const struct strset *set) * static void dump_set(const struct strset *set)
......
...@@ -19,7 +19,7 @@ static bool in_order(const char *value, unsigned int *count) ...@@ -19,7 +19,7 @@ static bool in_order(const char *value, unsigned int *count)
encode(template, *count); encode(template, *count);
ok1(streq(value, template)); ok1(streq(value, template));
(*count)++; (*count)++;
return false; return true;
} }
static bool in_order_by_2(const char *value, unsigned int *count) 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) ...@@ -28,13 +28,13 @@ static bool in_order_by_2(const char *value, unsigned int *count)
encode(template, *count); encode(template, *count);
ok1(streq(value, template)); ok1(streq(value, template));
(*count) += 2; (*count) += 2;
return false; return true;
} }
static bool dump(const char *value, void *unused) static bool dump(const char *value, void *unused)
{ {
diag("%s", value); diag("%s", value);
return false; return true;
} }
int main(void) int main(void)
......
...@@ -9,7 +9,7 @@ static bool find_string(const char *str, const char *cmp) ...@@ -9,7 +9,7 @@ static bool find_string(const char *str, const char *cmp)
{ {
if (strcmp(str, cmp) == 0) if (strcmp(str, cmp) == 0)
found = true; found = true;
return false; return true;
} }
int main(void) int main(void)
......
...@@ -10,7 +10,7 @@ static bool in_order(const char *value, unsigned int *count) ...@@ -10,7 +10,7 @@ static bool in_order(const char *value, unsigned int *count)
int i = atoi(value); int i = atoi(value);
ok1(*count == i); ok1(*count == i);
(*count)++; (*count)++;
return false; return true;
} }
static bool in_order_by_2(const char *value, unsigned int *count) 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) ...@@ -18,13 +18,13 @@ static bool in_order_by_2(const char *value, unsigned int *count)
int i = atoi(value); int i = atoi(value);
ok1(*count == i); ok1(*count == i);
(*count) += 2; (*count) += 2;
return false; return true;
} }
static bool dump(const char *value, void *unused) static bool dump(const char *value, void *unused)
{ {
diag("%s", value); diag("%s", value);
return false; return true;
} }
int main(void) int main(void)
......
...@@ -11,14 +11,14 @@ static bool in_order(const char *value, unsigned int *count) ...@@ -11,14 +11,14 @@ static bool in_order(const char *value, unsigned int *count)
int i = atoi(value); int i = atoi(value);
ok1(*count == i); ok1(*count == i);
(*count)++; (*count)++;
return false; return true;
} }
static bool find_empty(const char *value, char *empty) static bool find_empty(const char *value, char *empty)
{ {
if (value == empty) if (value == empty)
pass("Found empty entry!"); pass("Found empty entry!");
return false; return true;
} }
int main(void) int main(void)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment