1262395Sbapt/* Copyright (c) 2013, Vsevolod Stakhov 2262395Sbapt * All rights reserved. 3262395Sbapt * 4262395Sbapt * Redistribution and use in source and binary forms, with or without 5262395Sbapt * modification, are permitted provided that the following conditions are met: 6262395Sbapt * * Redistributions of source code must retain the above copyright 7262395Sbapt * notice, this list of conditions and the following disclaimer. 8262395Sbapt * * Redistributions in binary form must reproduce the above copyright 9262395Sbapt * notice, this list of conditions and the following disclaimer in the 10262395Sbapt * documentation and/or other materials provided with the distribution. 11262395Sbapt * 12262395Sbapt * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 13262395Sbapt * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 14262395Sbapt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 15262395Sbapt * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY 16262395Sbapt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 17262395Sbapt * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 18262395Sbapt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 19262395Sbapt * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 20262395Sbapt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 21262395Sbapt * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22262395Sbapt */ 23262395Sbapt 24262395Sbapt#ifndef __UCL_HASH_H 25262395Sbapt#define __UCL_HASH_H 26262395Sbapt 27262395Sbapt#include "ucl.h" 28262395Sbapt#include "uthash.h" 29262395Sbapt 30262395Sbapt/******************************************************************************/ 31262395Sbapt 32262395Sbapttypedef struct ucl_hash_node_s 33262395Sbapt{ 34268896Sbapt const ucl_object_t *data; 35262395Sbapt UT_hash_handle hh; 36262395Sbapt} ucl_hash_node_t; 37262395Sbapt 38262395Sbapttypedef int ucl_hash_cmp_func (const void* void_a, const void* void_b); 39262395Sbapttypedef void ucl_hash_free_func (void *ptr); 40262395Sbapttypedef void* ucl_hash_iter_t; 41262395Sbapt 42262395Sbapt 43262395Sbapt/** 44262395Sbapt * Linear chained hashtable. 45262395Sbapt */ 46262395Sbapttypedef struct ucl_hash_struct 47262395Sbapt{ 48262395Sbapt ucl_hash_node_t *buckets; /**< array of hash buckets. One list for each hash modulus. */ 49262395Sbapt} ucl_hash_t; 50262395Sbapt 51262395Sbapt 52262395Sbapt/** 53262395Sbapt * Initializes the hashtable. 54262395Sbapt */ 55262395Sbaptucl_hash_t* ucl_hash_create (void); 56262395Sbapt 57262395Sbapt/** 58262395Sbapt * Deinitializes the hashtable. 59262395Sbapt */ 60262395Sbaptvoid ucl_hash_destroy (ucl_hash_t* hashlin, ucl_hash_free_func *func); 61262395Sbapt 62262395Sbapt/** 63262395Sbapt * Inserts an element in the the hashtable. 64262395Sbapt */ 65268896Sbaptvoid ucl_hash_insert (ucl_hash_t* hashlin, const ucl_object_t *obj, const char *key, 66268896Sbapt unsigned keylen); 67262395Sbapt 68262395Sbapt/** 69262395Sbapt * Delete an element from the the hashtable. 70262395Sbapt */ 71268896Sbaptvoid ucl_hash_delete (ucl_hash_t* hashlin, const ucl_object_t *obj); 72262395Sbapt 73262395Sbapt/** 74262395Sbapt * Searches an element in the hashtable. 75262395Sbapt */ 76268896Sbaptconst ucl_object_t* ucl_hash_search (ucl_hash_t* hashlin, const char *key, 77268896Sbapt unsigned keylen); 78262395Sbapt 79262395Sbapt 80262395Sbapt/** 81262395Sbapt * Iterate over hash table 82262395Sbapt * @param hashlin hash 83262395Sbapt * @param iter iterator (must be NULL on first iteration) 84262395Sbapt * @return the next object 85262395Sbapt */ 86268896Sbaptconst void* ucl_hash_iterate (ucl_hash_t *hashlin, ucl_hash_iter_t *iter); 87262395Sbapt 88262395Sbapt/** 89262395Sbapt * Check whether an iterator has next element 90262395Sbapt */ 91262395Sbaptbool ucl_hash_iter_has_next (ucl_hash_iter_t iter); 92262395Sbapt 93262395Sbapt#endif 94