1 /* MIT License 2 * 3 * Copyright (c) 2024 Brad House 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a copy 6 * of this software and associated documentation files (the "Software"), to deal 7 * in the Software without restriction, including without limitation the rights 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 * copies of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice (including the next 13 * paragraph) shall be included in all copies or substantial portions of the 14 * Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 * 24 * SPDX-License-Identifier: MIT 25 */ 26 #ifndef __ARES__HTABLE_VPVP_H 27 #define __ARES__HTABLE_VPVP_H 28 29 /*! \addtogroup ares_htable_vpvp HashTable with void pointer Key and void 30 * pointer Value 31 * 32 * This data structure wraps the base ares_htable data structure in order to 33 * split the key and value data types as size_t and void pointer, respectively. 34 * 35 * Average time complexity: 36 * - Insert: O(1) 37 * - Search: O(1) 38 * - Delete: O(1) 39 * 40 * @{ 41 */ 42 43 struct ares_htable_vpvp; 44 45 /*! Opaque data type for size_t key, void pointer hash table implementation */ 46 typedef struct ares_htable_vpvp ares_htable_vpvp_t; 47 48 /*! Callback to free key stored in hashtable 49 * 50 * \param[in] key user-supplied key 51 */ 52 typedef void (*ares_htable_vpvp_key_free_t)(void *key); 53 54 /*! Callback to free value stored in hashtable 55 * 56 * \param[in] val user-supplied value 57 */ 58 typedef void (*ares_htable_vpvp_val_free_t)(void *val); 59 60 /*! Destroy hashtable 61 * 62 * \param[in] htable Initialized hashtable 63 */ 64 CARES_EXTERN void ares_htable_vpvp_destroy(ares_htable_vpvp_t *htable); 65 66 /*! Create size_t key, void pointer value hash table 67 * 68 * \param[in] key_free Optional. Call back to free user-supplied key. If 69 * NULL it is expected the caller will clean up any user 70 * supplied keys. 71 * \param[in] val_free Optional. Call back to free user-supplied value. If 72 * NULL it is expected the caller will clean up any user 73 * supplied values. 74 */ 75 CARES_EXTERN ares_htable_vpvp_t * 76 ares_htable_vpvp_create(ares_htable_vpvp_key_free_t key_free, 77 ares_htable_vpvp_val_free_t val_free); 78 79 /*! Insert key/value into hash table 80 * 81 * \param[in] htable Initialized hash table 82 * \param[in] key key to associate with value 83 * \param[in] val value to store (takes ownership). May be NULL. 84 * \return ARES_TRUE on success, ARES_FALSE on failure or out of memory 85 */ 86 CARES_EXTERN ares_bool_t ares_htable_vpvp_insert(ares_htable_vpvp_t *htable, 87 void *key, void *val); 88 89 /*! Retrieve value from hashtable based on key 90 * 91 * \param[in] htable Initialized hash table 92 * \param[in] key key to use to search 93 * \param[out] val Optional. Pointer to store value. 94 * \return ARES_TRUE on success, ARES_FALSE on failure 95 */ 96 CARES_EXTERN ares_bool_t ares_htable_vpvp_get(const ares_htable_vpvp_t *htable, 97 const void *key, void **val); 98 99 /*! Retrieve value from hashtable directly as return value. Caveat to this 100 * function over ares_htable_vpvp_get() is that if a NULL value is stored 101 * you cannot determine if the key is not found or the value is NULL. 102 * 103 * \param[in] htable Initialized hash table 104 * \param[in] key key to use to search 105 * \return value associated with key in hashtable or NULL 106 */ 107 CARES_EXTERN void *ares_htable_vpvp_get_direct(const ares_htable_vpvp_t *htable, 108 const void *key); 109 110 /*! Remove a value from the hashtable by key 111 * 112 * \param[in] htable Initialized hash table 113 * \param[in] key key to use to search 114 * \return ARES_TRUE if found, ARES_FALSE if not 115 */ 116 CARES_EXTERN ares_bool_t ares_htable_vpvp_remove(ares_htable_vpvp_t *htable, 117 const void *key); 118 119 /*! Retrieve the number of keys stored in the hash table 120 * 121 * \param[in] htable Initialized hash table 122 * \return count 123 */ 124 CARES_EXTERN size_t ares_htable_vpvp_num_keys(const ares_htable_vpvp_t *htable); 125 126 /*! @} */ 127 128 #endif /* __ARES__HTABLE_VPVP_H */ 129