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