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 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 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
35 struct hashmap_entry
{
36 struct hashmap_entry
*next
, *previous
, *bucket_next
, *bucket_previous
;
44 struct hashmap_entry
**data
;
45 struct hashmap_entry
*first_entry
;
48 unsigned (*hash_func
) (const void *p
);
49 int (*compare_func
) (const void*a
, const void*b
);
52 struct pa_hashmap
*pa_hashmap_new(unsigned (*hash_func
) (const void *p
), int (*compare_func
) (const void*a
, const void*b
)) {
54 h
= pa_xmalloc(sizeof(struct pa_hashmap
));
55 h
->data
= pa_xmalloc0(sizeof(struct hashmap_entry
*)*(h
->size
= 1023));
56 h
->first_entry
= NULL
;
58 h
->hash_func
= hash_func
? hash_func
: pa_idxset_trivial_hash_func
;
59 h
->compare_func
= compare_func
? compare_func
: pa_idxset_trivial_compare_func
;
63 static void remove(struct pa_hashmap
*h
, struct hashmap_entry
*e
) {
67 e
->next
->previous
= e
->previous
;
69 e
->previous
->next
= e
->next
;
71 h
->first_entry
= e
->next
;
74 e
->bucket_next
->bucket_previous
= e
->bucket_previous
;
75 if (e
->bucket_previous
)
76 e
->bucket_previous
->bucket_next
= e
->bucket_next
;
78 h
->data
[e
->hash
] = e
->bucket_next
;
84 void pa_hashmap_free(struct pa_hashmap
*h
, void (*free_func
)(void *p
, void *userdata
), void *userdata
) {
87 while (h
->first_entry
) {
89 free_func(h
->first_entry
->value
, userdata
);
90 remove(h
, h
->first_entry
);
97 static struct hashmap_entry
*get(struct pa_hashmap
*h
, unsigned hash
, const void *key
) {
98 struct hashmap_entry
*e
;
100 for (e
= h
->data
[hash
]; e
; e
= e
->bucket_next
)
101 if (h
->compare_func(e
->key
, key
) == 0)
107 int pa_hashmap_put(struct pa_hashmap
*h
, const void *key
, void *value
) {
108 struct hashmap_entry
*e
;
112 hash
= h
->hash_func(key
) % h
->size
;
114 if ((e
= get(h
, hash
, key
)))
117 e
= pa_xmalloc(sizeof(struct hashmap_entry
));
123 e
->next
= h
->first_entry
;
125 h
->first_entry
->previous
= e
;
128 e
->bucket_previous
= NULL
;
129 e
->bucket_next
= h
->data
[hash
];
131 h
->data
[hash
]->bucket_previous
= e
;
138 void* pa_hashmap_get(struct pa_hashmap
*h
, const void *key
) {
140 struct hashmap_entry
*e
;
143 hash
= h
->hash_func(key
) % h
->size
;
145 if (!(e
= get(h
, hash
, key
)))
151 void* pa_hashmap_remove(struct pa_hashmap
*h
, const void *key
) {
152 struct hashmap_entry
*e
;
157 hash
= h
->hash_func(key
) % h
->size
;
159 if (!(e
= get(h
, hash
, key
)))
167 unsigned pa_hashmap_ncontents(struct pa_hashmap
*h
) {
171 void *pa_hashmap_iterate(struct pa_hashmap
*h
, void **state
, const void **key
) {
175 *state
= h
->first_entry
;
177 *state
= ((struct hashmap_entry
*) *state
)->next
;
186 *key
= ((struct hashmap_entry
*) *state
)->key
;
188 return ((struct hashmap_entry
*) *state
)->value
;