1c99fb5f9SBaptiste Daroussin /* Copyright (c) 2013, Vsevolod Stakhov 2c99fb5f9SBaptiste Daroussin * All rights reserved. 3c99fb5f9SBaptiste Daroussin * 4c99fb5f9SBaptiste Daroussin * Redistribution and use in source and binary forms, with or without 5c99fb5f9SBaptiste Daroussin * modification, are permitted provided that the following conditions are met: 6c99fb5f9SBaptiste Daroussin * * Redistributions of source code must retain the above copyright 7c99fb5f9SBaptiste Daroussin * notice, this list of conditions and the following disclaimer. 8c99fb5f9SBaptiste Daroussin * * Redistributions in binary form must reproduce the above copyright 9c99fb5f9SBaptiste Daroussin * notice, this list of conditions and the following disclaimer in the 10c99fb5f9SBaptiste Daroussin * documentation and/or other materials provided with the distribution. 11c99fb5f9SBaptiste Daroussin * 12c99fb5f9SBaptiste Daroussin * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 13c99fb5f9SBaptiste Daroussin * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 14c99fb5f9SBaptiste Daroussin * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 15c99fb5f9SBaptiste Daroussin * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY 16c99fb5f9SBaptiste Daroussin * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 17c99fb5f9SBaptiste Daroussin * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 18c99fb5f9SBaptiste Daroussin * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 19c99fb5f9SBaptiste Daroussin * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 20c99fb5f9SBaptiste Daroussin * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 21c99fb5f9SBaptiste Daroussin * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22c99fb5f9SBaptiste Daroussin */ 23c99fb5f9SBaptiste Daroussin 24c99fb5f9SBaptiste Daroussin #ifndef __UCL_HASH_H 25c99fb5f9SBaptiste Daroussin #define __UCL_HASH_H 26c99fb5f9SBaptiste Daroussin 27c99fb5f9SBaptiste Daroussin #include "ucl.h" 28c99fb5f9SBaptiste Daroussin 29c99fb5f9SBaptiste Daroussin /******************************************************************************/ 30c99fb5f9SBaptiste Daroussin 318e3b1ab2SBaptiste Daroussin struct ucl_hash_node_s; 328e3b1ab2SBaptiste Daroussin typedef struct ucl_hash_node_s ucl_hash_node_t; 33c99fb5f9SBaptiste Daroussin 34d9f0ce31SBaptiste Daroussin typedef int (*ucl_hash_cmp_func) (const void* void_a, const void* void_b); 35d9f0ce31SBaptiste Daroussin typedef void (*ucl_hash_free_func) (void *ptr); 36c99fb5f9SBaptiste Daroussin typedef void* ucl_hash_iter_t; 37c99fb5f9SBaptiste Daroussin 38c99fb5f9SBaptiste Daroussin 39c99fb5f9SBaptiste Daroussin /** 40c99fb5f9SBaptiste Daroussin * Linear chained hashtable. 41c99fb5f9SBaptiste Daroussin */ 428e3b1ab2SBaptiste Daroussin struct ucl_hash_struct; 438e3b1ab2SBaptiste Daroussin typedef struct ucl_hash_struct ucl_hash_t; 44c99fb5f9SBaptiste Daroussin 45c99fb5f9SBaptiste Daroussin 46c99fb5f9SBaptiste Daroussin /** 47c99fb5f9SBaptiste Daroussin * Initializes the hashtable. 48c99fb5f9SBaptiste Daroussin */ 498e3b1ab2SBaptiste Daroussin ucl_hash_t* ucl_hash_create (bool ignore_case); 50c99fb5f9SBaptiste Daroussin 51c99fb5f9SBaptiste Daroussin /** 52c99fb5f9SBaptiste Daroussin * Deinitializes the hashtable. 53c99fb5f9SBaptiste Daroussin */ 54d9f0ce31SBaptiste Daroussin void ucl_hash_destroy (ucl_hash_t* hashlin, ucl_hash_free_func func); 55c99fb5f9SBaptiste Daroussin 56c99fb5f9SBaptiste Daroussin /** 57c99fb5f9SBaptiste Daroussin * Inserts an element in the the hashtable. 58*a0409676SBaptiste Daroussin * @return true on success, false on failure (i.e. ENOMEM) 59c99fb5f9SBaptiste Daroussin */ 60*a0409676SBaptiste Daroussin bool ucl_hash_insert (ucl_hash_t* hashlin, const ucl_object_t *obj, const char *key, 61b04a7a0bSBaptiste Daroussin unsigned keylen); 62c99fb5f9SBaptiste Daroussin 63c99fb5f9SBaptiste Daroussin /** 644bf54857SBaptiste Daroussin * Replace element in the hash 654bf54857SBaptiste Daroussin */ 664bf54857SBaptiste Daroussin void ucl_hash_replace (ucl_hash_t* hashlin, const ucl_object_t *old, 674bf54857SBaptiste Daroussin const ucl_object_t *new); 684bf54857SBaptiste Daroussin 694bf54857SBaptiste Daroussin /** 70c99fb5f9SBaptiste Daroussin * Delete an element from the the hashtable. 71c99fb5f9SBaptiste Daroussin */ 72b04a7a0bSBaptiste Daroussin void ucl_hash_delete (ucl_hash_t* hashlin, const ucl_object_t *obj); 73c99fb5f9SBaptiste Daroussin 74c99fb5f9SBaptiste Daroussin /** 75c99fb5f9SBaptiste Daroussin * Searches an element in the hashtable. 76c99fb5f9SBaptiste Daroussin */ 77b04a7a0bSBaptiste Daroussin const ucl_object_t* ucl_hash_search (ucl_hash_t* hashlin, const char *key, 78b04a7a0bSBaptiste Daroussin unsigned keylen); 79c99fb5f9SBaptiste Daroussin 80c99fb5f9SBaptiste Daroussin 81c99fb5f9SBaptiste Daroussin /** 82c99fb5f9SBaptiste Daroussin * Iterate over hash table 83c99fb5f9SBaptiste Daroussin * @param hashlin hash 84c99fb5f9SBaptiste Daroussin * @param iter iterator (must be NULL on first iteration) 85*a0409676SBaptiste Daroussin * @param ep pointer record exception (such as ENOMEM), could be NULL 86c99fb5f9SBaptiste Daroussin * @return the next object 87c99fb5f9SBaptiste Daroussin */ 88*a0409676SBaptiste Daroussin const void* ucl_hash_iterate2 (ucl_hash_t *hashlin, ucl_hash_iter_t *iter, int *ep); 89*a0409676SBaptiste Daroussin 90*a0409676SBaptiste Daroussin /** 91*a0409676SBaptiste Daroussin * Helper macro to support older code 92*a0409676SBaptiste Daroussin */ 93*a0409676SBaptiste Daroussin #define ucl_hash_iterate(hl, ip) ucl_hash_iterate2((hl), (ip), NULL) 94c99fb5f9SBaptiste Daroussin 95c99fb5f9SBaptiste Daroussin /** 96c99fb5f9SBaptiste Daroussin * Check whether an iterator has next element 97c99fb5f9SBaptiste Daroussin */ 988e3b1ab2SBaptiste Daroussin bool ucl_hash_iter_has_next (ucl_hash_t *hashlin, ucl_hash_iter_t iter); 99c99fb5f9SBaptiste Daroussin 100*a0409676SBaptiste Daroussin /** 101*a0409676SBaptiste Daroussin * Reserves space in hash 102*a0409676SBaptiste Daroussin * @return true on sucess, false on failure (e.g. ENOMEM) 103*a0409676SBaptiste Daroussin * @param hashlin 104*a0409676SBaptiste Daroussin */ 105*a0409676SBaptiste Daroussin bool ucl_hash_reserve (ucl_hash_t *hashlin, size_t sz); 106*a0409676SBaptiste Daroussin 107*a0409676SBaptiste Daroussin void ucl_hash_sort (ucl_hash_t *hashlin, enum ucl_object_keys_sort_flags fl); 108*a0409676SBaptiste Daroussin 109c99fb5f9SBaptiste Daroussin #endif 110