mirror of
https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
synced 2025-11-02 09:01:46 -05:00
cleanup hashmap.[ch] a little: use hash/compare func prototypes defined in idxset.h, add pa_hashmpa_{get,steal}_first
git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1263 fefdeb5f-60dc-0310-8127-8f9354f1896f
This commit is contained in:
parent
8be0cf6007
commit
dfa17b9f36
2 changed files with 46 additions and 11 deletions
|
|
@ -49,22 +49,25 @@ struct pa_hashmap {
|
|||
struct hashmap_entry *first_entry;
|
||||
|
||||
unsigned n_entries;
|
||||
unsigned (*hash_func) (const void *p);
|
||||
int (*compare_func) (const void*a, const void*b);
|
||||
pa_hash_func_t hash_func;
|
||||
pa_compare_func_t compare_func;
|
||||
};
|
||||
|
||||
pa_hashmap *pa_hashmap_new(unsigned (*hash_func) (const void *p), int (*compare_func) (const void*a, const void*b)) {
|
||||
pa_hashmap *pa_hashmap_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func) {
|
||||
pa_hashmap *h;
|
||||
h = pa_xmalloc(sizeof(pa_hashmap));
|
||||
h->data = pa_xmalloc0(sizeof(struct hashmap_entry*)*(h->size = BUCKETS));
|
||||
|
||||
h = pa_xnew(pa_hashmap, 1);
|
||||
h->data = pa_xnew0(struct hashmap_entry*, h->size = BUCKETS);
|
||||
h->first_entry = NULL;
|
||||
h->n_entries = 0;
|
||||
h->hash_func = hash_func ? hash_func : pa_idxset_trivial_hash_func;
|
||||
h->compare_func = compare_func ? compare_func : pa_idxset_trivial_compare_func;
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
static void remove(pa_hashmap *h, struct hashmap_entry *e) {
|
||||
assert(h);
|
||||
assert(e);
|
||||
|
||||
if (e->next)
|
||||
|
|
@ -102,7 +105,8 @@ void pa_hashmap_free(pa_hashmap*h, void (*free_func)(void *p, void *userdata), v
|
|||
|
||||
static struct hashmap_entry *get(pa_hashmap *h, unsigned hash, const void *key) {
|
||||
struct hashmap_entry *e;
|
||||
assert(h && hash < h->size);
|
||||
assert(h);
|
||||
assert(hash < h->size);
|
||||
|
||||
for (e = h->data[hash]; e; e = e->bucket_next)
|
||||
if (h->compare_func(e->key, key) == 0)
|
||||
|
|
@ -121,7 +125,7 @@ int pa_hashmap_put(pa_hashmap *h, const void *key, void *value) {
|
|||
if ((e = get(h, hash, key)))
|
||||
return -1;
|
||||
|
||||
e = pa_xmalloc(sizeof(struct hashmap_entry));
|
||||
e = pa_xnew(struct hashmap_entry, 1);
|
||||
e->hash = hash;
|
||||
e->key = key;
|
||||
e->value = value;
|
||||
|
|
@ -145,7 +149,8 @@ int pa_hashmap_put(pa_hashmap *h, const void *key, void *value) {
|
|||
void* pa_hashmap_get(pa_hashmap *h, const void *key) {
|
||||
unsigned hash;
|
||||
struct hashmap_entry *e;
|
||||
assert(h && key);
|
||||
|
||||
assert(h);
|
||||
|
||||
hash = h->hash_func(key) % h->size;
|
||||
|
||||
|
|
@ -159,7 +164,8 @@ void* pa_hashmap_remove(pa_hashmap *h, const void *key) {
|
|||
struct hashmap_entry *e;
|
||||
unsigned hash;
|
||||
void *data;
|
||||
assert(h && key);
|
||||
|
||||
assert(h);
|
||||
|
||||
hash = h->hash_func(key) % h->size;
|
||||
|
||||
|
|
@ -176,7 +182,8 @@ unsigned pa_hashmap_size(pa_hashmap *h) {
|
|||
}
|
||||
|
||||
void *pa_hashmap_iterate(pa_hashmap *h, void **state, const void **key) {
|
||||
assert(h && state);
|
||||
assert(h);
|
||||
assert(state);
|
||||
|
||||
if (!*state)
|
||||
*state = h->first_entry;
|
||||
|
|
@ -194,3 +201,25 @@ void *pa_hashmap_iterate(pa_hashmap *h, void **state, const void **key) {
|
|||
|
||||
return ((struct hashmap_entry*) *state)->value;
|
||||
}
|
||||
|
||||
void* pa_hashmap_steal_first(pa_hashmap *h) {
|
||||
void *data;
|
||||
|
||||
assert(h);
|
||||
|
||||
if (!h->first_entry)
|
||||
return NULL;
|
||||
|
||||
data = h->first_entry->value;
|
||||
remove(h, h->first_entry);
|
||||
return data;
|
||||
}
|
||||
|
||||
void *pa_hashmap_get_first(pa_hashmap *h) {
|
||||
assert(h);
|
||||
|
||||
if (!h->first_entry)
|
||||
return NULL;
|
||||
|
||||
return h->first_entry->value;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,8 @@
|
|||
USA.
|
||||
***/
|
||||
|
||||
#include <pulsecore/idxset.h>
|
||||
|
||||
/* Simple Implementation of a hash table. Memory management is the
|
||||
* user's job. It's a good idea to have the key pointer point to a
|
||||
* string in the value data. */
|
||||
|
|
@ -29,7 +31,7 @@
|
|||
typedef struct pa_hashmap pa_hashmap;
|
||||
|
||||
/* Create a new hashmap. Use the specified functions for hashing and comparing objects in the map */
|
||||
pa_hashmap *pa_hashmap_new(unsigned (*hash_func) (const void *p), int (*compare_func) (const void*a, const void*b));
|
||||
pa_hashmap *pa_hashmap_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func);
|
||||
|
||||
/* Free the hash table. Calls the specified function for every value in the table. The function may be NULL */
|
||||
void pa_hashmap_free(pa_hashmap*, void (*free_func)(void *p, void *userdata), void *userdata);
|
||||
|
|
@ -50,4 +52,8 @@ unsigned pa_hashmap_size(pa_hashmap *h);
|
|||
is returned. */
|
||||
void *pa_hashmap_iterate(pa_hashmap *h, void **state, const void**key);
|
||||
|
||||
void *pa_hashmap_steal_first(pa_hashmap *h);
|
||||
|
||||
void *pa_hashmap_get_first(pa_hashmap *h);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue