static void trivial(void) { char *missing = "missing"; char *items[] = {"zero", "one", "two", "three", NULL}; char *foo = "foo"; size_t ii, nitems = 4; hashset_t set = hashset_create(); if (set == NULL) { fprintf(stderr, "failed to create hashset instance\n"); abort(); } for (ii = 0; ii < nitems; ++ii) { hashset_add(set, items[ii]); } for (ii = 0; ii < nitems; ++ii) { assert(hashset_is_member(set, items[ii])); } assert(hashset_is_member(set, missing) == 0); assert(hashset_remove(set, items[1]) == 1); assert(hashset_num_items(set) == 3); assert(hashset_remove(set, items[1]) == 0); assert(hashset_add(set, foo) == 1); assert(hashset_add(set, foo) == 0); hashset_destroy(set); }
/** * Actually free the resources allocated by the dset (and all its entries). * Called by some other functions in libcouchbase */ void lcb_durability_dset_destroy(lcb_durability_set_t *dset) { lcb_size_t ii; lcb_t instance = dset->instance; if (dset->timer) { lcb_io_opt_t io = instance->settings.io; io->v.v0.delete_timer(io, dset->timer); io->v.v0.destroy_timer(io, dset->timer); dset->timer = NULL; } for (ii = 0; ii < dset->nentries; ii++) { lcb_durability_entry_t *ent = dset->entries + ii; free((void *)REQFLD(ent, key)); } hashset_remove(dset->instance->durability_polls, dset); if (dset->nentries > 1) { if (dset->ht) { genhash_free(dset->ht); } free(dset->entries); free(dset->valid_entries); } free(dset); lcb_maybe_breakout(instance); }
void test_hashset_remove() { HashSet *hs = hashset_new(); char *a = "foo"; char *b = "bar"; char *c = "baz"; char *d = "foo"; hashset_add(hs, a); hashset_add(hs, b); hashset_add(hs, c); hashset_add(hs, d); hashset_remove(hs, "bar"); size_t size = hashset_size(hs); cc_assert(size == 2, cc_msg("hashset_add: Expected size was 2, but got %d", size)); cc_assert(!hashset_contains(hs, "bar"), cc_msg("hashset_add: HashSet not expected to contain " "element %s", "foo")); hashset_destroy(hs); }
static void test_gaps(void) { hashset_t set = hashset_create(); /* fill the hashset */ hashset_add(set, (void *)0xbabe); hashset_add(set, (void *)0xbeef); hashset_add(set, (void *)0xbad); hashset_add(set, (void *)0xf00d); /* 0xf00d (nil) (nil) (nil) (nil) 0xbad 0xbabe 0xbeef */ /* make a gap */ hashset_remove(set, (void *)0xbeef); /* 0xf00d (nil) (nil) (nil) (nil) 0xbad 0xbabe 0x1 */ /* check that 0xf00d is still reachable */ assert(hashset_is_member(set, (void *)0xf00d)); /* add 0xbeef back */ hashset_add(set, (void *)0xbeef); /* 0xf00d (nil) (nil) (nil) (nil) 0xbad 0xbabe 0xbeef */ /* verify */ assert(hashset_is_member(set, (void *)0xbeef)); assert(hashset_is_member(set, (void *)0xf00d)); }
LIBCOUCHBASE_API libcouchbase_error_t libcouchbase_timer_destroy(libcouchbase_t instance, libcouchbase_timer_t timer) { if (hashset_is_member(instance->timers, timer)) { hashset_remove(instance->timers, timer); instance->io->delete_timer(instance->io, timer->event); instance->io->destroy_timer(instance->io, timer->event); free(timer); } return libcouchbase_synchandler_return(instance, LIBCOUCHBASE_SUCCESS); }
static void test_fill_with_deleted_items() { char *s = "some string"; hashset_t set = hashset_create(); if (set == NULL) abort(); /* fill `set` with deleted items */ for (int i = 0; i < 8; ++i) { hashset_add(set, s + i); hashset_remove(set, s + i); } /* this should not cause an infinite loop */ assert(hashset_is_member(set, s) == 0); hashset_destroy(set); }
hashset_t hashset_substraction(hashset_t set, hashset_t substrahend) { hashset_t ret = hashset_create(); hashset_union(ret,set); int i; for(i = 0; i < substrahend->nitems; i++) { char* value = substrahend->items[i]; if(value != NULL) { if(hashset_is_member(ret,value)) { hashset_remove(ret, value); } } } return ret; }
/* Add contents of file to hashset */ int remove_file(HashSet *hashset, char *curr) { FILE *f = fopen(curr, "r"); if(!f) { fprintf(stderr, "error opening file %s: %s\n", curr, strerror(errno)); return 1; } char buf[1024]; while(fgets(buf, 1024, f)) { char *curr; char *next = buf; next_word(&curr, &next); while(*curr) { if(hashset_remove(hashset, (void *)curr)) printf("removed \"%s\"\n", curr); next_word(&curr, &next); } } fclose(f); return 0; }
int main(int argc, char **argv) { char buf[BUF_SIZE]; HashSet *hashset = hashset_new(&string_hashcode, &string_equals, &string_copy, &string_delete); if(!hashset) { fprintf(stderr, "hashset_new() returned NULL\n"); return 1; } while(1) { printf("> "); fflush(stdout); if(fgets(buf, BUF_SIZE, stdin)==NULL) break; char *curr; char *next = buf; next_word(&curr, &next); if(!*curr) { //Blank line -- do nothing continue; } else if(!strcasecmp(curr, "a")) { next_word(&curr, &next); if(*(skip_ws(next))) { printf("Too many arguments\n"); } else { if(hashset_add(hashset, (void *)curr)) printf("Element added\n"); else printf("Element already exists\n"); hashset_print(hashset, stdout, &string_print); printf("\n"); } } else if(!strcasecmp(curr, "c")) { next_word(&curr, &next); if(*(skip_ws(next))) { printf("Too many arguments\n"); } else { printf(hashset_contains(hashset, (void *)curr) ? "true\n" : "false\n"); } } else if(!strcasecmp(curr, "r")) { next_word(&curr, &next); if(*(skip_ws(next))) { printf("Too many arguments\n"); } else { if(hashset_remove(hashset, (void *)curr)) printf("Element removed\n"); else printf("Element not found\n"); hashset_print(hashset, stdout, &string_print); printf("\n"); } } else if(!strcasecmp(curr, "x")) { next_word(&curr, &next); if(*(skip_ws(next))) { printf("Too many arguments\n"); } else { hashset_delete(hashset); hashset = hashset_new(&string_hashcode, &string_equals, &string_copy, &string_delete); if(!hashset) { fprintf(stderr, "hashset_new() returned NULL\n"); return 1; } hashset_print(hashset, stdout, &string_print); printf("\n"); } } else if(!strcasecmp(curr, "s")) { if(*(skip_ws(next))) { printf("Too many arguments\n"); } else { printf("%d\n", hashset_size(hashset)); } } else if(!strcasecmp(curr, "p")) { if(*(skip_ws(next))) { printf("Too many arguments\n"); } else { hashset_print(hashset, stdout, &string_print); printf("\n"); } } else if (!strcasecmp(curr, "fa")) { next_word(&curr, &next); if (*(skip_ws(next))) { printf("Too many arguments\n"); } else { if (add_file(hashset, curr)) printf("error loading file.\n"); else { hashset_print(hashset, stdout, &string_print); printf("\n"); } } } else if (!strcasecmp(curr, "fr")) { next_word(&curr, &next); if (*(skip_ws(next))) { printf("Too many arguments\n"); } else { if (remove_file(hashset, curr)) printf("error loading file.\n"); else { hashset_print(hashset, stdout, &string_print); printf("\n"); } } } else if(!strcasecmp(curr, "quit")) { if(*(skip_ws(next))) { printf("Too many arguments\n"); } else { break; } } else if(!strcasecmp(curr, "h") || !strcasecmp(curr, "help")) { print_help(); } else { printf("Invalid command\n"); } printf("\n"); } if(ferror(stdin)) perror("fgets() failed"); hashset_delete(hashset); return 0; }