11cb0ef41Sopenharmony_ci/* MIT License 21cb0ef41Sopenharmony_ci * 31cb0ef41Sopenharmony_ci * Copyright (c) 2023 Brad House 41cb0ef41Sopenharmony_ci * 51cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 61cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to deal 71cb0ef41Sopenharmony_ci * in the Software without restriction, including without limitation the rights 81cb0ef41Sopenharmony_ci * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 91cb0ef41Sopenharmony_ci * copies of the Software, and to permit persons to whom the Software is 101cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions: 111cb0ef41Sopenharmony_ci * 121cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice (including the next 131cb0ef41Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 141cb0ef41Sopenharmony_ci * Software. 151cb0ef41Sopenharmony_ci * 161cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 171cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 181cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 191cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 201cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 211cb0ef41Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 221cb0ef41Sopenharmony_ci * SOFTWARE. 231cb0ef41Sopenharmony_ci * 241cb0ef41Sopenharmony_ci * SPDX-License-Identifier: MIT 251cb0ef41Sopenharmony_ci */ 261cb0ef41Sopenharmony_ci#ifndef __ARES__HTABLE_STRVP_H 271cb0ef41Sopenharmony_ci#define __ARES__HTABLE_STRVP_H 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci/*! \addtogroup ares__htable_strvp HashTable with string Key and void pointer 301cb0ef41Sopenharmony_ci * Value 311cb0ef41Sopenharmony_ci * 321cb0ef41Sopenharmony_ci * This data structure wraps the base ares__htable data structure in order to 331cb0ef41Sopenharmony_ci * split the key and value data types as string and void pointer, respectively. 341cb0ef41Sopenharmony_ci * 351cb0ef41Sopenharmony_ci * Average time complexity: 361cb0ef41Sopenharmony_ci * - Insert: O(1) 371cb0ef41Sopenharmony_ci * - Search: O(1) 381cb0ef41Sopenharmony_ci * - Delete: O(1) 391cb0ef41Sopenharmony_ci * 401cb0ef41Sopenharmony_ci * @{ 411cb0ef41Sopenharmony_ci */ 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_cistruct ares__htable_strvp; 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci/*! Opaque data type for size_t key, void pointer hash table implementation */ 461cb0ef41Sopenharmony_citypedef struct ares__htable_strvp ares__htable_strvp_t; 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci/*! Callback to free value stored in hashtable 491cb0ef41Sopenharmony_ci * 501cb0ef41Sopenharmony_ci * \param[in] val user-supplied value 511cb0ef41Sopenharmony_ci */ 521cb0ef41Sopenharmony_citypedef void (*ares__htable_strvp_val_free_t)(void *val); 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci/*! Destroy hashtable 551cb0ef41Sopenharmony_ci * 561cb0ef41Sopenharmony_ci * \param[in] htable Initialized hashtable 571cb0ef41Sopenharmony_ci */ 581cb0ef41Sopenharmony_civoid ares__htable_strvp_destroy(ares__htable_strvp_t *htable); 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci/*! Create string, void pointer value hash table 611cb0ef41Sopenharmony_ci * 621cb0ef41Sopenharmony_ci * \param[in] val_free Optional. Call back to free user-supplied value. If 631cb0ef41Sopenharmony_ci * NULL it is expected the caller will clean up any user 641cb0ef41Sopenharmony_ci * supplied values. 651cb0ef41Sopenharmony_ci */ 661cb0ef41Sopenharmony_ciares__htable_strvp_t * 671cb0ef41Sopenharmony_ci ares__htable_strvp_create(ares__htable_strvp_val_free_t val_free); 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci/*! Insert key/value into hash table 701cb0ef41Sopenharmony_ci * 711cb0ef41Sopenharmony_ci * \param[in] htable Initialized hash table 721cb0ef41Sopenharmony_ci * \param[in] key key to associate with value 731cb0ef41Sopenharmony_ci * \param[in] val value to store (takes ownership). May be NULL. 741cb0ef41Sopenharmony_ci * \return ARES_TRUE on success, ARES_FALSE on failure or out of memory 751cb0ef41Sopenharmony_ci */ 761cb0ef41Sopenharmony_ciares_bool_t ares__htable_strvp_insert(ares__htable_strvp_t *htable, 771cb0ef41Sopenharmony_ci const char *key, void *val); 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci/*! Retrieve value from hashtable based on key 801cb0ef41Sopenharmony_ci * 811cb0ef41Sopenharmony_ci * \param[in] htable Initialized hash table 821cb0ef41Sopenharmony_ci * \param[in] key key to use to search 831cb0ef41Sopenharmony_ci * \param[out] val Optional. Pointer to store value. 841cb0ef41Sopenharmony_ci * \return ARES_TRUE on success, ARES_FALSE on failure 851cb0ef41Sopenharmony_ci */ 861cb0ef41Sopenharmony_ciares_bool_t ares__htable_strvp_get(const ares__htable_strvp_t *htable, 871cb0ef41Sopenharmony_ci const char *key, void **val); 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci/*! Retrieve value from hashtable directly as return value. Caveat to this 901cb0ef41Sopenharmony_ci * function over ares__htable_strvp_get() is that if a NULL value is stored 911cb0ef41Sopenharmony_ci * you cannot determine if the key is not found or the value is NULL. 921cb0ef41Sopenharmony_ci * 931cb0ef41Sopenharmony_ci * \param[in] htable Initialized hash table 941cb0ef41Sopenharmony_ci * \param[in] key key to use to search 951cb0ef41Sopenharmony_ci * \return value associated with key in hashtable or NULL 961cb0ef41Sopenharmony_ci */ 971cb0ef41Sopenharmony_civoid *ares__htable_strvp_get_direct(const ares__htable_strvp_t *htable, 981cb0ef41Sopenharmony_ci const char *key); 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci/*! Remove a value from the hashtable by key 1011cb0ef41Sopenharmony_ci * 1021cb0ef41Sopenharmony_ci * \param[in] htable Initialized hash table 1031cb0ef41Sopenharmony_ci * \param[in] key key to use to search 1041cb0ef41Sopenharmony_ci * \return ARES_TRUE if found, ARES_FALSE if not 1051cb0ef41Sopenharmony_ci */ 1061cb0ef41Sopenharmony_ciares_bool_t ares__htable_strvp_remove(ares__htable_strvp_t *htable, 1071cb0ef41Sopenharmony_ci const char *key); 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ci/*! Retrieve the number of keys stored in the hash table 1101cb0ef41Sopenharmony_ci * 1111cb0ef41Sopenharmony_ci * \param[in] htable Initialized hash table 1121cb0ef41Sopenharmony_ci * \return count 1131cb0ef41Sopenharmony_ci */ 1141cb0ef41Sopenharmony_cisize_t ares__htable_strvp_num_keys(const ares__htable_strvp_t *htable); 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci/*! @} */ 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ci#endif /* __ARES__HTABLE_STVP_H */ 119