153a5a1b3Sopenharmony_ci#ifndef foopulsecoreidxsethfoo
253a5a1b3Sopenharmony_ci#define foopulsecoreidxsethfoo
353a5a1b3Sopenharmony_ci
453a5a1b3Sopenharmony_ci/***
553a5a1b3Sopenharmony_ci  This file is part of PulseAudio.
653a5a1b3Sopenharmony_ci
753a5a1b3Sopenharmony_ci  Copyright 2004-2008 Lennart Poettering
853a5a1b3Sopenharmony_ci  Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
953a5a1b3Sopenharmony_ci
1053a5a1b3Sopenharmony_ci  PulseAudio is free software; you can redistribute it and/or modify
1153a5a1b3Sopenharmony_ci  it under the terms of the GNU Lesser General Public License as
1253a5a1b3Sopenharmony_ci  published by the Free Software Foundation; either version 2.1 of the
1353a5a1b3Sopenharmony_ci  License, or (at your option) any later version.
1453a5a1b3Sopenharmony_ci
1553a5a1b3Sopenharmony_ci  PulseAudio is distributed in the hope that it will be useful, but
1653a5a1b3Sopenharmony_ci  WITHOUT ANY WARRANTY; without even the implied warranty of
1753a5a1b3Sopenharmony_ci  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1853a5a1b3Sopenharmony_ci  Lesser General Public License for more details.
1953a5a1b3Sopenharmony_ci
2053a5a1b3Sopenharmony_ci  You should have received a copy of the GNU Lesser General Public
2153a5a1b3Sopenharmony_ci  License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
2253a5a1b3Sopenharmony_ci***/
2353a5a1b3Sopenharmony_ci
2453a5a1b3Sopenharmony_ci#include <inttypes.h>
2553a5a1b3Sopenharmony_ci
2653a5a1b3Sopenharmony_ci#include <pulse/def.h>
2753a5a1b3Sopenharmony_ci
2853a5a1b3Sopenharmony_ci#include <pulsecore/macro.h>
2953a5a1b3Sopenharmony_ci
3053a5a1b3Sopenharmony_ci/* A combination of a set and a dynamic array. Entries are indexable
3153a5a1b3Sopenharmony_ci * both through an automatically generated numeric index and the
3253a5a1b3Sopenharmony_ci * entry's data pointer. As usual, memory management is the user's
3353a5a1b3Sopenharmony_ci * job. */
3453a5a1b3Sopenharmony_ci
3553a5a1b3Sopenharmony_ci/* A special index value denoting the invalid index. */
3653a5a1b3Sopenharmony_ci#define PA_IDXSET_INVALID ((uint32_t) -1)
3753a5a1b3Sopenharmony_ci
3853a5a1b3Sopenharmony_ci/* Generic implementations for hash and comparison functions. Just
3953a5a1b3Sopenharmony_ci * compares the pointer or calculates the hash value directly from the
4053a5a1b3Sopenharmony_ci * pointer value. */
4153a5a1b3Sopenharmony_ciunsigned pa_idxset_trivial_hash_func(const void *p);
4253a5a1b3Sopenharmony_ciint pa_idxset_trivial_compare_func(const void *a, const void *b);
4353a5a1b3Sopenharmony_ci
4453a5a1b3Sopenharmony_ci/* Generic implementations for hash and comparison functions for strings. */
4553a5a1b3Sopenharmony_ciunsigned pa_idxset_string_hash_func(const void *p);
4653a5a1b3Sopenharmony_ciint pa_idxset_string_compare_func(const void *a, const void *b);
4753a5a1b3Sopenharmony_ci
4853a5a1b3Sopenharmony_citypedef unsigned (*pa_hash_func_t)(const void *p);
4953a5a1b3Sopenharmony_citypedef int (*pa_compare_func_t)(const void *a, const void *b);
5053a5a1b3Sopenharmony_citypedef void *(*pa_copy_func_t)(const void *p);
5153a5a1b3Sopenharmony_ci
5253a5a1b3Sopenharmony_citypedef struct pa_idxset pa_idxset;
5353a5a1b3Sopenharmony_ci
5453a5a1b3Sopenharmony_ci/* Instantiate a new idxset with the specified hash and comparison functions */
5553a5a1b3Sopenharmony_cipa_idxset* pa_idxset_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func);
5653a5a1b3Sopenharmony_ci
5753a5a1b3Sopenharmony_ci/* Free the idxset. When the idxset is not empty the specified function is called for every entry contained */
5853a5a1b3Sopenharmony_civoid pa_idxset_free(pa_idxset *s, pa_free_cb_t free_cb);
5953a5a1b3Sopenharmony_ci
6053a5a1b3Sopenharmony_ci/* Store a new item in the idxset. The index of the item is returned in *idx */
6153a5a1b3Sopenharmony_ciint pa_idxset_put(pa_idxset*s, void *p, uint32_t *idx);
6253a5a1b3Sopenharmony_ci
6353a5a1b3Sopenharmony_ci/* Get the entry by its idx */
6453a5a1b3Sopenharmony_civoid* pa_idxset_get_by_index(pa_idxset*s, uint32_t idx);
6553a5a1b3Sopenharmony_ci
6653a5a1b3Sopenharmony_ci/* Get the entry by its data. The index is returned in *idx */
6753a5a1b3Sopenharmony_civoid* pa_idxset_get_by_data(pa_idxset*s, const void *p, uint32_t *idx);
6853a5a1b3Sopenharmony_ci
6953a5a1b3Sopenharmony_ci/* Similar to pa_idxset_get_by_index(), but removes the entry from the idxset. */
7053a5a1b3Sopenharmony_civoid* pa_idxset_remove_by_index(pa_idxset*s, uint32_t idx);
7153a5a1b3Sopenharmony_ci
7253a5a1b3Sopenharmony_ci/* Similar to pa_idxset_get_by_data(), but removes the entry from the idxset */
7353a5a1b3Sopenharmony_civoid* pa_idxset_remove_by_data(pa_idxset*s, const void *p, uint32_t *idx);
7453a5a1b3Sopenharmony_ci
7553a5a1b3Sopenharmony_ci/* If free_cb is not NULL, it's called for each entry. */
7653a5a1b3Sopenharmony_civoid pa_idxset_remove_all(pa_idxset *s, pa_free_cb_t free_cb);
7753a5a1b3Sopenharmony_ci
7853a5a1b3Sopenharmony_ci/* This may be used to iterate through all entries. When called with
7953a5a1b3Sopenharmony_ci   an invalid index value it returns the first entry, otherwise the
8053a5a1b3Sopenharmony_ci   next following. The function is best called with *idx =
8153a5a1b3Sopenharmony_ci   PA_IDXSET_VALID first. It is safe to manipulate the idxset between
8253a5a1b3Sopenharmony_ci   the calls. It is not guaranteed that all entries have already been
8353a5a1b3Sopenharmony_ci   returned before the an entry is returned the second time.*/
8453a5a1b3Sopenharmony_civoid* pa_idxset_rrobin(pa_idxset *s, uint32_t *idx);
8553a5a1b3Sopenharmony_ci
8653a5a1b3Sopenharmony_ci/* Iterate through the idxset. At first iteration state should be NULL */
8753a5a1b3Sopenharmony_civoid *pa_idxset_iterate(pa_idxset *s, void **state, uint32_t *idx);
8853a5a1b3Sopenharmony_ci
8953a5a1b3Sopenharmony_ci/* Return the oldest entry in the idxset and remove it. If idx is not NULL fill in its index in *idx */
9053a5a1b3Sopenharmony_civoid* pa_idxset_steal_first(pa_idxset *s, uint32_t *idx);
9153a5a1b3Sopenharmony_ci
9253a5a1b3Sopenharmony_ci/* Return the oldest entry in the idxset. Fill in its index in *idx. */
9353a5a1b3Sopenharmony_civoid* pa_idxset_first(pa_idxset *s, uint32_t *idx);
9453a5a1b3Sopenharmony_ci
9553a5a1b3Sopenharmony_ci/* Return the entry following the entry indexed by *idx.  After the
9653a5a1b3Sopenharmony_ci * call *index contains the index of the returned
9753a5a1b3Sopenharmony_ci * object. pa_idxset_first() and pa_idxset_next() may be used to
9853a5a1b3Sopenharmony_ci * iterate through the set.*/
9953a5a1b3Sopenharmony_civoid *pa_idxset_next(pa_idxset *s, uint32_t *idx);
10053a5a1b3Sopenharmony_ci
10153a5a1b3Sopenharmony_ci/* Return the current number of entries in the idxset */
10253a5a1b3Sopenharmony_ciunsigned pa_idxset_size(pa_idxset*s);
10353a5a1b3Sopenharmony_ci
10453a5a1b3Sopenharmony_ci/* Return true of the idxset is empty */
10553a5a1b3Sopenharmony_cibool pa_idxset_isempty(pa_idxset *s);
10653a5a1b3Sopenharmony_ci
10753a5a1b3Sopenharmony_ci/* Duplicate the idxset. This will not copy the actual indexes. If copy_func is
10853a5a1b3Sopenharmony_ci * set, each entry is copied using the provided function, otherwise a shallow
10953a5a1b3Sopenharmony_ci * copy will be made. */
11053a5a1b3Sopenharmony_cipa_idxset *pa_idxset_copy(pa_idxset *s, pa_copy_func_t copy_func);
11153a5a1b3Sopenharmony_ci
11253a5a1b3Sopenharmony_ci/* A macro to ease iteration through all entries */
11353a5a1b3Sopenharmony_ci#define PA_IDXSET_FOREACH(e, s, idx) \
11453a5a1b3Sopenharmony_ci    for ((e) = pa_idxset_first((s), &(idx)); (e); (e) = pa_idxset_next((s), &(idx)))
11553a5a1b3Sopenharmony_ci
11653a5a1b3Sopenharmony_ci#endif
117