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 /* Return true if item is in idxset */ 70 bool pa_idxset_contains(pa_idxset *s, const void *p); 71 72 /* Similar to pa_idxset_get_by_index(), but removes the entry from the idxset. */ 73 void* pa_idxset_remove_by_index(pa_idxset*s, uint32_t idx); 74 75 /* Similar to pa_idxset_get_by_data(), but removes the entry from the idxset */ 76 void* pa_idxset_remove_by_data(pa_idxset*s, const void *p, uint32_t *idx); 77 78 /* If free_cb is not NULL, it's called for each entry. */ 79 void pa_idxset_remove_all(pa_idxset *s, pa_free_cb_t free_cb); 80 81 /* This may be used to iterate through all entries. When called with 82 an invalid index value it returns the first entry, otherwise the 83 next following. The function is best called with *idx = 84 PA_IDXSET_VALID first. It is safe to manipulate the idxset between 85 the calls. It is not guaranteed that all entries have already been 86 returned before the an entry is returned the second time.*/ 87 void* pa_idxset_rrobin(pa_idxset *s, uint32_t *idx); 88 89 /* Iterate through the idxset. At first iteration state should be NULL */ 90 void *pa_idxset_iterate(pa_idxset *s, void **state, uint32_t *idx); 91 void *pa_idxset_reverse_iterate(pa_idxset *s, void **state, uint32_t *idx); 92 93 /* Return the oldest or newest entry in the idxset and remove it. 94 * If idx is not NULL fill in its index in *idx */ 95 void* pa_idxset_steal_first(pa_idxset *s, uint32_t *idx); 96 void* pa_idxset_steal_last(pa_idxset *s, uint32_t *idx); 97 98 /* Return the oldest or newest entry in the idxset. 99 * Fill in its index in *idx. */ 100 void* pa_idxset_first(pa_idxset *s, uint32_t *idx); 101 void* pa_idxset_last(pa_idxset *s, uint32_t *idx); 102 103 /* Return the entry following or preceding the entry indexed by *idx. 104 * After the call *index contains the index of the returned object. 105 * pa_idxset_first() and pa_idxset_next() may be used to iterate through 106 * the set. pa_idxset_last() and pa_idxset_previous() may be used to 107 * iterate through the set in reverse. */ 108 void *pa_idxset_next(pa_idxset *s, uint32_t *idx); 109 void *pa_idxset_previous(pa_idxset *s, uint32_t *idx); 110 111 /* Return the current number of entries in the idxset */ 112 unsigned pa_idxset_size(pa_idxset*s); 113 114 /* Return true of the idxset is empty */ 115 bool pa_idxset_isempty(pa_idxset *s); 116 117 /* Return true if s and t have no entries in common */ 118 bool pa_idxset_isdisjoint(pa_idxset *s, pa_idxset *t); 119 120 /* Return true if all entries in s are also in t */ 121 bool pa_idxset_issubset(pa_idxset *s, pa_idxset *t); 122 123 /* Return true if all entries in t are also in s */ 124 bool pa_idxset_issuperset(pa_idxset *s, pa_idxset *t); 125 126 /* Return true if s and t have all entries in common */ 127 bool pa_idxset_equals(pa_idxset *s, pa_idxset *t); 128 129 /* Duplicate the idxset. This will not copy the actual indexes. If copy_func is 130 * set, each entry is copied using the provided function, otherwise a shallow 131 * copy will be made. */ 132 pa_idxset *pa_idxset_copy(pa_idxset *s, pa_copy_func_t copy_func); 133 134 /* A macro to ease iteration through all entries */ 135 #define PA_IDXSET_FOREACH(e, s, idx) \ 136 for ((e) = pa_idxset_first((s), &(idx)); (e); (e) = pa_idxset_next((s), &(idx))) 137 138 #endif