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_VPSTR_H 27 #define __ARES__HTABLE_VPSTR_H 28 29 /*! \addtogroup ares_htable_vpstr HashTable with void pointer Key and string 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 void pointer 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_vpstr; 44 45 /*! Opaque data type for void pointer key, string value hash table 46 * implementation */ 47 typedef struct ares_htable_vpstr ares_htable_vpstr_t; 48 49 /*! Destroy hashtable 50 * 51 * \param[in] htable Initialized hashtable 52 */ 53 CARES_EXTERN void ares_htable_vpstr_destroy(ares_htable_vpstr_t *htable); 54 55 /*! Create void pointer key, string value hash table 56 * 57 */ 58 CARES_EXTERN ares_htable_vpstr_t *ares_htable_vpstr_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_vpstr_insert(ares_htable_vpstr_t *htable, 68 void *key, const char *val); 69 70 /*! Retrieve value from hashtable based on key 71 * 72 * \param[in] htable Initialized hash table 73 * \param[in] key key to use to search 74 * \param[out] val Optional. Pointer to store value. 75 * \return ARES_TRUE on success, ARES_FALSE on failure 76 */ 77 CARES_EXTERN ares_bool_t ares_htable_vpstr_get( 78 const ares_htable_vpstr_t *htable, const void *key, const char **val); 79 80 /*! Retrieve value from hashtable directly as return value. Caveat to this 81 * function over ares_htable_vpstr_get() is that if a NULL value is stored 82 * you cannot determine if the key is not found or the value is NULL. 83 * 84 * \param[in] htable Initialized hash table 85 * \param[in] key key to use to search 86 * \return value associated with key in hashtable or NULL 87 */ 88 CARES_EXTERN const char * 89 ares_htable_vpstr_get_direct(const ares_htable_vpstr_t *htable, 90 const void *key); 91 92 /*! Remove a value from the hashtable by key 93 * 94 * \param[in] htable Initialized hash table 95 * \param[in] key key to use to search 96 * \return ARES_TRUE if found, ARES_FALSE if not 97 */ 98 CARES_EXTERN ares_bool_t ares_htable_vpstr_remove(ares_htable_vpstr_t *htable, 99 const void *key); 100 101 /*! Retrieve the number of keys stored in the hash table 102 * 103 * \param[in] htable Initialized hash table 104 * \return count 105 */ 106 CARES_EXTERN size_t 107 ares_htable_vpstr_num_keys(const ares_htable_vpstr_t *htable); 108 109 /*! @} */ 110 111 #endif /* __ARES__HTABLE_VPSTR_H */ 112