4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <pulse/xmalloc.h>
32 #include <pulsecore/idxset.h>
33 #include <pulsecore/log.h>
39 struct hashmap_entry
{
40 struct hashmap_entry
*next
, *previous
, *bucket_next
, *bucket_previous
;
48 struct hashmap_entry
**data
;
49 struct hashmap_entry
*first_entry
;
52 unsigned (*hash_func
) (const void *p
);
53 int (*compare_func
) (const void*a
, const void*b
);
56 pa_hashmap
*pa_hashmap_new(unsigned (*hash_func
) (const void *p
), int (*compare_func
) (const void*a
, const void*b
)) {
58 h
= pa_xmalloc(sizeof(pa_hashmap
));
59 h
->data
= pa_xmalloc0(sizeof(struct hashmap_entry
*)*(h
->size
= BUCKETS
));
60 h
->first_entry
= NULL
;
62 h
->hash_func
= hash_func
? hash_func
: pa_idxset_trivial_hash_func
;
63 h
->compare_func
= compare_func
? compare_func
: pa_idxset_trivial_compare_func
;
67 static void remove(pa_hashmap
*h
, struct hashmap_entry
*e
) {
71 e
->next
->previous
= e
->previous
;
73 e
->previous
->next
= e
->next
;
75 h
->first_entry
= e
->next
;
78 e
->bucket_next
->bucket_previous
= e
->bucket_previous
;
79 if (e
->bucket_previous
)
80 e
->bucket_previous
->bucket_next
= e
->bucket_next
;
82 assert(e
->hash
< h
->size
);
83 h
->data
[e
->hash
] = e
->bucket_next
;
90 void pa_hashmap_free(pa_hashmap
*h
, void (*free_func
)(void *p
, void *userdata
), void *userdata
) {
93 while (h
->first_entry
) {
95 free_func(h
->first_entry
->value
, userdata
);
96 remove(h
, h
->first_entry
);
103 static struct hashmap_entry
*get(pa_hashmap
*h
, unsigned hash
, const void *key
) {
104 struct hashmap_entry
*e
;
105 assert(h
&& hash
< h
->size
);
107 for (e
= h
->data
[hash
]; e
; e
= e
->bucket_next
)
108 if (h
->compare_func(e
->key
, key
) == 0)
114 int pa_hashmap_put(pa_hashmap
*h
, const void *key
, void *value
) {
115 struct hashmap_entry
*e
;
119 hash
= h
->hash_func(key
) % h
->size
;
121 if ((e
= get(h
, hash
, key
)))
124 e
= pa_xmalloc(sizeof(struct hashmap_entry
));
130 e
->next
= h
->first_entry
;
132 h
->first_entry
->previous
= e
;
135 e
->bucket_previous
= NULL
;
136 e
->bucket_next
= h
->data
[hash
];
138 h
->data
[hash
]->bucket_previous
= e
;
145 void* pa_hashmap_get(pa_hashmap
*h
, const void *key
) {
147 struct hashmap_entry
*e
;
150 hash
= h
->hash_func(key
) % h
->size
;
152 if (!(e
= get(h
, hash
, key
)))
158 void* pa_hashmap_remove(pa_hashmap
*h
, const void *key
) {
159 struct hashmap_entry
*e
;
164 hash
= h
->hash_func(key
) % h
->size
;
166 if (!(e
= get(h
, hash
, key
)))
174 unsigned pa_hashmap_size(pa_hashmap
*h
) {
178 void *pa_hashmap_iterate(pa_hashmap
*h
, void **state
, const void **key
) {
182 *state
= h
->first_entry
;
184 *state
= ((struct hashmap_entry
*) *state
)->next
;
193 *key
= ((struct hashmap_entry
*) *state
)->key
;
195 return ((struct hashmap_entry
*) *state
)->value
;