Commit 7c69053b authored by Rusty Russell's avatar Rusty Russell

setset: fix API to match strmap and common sense.

strset_set -> strset_add
strset_clear -> strset_del
strset_test -> strset_get
strset_destroy -> strset_clear
parent 140cd169
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
* for (word = strtok(file, " \t\r\n"); * for (word = strtok(file, " \t\r\n");
* word; * word;
* word = strtok(NULL, " \t\r\n")) { * word = strtok(NULL, " \t\r\n")) {
* strset_set(&words, word); * strset_add(&words, word);
* } * }
* strset_iterate(&words, dump, NULL); * strset_iterate(&words, dump, NULL);
* printf("\n"); * printf("\n");
......
...@@ -57,7 +57,7 @@ static const char *closest(struct strset n, const char *member) ...@@ -57,7 +57,7 @@ static const char *closest(struct strset n, const char *member)
return n.u.s; return n.u.s;
} }
char *strset_test(const struct strset *set, const char *member) char *strset_get(const struct strset *set, const char *member)
{ {
const char *str; const char *str;
...@@ -90,7 +90,7 @@ static bool set_string(struct strset *set, ...@@ -90,7 +90,7 @@ static bool set_string(struct strset *set,
return true; return true;
} }
bool strset_set(struct strset *set, const char *member) bool strset_add(struct strset *set, const char *member)
{ {
size_t len = strlen(member); size_t len = strlen(member);
const u8 *bytes = (const u8 *)member; const u8 *bytes = (const u8 *)member;
...@@ -163,7 +163,7 @@ bool strset_set(struct strset *set, const char *member) ...@@ -163,7 +163,7 @@ bool strset_set(struct strset *set, const char *member)
return true; return true;
} }
char *strset_clear(struct strset *set, const char *member) char *strset_del(struct strset *set, const char *member)
{ {
size_t len = strlen(member); size_t len = strlen(member);
const u8 *bytes = (const u8 *)member; const u8 *bytes = (const u8 *)member;
...@@ -290,20 +290,20 @@ const struct strset *strset_prefix(const struct strset *set, const char *prefix) ...@@ -290,20 +290,20 @@ const struct strset *strset_prefix(const struct strset *set, const char *prefix)
return top; return top;
} }
static void destroy(struct strset n) static void clear(struct strset n)
{ {
if (!n.u.s[0]) { if (!n.u.s[0]) {
if (likely(n.u.n->byte_num != (size_t)-1)) { if (likely(n.u.n->byte_num != (size_t)-1)) {
destroy(n.u.n->child[0]); clear(n.u.n->child[0]);
destroy(n.u.n->child[1]); clear(n.u.n->child[1]);
} }
free(n.u.n); free(n.u.n);
} }
} }
void strset_destroy(struct strset *set) void strset_clear(struct strset *set)
{ {
if (set->u.n) if (set->u.n)
destroy(*set); clear(*set);
set->u.n = NULL; set->u.n = NULL;
} }
...@@ -48,7 +48,7 @@ static inline bool strset_empty(const struct strset *set) ...@@ -48,7 +48,7 @@ static inline bool strset_empty(const struct strset *set)
} }
/** /**
* strset_test - is this a member of this string set? * strset_get - is this a member of this string set?
* @set: the set. * @set: the set.
* @member: the string to search for. * @member: the string to search for.
* *
...@@ -56,13 +56,13 @@ static inline bool strset_empty(const struct strset *set) ...@@ -56,13 +56,13 @@ static inline bool strset_empty(const struct strset *set)
* = ENOENT). * = ENOENT).
* *
* Example: * Example:
* if (strset_test(&set, "hello")) * if (strset_get(&set, "hello"))
* printf("hello is in the set\n"); * printf("hello is in the set\n");
*/ */
char *strset_test(const struct strset *set, const char *member); char *strset_get(const struct strset *set, const char *member);
/** /**
* strset_set - place a member in the string set. * strset_add - place a member in the string set.
* @set: the set. * @set: the set.
* @member: the string to place in the set. * @member: the string to place in the set.
* *
...@@ -73,38 +73,38 @@ char *strset_test(const struct strset *set, const char *member); ...@@ -73,38 +73,38 @@ char *strset_test(const struct strset *set, const char *member);
* you want a copy in the set, use strdup(). * you want a copy in the set, use strdup().
* *
* Example: * Example:
* if (!strset_set(&set, "goodbye")) * if (!strset_add(&set, "goodbye"))
* printf("goodbye was already in the set\n"); * printf("goodbye was already in the set\n");
*/ */
bool strset_set(struct strset *set, const char *member); bool strset_add(struct strset *set, const char *member);
/** /**
* strset_clear - remove a member from the string set. * strset_del - remove a member from the string set.
* @set: the set. * @set: the set.
* @member: the string to remove from the set. * @member: the string to remove from the set.
* *
* This returns the string which was passed to strset_set(), or NULL if * This returns the string which was passed to strset_add(), or NULL if
* the string was not in the map (in which case it sets errno = ENOENT). * the string was not in the map (in which case it sets errno = ENOENT).
* *
* This means that if you allocated a string (eg. using strdup()), you can * This means that if you allocated a string (eg. using strdup()), you can
* free it here. * free it here.
* *
* Example: * Example:
* if (!strset_clear(&set, "goodbye")) * if (!strset_del(&set, "goodbye"))
* printf("goodbye was not in the set?\n"); * printf("goodbye was not in the set?\n");
*/ */
char *strset_clear(struct strset *set, const char *member); char *strset_del(struct strset *set, const char *member);
/** /**
* strset_destroy - remove every member from the set. * strset_clear - remove every member from the set.
* @set: the set. * @set: the set.
* *
* The set will be empty after this. * The set will be empty after this.
* *
* Example: * Example:
* strset_destroy(&set); * strset_clear(&set);
*/ */
void strset_destroy(struct strset *set); void strset_clear(struct strset *set);
/** /**
* strset_iterate - ordered iteration over a set * strset_iterate - ordered iteration over a set
......
...@@ -53,7 +53,7 @@ int main(void) ...@@ -53,7 +53,7 @@ int main(void)
} }
for (i = 0; i < NUM; i++) for (i = 0; i < NUM; i++)
strset_set(&set, str[i]); strset_add(&set, str[i]);
strset_iterate(&set, dump, NULL); strset_iterate(&set, dump, NULL);
...@@ -63,13 +63,13 @@ int main(void) ...@@ -63,13 +63,13 @@ int main(void)
/* Preserve order after deletion. */ /* Preserve order after deletion. */
for (i = 0; i < NUM; i += 2) for (i = 0; i < NUM; i += 2)
ok1(strset_clear(&set, str[i]) == str[i]); ok1(strset_del(&set, str[i]) == str[i]);
i = 1; i = 1;
strset_iterate(&set, in_order_by_2, &i); strset_iterate(&set, in_order_by_2, &i);
for (i = 1; i < NUM; i += 2) for (i = 1; i < NUM; i += 2)
ok1(strset_clear(&set, str[i]) == str[i]); ok1(strset_del(&set, str[i]) == str[i]);
/* empty traverse. */ /* empty traverse. */
strset_iterate(&set, in_order_by_2, (unsigned int *)NULL); strset_iterate(&set, in_order_by_2, (unsigned int *)NULL);
......
...@@ -19,11 +19,11 @@ int main(void) ...@@ -19,11 +19,11 @@ int main(void)
plan_tests(3); plan_tests(3);
strset_init(&set); strset_init(&set);
ok1(strset_set(&set, "hello")); ok1(strset_add(&set, "hello"));
ok1(strset_set(&set, "world")); ok1(strset_add(&set, "world"));
strset_iterate(&set, find_string, (const char *)"hello"); strset_iterate(&set, find_string, (const char *)"hello");
ok1(found); ok1(found);
strset_destroy(&set); strset_clear(&set);
/* This exits depending on whether all tests passed */ /* This exits depending on whether all tests passed */
return exit_status(); return exit_status();
......
...@@ -43,7 +43,7 @@ int main(void) ...@@ -43,7 +43,7 @@ int main(void)
} }
for (i = 0; i < NUM; i++) for (i = 0; i < NUM; i++)
strset_set(&set, str[i]); strset_add(&set, str[i]);
strset_iterate(&set, dump, NULL); strset_iterate(&set, dump, NULL);
...@@ -53,25 +53,25 @@ int main(void) ...@@ -53,25 +53,25 @@ int main(void)
/* Preserve order after deletion. */ /* Preserve order after deletion. */
for (i = 0; i < NUM; i += 2) for (i = 0; i < NUM; i += 2)
ok1(strset_clear(&set, str[i]) == str[i]); ok1(strset_del(&set, str[i]) == str[i]);
i = 1; i = 1;
strset_iterate(&set, in_order_by_2, &i); strset_iterate(&set, in_order_by_2, &i);
for (i = 1; i < NUM; i += 2) for (i = 1; i < NUM; i += 2)
ok1(strset_clear(&set, str[i]) == str[i]); ok1(strset_del(&set, str[i]) == str[i]);
/* empty traverse. */ /* empty traverse. */
strset_iterate(&set, in_order_by_2, (unsigned int *)NULL); strset_iterate(&set, in_order_by_2, (unsigned int *)NULL);
/* insert backwards, should be fine. */ /* insert backwards, should be fine. */
for (i = 0; i < NUM; i++) for (i = 0; i < NUM; i++)
strset_set(&set, str[NUM-1-i]); strset_add(&set, str[NUM-1-i]);
i = 0; i = 0;
strset_iterate(&set, in_order, &i); strset_iterate(&set, in_order, &i);
strset_destroy(&set); strset_clear(&set);
for (i = 0; i < NUM; i++) for (i = 0; i < NUM; i++)
free(str[i]); free(str[i]);
......
...@@ -38,7 +38,7 @@ int main(void) ...@@ -38,7 +38,7 @@ int main(void)
} }
for (i = 0; i < NUM; i++) for (i = 0; i < NUM; i++)
strset_set(&set, str[i]); strset_add(&set, str[i]);
/* Nothing */ /* Nothing */
sub = strset_prefix(&set, "a"); sub = strset_prefix(&set, "a");
...@@ -70,13 +70,13 @@ int main(void) ...@@ -70,13 +70,13 @@ int main(void)
/* Everything, *plus* empty string. */ /* Everything, *plus* empty string. */
empty = strdup(""); empty = strdup("");
strset_set(&set, empty); strset_add(&set, empty);
sub = strset_prefix(&set, ""); sub = strset_prefix(&set, "");
/* Check we get *our* empty string back! */ /* Check we get *our* empty string back! */
strset_iterate(sub, find_empty, empty); strset_iterate(sub, find_empty, empty);
strset_destroy(&set); strset_clear(&set);
for (i = 0; i < NUM; i++) for (i = 0; i < NUM; i++)
free(str[i]); free(str[i]);
......
...@@ -14,53 +14,53 @@ int main(void) ...@@ -14,53 +14,53 @@ int main(void)
strset_init(&set); strset_init(&set);
ok1(!strset_test(&set, str)); ok1(!strset_get(&set, str));
ok1(errno == ENOENT); ok1(errno == ENOENT);
ok1(!strset_test(&set, none)); ok1(!strset_get(&set, none));
ok1(errno == ENOENT); ok1(errno == ENOENT);
ok1(!strset_clear(&set, str)); ok1(!strset_del(&set, str));
ok1(errno == ENOENT); ok1(errno == ENOENT);
ok1(!strset_clear(&set, none)); ok1(!strset_del(&set, none));
ok1(errno == ENOENT); ok1(errno == ENOENT);
ok1(strset_set(&set, str)); ok1(strset_add(&set, str));
ok1(strset_test(&set, str)); ok1(strset_get(&set, str));
/* We compare the string, not the pointer. */ /* We compare the string, not the pointer. */
ok1(strset_test(&set, dup)); ok1(strset_get(&set, dup));
ok1(!strset_test(&set, none)); ok1(!strset_get(&set, none));
ok1(errno == ENOENT); ok1(errno == ENOENT);
/* Add of duplicate should fail. */ /* Add of duplicate should fail. */
ok1(!strset_set(&set, dup)); ok1(!strset_add(&set, dup));
ok1(errno == EEXIST); ok1(errno == EEXIST);
/* Delete should return original string. */ /* Delete should return original string. */
ok1(strset_clear(&set, dup) == str); ok1(strset_del(&set, dup) == str);
ok1(!strset_test(&set, str)); ok1(!strset_get(&set, str));
ok1(errno == ENOENT); ok1(errno == ENOENT);
ok1(!strset_test(&set, none)); ok1(!strset_get(&set, none));
ok1(errno == ENOENT); ok1(errno == ENOENT);
/* Try insert and delete of empty string. */ /* Try insert and delete of empty string. */
ok1(strset_set(&set, none)); ok1(strset_add(&set, none));
ok1(strset_test(&set, none)); ok1(strset_get(&set, none));
ok1(!strset_test(&set, str)); ok1(!strset_get(&set, str));
ok1(errno == ENOENT); ok1(errno == ENOENT);
/* Delete should return original string. */ /* Delete should return original string. */
ok1(strset_clear(&set, "") == none); ok1(strset_del(&set, "") == none);
ok1(!strset_test(&set, str)); ok1(!strset_get(&set, str));
ok1(errno == ENOENT); ok1(errno == ENOENT);
ok1(!strset_test(&set, none)); ok1(!strset_get(&set, none));
ok1(errno == ENOENT); ok1(errno == ENOENT);
/* Both at once... */ /* Both at once... */
ok1(strset_set(&set, none)); ok1(strset_add(&set, none));
ok1(strset_set(&set, str)); ok1(strset_add(&set, str));
ok1(strset_test(&set, str)); ok1(strset_get(&set, str));
ok1(strset_test(&set, none)); ok1(strset_get(&set, none));
ok1(strset_clear(&set, "") == none); ok1(strset_del(&set, "") == none);
ok1(strset_clear(&set, dup) == str); ok1(strset_del(&set, dup) == str);
ok1(set.u.n == NULL); ok1(set.u.n == NULL);
free(dup); free(dup);
......
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