Exemple #1
0
int
main(int argc, char **argv)
{
    char buf[MAX_LINE_LEN+1];
    size_t len;
    struct orderedSet *s;
    struct orderedSet *s2;
    uint64_t hashValue;

    if(argc != 1) {
        fprintf(stderr, "Usage: %s\n", argv[0]);
        return 1;
    }

    s = orderedSetCreate();

    while(fgets(buf, MAX_LINE_LEN, stdin) != 0) {
        /* eat the newline */
        len = strlen(buf);
        assert(buf[len-1] == '\n');
        buf[len-1] = '\0';

        switch(buf[0]) {
            case '+':
                /* add an element */
                orderedSetInsert(s, buf+1);
                break;
            case '-':
                /* remove an element */
                orderedSetDelete(s, buf+1);
                break;
            case 's':
                /* print size of the set */
                printf("%zu\n", orderedSetSize(s));
                break;
            case 'p':
                /* print all the elements */
                s2 = orderedSetFilter(s, putsWrapper, 0);
                assert(orderedSetSize(s2) == 0);
                orderedSetDestroy(s2);
                break;
            case 'h':
                /* print hash of all the elements */
                hashValue = 0;
                s2 = orderedSetFilter(s, hashWrapper, &hashValue);
                assert(orderedSetSize(s2) == 0);
                orderedSetDestroy(s2);
                printf("%016" PRIx64 "\n", hashValue);
                break;
            case 'f':
                /* filter */
                s2 = orderedSetFilter(s, grep, buf+1);
                orderedSetDestroy(s);
                s = s2;
                break;
            default:
                fprintf(stderr, "Unrecognized command %c\n", buf[0]);
                return 2;
        }
    }

    orderedSetDestroy(s);

    return 0;
}
Exemple #2
0
/*this function takes a pointer to an ordered set, a pointer to a root node, a function pointer and an argument pointer, and inserts
the key of the root node into the ordered set if the predicate called on arg and root->key is not zero*/
static void operateFilter(struct orderedSet *s, struct node *root, int (*predicate)(void *arg, const char *c), void *arg) {
	if ((predicate(arg, root->key)) != 0) { /*make sure results of predicate function are not zero*/
		orderedSetInsert(s, root->key); /*insert string into orderedSet*/
	}
}