4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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
11 published by the Free Software Foundation; either version 2.1 of the
12 License, or (at your option) any later version.
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 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <pulse/xmalloc.h>
34 #include <pulsecore/macro.h>
35 #include <pulsecore/flist.h>
44 struct idxset_entry
*hash_prev
, *hash_next
;
45 struct idxset_entry
* iterate_prev
, *iterate_next
;
49 pa_hash_func_t hash_func
;
50 pa_compare_func_t compare_func
;
52 unsigned hash_table_size
, n_entries
;
53 struct idxset_entry
**hash_table
, **array
, *iterate_list_head
, *iterate_list_tail
;
54 uint32_t index
, start_index
, array_size
;
57 PA_STATIC_FLIST_DECLARE(entries
, 0, pa_xfree
);
59 unsigned pa_idxset_string_hash_func(const void *p
) {
64 hash
= 31 * hash
+ *c
;
69 int pa_idxset_string_compare_func(const void *a
, const void *b
) {
73 unsigned pa_idxset_trivial_hash_func(const void *p
) {
74 return PA_PTR_TO_UINT(p
);
77 int pa_idxset_trivial_compare_func(const void *a
, const void *b
) {
81 pa_idxset
* pa_idxset_new(pa_hash_func_t hash_func
, pa_compare_func_t compare_func
) {
84 s
= pa_xnew(pa_idxset
, 1);
85 s
->hash_func
= hash_func
? hash_func
: pa_idxset_trivial_hash_func
;
86 s
->compare_func
= compare_func
? compare_func
: pa_idxset_trivial_compare_func
;
87 s
->hash_table_size
= 127;
88 s
->hash_table
= pa_xnew0(struct idxset_entry
*, s
->hash_table_size
);
95 s
->iterate_list_head
= s
->iterate_list_tail
= NULL
;
100 void pa_idxset_free(pa_idxset
*s
, void (*free_func
) (void *p
, void *userdata
), void *userdata
) {
103 while (s
->iterate_list_head
) {
104 struct idxset_entry
*e
= s
->iterate_list_head
;
105 s
->iterate_list_head
= s
->iterate_list_head
->iterate_next
;
108 free_func(e
->data
, userdata
);
110 if (pa_flist_push(PA_STATIC_FLIST_GET(entries
), e
) < 0)
114 pa_xfree(s
->hash_table
);
119 static struct idxset_entry
* hash_scan(pa_idxset
*s
, struct idxset_entry
* e
, const void *p
) {
122 pa_assert(s
->compare_func
);
123 for (; e
; e
= e
->hash_next
)
124 if (s
->compare_func(e
->data
, p
) == 0)
130 static void extend_array(pa_idxset
*s
, uint32_t idx
) {
132 struct idxset_entry
** n
;
135 pa_assert(idx
>= s
->start_index
);
137 if (idx
< s
->start_index
+ s
->array_size
)
140 for (i
= 0; i
< s
->array_size
; i
++)
144 l
= idx
- s
->start_index
- i
+ 100;
145 n
= pa_xnew0(struct idxset_entry
*, l
);
147 for (j
= 0; j
< s
->array_size
-i
; j
++)
148 n
[j
] = s
->array
[i
+j
];
157 static struct idxset_entry
** array_index(pa_idxset
*s
, uint32_t idx
) {
160 if (idx
>= s
->start_index
+ s
->array_size
)
163 if (idx
< s
->start_index
)
166 return s
->array
+ idx
- s
->start_index
;
169 int pa_idxset_put(pa_idxset
*s
, void *p
, uint32_t *idx
) {
171 struct idxset_entry
*e
, **a
;
176 pa_assert(s
->hash_func
);
177 h
= s
->hash_func(p
) % s
->hash_table_size
;
179 pa_assert(s
->hash_table
);
180 if ((e
= hash_scan(s
, s
->hash_table
[h
], p
))) {
187 if (!(e
= pa_flist_pop(PA_STATIC_FLIST_GET(entries
))))
188 e
= pa_xnew(struct idxset_entry
, 1);
190 e
->index
= s
->index
++;
193 /* Insert into hash table */
194 e
->hash_next
= s
->hash_table
[h
];
196 if (s
->hash_table
[h
])
197 s
->hash_table
[h
]->hash_prev
= e
;
198 s
->hash_table
[h
] = e
;
200 /* Insert into array */
201 extend_array(s
, e
->index
);
202 a
= array_index(s
, e
->index
);
206 /* Insert into linked list */
207 e
->iterate_next
= NULL
;
208 e
->iterate_prev
= s
->iterate_list_tail
;
209 if (s
->iterate_list_tail
) {
210 pa_assert(s
->iterate_list_head
);
211 s
->iterate_list_tail
->iterate_next
= e
;
213 pa_assert(!s
->iterate_list_head
);
214 s
->iterate_list_head
= e
;
216 s
->iterate_list_tail
= e
;
219 pa_assert(s
->n_entries
>= 1);
227 void* pa_idxset_get_by_index(pa_idxset
*s
, uint32_t idx
) {
228 struct idxset_entry
**a
;
231 if (!(a
= array_index(s
, idx
)))
240 void* pa_idxset_get_by_data(pa_idxset
*s
, const void *p
, uint32_t *idx
) {
242 struct idxset_entry
*e
;
247 pa_assert(s
->hash_func
);
248 h
= s
->hash_func(p
) % s
->hash_table_size
;
250 pa_assert(s
->hash_table
);
251 if (!(e
= hash_scan(s
, s
->hash_table
[h
], p
)))
260 static void remove_entry(pa_idxset
*s
, struct idxset_entry
*e
) {
261 struct idxset_entry
**a
;
266 /* Remove from array */
267 a
= array_index(s
, e
->index
);
268 pa_assert(a
&& *a
&& *a
== e
);
271 /* Remove from linked list */
273 e
->iterate_next
->iterate_prev
= e
->iterate_prev
;
275 s
->iterate_list_tail
= e
->iterate_prev
;
278 e
->iterate_prev
->iterate_next
= e
->iterate_next
;
280 s
->iterate_list_head
= e
->iterate_next
;
282 /* Remove from hash table */
284 e
->hash_next
->hash_prev
= e
->hash_prev
;
287 e
->hash_prev
->hash_next
= e
->hash_next
;
289 s
->hash_table
[e
->hash_value
] = e
->hash_next
;
291 if (pa_flist_push(PA_STATIC_FLIST_GET(entries
), e
) < 0)
294 pa_assert(s
->n_entries
>= 1);
298 void* pa_idxset_remove_by_index(pa_idxset
*s
, uint32_t idx
) {
299 struct idxset_entry
**a
;
304 if (!(a
= array_index(s
, idx
)))
316 void* pa_idxset_remove_by_data(pa_idxset
*s
, const void *data
, uint32_t *idx
) {
317 struct idxset_entry
*e
;
323 pa_assert(s
->hash_func
);
324 h
= s
->hash_func(data
) % s
->hash_table_size
;
326 pa_assert(s
->hash_table
);
327 if (!(e
= hash_scan(s
, s
->hash_table
[h
], data
)))
339 void* pa_idxset_rrobin(pa_idxset
*s
, uint32_t *idx
) {
340 struct idxset_entry
**a
, *e
= NULL
;
345 if ((a
= array_index(s
, *idx
)) && *a
)
346 e
= (*a
)->iterate_next
;
349 e
= s
->iterate_list_head
;
358 void* pa_idxset_first(pa_idxset
*s
, uint32_t *idx
) {
361 if (!s
->iterate_list_head
)
365 *idx
= s
->iterate_list_head
->index
;
366 return s
->iterate_list_head
->data
;
369 void *pa_idxset_next(pa_idxset
*s
, uint32_t *idx
) {
370 struct idxset_entry
**a
, *e
= NULL
;
375 if ((a
= array_index(s
, *idx
)) && *a
)
376 e
= (*a
)->iterate_next
;
382 *idx
= PA_IDXSET_INVALID
;
387 int pa_idxset_foreach(pa_idxset
*s
, int (*func
)(void *p
, uint32_t idx
, int *del
, void*userdata
), void *userdata
) {
388 struct idxset_entry
*e
;
393 e
= s
->iterate_list_head
;
396 struct idxset_entry
*n
= e
->iterate_next
;
398 r
= func(e
->data
, e
->index
, &del
, userdata
);
412 unsigned pa_idxset_size(pa_idxset
*s
) {
418 int pa_idxset_isempty(pa_idxset
*s
) {
421 return s
->n_entries
== 0;