-
Notifications
You must be signed in to change notification settings - Fork 0
/
n_hash_keys.c
56 lines (42 loc) · 1.14 KB
/
n_hash_keys.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "n_hash_int.h"
#include "narray.h"
#include "nmalloc.h"
tn_array *n_hash_keys_ext(const tn_hash *ht, int cp)
{
register size_t i;
register struct hash_bucket *tmp;
tn_array *keys;
tn_fn_free fn = NULL;
if (cp)
fn = free;
keys = n_array_new(ht->items, fn, (tn_fn_cmp)strcmp);
for (i = 0; i < ht->size; i++) {
if (ht->table[i] == NULL)
continue;
for (tmp = ht->table[i]; tmp != NULL; tmp = tmp->next)
n_array_push(keys, cp ? n_strdup(tmp->key) : tmp->key);
}
return keys;
}
#undef n_hash_keys
tn_array *n_hash_keys(const tn_hash *ht)
{
return n_hash_keys_ext(ht, 0);
}
tn_array *n_hash_values(const tn_hash *ht)
{
register size_t i;
register struct hash_bucket *tmp;
tn_array *values;
values = n_array_new(ht->items, NULL, NULL);
for (i = 0; i < ht->size; i++) {
if (ht->table[i] == NULL)
continue;
for (tmp = ht->table[i]; tmp != NULL; tmp = tmp->next)
n_array_push(values, tmp->data);
}
return values;
}