4 This file is part of polypaudio.
6 polypaudio 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 polypaudio 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 polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
37 struct hashmap_entry
{
38 struct hashmap_entry
*next
, *previous
, *bucket_next
, *bucket_previous
;
46 struct hashmap_entry
**data
;
47 struct hashmap_entry
*first_entry
;
50 unsigned (*hash_func
) (const void *p
);
51 int (*compare_func
) (const void*a
, const void*b
);
54 pa_hashmap
*pa_hashmap_new(unsigned (*hash_func
) (const void *p
), int (*compare_func
) (const void*a
, const void*b
)) {
56 h
= pa_xmalloc(sizeof(pa_hashmap
));
57 h
->data
= pa_xmalloc0(sizeof(struct hashmap_entry
*)*(h
->size
= BUCKETS
));
58 h
->first_entry
= NULL
;
60 h
->hash_func
= hash_func
? hash_func
: pa_idxset_trivial_hash_func
;
61 h
->compare_func
= compare_func
? compare_func
: pa_idxset_trivial_compare_func
;
65 static void remove(pa_hashmap
*h
, struct hashmap_entry
*e
) {
69 e
->next
->previous
= e
->previous
;
71 e
->previous
->next
= e
->next
;
73 h
->first_entry
= e
->next
;
76 e
->bucket_next
->bucket_previous
= e
->bucket_previous
;
77 if (e
->bucket_previous
)
78 e
->bucket_previous
->bucket_next
= e
->bucket_next
;
80 assert(e
->hash
< h
->size
);
81 h
->data
[e
->hash
] = e
->bucket_next
;
88 void pa_hashmap_free(pa_hashmap
*h
, void (*free_func
)(void *p
, void *userdata
), void *userdata
) {
91 while (h
->first_entry
) {
93 free_func(h
->first_entry
->value
, userdata
);
94 remove(h
, h
->first_entry
);
101 static struct hashmap_entry
*get(pa_hashmap
*h
, unsigned hash
, const void *key
) {
102 struct hashmap_entry
*e
;
103 assert(h
&& hash
< h
->size
);
105 for (e
= h
->data
[hash
]; e
; e
= e
->bucket_next
)
106 if (h
->compare_func(e
->key
, key
) == 0)
112 int pa_hashmap_put(pa_hashmap
*h
, const void *key
, void *value
) {
113 struct hashmap_entry
*e
;
117 hash
= h
->hash_func(key
) % h
->size
;
119 if ((e
= get(h
, hash
, key
)))
122 e
= pa_xmalloc(sizeof(struct hashmap_entry
));
128 e
->next
= h
->first_entry
;
130 h
->first_entry
->previous
= e
;
133 e
->bucket_previous
= NULL
;
134 e
->bucket_next
= h
->data
[hash
];
136 h
->data
[hash
]->bucket_previous
= e
;
143 void* pa_hashmap_get(pa_hashmap
*h
, const void *key
) {
145 struct hashmap_entry
*e
;
148 hash
= h
->hash_func(key
) % h
->size
;
150 if (!(e
= get(h
, hash
, key
)))
156 void* pa_hashmap_remove(pa_hashmap
*h
, const void *key
) {
157 struct hashmap_entry
*e
;
162 hash
= h
->hash_func(key
) % h
->size
;
164 if (!(e
= get(h
, hash
, key
)))
172 unsigned pa_hashmap_size(pa_hashmap
*h
) {
176 void *pa_hashmap_iterate(pa_hashmap
*h
, void **state
, const void **key
) {
180 *state
= h
->first_entry
;
182 *state
= ((struct hashmap_entry
*) *state
)->next
;
191 *key
= ((struct hashmap_entry
*) *state
)->key
;
193 return ((struct hashmap_entry
*) *state
)->value
;