Example #1
0
/*this function takes a pointer to an ordered set, a function pointer and an argument pointer, 
and returns a pointer to a new orderedSet, which contains all the strings in the initial orderedSet, 
that when predicate was called on them, !=0*/
struct orderedSet *orderedSetFilter(const struct orderedSet *s, int (*predicate)(void *arg, const char* c), void *arg) {
	struct orderedSet *s2; /*pointer to new ordered set*/
	s2 = orderedSetCreate(); /*create the struct/malloc memory for it*/
	setFilter(s2, s->set, predicate, arg); /*call predicate on every element in s in increasing order, and insert the key into the new ordered set if results !=0*/

	return s2; /*return pointer to new ordered set*/
}
Example #2
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;
}