]> code.delx.au - pulseaudio/blob - src/pulsecore/hashmap.h
hashmap: Add pa_hashmap_remove_and_free()
[pulseaudio] / src / pulsecore / hashmap.h
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, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #include <pulse/def.h>
26
27 #include <pulsecore/idxset.h>
28
29 /* Simple Implementation of a hash table. Memory management is the
30 * user's job. It's a good idea to have the key pointer point to a
31 * string in the value data. The insertion order is preserved when
32 * iterating. */
33
34 typedef struct pa_hashmap pa_hashmap;
35
36 /* Create a new hashmap. Use the specified functions for hashing and comparing objects in the map */
37 pa_hashmap *pa_hashmap_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func);
38
39 /* Create a new hashmap. Use the specified functions for hashing and comparing objects in the map, and functions to free the key
40 * and value (either or both can be NULL). */
41 pa_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);
42
43 /* Free the hash table. */
44 void pa_hashmap_free(pa_hashmap*);
45
46 /* Add an entry to the hashmap. Returns non-zero when the entry already exists */
47 int pa_hashmap_put(pa_hashmap *h, void *key, void *value);
48
49 /* Return an entry from the hashmap */
50 void* pa_hashmap_get(pa_hashmap *h, const void *key);
51
52 /* Returns the data of the entry while removing */
53 void* pa_hashmap_remove(pa_hashmap *h, const void *key);
54
55 /* Removes the entry and frees the entry data. Returns a negative value if the
56 * entry is not found. FIXME: This function shouldn't be needed.
57 * pa_hashmap_remove() should free the entry data, and the current semantics of
58 * pa_hashmap_remove() should be implemented by a function called
59 * pa_hashmap_steal(). */
60 int pa_hashmap_remove_and_free(pa_hashmap *h, const void *key);
61
62 /* Remove all entries but don't free the hashmap */
63 void pa_hashmap_remove_all(pa_hashmap *h);
64
65 /* Return the current number of entries of the hashmap */
66 unsigned pa_hashmap_size(pa_hashmap *h);
67
68 /* Return true if the hashmap is empty */
69 bool pa_hashmap_isempty(pa_hashmap *h);
70
71 /* May be used to iterate through the hashmap. Initially the opaque
72 pointer *state has to be set to NULL. The hashmap may not be
73 modified during iteration -- except for deleting the current entry
74 via pa_hashmap_remove(). The key of the entry is returned in *key,
75 if key is non-NULL. After the last entry in the hashmap NULL is
76 returned. */
77 void *pa_hashmap_iterate(pa_hashmap *h, void **state, const void**key);
78
79 /* Same as pa_hashmap_iterate() but goes backwards */
80 void *pa_hashmap_iterate_backwards(pa_hashmap *h, void **state, const void**key);
81
82 /* Remove the oldest entry in the hashmap and return it */
83 void *pa_hashmap_steal_first(pa_hashmap *h);
84
85 /* Return the oldest entry in the hashmap */
86 void* pa_hashmap_first(pa_hashmap *h);
87
88 /* Return the newest entry in the hashmap */
89 void* pa_hashmap_last(pa_hashmap *h);
90
91 /* A macro to ease iteration through all entries */
92 #define PA_HASHMAP_FOREACH(e, h, state) \
93 for ((state) = NULL, (e) = pa_hashmap_iterate((h), &(state), NULL); (e); (e) = pa_hashmap_iterate((h), &(state), NULL))
94
95 /* A macro to ease itration through all key, value pairs */
96 #define PA_HASHMAP_FOREACH_KV(k, e, h, state) \
97 for ((state) = NULL, (e) = pa_hashmap_iterate((h), &(state), (const void **) &(k)); (e); (e) = pa_hashmap_iterate((h), &(state), (const void **) &(k)))
98
99 /* A macro to ease iteration through all entries, backwards */
100 #define PA_HASHMAP_FOREACH_BACKWARDS(e, h, state) \
101 for ((state) = NULL, (e) = pa_hashmap_iterate_backwards((h), &(state), NULL); (e); (e) = pa_hashmap_iterate_backwards((h), &(state), NULL))
102
103 #endif