C Collections is a light and short C implementation of usefull collections. It contains:
fixed typevectorstring-intmapstring-fixed typemap (svmap)dynamic typelist (dynlist)
Vector example:
vector_t v; vector_init(&v, char); { char c = '*'; vector_add(&v, &c); if (vector_err(&v)) return -1; } printf("%c\n", vector_get_val(&v, char, 0)); vector_free(&v);Should output:
*Map example:
map_t m; map_init(&m); map_add(&m, "hello", 42); if (map_err(&m)) return -1; map_add(&m, "world", 69); if (map_err(&m)) return -1; map_balance(&m); map_foreach_i(&m) { printf("[%s] = %d\n", map_iter_key(i), map_iter_value(i)); } map_del(&m, map_get(&m, "hello")); map_balance(&m); map_foreach_i(&m) { printf("[%s] = %d\n", map_iter_key(i), map_iter_value(i)); } map_free(&m);Should output:
[hello] = 42 [world] = 69 [world] = 69Svmap example:
svmap_t svm; svmap_init(&svm, char); { char c = '%'; svmap_add(&svm, "hello", &c); if (svmap_err(&svm)) return -1; c = '$'; svmap_add(&svm, "world", &c); if (svmap_err(&svm)) return -1; svmap_balance(&svm); } svmap_foreach_i(&svm) { char c = svmap_iter_value_val(&svm, char, i); printf("[%s] = '%c'\n", svmap_iter_key(i), c); } svmap_free(&svm);Should output:
[hello] = '%' [world] = '$'Dynlist example:
dynlist_t l; enum { CHAR, STR, INT }; dynlist_init(&l); dynlist_setup(&l, char); dynlist_setup(&l, char *); dynlist_setup(&l, int); { char c = '&'; char *s = "hello"; int i = 42; dynlist_add(&l, INT, &i); dynlist_add(&l, CHAR, &c); dynlist_add(&l, STR, &s); s = "world"; dynlist_add(&l, STR, &s); } dynlist_foreach_i(&l) switch (dynlist_iter_type(i)) { case CHAR: printf("Got a char: '%c'\n", dynlist_iter_get_val(&l, char, i)); break; case STR: printf("Got a string: \"%s\"\n", dynlist_iter_get_val(&l, char *, i)); break; case INT: printf("Got an int: %d\n", dynlist_iter_get_val(&l, int, i)); break; } dynlist_free(&l);Should output:
Got an int: 42 Got a char: '&' Got a string: "hello" Got a string: "world"
Just download as ZIP or check out how to clone a gist.
This project is header only, the only thing needed is to include the header you need or to copy it if it's short enough.
Read carefully all the examples above and refer to the sources for more informations. Feel free to commit comments or corrections.
No license available yet.