• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef foopulsecoreidxsethfoo
2 #define foopulsecoreidxsethfoo
3 
4 /***
5   This file is part of PulseAudio.
6 
7   Copyright 2004-2008 Lennart Poettering
8   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 
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.
14 
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.
19 
20   You should have received a copy of the GNU Lesser General Public
21   License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
22 ***/
23 
24 #include <inttypes.h>
25 
26 #include <pulse/def.h>
27 
28 #include <pulsecore/macro.h>
29 
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
33  * job. */
34 
35 /* A special index value denoting the invalid index. */
36 #define PA_IDXSET_INVALID ((uint32_t) -1)
37 
38 /* Generic implementations for hash and comparison functions. Just
39  * compares the pointer or calculates the hash value directly from the
40  * pointer value. */
41 unsigned pa_idxset_trivial_hash_func(const void *p);
42 int pa_idxset_trivial_compare_func(const void *a, const void *b);
43 
44 /* Generic implementations for hash and comparison functions for strings. */
45 unsigned pa_idxset_string_hash_func(const void *p);
46 int pa_idxset_string_compare_func(const void *a, const void *b);
47 
48 typedef unsigned (*pa_hash_func_t)(const void *p);
49 typedef int (*pa_compare_func_t)(const void *a, const void *b);
50 typedef void *(*pa_copy_func_t)(const void *p);
51 
52 typedef struct pa_idxset pa_idxset;
53 
54 /* Instantiate a new idxset with the specified hash and comparison functions */
55 pa_idxset* pa_idxset_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func);
56 
57 /* Free the idxset. When the idxset is not empty the specified function is called for every entry contained */
58 void pa_idxset_free(pa_idxset *s, pa_free_cb_t free_cb);
59 
60 /* Store a new item in the idxset. The index of the item is returned in *idx */
61 int pa_idxset_put(pa_idxset*s, void *p, uint32_t *idx);
62 
63 /* Get the entry by its idx */
64 void* pa_idxset_get_by_index(pa_idxset*s, uint32_t idx);
65 
66 /* Get the entry by its data. The index is returned in *idx */
67 void* pa_idxset_get_by_data(pa_idxset*s, const void *p, uint32_t *idx);
68 
69 /* Similar to pa_idxset_get_by_index(), but removes the entry from the idxset. */
70 void* pa_idxset_remove_by_index(pa_idxset*s, uint32_t idx);
71 
72 /* Similar to pa_idxset_get_by_data(), but removes the entry from the idxset */
73 void* pa_idxset_remove_by_data(pa_idxset*s, const void *p, uint32_t *idx);
74 
75 /* If free_cb is not NULL, it's called for each entry. */
76 void pa_idxset_remove_all(pa_idxset *s, pa_free_cb_t free_cb);
77 
78 /* This may be used to iterate through all entries. When called with
79    an invalid index value it returns the first entry, otherwise the
80    next following. The function is best called with *idx =
81    PA_IDXSET_VALID first. It is safe to manipulate the idxset between
82    the calls. It is not guaranteed that all entries have already been
83    returned before the an entry is returned the second time.*/
84 void* pa_idxset_rrobin(pa_idxset *s, uint32_t *idx);
85 
86 /* Iterate through the idxset. At first iteration state should be NULL */
87 void *pa_idxset_iterate(pa_idxset *s, void **state, uint32_t *idx);
88 
89 /* Return the oldest entry in the idxset and remove it. If idx is not NULL fill in its index in *idx */
90 void* pa_idxset_steal_first(pa_idxset *s, uint32_t *idx);
91 
92 /* Return the oldest entry in the idxset. Fill in its index in *idx. */
93 void* pa_idxset_first(pa_idxset *s, uint32_t *idx);
94 
95 /* Return the entry following the entry indexed by *idx.  After the
96  * call *index contains the index of the returned
97  * object. pa_idxset_first() and pa_idxset_next() may be used to
98  * iterate through the set.*/
99 void *pa_idxset_next(pa_idxset *s, uint32_t *idx);
100 
101 /* Return the current number of entries in the idxset */
102 unsigned pa_idxset_size(pa_idxset*s);
103 
104 /* Return true of the idxset is empty */
105 bool pa_idxset_isempty(pa_idxset *s);
106 
107 /* Duplicate the idxset. This will not copy the actual indexes. If copy_func is
108  * set, each entry is copied using the provided function, otherwise a shallow
109  * copy will be made. */
110 pa_idxset *pa_idxset_copy(pa_idxset *s, pa_copy_func_t copy_func);
111 
112 /* A macro to ease iteration through all entries */
113 #define PA_IDXSET_FOREACH(e, s, idx) \
114     for ((e) = pa_idxset_first((s), &(idx)); (e); (e) = pa_idxset_next((s), &(idx)))
115 
116 #endif
117