1 #ifndef foopulsecoreidxsethfoo
2 #define foopulsecoreidxsethfoo
5 This file is part of PulseAudio.
7 Copyright 2004-2008 Lennart Poettering
8 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
10 PulseAudio is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as
12 published by the Free Software Foundation; either version 2.1 of the
13 License, or (at your option) any later version.
15 PulseAudio is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with PulseAudio; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <pulsecore/macro.h>
30 /* A combination of a set and a dynamic array. Entries are indexable
31 * both through an automatically generated numeric index and the
32 * entry's data pointer. As usual, memory management is the user's
35 /* A special index value denoting the invalid index. */
36 #define PA_IDXSET_INVALID ((uint32_t) -1)
38 /* Similar to pa_free_cb_t, but takes a userdata argument */
39 typedef void (*pa_free2_cb_t
)(void *p
, void *userdata
);
41 /* Generic implementations for hash and comparison functions. Just
42 * compares the pointer or calculates the hash value directly from the
44 unsigned pa_idxset_trivial_hash_func(const void *p
);
45 int pa_idxset_trivial_compare_func(const void *a
, const void *b
);
47 /* Generic implementations for hash and comparison functions for strings. */
48 unsigned pa_idxset_string_hash_func(const void *p
);
49 int pa_idxset_string_compare_func(const void *a
, const void *b
);
51 typedef unsigned (*pa_hash_func_t
)(const void *p
);
52 typedef int (*pa_compare_func_t
)(const void *a
, const void *b
);
54 typedef struct pa_idxset pa_idxset
;
56 /* Instantiate a new idxset with the specified hash and comparison functions */
57 pa_idxset
* pa_idxset_new(pa_hash_func_t hash_func
, pa_compare_func_t compare_func
);
59 /* Free the idxset. When the idxset is not empty the specified function is called for every entry contained */
60 void pa_idxset_free(pa_idxset
*s
, pa_free2_cb_t free_cb
, void *userdata
);
62 /* Store a new item in the idxset. The index of the item is returned in *idx */
63 int pa_idxset_put(pa_idxset
*s
, void *p
, uint32_t *idx
);
65 /* Get the entry by its idx */
66 void* pa_idxset_get_by_index(pa_idxset
*s
, uint32_t idx
);
68 /* Get the entry by its data. The index is returned in *idx */
69 void* pa_idxset_get_by_data(pa_idxset
*s
, const void *p
, uint32_t *idx
);
71 /* Similar to pa_idxset_get_by_index(), but removes the entry from the idxset. */
72 void* pa_idxset_remove_by_index(pa_idxset
*s
, uint32_t idx
);
74 /* Similar to pa_idxset_get_by_data(), but removes the entry from the idxset */
75 void* pa_idxset_remove_by_data(pa_idxset
*s
, const void *p
, uint32_t *idx
);
77 /* This may be used to iterate through all entries. When called with
78 an invalid index value it returns the first entry, otherwise the
79 next following. The function is best called with *idx =
80 PA_IDXSET_VALID first. It is safe to manipulate the idxset between
81 the calls. It is not guaranteed that all entries have already been
82 returned before the an entry is returned the second time.*/
83 void* pa_idxset_rrobin(pa_idxset
*s
, uint32_t *idx
);
85 /* Iterate through the idxset. At first iteration state should be NULL */
86 void *pa_idxset_iterate(pa_idxset
*s
, void **state
, uint32_t *idx
);
88 /* Return the oldest entry in the idxset and remove it. If idx is not NULL fill in its index in *idx */
89 void* pa_idxset_steal_first(pa_idxset
*s
, uint32_t *idx
);
91 /* Return the oldest entry in the idxset. Fill in its index in *idx. */
92 void* pa_idxset_first(pa_idxset
*s
, uint32_t *idx
);
94 /* Return the entry following the entry indexed by *idx. After the
95 * call *index contains the index of the returned
96 * object. pa_idxset_first() and pa_idxset_next() may be used to
97 * iterate through the set.*/
98 void *pa_idxset_next(pa_idxset
*s
, uint32_t *idx
);
100 /* Return the current number of entries in the idxset */
101 unsigned pa_idxset_size(pa_idxset
*s
);
103 /* Return TRUE of the idxset is empty */
104 pa_bool_t
pa_idxset_isempty(pa_idxset
*s
);
106 /* Duplicate the idxset. This will not copy the actual indexes */
107 pa_idxset
*pa_idxset_copy(pa_idxset
*s
);
109 /* A macro to ease iteration through all entries */
110 #define PA_IDXSET_FOREACH(e, s, idx) \
111 for ((e) = pa_idxset_first((s), &(idx)); (e); (e) = pa_idxset_next((s), &(idx)))