forked from Telodendria/Telodendria
Add HashMapKeys() and HashMapValues() functions for convenience.
This commit is contained in:
parent
b65394ab50
commit
68b644a4f2
2 changed files with 73 additions and 0 deletions
|
@ -25,6 +25,7 @@
|
|||
|
||||
#include <Memory.h>
|
||||
#include <Str.h>
|
||||
#include <Array.h>
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -46,6 +46,8 @@
|
|||
|
||||
#include <stddef.h>
|
||||
|
||||
#include <Array.h>
|
||||
|
||||
/**
|
||||
* 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 */
|
||||
|
|
Loading…
Reference in a new issue