2001-02-19 21:15:25 +00:00
|
|
|
/*
|
2001-02-21 19:35:06 +00:00
|
|
|
hash.h
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
hash tables
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-06-19 02:01:18 +00:00
|
|
|
Copyright (C) 2000 Bill Currie <bill@taniwha.org>
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
*/
|
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
#ifndef __hash_h
|
|
|
|
#define __hash_h
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-06-11 19:17:40 +00:00
|
|
|
typedef struct hashtab_s hashtab_t;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-06-11 19:37:25 +00:00
|
|
|
/*
|
|
|
|
create a new hash table:
|
|
|
|
tsize: table size. larger values will give better distribution, but
|
|
|
|
use more memory.
|
|
|
|
gk: a function that returns a string to be used as the key for
|
|
|
|
inserting or finding the element. First parameter is a pointer
|
|
|
|
to the element from which to extract the key, the second is
|
|
|
|
the user data pointer.
|
|
|
|
f: a function to free the element. Only ever called from
|
|
|
|
Hash_FlushTable and Hash_DelTable. The first parameter is the
|
|
|
|
element to be freed and the second is the user data pointer.
|
|
|
|
ud: user data pointer. set to whatever you want, it will be passed
|
|
|
|
to the get key and free functions as the second parameter.
|
|
|
|
returns a pointer to the hash table (to be passed to the other functions)
|
|
|
|
or 0 on error.
|
|
|
|
|
|
|
|
multiple inserions of the same key are fine; later insertions override
|
|
|
|
previous ones until the later one is removed (Hash_Del).
|
|
|
|
*/
|
2001-06-04 04:52:14 +00:00
|
|
|
hashtab_t *Hash_NewTable (int tsize, const char *(*gk)(void*,void*),
|
2001-03-05 05:11:26 +00:00
|
|
|
void (*f)(void*,void*), void *ud);
|
2001-06-11 19:37:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
delete a hash table:
|
|
|
|
tab: the table to be deleted
|
|
|
|
*/
|
2001-02-21 19:35:06 +00:00
|
|
|
void Hash_DelTable (hashtab_t *tab);
|
2001-06-11 19:37:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
clean out all the entries from a hash table, starting over again:
|
|
|
|
tab: the table to be cleared
|
|
|
|
*/
|
2001-03-05 05:11:26 +00:00
|
|
|
void Hash_FlushTable (hashtab_t *tab);
|
2001-06-11 19:37:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
add an entry to a hash table:
|
|
|
|
tab: the table to be added to
|
|
|
|
ele: the element to add to the table
|
|
|
|
returns 0 for success, -1 for error.
|
|
|
|
*/
|
2001-02-21 19:35:06 +00:00
|
|
|
int Hash_Add (hashtab_t *tab, void *ele);
|
2001-06-11 19:37:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
find an element within a hash table:
|
|
|
|
tab: the table to search
|
|
|
|
key: the key string identifying the element being searched for
|
|
|
|
returns a pointer to the element if found, otherwise 0.
|
|
|
|
*/
|
2001-02-21 19:35:06 +00:00
|
|
|
void *Hash_Find (hashtab_t *tab, const char *key);
|
2001-06-11 19:37:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
delete an element from a hash table:
|
|
|
|
tab: the table to remove the element from
|
|
|
|
key: the key string identifying the element to be deleted
|
|
|
|
returns 0 on success, -1 if the element could not be found.
|
|
|
|
Does /NOT/ call the free element function. That is the caller's
|
|
|
|
responsibility.
|
|
|
|
*/
|
2001-02-21 19:35:06 +00:00
|
|
|
int Hash_Del (hashtab_t *tab, const char *key);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
#endif // __hash_h
|