quickjs-tart

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

ares_htable_asvp.h (4817B)


      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_ASVP_H
     27 #define __ARES__HTABLE_ASVP_H
     28 
     29 /*! \addtogroup ares_htable_asvp HashTable with ares_socket_t Key and
     30  *                                void 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 ares_socket_t and void pointer,
     34  * respectively.
     35  *
     36  * Average time complexity:
     37  *  - Insert: O(1)
     38  *  - Search: O(1)
     39  *  - Delete: O(1)
     40  *
     41  * @{
     42  */
     43 
     44 struct ares_htable_asvp;
     45 
     46 /*! Opaque data type for ares_socket_t key, void pointer hash table
     47  *  implementation */
     48 typedef struct ares_htable_asvp ares_htable_asvp_t;
     49 
     50 /*! Callback to free value stored in hashtable
     51  *
     52  *  \param[in] val  user-supplied value
     53  */
     54 typedef void (*ares_htable_asvp_val_free_t)(void *val);
     55 
     56 /*! Destroy hashtable
     57  *
     58  *  \param[in] htable  Initialized hashtable
     59  */
     60 CARES_EXTERN void ares_htable_asvp_destroy(ares_htable_asvp_t *htable);
     61 
     62 /*! Create size_t key, void pointer value hash table
     63  *
     64  *  \param[in] val_free  Optional. Call back to free user-supplied value.  If
     65  *                       NULL it is expected the caller will clean up any user
     66  *                       supplied values.
     67  */
     68 CARES_EXTERN ares_htable_asvp_t *
     69   ares_htable_asvp_create(ares_htable_asvp_val_free_t val_free);
     70 
     71 /*! Retrieve an array of keys from the hashtable.
     72  *
     73  *  \param[in]  htable   Initialized hashtable
     74  *  \param[out] num      Count of returned keys
     75  *  \return Array of keys in the hashtable. Must be free'd with ares_free().
     76  */
     77 CARES_EXTERN ares_socket_t *
     78   ares_htable_asvp_keys(const ares_htable_asvp_t *htable, size_t *num);
     79 
     80 
     81 /*! Insert key/value into hash table
     82  *
     83  *  \param[in] htable Initialized hash table
     84  *  \param[in] key    key to associate with value
     85  *  \param[in] val    value to store (takes ownership). May be NULL.
     86  *  \return ARES_TRUE on success, ARES_FALSE on out of memory or misuse
     87  */
     88 CARES_EXTERN ares_bool_t ares_htable_asvp_insert(ares_htable_asvp_t *htable,
     89                                                  ares_socket_t key, void *val);
     90 
     91 /*! Retrieve value from hashtable based on key
     92  *
     93  *  \param[in]  htable  Initialized hash table
     94  *  \param[in]  key     key to use to search
     95  *  \param[out] val     Optional.  Pointer to store value.
     96  *  \return ARES_TRUE on success, ARES_FALSE on failure
     97  */
     98 CARES_EXTERN ares_bool_t ares_htable_asvp_get(const ares_htable_asvp_t *htable,
     99                                               ares_socket_t key, void **val);
    100 
    101 /*! Retrieve value from hashtable directly as return value.  Caveat to this
    102  *  function over ares_htable_asvp_get() is that if a NULL value is stored
    103  *  you cannot determine if the key is not found or the value is NULL.
    104  *
    105  *  \param[in] htable  Initialized hash table
    106  *  \param[in] key     key to use to search
    107  *  \return value associated with key in hashtable or NULL
    108  */
    109 CARES_EXTERN void *ares_htable_asvp_get_direct(const ares_htable_asvp_t *htable,
    110                                                ares_socket_t             key);
    111 
    112 /*! Remove a value from the hashtable by key
    113  *
    114  *  \param[in] htable  Initialized hash table
    115  *  \param[in] key     key to use to search
    116  *  \return ARES_TRUE if found, ARES_FALSE if not found
    117  */
    118 CARES_EXTERN ares_bool_t ares_htable_asvp_remove(ares_htable_asvp_t *htable,
    119                                                  ares_socket_t       key);
    120 
    121 /*! Retrieve the number of keys stored in the hash table
    122  *
    123  *  \param[in] htable  Initialized hash table
    124  *  \return count
    125  */
    126 CARES_EXTERN size_t ares_htable_asvp_num_keys(const ares_htable_asvp_t *htable);
    127 
    128 /*! @} */
    129 
    130 #endif /* __ARES__HTABLE_ASVP_H */