1#ifndef foopulsecorehashmaphfoo
2#define foopulsecorehashmaphfoo
3
4/***
5  This file is part of PulseAudio.
6
7  Copyright 2004-2008 Lennart Poettering
8
9  PulseAudio is free software; you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published
11  by the Free Software Foundation; either version 2.1 of the License,
12  or (at your option) any later version.
13
14  PulseAudio is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU Lesser General Public License
20  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
21***/
22
23#include <pulse/def.h>
24
25#include <pulsecore/idxset.h>
26
27/* Simple Implementation of a hash table. Memory management is the
28 * user's job. It's a good idea to have the key pointer point to a
29 * string in the value data. The insertion order is preserved when
30 * iterating. */
31
32typedef struct pa_hashmap pa_hashmap;
33
34/* Create a new hashmap. Use the specified functions for hashing and comparing objects in the map */
35pa_hashmap *pa_hashmap_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func);
36
37/* Create a new hashmap. Use the specified functions for hashing and comparing objects in the map, and functions to free the key
38 * and value (either or both can be NULL). */
39pa_hashmap *pa_hashmap_new_full(pa_hash_func_t hash_func, pa_compare_func_t compare_func, pa_free_cb_t key_free_func, pa_free_cb_t value_free_func);
40
41/* Free the hash table. */
42void pa_hashmap_free(pa_hashmap*);
43
44/* Add an entry to the hashmap. Returns non-zero when the entry already exists */
45int pa_hashmap_put(pa_hashmap *h, void *key, void *value);
46
47/* Return an entry from the hashmap */
48void* pa_hashmap_get(const pa_hashmap *h, const void *key);
49
50/* Returns the data of the entry while removing */
51void* pa_hashmap_remove(pa_hashmap *h, const void *key);
52
53/* Removes the entry and frees the entry data. Returns a negative value if the
54 * entry is not found. FIXME: This function shouldn't be needed.
55 * pa_hashmap_remove() should free the entry data, and the current semantics of
56 * pa_hashmap_remove() should be implemented by a function called
57 * pa_hashmap_steal(). */
58int pa_hashmap_remove_and_free(pa_hashmap *h, const void *key);
59
60/* Remove all entries but don't free the hashmap */
61void pa_hashmap_remove_all(pa_hashmap *h);
62
63/* Return the current number of entries of the hashmap */
64unsigned pa_hashmap_size(const pa_hashmap *h);
65
66/* Return true if the hashmap is empty */
67bool pa_hashmap_isempty(const pa_hashmap *h);
68
69/* May be used to iterate through the hashmap. Initially the opaque
70   pointer *state has to be set to NULL. The hashmap may not be
71   modified during iteration -- except for deleting the current entry
72   via pa_hashmap_remove(). The key of the entry is returned in *key,
73   if key is non-NULL. After the last entry in the hashmap NULL is
74   returned. */
75void *pa_hashmap_iterate(const pa_hashmap *h, void **state, const void**key);
76
77/* Same as pa_hashmap_iterate() but goes backwards */
78void *pa_hashmap_iterate_backwards(const pa_hashmap *h, void **state, const void**key);
79
80/* Remove the oldest entry in the hashmap and return it */
81void *pa_hashmap_steal_first(pa_hashmap *h);
82
83/* Return the oldest entry in the hashmap */
84void* pa_hashmap_first(const pa_hashmap *h);
85
86/* Return the newest entry in the hashmap */
87void* pa_hashmap_last(const pa_hashmap *h);
88
89/* A macro to ease iteration through all entries */
90#define PA_HASHMAP_FOREACH(e, h, state) \
91    for ((state) = NULL, (e) = pa_hashmap_iterate((h), &(state), NULL); (e); (e) = pa_hashmap_iterate((h), &(state), NULL))
92
93/* A macro to ease itration through all key, value pairs */
94#define PA_HASHMAP_FOREACH_KV(k, e, h, state) \
95    for ((state) = NULL, (e) = pa_hashmap_iterate((h), &(state), (const void **) &(k)); (e); (e) = pa_hashmap_iterate((h), &(state), (const void **) &(k)))
96
97/* A macro to ease iteration through all entries, backwards */
98#define PA_HASHMAP_FOREACH_BACKWARDS(e, h, state) \
99    for ((state) = NULL, (e) = pa_hashmap_iterate_backwards((h), &(state), NULL); (e); (e) = pa_hashmap_iterate_backwards((h), &(state), NULL))
100
101#endif
102