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_DICT_H 27 #define __ARES__HTABLE_DICT_H 28 29 /*! \addtogroup ares_htable_dict HashTable with case-insensitive string Key and 30 * string 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 string, 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_dict; 44 45 /*! Opaque data type for string key, string value hash table 46 * implementation */ 47 typedef struct ares_htable_dict ares_htable_dict_t; 48 49 /*! Destroy hashtable 50 * 51 * \param[in] htable Initialized hashtable 52 */ 53 CARES_EXTERN void ares_htable_dict_destroy(ares_htable_dict_t *htable); 54 55 /*! Create void pointer key, string value hash table 56 * 57 */ 58 CARES_EXTERN ares_htable_dict_t *ares_htable_dict_create(void); 59 60 /*! Insert key/value into hash table 61 * 62 * \param[in] htable Initialized hash table 63 * \param[in] key key to associate with value 64 * \param[in] val value to store (duplicates). 65 * \return ARES_TRUE on success, ARES_FALSE on failure or out of memory 66 */ 67 CARES_EXTERN ares_bool_t ares_htable_dict_insert(ares_htable_dict_t *htable, 68 const char *key, 69 const char *val); 70 71 /*! Retrieve value from hashtable based on key 72 * 73 * \param[in] htable Initialized hash table 74 * \param[in] key key to use to search 75 * \param[out] val Optional. Pointer to store value. 76 * \return ARES_TRUE on success, ARES_FALSE on failure 77 */ 78 CARES_EXTERN ares_bool_t ares_htable_dict_get(const ares_htable_dict_t *htable, 79 const char *key, 80 const char **val); 81 82 /*! Retrieve value from hashtable directly as return value. Caveat to this 83 * function over ares_htable_dict_get() is that if a NULL value is stored 84 * you cannot determine if the key is not found or the value is NULL. 85 * 86 * \param[in] htable Initialized hash table 87 * \param[in] key key to use to search 88 * \return value associated with key in hashtable or NULL 89 */ 90 CARES_EXTERN const char * 91 ares_htable_dict_get_direct(const ares_htable_dict_t *htable, 92 const char *key); 93 94 /*! Remove a value from the hashtable by key 95 * 96 * \param[in] htable Initialized hash table 97 * \param[in] key key to use to search 98 * \return ARES_TRUE if found, ARES_FALSE if not 99 */ 100 CARES_EXTERN ares_bool_t ares_htable_dict_remove(ares_htable_dict_t *htable, 101 const char *key); 102 103 /*! Retrieve the number of keys stored in the hash table 104 * 105 * \param[in] htable Initialized hash table 106 * \return count 107 */ 108 CARES_EXTERN size_t ares_htable_dict_num_keys(const ares_htable_dict_t *htable); 109 110 /*! Retrieve an array of keys from the hashtable. 111 * 112 * \param[in] htable Initialized hashtable 113 * \param[out] num Count of returned keys 114 * \return Array of keys in the hashtable. Must be free'd with 115 * ares_free_array(strs, num, ares_free); 116 */ 117 CARES_EXTERN char **ares_htable_dict_keys(const ares_htable_dict_t *htable, 118 size_t *num); 119 120 121 /*! @} */ 122 123 #endif /* __ARES__HTABLE_DICT_H */ 124