/** * This function searches for, and removes an entry from the set. * * If the caller has previously found a struct int_set_entry pointer, * (from calling int_set_search or remembering it from int_set_add), * then int_set_remove_entry can be called instead to avoid an extra * search. */ void int_set_remove(struct int_set *set, uint32_t value) { struct int_set_entry *entry; entry = int_set_search(set, value); int_set_remove_entry(set, entry); }
/* Does the set contain an entry with the given value. */ bool int_set_contains(struct int_set *set, uint32_t value) { struct int_set_entry *entry; entry = int_set_search(set, value); return entry != NULL; }
int main(int argc, char *argv[]) { FILE *fin = stdin; int_set_type *is = NULL; int tmp, ret; if (argc > 1) { fin = fopen(argv[1], "r"); } if (fin == NULL) { perror(argv[1]); return 1; } is = int_set_new(); while (fscanf(fin, "%d", &tmp) != EOF) { ret = int_set_insert(is, tmp); if (ret == 0) { fprintf(stdout, "this one exists: %d!\n", tmp); } } fprintf(stdout, "set size: %d\n", is->size); fprintf(stdout, "search:\n"); while (fscanf(fin, "%d", &tmp) != EOF) { ret = int_set_search(is, tmp); if (ret) { fprintf(stdout, "found: %d\n", tmp); } else { fprintf(stdout, "not found: %d\n", tmp); } } fprintf(stdout, "remove:\n"); while (fscanf(fin, "%d", &tmp) != EOF) { ret = int_set_remove(is, tmp); if (ret) { fprintf(stdout, "removed: %d, set size: %d\n", tmp, is->size); } else { fprintf(stdout, "not found: %d\n", tmp); } } int_set_del(is); fclose(fin); return 0; }
int main(int argc, char **argv) { struct int_set *set; /* Use two values with the lowest bits in common so they will * hash to the same initial entry, so we can test the deletion * behavior for chained objects. */ uint32_t value1 = 0x00000123; uint32_t value2 = 0x10000123; uint32_t value3 = 0x20000123; struct int_set_entry *entry; set = int_set_create(); int_set_add(set, value1); int_set_add(set, value2); int_set_add(set, value3); assert(int_set_contains(set, value3)); entry = int_set_search(set, value3); assert(entry->value == value3); assert(int_set_contains(set, value2)); entry = int_set_search(set, value2); assert(entry->value == value2); assert(int_set_contains(set, value1)); entry = int_set_search(set, value1); assert(entry->value == value1); int_set_remove_entry(set, entry); int_set_remove(set, value2); assert(!int_set_contains(set, value1)); entry = int_set_search(set, value1); assert(entry == NULL); assert(!int_set_contains(set, value2)); entry = int_set_search(set, value2); assert(entry == NULL); assert(int_set_contains(set, value3)); entry = int_set_search(set, value3); assert(entry->value == value3); int_set_destroy(set); return 0; }