1 /* MIT License 2 * 3 * Copyright (c) 2023 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_STRVP_H 27 #define __ARES__HTABLE_STRVP_H 28 29 /*! \addtogroup ares_htable_strvp HashTable with string Key and void pointer 30 * 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 string 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_strvp; 44 45 /*! Opaque data type for size_t key, void pointer hash table implementation */ 46 typedef struct ares_htable_strvp ares_htable_strvp_t; 47 48 /*! Callback to free value stored in hashtable 49 * 50 * \param[in] val user-supplied value 51 */ 52 typedef void (*ares_htable_strvp_val_free_t)(void *val); 53 54 /*! Destroy hashtable 55 * 56 * \param[in] htable Initialized hashtable 57 */ 58 CARES_EXTERN void ares_htable_strvp_destroy(ares_htable_strvp_t *htable); 59 60 /*! Create string, void pointer value hash table 61 * 62 * \param[in] val_free Optional. Call back to free user-supplied value. If 63 * NULL it is expected the caller will clean up any user 64 * supplied values. 65 */ 66 CARES_EXTERN ares_htable_strvp_t * 67 ares_htable_strvp_create(ares_htable_strvp_val_free_t val_free); 68 69 /*! Insert key/value into hash table 70 * 71 * \param[in] htable Initialized hash table 72 * \param[in] key key to associate with value 73 * \param[in] val value to store (takes ownership). May be NULL. 74 * \return ARES_TRUE on success, ARES_FALSE on failure or out of memory 75 */ 76 CARES_EXTERN ares_bool_t ares_htable_strvp_insert(ares_htable_strvp_t *htable, 77 const char *key, void *val); 78 79 /*! Retrieve value from hashtable based on key 80 * 81 * \param[in] htable Initialized hash table 82 * \param[in] key key to use to search 83 * \param[out] val Optional. Pointer to store value. 84 * \return ARES_TRUE on success, ARES_FALSE on failure 85 */ 86 CARES_EXTERN ares_bool_t ares_htable_strvp_get( 87 const ares_htable_strvp_t *htable, const char *key, void **val); 88 89 /*! Retrieve value from hashtable directly as return value. Caveat to this 90 * function over ares_htable_strvp_get() is that if a NULL value is stored 91 * you cannot determine if the key is not found or the value is NULL. 92 * 93 * \param[in] htable Initialized hash table 94 * \param[in] key key to use to search 95 * \return value associated with key in hashtable or NULL 96 */ 97 CARES_EXTERN void * 98 ares_htable_strvp_get_direct(const ares_htable_strvp_t *htable, 99 const char *key); 100 101 /*! Remove a value from the hashtable by key 102 * 103 * \param[in] htable Initialized hash table 104 * \param[in] key key to use to search 105 * \return ARES_TRUE if found, ARES_FALSE if not 106 */ 107 CARES_EXTERN ares_bool_t ares_htable_strvp_remove(ares_htable_strvp_t *htable, 108 const char *key); 109 110 /*! Remove the value from the hashtable, and return the value instead of 111 * calling the val_free passed to ares_htable_strvp_create(). 112 * 113 * \param[in] htable Initialized hash table 114 * \param[in] key key to use to search 115 * \return value in hashtable or NULL on error 116 */ 117 CARES_EXTERN void *ares_htable_strvp_claim(ares_htable_strvp_t *htable, 118 const char *key); 119 120 /*! Retrieve the number of keys stored in the hash table 121 * 122 * \param[in] htable Initialized hash table 123 * \return count 124 */ 125 CARES_EXTERN size_t 126 ares_htable_strvp_num_keys(const ares_htable_strvp_t *htable); 127 128 /*! @} */ 129 130 #endif /* __ARES__HTABLE_STRVP_H */ 131