/***************************************************************************** * Copyright (c) 2005 Daniel Lerch Hostalot * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. ****************************************************************************/ #ifndef __HASH_H__ #define __HASH_H__ #ifdef __cplusplus extern "C" { #endif #include "list.h" #include /** * Par clave-valor */ typedef struct hash_pair_t { void* key; void* value; } hash_pair_t; /** * Tipo de representacion de una tabla de hash. */ typedef struct hash_t { size_t size; size_t maxsize; int (*hashfunc)(void*, size_t); int (*cmpfunc)(void*, void*); list_t** table; } hash_t; /** * Crea un tipo hash. * * @return hash_t*: tipo tabla de hash. */ void* hash_new (unsigned int maxsize); /** * Libera la memoria utilizada por la tabla de hash. * Si no se proporciona una funcion para liberar los elementos insertador, * (NULL como argumento) no se liberaran. * * @param *obj: objeto con el que trabaja. * @param freefunc_key: funcion para liberar memoria reservada por la clave. * @param freefunc_value: funcion para liberar memoria reservada por el valor.. */ void hash_free (hash_t* obj, void (*freefunc_key)(void*), void (*freefunc_value)(void*)); /** * Modifica la funcion de hash. * Por defecto dispone de una funcion de hash optimizada para cadenas. * * @param *obj: objeto con el que trabaja. * @param hashfunc: funcion de hash a utilizar. */ void hash_set_hashfunc (hash_t* obj, int (*hashfunc)(void*, size_t)); /** * AƱade una relacion clave-valor a la tabla. * * @param *obj: objeto con el que trabaja. * @param *key: clave de acceso al elemento. * @param *value: elemento. */ void hash_add (hash_t* obj, void* key, void *value); /** * Obtiene un elemento de la tabla a partir de su clave. * * @param *obj: objeto con el que trabaja. * @param *key: clave de acceso al elemento. */ void* hash_get (hash_t* obj, void *key); /** * Borra un elemento de la tabla a partir de su clave. * * @param *obj: objeto con el que trabaja. * @param *key: clave de acceso al elemento. */ void* hash_remove (hash_t* obj, void *key); /** * Mira si existe un elemento a partir de su clave. * * @param *obj: objeto con el que trabaja. * @param *key: clave del elemento. * @return int: 1 si existe algun elmento con clave key, 0 en caso contrario. */ int hash_contains_key (hash_t* obj, void *key); /** * Retorna el numero de elementos en la tabla de hash. * * @param *obj: objeto con el que trabaja. * @return int: numero de elementos en la lista. */ int hash_size (list_t* obj); /** * Establece una funcion de comparacion personalizada. * * @param *obj: objeto con el que trabaja. * @param *cmpfunc(...): funcion de comparacion que retorna 0 si param1=param2, * un entero menor que 1 si param1param2. */ void hash_set_cmpfunc (hash_t* obj, int (*cmpfunc)(void*, void*)); #ifdef __cplusplus } #endif #endif