153a5a1b3Sopenharmony_ci#ifndef foopulsecorehashmaphfoo 253a5a1b3Sopenharmony_ci#define foopulsecorehashmaphfoo 353a5a1b3Sopenharmony_ci 453a5a1b3Sopenharmony_ci/*** 553a5a1b3Sopenharmony_ci This file is part of PulseAudio. 653a5a1b3Sopenharmony_ci 753a5a1b3Sopenharmony_ci Copyright 2004-2008 Lennart Poettering 853a5a1b3Sopenharmony_ci 953a5a1b3Sopenharmony_ci PulseAudio is free software; you can redistribute it and/or modify 1053a5a1b3Sopenharmony_ci it under the terms of the GNU Lesser General Public License as published 1153a5a1b3Sopenharmony_ci by the Free Software Foundation; either version 2.1 of the License, 1253a5a1b3Sopenharmony_ci or (at your option) any later version. 1353a5a1b3Sopenharmony_ci 1453a5a1b3Sopenharmony_ci PulseAudio is distributed in the hope that it will be useful, but 1553a5a1b3Sopenharmony_ci WITHOUT ANY WARRANTY; without even the implied warranty of 1653a5a1b3Sopenharmony_ci MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1753a5a1b3Sopenharmony_ci General Public License for more details. 1853a5a1b3Sopenharmony_ci 1953a5a1b3Sopenharmony_ci You should have received a copy of the GNU Lesser General Public License 2053a5a1b3Sopenharmony_ci along with PulseAudio; if not, see <http://www.gnu.org/licenses/>. 2153a5a1b3Sopenharmony_ci***/ 2253a5a1b3Sopenharmony_ci 2353a5a1b3Sopenharmony_ci#include <pulse/def.h> 2453a5a1b3Sopenharmony_ci 2553a5a1b3Sopenharmony_ci#include <pulsecore/idxset.h> 2653a5a1b3Sopenharmony_ci 2753a5a1b3Sopenharmony_ci/* Simple Implementation of a hash table. Memory management is the 2853a5a1b3Sopenharmony_ci * user's job. It's a good idea to have the key pointer point to a 2953a5a1b3Sopenharmony_ci * string in the value data. The insertion order is preserved when 3053a5a1b3Sopenharmony_ci * iterating. */ 3153a5a1b3Sopenharmony_ci 3253a5a1b3Sopenharmony_citypedef struct pa_hashmap pa_hashmap; 3353a5a1b3Sopenharmony_ci 3453a5a1b3Sopenharmony_ci/* Create a new hashmap. Use the specified functions for hashing and comparing objects in the map */ 3553a5a1b3Sopenharmony_cipa_hashmap *pa_hashmap_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func); 3653a5a1b3Sopenharmony_ci 3753a5a1b3Sopenharmony_ci/* Create a new hashmap. Use the specified functions for hashing and comparing objects in the map, and functions to free the key 3853a5a1b3Sopenharmony_ci * and value (either or both can be NULL). */ 3953a5a1b3Sopenharmony_cipa_hashmap *pa_hashmap_new_full(pa_hash_func_t hash_func, pa_compare_func_t compare_func, pa_free_cb_t key_free_func, pa_free_cb_t value_free_func); 4053a5a1b3Sopenharmony_ci 4153a5a1b3Sopenharmony_ci/* Free the hash table. */ 4253a5a1b3Sopenharmony_civoid pa_hashmap_free(pa_hashmap*); 4353a5a1b3Sopenharmony_ci 4453a5a1b3Sopenharmony_ci/* Add an entry to the hashmap. Returns non-zero when the entry already exists */ 4553a5a1b3Sopenharmony_ciint pa_hashmap_put(pa_hashmap *h, void *key, void *value); 4653a5a1b3Sopenharmony_ci 4753a5a1b3Sopenharmony_ci/* Return an entry from the hashmap */ 4853a5a1b3Sopenharmony_civoid* pa_hashmap_get(const pa_hashmap *h, const void *key); 4953a5a1b3Sopenharmony_ci 5053a5a1b3Sopenharmony_ci/* Returns the data of the entry while removing */ 5153a5a1b3Sopenharmony_civoid* pa_hashmap_remove(pa_hashmap *h, const void *key); 5253a5a1b3Sopenharmony_ci 5353a5a1b3Sopenharmony_ci/* Removes the entry and frees the entry data. Returns a negative value if the 5453a5a1b3Sopenharmony_ci * entry is not found. FIXME: This function shouldn't be needed. 5553a5a1b3Sopenharmony_ci * pa_hashmap_remove() should free the entry data, and the current semantics of 5653a5a1b3Sopenharmony_ci * pa_hashmap_remove() should be implemented by a function called 5753a5a1b3Sopenharmony_ci * pa_hashmap_steal(). */ 5853a5a1b3Sopenharmony_ciint pa_hashmap_remove_and_free(pa_hashmap *h, const void *key); 5953a5a1b3Sopenharmony_ci 6053a5a1b3Sopenharmony_ci/* Remove all entries but don't free the hashmap */ 6153a5a1b3Sopenharmony_civoid pa_hashmap_remove_all(pa_hashmap *h); 6253a5a1b3Sopenharmony_ci 6353a5a1b3Sopenharmony_ci/* Return the current number of entries of the hashmap */ 6453a5a1b3Sopenharmony_ciunsigned pa_hashmap_size(const pa_hashmap *h); 6553a5a1b3Sopenharmony_ci 6653a5a1b3Sopenharmony_ci/* Return true if the hashmap is empty */ 6753a5a1b3Sopenharmony_cibool pa_hashmap_isempty(const pa_hashmap *h); 6853a5a1b3Sopenharmony_ci 6953a5a1b3Sopenharmony_ci/* May be used to iterate through the hashmap. Initially the opaque 7053a5a1b3Sopenharmony_ci pointer *state has to be set to NULL. The hashmap may not be 7153a5a1b3Sopenharmony_ci modified during iteration -- except for deleting the current entry 7253a5a1b3Sopenharmony_ci via pa_hashmap_remove(). The key of the entry is returned in *key, 7353a5a1b3Sopenharmony_ci if key is non-NULL. After the last entry in the hashmap NULL is 7453a5a1b3Sopenharmony_ci returned. */ 7553a5a1b3Sopenharmony_civoid *pa_hashmap_iterate(const pa_hashmap *h, void **state, const void**key); 7653a5a1b3Sopenharmony_ci 7753a5a1b3Sopenharmony_ci/* Same as pa_hashmap_iterate() but goes backwards */ 7853a5a1b3Sopenharmony_civoid *pa_hashmap_iterate_backwards(const pa_hashmap *h, void **state, const void**key); 7953a5a1b3Sopenharmony_ci 8053a5a1b3Sopenharmony_ci/* Remove the oldest entry in the hashmap and return it */ 8153a5a1b3Sopenharmony_civoid *pa_hashmap_steal_first(pa_hashmap *h); 8253a5a1b3Sopenharmony_ci 8353a5a1b3Sopenharmony_ci/* Return the oldest entry in the hashmap */ 8453a5a1b3Sopenharmony_civoid* pa_hashmap_first(const pa_hashmap *h); 8553a5a1b3Sopenharmony_ci 8653a5a1b3Sopenharmony_ci/* Return the newest entry in the hashmap */ 8753a5a1b3Sopenharmony_civoid* pa_hashmap_last(const pa_hashmap *h); 8853a5a1b3Sopenharmony_ci 8953a5a1b3Sopenharmony_ci/* A macro to ease iteration through all entries */ 9053a5a1b3Sopenharmony_ci#define PA_HASHMAP_FOREACH(e, h, state) \ 9153a5a1b3Sopenharmony_ci for ((state) = NULL, (e) = pa_hashmap_iterate((h), &(state), NULL); (e); (e) = pa_hashmap_iterate((h), &(state), NULL)) 9253a5a1b3Sopenharmony_ci 9353a5a1b3Sopenharmony_ci/* A macro to ease itration through all key, value pairs */ 9453a5a1b3Sopenharmony_ci#define PA_HASHMAP_FOREACH_KV(k, e, h, state) \ 9553a5a1b3Sopenharmony_ci for ((state) = NULL, (e) = pa_hashmap_iterate((h), &(state), (const void **) &(k)); (e); (e) = pa_hashmap_iterate((h), &(state), (const void **) &(k))) 9653a5a1b3Sopenharmony_ci 9753a5a1b3Sopenharmony_ci/* A macro to ease iteration through all entries, backwards */ 9853a5a1b3Sopenharmony_ci#define PA_HASHMAP_FOREACH_BACKWARDS(e, h, state) \ 9953a5a1b3Sopenharmony_ci for ((state) = NULL, (e) = pa_hashmap_iterate_backwards((h), &(state), NULL); (e); (e) = pa_hashmap_iterate_backwards((h), &(state), NULL)) 10053a5a1b3Sopenharmony_ci 10153a5a1b3Sopenharmony_ci#endif 102