]> git.ozlabs.org Git - ccan/commitdiff
strset, strmap: invert iterator function meaning.
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 1 Dec 2011 06:13:51 +0000 (16:43 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 1 Dec 2011 06:13:51 +0000 (16:43 +1030)
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).

12 files changed:
ccan/strmap/_info
ccan/strmap/strmap.c
ccan/strmap/strmap.h
ccan/strmap/test/run-order.c
ccan/strmap/test/run-prefix.c
ccan/strset/_info
ccan/strset/strset.c
ccan/strset/strset.h
ccan/strset/test/run-hibit.c
ccan/strset/test/run-iterate-const.c
ccan/strset/test/run-order.c
ccan/strset/test/run-prefix.c

index 9832f80dbcf48c734f42466237352c1e35a951d5..82f20d13fa2bfe7a6b09d3b4451fe583e3ee1e0d 100644 (file)
@@ -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[])
index 7f21d1932e089c1961eb5da51cfe2cfb206eb738..9fa51d0d40db4cf89fb16041585d71ea163b2238 100644 (file)
@@ -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,
index cf77e94959d9cfe1960601be395900f3f9f71353..8fabc359acc64e2d4a4603e7097cfe93b3b2be17 100644 (file)
@@ -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)
index 417a7406b537a173d3b5b99701c60505e2bcc94b..68062eab0f9583a8aa2cd7bea26735ef14809214 100644 (file)
@@ -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)
index f6eb17796094a7c8aaf3e2e6d38857f06c07b4c9..9c5156e6e57dd8c7ba09d85a1ca43138341497b8 100644 (file)
@@ -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)
index a3192325db721c67e374c354375317216709c577..b5df9dfa5dc9e1a1aca0c52b1b3b479017ace365 100644 (file)
@@ -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)
index 27d9d4568ad8559c7aa97eff9b9ed32cbee3106e..06b0d7a76c35fe119353310888b4dbf1828d949b 100644 (file)
@@ -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,
index 05e1e358a8ee2f56fb82a7b1124fc15a783c7013..9d6f1ae343f5b4904e3eaf725d26c6a89cc7f331 100644 (file)
@@ -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)
index 833095616b9b99e6bf360cd86ad43d88f2051857..82f4c9225147e1d5f05358e6ec38a9d53afaaa5c 100644 (file)
@@ -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)
index daa27504ef25b207643bf51b1023755b245c68c8..9f2b13e2634914231c3e2ff721da30ba171b070c 100644 (file)
@@ -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)
index bc3b2984b5f894dab44c70ab81398a01f689c54e..910a9f7a2b55feb1ffbebbc42cee5bb5cc9a60fc 100644 (file)
@@ -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)
index 1be6283374bbcda462cdd99a12c29d6df16ce556..e88f2dd0c141ae185b576b93750a50dd335a6f43 100644 (file)
@@ -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)