From a1d84e39359fed52770f9d15650aabc6bcce6910 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 17 Jun 2009 03:02:19 +0200 Subject: hashmap: implement api to iterate a hashmap backwards --- src/pulsecore/hashmap.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/pulsecore/hashmap.c') diff --git a/src/pulsecore/hashmap.c b/src/pulsecore/hashmap.c index e957c5ba..b549cb1c 100644 --- a/src/pulsecore/hashmap.c +++ b/src/pulsecore/hashmap.c @@ -237,6 +237,39 @@ at_end: return NULL; } +void *pa_hashmap_iterate_backwards(pa_hashmap *h, void **state, const void **key) { + struct hashmap_entry *e; + + pa_assert(h); + pa_assert(state); + + if (*state == (void*) -1) + goto at_beginning; + + if (!*state && !h->iterate_list_tail) + goto at_beginning; + + e = *state ? *state : h->iterate_list_tail; + + if (e->iterate_previous) + *state = e->iterate_previous; + else + *state = (void*) -1; + + if (key) + *key = e->key; + + return e->value; + +at_beginning: + *state = (void *) -1; + + if (key) + *key = NULL; + + return NULL; +} + void* pa_hashmap_first(pa_hashmap *h) { pa_assert(h); -- cgit From c6830bd9dc53ee745ac331c4ab1c55134562d114 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 17 Jun 2009 03:02:34 +0200 Subject: hashmap: implement pa_hashmap_last() --- src/pulsecore/hashmap.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/pulsecore/hashmap.c') diff --git a/src/pulsecore/hashmap.c b/src/pulsecore/hashmap.c index b549cb1c..1fac97eb 100644 --- a/src/pulsecore/hashmap.c +++ b/src/pulsecore/hashmap.c @@ -279,6 +279,15 @@ void* pa_hashmap_first(pa_hashmap *h) { return h->iterate_list_head->value; } +void* pa_hashmap_last(pa_hashmap *h) { + pa_assert(h); + + if (!h->iterate_list_tail) + return NULL; + + return h->iterate_list_tail->value; +} + void* pa_hashmap_steal_first(pa_hashmap *h) { void *data; -- cgit