#include #include #include #include "iset.h" void print_item(int item, int level, void *arg); /** * A command-line driven test of iset. The arguments is a sequence of operations * to perform on a set. The operations are * a num to add an integer * r num to remove an integer * c num to test if an integer in in the set * n to compute the number of integers in the set * p to print the tree */ int main(int argc, char **argv) { iset *s = iset_create(); for (int i = 1; i < argc; i++) { if (strlen(argv[i]) > 0) { int num; switch (argv[i][0]) { case 'a': if (i + 1 < argc) { num = atoi(argv[i + 1]); if (iset_add(s, num)) { printf("%d added\n", num); } else { printf("%d already present\n", num); } } break; case 'r': if (i + 1 < argc) { num = atoi(argv[i + 1]); if (iset_remove(s, num)) { printf("%d removed\n", num); } else { printf("%d not present\n", num); } } break; case 'c': if (i + 1 < argc) { num = atoi(argv[i + 1]); if (iset_contains(s, num)) { printf("%d present\n", num); } else { printf("%d not present\n", num); } } break; case 'n': printf("element count = %d\n", iset_size(s)); break; case 'p': iset_for_each(s, print_item, NULL); break; } } } iset_destroy(s); } void print_item(int item, int level, void *arg) { printf("level %d: %d\n", level, item); }