diff --git a/src/HashMap.c b/src/HashMap.c index 620092d..b96973a 100644 --- a/src/HashMap.c +++ b/src/HashMap.c @@ -25,6 +25,7 @@ #include #include +#include #include #include @@ -399,3 +400,57 @@ HashMapIterateFree(char *key, void *value) Free(value); } } + +Array * +HashMapKeys(HashMap * map) +{ + Array *arr; + + char *key; + void *val; + + if (!map) + { + return NULL; + } + + arr = ArrayCreate(); + if (!arr) + { + return NULL; + } + + while (HashMapIterate(map, &key, &val)) + { + ArrayAdd(arr, key); + } + + return arr; +} + +Array * +HashMapValues(HashMap *map) +{ + Array *arr; + + char *key; + void *val; + + if (!map) + { + return NULL; + } + + arr = ArrayCreate(); + if (!arr) + { + return NULL; + } + + while (HashMapIterate(map, &key, &val)) + { + ArrayAdd(arr, val); + } + + return arr; +} diff --git a/src/include/HashMap.h b/src/include/HashMap.h index 57ed882..1359400 100644 --- a/src/include/HashMap.h +++ b/src/include/HashMap.h @@ -46,6 +46,8 @@ #include +#include + /** * These functions operate on an opaque structure, which the caller * has no knowledge about. @@ -164,4 +166,20 @@ extern int HashMapIterate(HashMap *, char **, void **); extern int HashMapIterateReentrant(HashMap *, char **, void **, size_t *); +/** + * Collect the string keys of a hash map and return them as an array. + * The returned array holds pointers to the strings stored in the + * hash map, so the strings should NOT be freed; it is sufficient to + * free the array itself. Likewise, once the hash map is freed, the + * array elements are invalid and the array should be freed. + */ +extern Array * HashMapKeys(HashMap *); + +/** + * Collect the values of a hash map and return them as an array. The + * returned array holds the same pointers to the values as the hash + * map. + */ +extern Array * HashMapValues(HashMap *); + #endif /* CYTOPLASM_HASHMAP_H */