quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

ares_htable_szvp.h (4407B)


      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_STVP_H
     27 #define __ARES__HTABLE_STVP_H
     28 
     29 /*! \addtogroup ares_htable_szvp HashTable with size_t 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 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_szvp;
     44 
     45 /*! Opaque data type for size_t key, void pointer hash table implementation */
     46 typedef struct ares_htable_szvp ares_htable_szvp_t;
     47 
     48 /*! Callback to free value stored in hashtable
     49  *
     50  *  \param[in] val  user-supplied value
     51  */
     52 typedef void (*ares_htable_szvp_val_free_t)(void *val);
     53 
     54 /*! Destroy hashtable
     55  *
     56  *  \param[in] htable  Initialized hashtable
     57  */
     58 CARES_EXTERN void ares_htable_szvp_destroy(ares_htable_szvp_t *htable);
     59 
     60 /*! Create size_t key, 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_szvp_t *
     67   ares_htable_szvp_create(ares_htable_szvp_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_szvp_insert(ares_htable_szvp_t *htable,
     77                                                  size_t 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_szvp_get(const ares_htable_szvp_t *htable,
     87                                               size_t key, void **val);
     88 
     89 /*! Retrieve value from hashtable directly as return value.  Caveat to this
     90  *  function over ares_htable_szvp_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 *ares_htable_szvp_get_direct(const ares_htable_szvp_t *htable,
     98                                                size_t                    key);
     99 
    100 /*! Remove a value from the hashtable by key
    101  *
    102  *  \param[in] htable  Initialized hash table
    103  *  \param[in] key     key to use to search
    104  *  \return ARES_TRUE if found, ARES_FALSE if not
    105  */
    106 CARES_EXTERN ares_bool_t ares_htable_szvp_remove(ares_htable_szvp_t *htable,
    107                                                  size_t              key);
    108 
    109 /*! Retrieve the number of keys stored in the hash table
    110  *
    111  *  \param[in] htable  Initialized hash table
    112  *  \return count
    113  */
    114 CARES_EXTERN size_t ares_htable_szvp_num_keys(const ares_htable_szvp_t *htable);
    115 
    116 /*! @} */
    117 
    118 #endif /* __ARES__HTABLE_STVP_H */