]> code.delx.au - pulseaudio/blob - src/polypcore/idxset.c
unhide padsp
[pulseaudio] / src / polypcore / idxset.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
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 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <polyp/xmalloc.h>
32
33 #include "idxset.h"
34
35 typedef struct idxset_entry {
36 void *data;
37 uint32_t index;
38 unsigned hash_value;
39
40 struct idxset_entry *hash_prev, *hash_next;
41 struct idxset_entry* iterate_prev, *iterate_next;
42 } idxset_entry;
43
44 struct pa_idxset {
45 unsigned (*hash_func) (const void *p);
46 int (*compare_func)(const void *a, const void *b);
47
48 unsigned hash_table_size, n_entries;
49 idxset_entry **hash_table, **array, *iterate_list_head, *iterate_list_tail;
50 uint32_t index, start_index, array_size;
51 };
52
53 unsigned pa_idxset_string_hash_func(const void *p) {
54 unsigned hash = 0;
55 const char *c;
56
57 for (c = p; *c; c++)
58 hash = 31 * hash + *c;
59
60 return hash;
61 }
62
63 int pa_idxset_string_compare_func(const void *a, const void *b) {
64 return strcmp(a, b);
65 }
66
67 unsigned pa_idxset_trivial_hash_func(const void *p) {
68 return (unsigned) (long) p;
69 }
70
71 int pa_idxset_trivial_compare_func(const void *a, const void *b) {
72 return a != b;
73 }
74
75 pa_idxset* pa_idxset_new(unsigned (*hash_func) (const void *p), int (*compare_func) (const void*a, const void*b)) {
76 pa_idxset *s;
77
78 s = pa_xnew(pa_idxset, 1);
79 s->hash_func = hash_func ? hash_func : pa_idxset_trivial_hash_func;
80 s->compare_func = compare_func ? compare_func : pa_idxset_trivial_compare_func;
81 s->hash_table_size = 1023;
82 s->hash_table = pa_xmalloc0(sizeof(idxset_entry*)*s->hash_table_size);
83 s->array = NULL;
84 s->array_size = 0;
85 s->index = 0;
86 s->start_index = 0;
87 s->n_entries = 0;
88
89 s->iterate_list_head = s->iterate_list_tail = NULL;
90
91 return s;
92 }
93
94 void pa_idxset_free(pa_idxset *s, void (*free_func) (void *p, void *userdata), void *userdata) {
95 assert(s);
96
97 while (s->iterate_list_head) {
98 idxset_entry *e = s->iterate_list_head;
99 s->iterate_list_head = s->iterate_list_head->iterate_next;
100
101 if (free_func)
102 free_func(e->data, userdata);
103 pa_xfree(e);
104 }
105
106 pa_xfree(s->hash_table);
107 pa_xfree(s->array);
108 pa_xfree(s);
109 }
110
111 static idxset_entry* hash_scan(pa_idxset *s, idxset_entry* e, const void *p) {
112 assert(p);
113
114 assert(s->compare_func);
115 for (; e; e = e->hash_next)
116 if (s->compare_func(e->data, p) == 0)
117 return e;
118
119 return NULL;
120 }
121
122 static void extend_array(pa_idxset *s, uint32_t idx) {
123 uint32_t i, j, l;
124 idxset_entry** n;
125 assert(idx >= s->start_index);
126
127 if (idx < s->start_index + s->array_size)
128 return;
129
130 for (i = 0; i < s->array_size; i++)
131 if (s->array[i])
132 break;
133
134 l = idx - s->start_index - i + 100;
135 n = pa_xnew0(idxset_entry*, l);
136
137 for (j = 0; j < s->array_size-i; j++)
138 n[j] = s->array[i+j];
139
140 pa_xfree(s->array);
141
142 s->array = n;
143 s->array_size = l;
144 s->start_index += i;
145 }
146
147 static idxset_entry** array_index(pa_idxset*s, uint32_t idx) {
148 if (idx >= s->start_index + s->array_size)
149 return NULL;
150
151 if (idx < s->start_index)
152 return NULL;
153
154 return s->array + (idx - s->start_index);
155 }
156
157 int pa_idxset_put(pa_idxset*s, void *p, uint32_t *idx) {
158 unsigned h;
159 idxset_entry *e, **a;
160 assert(s && p);
161
162 assert(s->hash_func);
163 h = s->hash_func(p) % s->hash_table_size;
164
165 assert(s->hash_table);
166 if ((e = hash_scan(s, s->hash_table[h], p))) {
167 if (idx)
168 *idx = e->index;
169
170 return -1;
171 }
172
173 e = pa_xmalloc(sizeof(idxset_entry));
174 e->data = p;
175 e->index = s->index++;
176 e->hash_value = h;
177
178 /* Insert into hash table */
179 e->hash_next = s->hash_table[h];
180 e->hash_prev = NULL;
181 if (s->hash_table[h])
182 s->hash_table[h]->hash_prev = e;
183 s->hash_table[h] = e;
184
185 /* Insert into array */
186 extend_array(s, e->index);
187 a = array_index(s, e->index);
188 assert(a && !*a);
189 *a = e;
190
191 /* Insert into linked list */
192 e->iterate_next = NULL;
193 e->iterate_prev = s->iterate_list_tail;
194 if (s->iterate_list_tail) {
195 assert(s->iterate_list_head);
196 s->iterate_list_tail->iterate_next = e;
197 } else {
198 assert(!s->iterate_list_head);
199 s->iterate_list_head = e;
200 }
201 s->iterate_list_tail = e;
202
203 s->n_entries++;
204 assert(s->n_entries >= 1);
205
206 if (idx)
207 *idx = e->index;
208
209 return 0;
210 }
211
212 void* pa_idxset_get_by_index(pa_idxset*s, uint32_t idx) {
213 idxset_entry **a;
214 assert(s);
215
216 if (!(a = array_index(s, idx)))
217 return NULL;
218
219 if (!*a)
220 return NULL;
221
222 return (*a)->data;
223 }
224
225 void* pa_idxset_get_by_data(pa_idxset*s, const void *p, uint32_t *idx) {
226 unsigned h;
227 idxset_entry *e;
228 assert(s && p);
229
230 assert(s->hash_func);
231 h = s->hash_func(p) % s->hash_table_size;
232
233 assert(s->hash_table);
234 if (!(e = hash_scan(s, s->hash_table[h], p)))
235 return NULL;
236
237 if (idx)
238 *idx = e->index;
239
240 return e->data;
241 }
242
243 static void remove_entry(pa_idxset *s, idxset_entry *e) {
244 idxset_entry **a;
245 assert(s && e);
246
247 /* Remove from array */
248 a = array_index(s, e->index);
249 assert(a && *a && *a == e);
250 *a = NULL;
251
252 /* Remove from linked list */
253 if (e->iterate_next)
254 e->iterate_next->iterate_prev = e->iterate_prev;
255 else
256 s->iterate_list_tail = e->iterate_prev;
257
258 if (e->iterate_prev)
259 e->iterate_prev->iterate_next = e->iterate_next;
260 else
261 s->iterate_list_head = e->iterate_next;
262
263 /* Remove from hash table */
264 if (e->hash_next)
265 e->hash_next->hash_prev = e->hash_prev;
266
267 if (e->hash_prev)
268 e->hash_prev->hash_next = e->hash_next;
269 else
270 s->hash_table[e->hash_value] = e->hash_next;
271
272 pa_xfree(e);
273
274 assert(s->n_entries >= 1);
275 s->n_entries--;
276 }
277
278 void* pa_idxset_remove_by_index(pa_idxset*s, uint32_t idx) {
279 idxset_entry **a;
280 void *data;
281
282 assert(s);
283
284 if (!(a = array_index(s, idx)))
285 return NULL;
286
287 data = (*a)->data;
288 remove_entry(s, *a);
289
290 return data;
291 }
292
293 void* pa_idxset_remove_by_data(pa_idxset*s, const void *data, uint32_t *idx) {
294 idxset_entry *e;
295 unsigned h;
296 void *r;
297
298 assert(s->hash_func);
299 h = s->hash_func(data) % s->hash_table_size;
300
301 assert(s->hash_table);
302 if (!(e = hash_scan(s, s->hash_table[h], data)))
303 return NULL;
304
305 r = e->data;
306 if (idx)
307 *idx = e->index;
308
309 remove_entry(s, e);
310
311 return r;
312 }
313
314 void* pa_idxset_rrobin(pa_idxset *s, uint32_t *idx) {
315 idxset_entry **a, *e = NULL;
316 assert(s && idx);
317
318 if ((a = array_index(s, *idx)) && *a)
319 e = (*a)->iterate_next;
320
321 if (!e)
322 e = s->iterate_list_head;
323
324 if (!e)
325 return NULL;
326
327 *idx = e->index;
328 return e->data;
329 }
330
331 void* pa_idxset_first(pa_idxset *s, uint32_t *idx) {
332 assert(s);
333
334 if (!s->iterate_list_head)
335 return NULL;
336
337 if (idx)
338 *idx = s->iterate_list_head->index;
339 return s->iterate_list_head->data;
340 }
341
342 void *pa_idxset_next(pa_idxset *s, uint32_t *idx) {
343 idxset_entry **a, *e = NULL;
344 assert(s && idx);
345
346 if ((a = array_index(s, *idx)) && *a)
347 e = (*a)->iterate_next;
348
349 if (e) {
350 *idx = e->index;
351 return e->data;
352 } else {
353 *idx = PA_IDXSET_INVALID;
354 return NULL;
355 }
356 }
357
358
359 int pa_idxset_foreach(pa_idxset*s, int (*func)(void *p, uint32_t idx, int *del, void*userdata), void *userdata) {
360 idxset_entry *e;
361 assert(s && func);
362
363 e = s->iterate_list_head;
364 while (e) {
365 int del = 0, r;
366 idxset_entry *n = e->iterate_next;
367
368 r = func(e->data, e->index, &del, userdata);
369
370 if (del)
371 remove_entry(s, e);
372
373 if (r < 0)
374 return r;
375
376 e = n;
377 }
378
379 return 0;
380 }
381
382 unsigned pa_idxset_size(pa_idxset*s) {
383 assert(s);
384 return s->n_entries;
385 }
386
387 int pa_idxset_isempty(pa_idxset *s) {
388 assert(s);
389 return s->n_entries == 0;
390 }
391