1f14f4646Smrg/* 2f14f4646Smrg * Copyright (c) 2007,2008 Paulo Cesar Pereira de Andrade 3f14f4646Smrg * 4f14f4646Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5f14f4646Smrg * copy of this software and associated documentation files (the "Software"), 6f14f4646Smrg * to deal in the Software without restriction, including without limitation 7f14f4646Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8f14f4646Smrg * and/or sell copies of the Software, and to permit persons to whom the 9f14f4646Smrg * Software is furnished to do so, subject to the following conditions: 10f14f4646Smrg * 11f14f4646Smrg * The above copyright notice and this permission notice (including the next 12f14f4646Smrg * paragraph) shall be included in all copies or substantial portions of the 13f14f4646Smrg * Software. 14f14f4646Smrg * 15f14f4646Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16f14f4646Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17f14f4646Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18f14f4646Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19f14f4646Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20f14f4646Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21f14f4646Smrg * DEALINGS IN THE SOFTWARE. 22f14f4646Smrg * 23f14f4646Smrg * Author: Paulo Cesar Pereira de Andrade 24f14f4646Smrg */ 25f14f4646Smrg 26f14f4646Smrg/* Generic hash table */ 27f14f4646Smrg 28f14f4646Smrg#ifndef _util_h 29f14f4646Smrg#define _util_h 30f14f4646Smrg 31f14f4646Smrg/* 32f14f4646Smrg * Types 33f14f4646Smrg */ 34f14f4646Smrgtypedef struct _hash_key hash_key; 35f14f4646Smrgtypedef struct _hash_entry hash_entry; 36f14f4646Smrgtypedef struct _hash_table hash_table; 37f14f4646Smrgtypedef int (*hash_compare)(hash_key *left, hash_key *right); 38f14f4646Smrg 39f14f4646Smrgstruct _hash_key { 40f14f4646Smrg char *value; 41f14f4646Smrg unsigned int length; 42f14f4646Smrg}; 43f14f4646Smrg 44f14f4646Smrgstruct _hash_entry { 45f14f4646Smrg hash_key *key; 46f14f4646Smrg hash_entry *next; 47f14f4646Smrg}; 48f14f4646Smrg 49f14f4646Smrgstruct _hash_table { 50f14f4646Smrg hash_entry **entries; 51f14f4646Smrg unsigned int count; /* length of entries */ 52f14f4646Smrg unsigned int length; /* sum of entries */ 53f14f4646Smrg hash_compare compare; 54f14f4646Smrg 55f14f4646Smrg struct { 56f14f4646Smrg int offset; 57f14f4646Smrg hash_entry *entry; 58f14f4646Smrg } iter; 59f14f4646Smrg}; 60f14f4646Smrg 61f14f4646Smrg/* 62f14f4646Smrg * Prototypes 63f14f4646Smrg */ 64f14f4646Smrghash_table *hash_new(unsigned int length, hash_compare compare); 65f14f4646Smrghash_entry *hash_put(hash_table *hash, hash_entry *entry); 66f14f4646Smrghash_entry *hash_get(hash_table *hash, hash_key *name); 67f765521fSmrghash_entry * hash_check(hash_table *hash, const char *name, unsigned int length); 68f14f4646Smrgvoid hash_rem(hash_table *hash, hash_entry *entry); 69f14f4646Smrg/* Removes from hash table but doesn't release any memory */ 70f14f4646Smrghash_entry *hash_rem_no_free(hash_table *hash, hash_entry *entry); 71f14f4646Smrgvoid hash_rehash(hash_table *hash, unsigned int length); 72f14f4646Smrghash_entry *hash_iter_first(hash_table *hash); 73f14f4646Smrghash_entry *hash_iter_next(hash_table *hash); 74f14f4646Smrg 75f14f4646Smrg/* Frees all data. When casting to another type, use the 76f14f4646Smrg * iterator to free extra data */ 77f14f4646Smrgvoid hash_clr(hash_table *hash); 78f14f4646Smrgvoid hash_del(hash_table *hash); 79f14f4646Smrg 80f14f4646Smrg#endif /* _util_h */ 81