projects
/
rocksndiamonds.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
added function parameters for hash table creation to free keys or values
[rocksndiamonds.git]
/
src
/
libgame
/
hash.c
diff --git
a/src/libgame/hash.c
b/src/libgame/hash.c
index 65f4270c16011f033a7c24f503ec3a68cb66c2ac..101ec1214a30aae2faddd478c24f53321d401832 100644
(file)
--- a/
src/libgame/hash.c
+++ b/
src/libgame/hash.c
@@
-1,15
+1,13
@@
-/***********************************************************
-* Artsoft Retro-Game Library *
-*----------------------------------------------------------*
-* (c) 1994-2006 Artsoft Entertainment *
-* Holger Schemel *
-* Detmolder Strasse 189 *
-* 33604 Bielefeld *
-* Germany *
-* e-mail: info@artsoft.org *
-*----------------------------------------------------------*
-* hash.c *
-***********************************************************/
+// ============================================================================
+// Artsoft Retro-Game Library
+// ----------------------------------------------------------------------------
+// (c) 1995-2014 by Artsoft Entertainment
+// Holger Schemel
+// info@artsoft.org
+// https://www.artsoft.org/
+// ----------------------------------------------------------------------------
+// hash.c
+// ============================================================================
/*
* Copyright (C) 2002 Christopher Clark <firstname.lastname@cl.cam.ac.uk>
/*
* Copyright (C) 2002 Christopher Clark <firstname.lastname@cl.cam.ac.uk>
@@
-43,9
+41,11
@@
/*****************************************************************************/
struct hashtable *
/*****************************************************************************/
struct hashtable *
-create_hashtable(unsigned int minsize, float maxloadfactor,
- unsigned int (*hashf) (void*),
- int (*eqf) (void*,void*))
+create_hashtable_ext(unsigned int minsize, float maxloadfactor,
+ unsigned int (*hashf) (void*),
+ int (*eqf) (void*, void*),
+ void (*freekfn) (void*),
+ void (*freevfn) (void*))
{
struct hashtable *h;
unsigned int i, size = 1u;
{
struct hashtable *h;
unsigned int i, size = 1u;
@@
-72,7
+72,7
@@
create_hashtable(unsigned int minsize, float maxloadfactor,
return NULL;
}
return NULL;
}
- for (i
=
0; i < size; i++)
+ for (i
=
0; i < size; i++)
h->table[i] = NULL;
h->tablelength = size;
h->table[i] = NULL;
h->tablelength = size;
@@
-80,10
+80,21
@@
create_hashtable(unsigned int minsize, float maxloadfactor,
h->hashfn = hashf;
h->eqfn = eqf;
h->loadlimit = (unsigned int) ((float)size * maxloadfactor);
h->hashfn = hashf;
h->eqfn = eqf;
h->loadlimit = (unsigned int) ((float)size * maxloadfactor);
+ h->freekfn = freekfn;
+ h->freevfn = freevfn;
return h;
}
return h;
}
+struct hashtable *
+create_hashtable(unsigned int (*hashf) (void*),
+ int (*eqf) (void*, void*),
+ void (*freekfn) (void*),
+ void (*freevfn) (void*))
+{
+ return create_hashtable_ext(16, 0.75, hashf, eqf, freekfn, freevfn);
+}
+
/*****************************************************************************/
static unsigned int
hash(struct hashtable *h, void *k)
/*****************************************************************************/
static unsigned int
hash(struct hashtable *h, void *k)
@@
-136,7
+147,7
@@
hashtable_expand(struct hashtable *h)
while ((e = h->table[i]) != NULL)
{
h->table[i] = e->next;
while ((e = h->table[i]) != NULL)
{
h->table[i] = e->next;
- index = indexFor(newsize,e->h);
+ index = indexFor(newsize,
e->h);
e->next = newtable[index];
newtable[index] = e;
}
e->next = newtable[index];
newtable[index] = e;
}
@@
-162,7
+173,7
@@
hashtable_expand(struct hashtable *h)
{
for (pE = &(newtable[i]), e = *pE; e != NULL; e = *pE)
{
{
for (pE = &(newtable[i]), e = *pE; e != NULL; e = *pE)
{
- index = indexFor(newsize,e->h);
+ index = indexFor(newsize,
e->h);
if (index == i)
{
if (index == i)
{
@@
-218,8
+229,8
@@
hashtable_insert(struct hashtable *h, void *k, void *v)
return 0;
}
return 0;
}
- e->h = hash(h,k);
- index = indexFor(h->tablelength,e->h);
+ e->h = hash(h,
k);
+ index = indexFor(h->tablelength,
e->h);
e->k = k;
e->v = v;
e->next = h->table[index];
e->k = k;
e->v = v;
e->next = h->table[index];
@@
-235,8
+246,8
@@
hashtable_change(struct hashtable *h, void *k, void *v)
struct entry *e;
unsigned int hashvalue, index;
struct entry *e;
unsigned int hashvalue, index;
- hashvalue = hash(h,k);
- index = indexFor(h->tablelength,hashvalue);
+ hashvalue = hash(h,
k);
+ index = indexFor(h->tablelength,
hashvalue);
e = h->table[index];
while (e != NULL)
e = h->table[index];
while (e != NULL)
@@
-244,7
+255,8
@@
hashtable_change(struct hashtable *h, void *k, void *v)
/* Check hash value to short circuit heavier comparison */
if ((hashvalue == e->h) && (h->eqfn(k, e->k)))
{
/* Check hash value to short circuit heavier comparison */
if ((hashvalue == e->h) && (h->eqfn(k, e->k)))
{
- free(e->v);
+ if (h->freevfn != NULL)
+ h->freevfn(e->v);
e->v = v;
return -1;
e->v = v;
return -1;
@@
-263,8
+275,8
@@
hashtable_search(struct hashtable *h, void *k)
struct entry *e;
unsigned int hashvalue, index;
struct entry *e;
unsigned int hashvalue, index;
- hashvalue = hash(h,k);
- index = indexFor(h->tablelength,hashvalue);
+ hashvalue = hash(h,
k);
+ index = indexFor(h->tablelength,
hashvalue);
e = h->table[index];
while (e != NULL)
e = h->table[index];
while (e != NULL)
@@
-289,7
+301,7
@@
hashtable_remove(struct hashtable *h, void *k)
struct entry *e;
struct entry **pE;
void *v;
struct entry *e;
struct entry **pE;
void *v;
- unsigned int index = indexFor(h->tablelength,
hash(h,
k));
+ unsigned int index = indexFor(h->tablelength,
hash(h,
k));
pE = &(h->table[index]);
e = *pE;
pE = &(h->table[index]);
e = *pE;
@@
-300,8
+312,13
@@
hashtable_remove(struct hashtable *h, void *k)
{
*pE = e->next;
h->entrycount--;
{
*pE = e->next;
h->entrycount--;
- v = e->v;
- free(e->k);
+ v = NULL;
+ if (h->freekfn != NULL)
+ h->freekfn(e->k);
+ if (h->freevfn != NULL)
+ h->freevfn(e->v);
+ else
+ v = e->v;
free(e);
return v;
free(e);
return v;
@@
-317,7
+334,7
@@
hashtable_remove(struct hashtable *h, void *k)
/*****************************************************************************/
/* destroy */
void
/*****************************************************************************/
/* destroy */
void
-hashtable_destroy(struct hashtable *h
, int free_values
)
+hashtable_destroy(struct hashtable *h)
{
unsigned int i;
struct entry *e, *f;
{
unsigned int i;
struct entry *e, *f;
@@
-331,10
+348,10
@@
hashtable_destroy(struct hashtable *h, int free_values)
{
f = e;
e = e->next;
{
f = e;
e = e->next;
- free(f->k);
-
- if (
free_values
)
-
free
(f->v);
+ if (h->freekfn != NULL)
+ h->freekfn(f->k);
+ if (
h->freevfn != NULL
)
+
h->freevfn
(f->v);
free(f);
}
free(f);
}
@@
-344,7
+361,6
@@
hashtable_destroy(struct hashtable *h, int free_values)
free(h);
}
free(h);
}
-
/*****************************************************************************/
/* hashtable_iterator - iterator constructor */
/*****************************************************************************/
/* hashtable_iterator - iterator constructor */
@@
-381,7
+397,7
@@
hashtable_iterator(struct hashtable *h)
}
/*****************************************************************************/
}
/*****************************************************************************/
-/* key - return the key of the (key,value) pair at the current position */
+/* key - return the key of the (key,
value) pair at the current position */
void *
hashtable_iterator_key(struct hashtable_itr *i)
void *
hashtable_iterator_key(struct hashtable_itr *i)
@@
-390,7
+406,7
@@
hashtable_iterator_key(struct hashtable_itr *i)
}
/*****************************************************************************/
}
/*****************************************************************************/
-/* value - return the value of the (key,value) pair at the current position */
+/* value - return the value of the (key,
value) pair at the current position */
void *
hashtable_iterator_value(struct hashtable_itr *i)
void *
hashtable_iterator_value(struct hashtable_itr *i)
@@
-405,7
+421,7
@@
hashtable_iterator_value(struct hashtable_itr *i)
int
hashtable_iterator_advance(struct hashtable_itr *itr)
{
int
hashtable_iterator_advance(struct hashtable_itr *itr)
{
- unsigned int j,tablelength;
+ unsigned int j,
tablelength;
struct entry **table;
struct entry *next;
struct entry **table;
struct entry *next;