summaryrefslogtreecommitdiff
path: root/ecp
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2022-01-30 02:01:37 +0100
committerUros Majstorovic <majstor@majstor.org>2022-01-30 02:01:37 +0100
commitceb32f60ef7d6210883acf1f17500f87cac8888c (patch)
treef7da899c7ddc3ffa13a43e4f37e38a2d4a184671 /ecp
parenta4f22127be441c4c158c10fe65916872d99253d2 (diff)
hashtable fix
Diffstat (limited to 'ecp')
-rwxr-xr-xecp/src/htable/hashtable.c222
-rwxr-xr-xecp/src/htable/hashtable.h102
-rwxr-xr-xecp/src/htable/hashtable_itr.c42
-rwxr-xr-xecp/src/htable/hashtable_itr.h29
-rwxr-xr-xecp/src/htable/hashtable_private.h37
-rw-r--r--ecp/src/htable/htable.c7
6 files changed, 267 insertions, 172 deletions
diff --git a/ecp/src/htable/hashtable.c b/ecp/src/htable/hashtable.c
index de15827..1891f1b 100755
--- a/ecp/src/htable/hashtable.c
+++ b/ecp/src/htable/hashtable.c
@@ -2,80 +2,93 @@
#include "hashtable.h"
#include "hashtable_private.h"
+#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
-/*
-Credit for primes table: Aaron Krowne
- http://br.endernet.org/~akrowne/
- http://planetmath.org/encyclopedia/GoodHashTablePrimes.html
-*/
-static const unsigned int primes[] = {
-53, 97, 193, 389,
-769, 1543, 3079, 6151,
-12289, 24593, 49157, 98317,
-196613, 393241, 786433, 1572869,
-3145739, 6291469, 12582917, 25165843,
-50331653, 100663319, 201326611, 402653189,
-805306457, 1610612741
-};
-const unsigned int prime_table_length = sizeof(primes)/sizeof(primes[0]);
-const float max_load_factor = 0.65;
+#define MAX_LOAD_FACTOR 0.65F
/*****************************************************************************/
struct hashtable *
-create_hashtable(unsigned int minsize,
- unsigned int (*hash_fn) (void*),
- int (*eq_fn) (void*,void*),
- void *(*malloc_fn) (size_t),
- void *(*realloc_fn) (void*,size_t),
- void (*free_fn) (void*))
+hashtable_create(unsigned int minsize,
+ unsigned int (*hashf) (void*),
+ int (*eqf) (void*,void*))
{
- struct hashtable *h = NULL;
- unsigned int pindex, size = primes[0];
-
- /* Check requested hashtable isn't too large */
- if (minsize > (1u << 30)) return NULL;
+ struct hashtable *h;
+ struct entry **t;
+ unsigned int size;
+ size = hashtable_prime_size(minsize);
+ if (0 == size) return NULL;
+ h = (struct hashtable *)malloc(sizeof(struct hashtable));
+ if (NULL == h) return NULL; /*oom*/
+ t = (struct entry **)malloc(sizeof(struct entry*) * size);
+ if (NULL == t) { free(h); return NULL; } /*oom*/
+ hashtable_create_static(h,t,size,hashf,eqf);
+ return h;
+}
+
+/*****************************************************************************/
+void
+hashtable_create_static(struct hashtable *h, struct entry **t,
+ unsigned int size,
+ unsigned int (*hashf) (void*),
+ int (*eqf) (void*,void*))
+{
+ memset(t, 0, size * sizeof(struct entry *));
+ h->table = t;
+ h->tablelength = size;
+ h->entrycount = 0;
+ h->hashfn = hashf;
+ h->eqfn = eqf;
+ h->loadlimit = (unsigned int) ceil(size * MAX_LOAD_FACTOR);
+}
- malloc_fn = malloc_fn ? malloc_fn : malloc;
- realloc_fn = realloc_fn ? realloc_fn : realloc;
- free_fn = free_fn ? free_fn : free;
- h = malloc_fn(sizeof(struct hashtable));
- if (NULL == h) return NULL;
- memset(h, 0, sizeof(struct hashtable));
-
+unsigned int
+hashtable_prime_size(unsigned int minsize)
+{
+ /*
+ Credit for primes table: Aaron Krowne
+ http://br.endernet.org/~akrowne/
+ http://planetmath.org/encyclopedia/GoodHashTablePrimes.html
+ */
+ const unsigned int primes[] = {
+ 53, 97, 193, 389,
+ 769, 1543, 3079, 6151,
+ 12289, 24593, 49157, 98317,
+ 196613, 393241, 786433, 1572869,
+ 3145739, 6291469, 12582917, 25165843,
+ 50331653, 100663319, 201326611, 402653189,
+ 805306457, 1610612741
+ };
+ const unsigned int prime_table_length = sizeof(primes)/sizeof(primes[0]);
+ unsigned int pindex, size = 0;
+ /* Check requested hashtable isn't too large */
+ if (minsize > (1u << 30)) return 0;
/* Enforce size as prime */
for (pindex=0; pindex < prime_table_length; pindex++) {
- if (primes[pindex] > minsize) { size = primes[pindex]; break; }
+ if (primes[pindex] >= minsize) { size = primes[pindex]; break; }
}
- h->fn_malloc = malloc_fn;
- h->fn_realloc = realloc_fn;
- h->fn_free = free_fn;
- h->tablelength = size;
- h->primeindex = pindex;
- h->entrycount = 0;
- h->fn_hash = hash_fn;
- h->fn_eq = eq_fn;
- h->loadlimit = (unsigned int) ceil(size * max_load_factor);
- h->table = malloc_fn(sizeof(struct entry *) * size);
- if (NULL == h->table) { free_fn(h); return NULL; } /*oom*/
- memset(h->table, 0, size * sizeof(struct entry *));
- return h;
+ return size;
}
/*****************************************************************************/
+/* key - return the key of the (key,value) pair from hash table entry */
+/* value - return the value of the (key,value) pair from hash table entry */
+
+void *
+hashtable_entry_key(struct entry *e)
+{ return e->k; }
+
+void *
+hashtable_entry_value(struct entry *e)
+{ return e->v; }
+
+/*****************************************************************************/
unsigned int
hashtable_hash(struct hashtable *h, void *k)
{
- /* Aim to protect against poor hash functions by adding logic here
- * - logic taken from java 1.4 hashtable source */
- unsigned int i = h->fn_hash(k);
- i += ~(i << 9);
- i ^= ((i >> 14) | (i << 18)); /* >>> */
- i += (i << 4);
- i ^= ((i >> 10) | (i << 22)); /* >>> */
- return i;
+ return h->hashfn(k);
}
/*****************************************************************************/
@@ -87,11 +100,11 @@ hashtable_expand(struct hashtable *h)
struct entry *e;
struct entry **pE;
unsigned int newsize, i, index;
+ newsize = hashtable_prime_size(h->tablelength + 1);
/* Check we're not hitting max capacity */
- if (h->primeindex == (prime_table_length - 1)) return 0;
- newsize = primes[++(h->primeindex)];
+ if (0 == newsize) return 0;
- newtable = h->fn_malloc(sizeof(struct entry *) * newsize);
+ newtable = (struct entry **)malloc(sizeof(struct entry*) * newsize);
if (NULL != newtable)
{
memset(newtable, 0, newsize * sizeof(struct entry *));
@@ -105,14 +118,15 @@ hashtable_expand(struct hashtable *h)
newtable[index] = e;
}
}
- h->fn_free(h->table);
+ free(h->table);
h->table = newtable;
}
/* Plan B: realloc instead */
- else
+ else
{
- newtable = h->fn_realloc(h->table, newsize * sizeof(struct entry *));
- if (NULL == newtable) { (h->primeindex)--; return 0; }
+ newtable = (struct entry **)
+ realloc(h->table, newsize * sizeof(struct entry *));
+ if (NULL == newtable) return 0;
h->table = newtable;
memset(newtable[h->tablelength], 0, newsize - h->tablelength);
for (i = 0; i < h->tablelength; i++) {
@@ -132,7 +146,7 @@ hashtable_expand(struct hashtable *h)
}
}
h->tablelength = newsize;
- h->loadlimit = (unsigned int) ceil(newsize * max_load_factor);
+ h->loadlimit = (unsigned int) ceil(newsize * MAX_LOAD_FACTOR);
return -1;
}
@@ -147,8 +161,6 @@ hashtable_count(struct hashtable *h)
int
hashtable_insert(struct hashtable *h, void *k, void *v)
{
- /* This method allows duplicate keys - but they shouldn't be used */
- unsigned int index;
struct entry *e;
if (++(h->entrycount) > h->loadlimit)
{
@@ -158,15 +170,23 @@ hashtable_insert(struct hashtable *h, void *k, void *v)
* element may be ok. Next time we insert, we'll try expanding again.*/
hashtable_expand(h);
}
- e = h->fn_malloc(sizeof(struct entry));
+ e = (struct entry *)malloc(sizeof(struct entry));
if (NULL == e) { --(h->entrycount); return 0; } /*oom*/
+ hashtable_insert_static(h,e,k,v);
+ return -1;
+}
+
+void
+hashtable_insert_static(struct hashtable *h, struct entry *e, void *k, void *v)
+{
+ /* This method allows duplicate keys - but they shouldn't be used */
+ unsigned int index;
e->h = hashtable_hash(h,k);
index = indexFor(h->tablelength,e->h);
e->k = k;
e->v = v;
e->next = h->table[index];
h->table[index] = e;
- return -1;
}
/*****************************************************************************/
@@ -181,24 +201,34 @@ hashtable_search(struct hashtable *h, void *k)
while (NULL != e)
{
/* Check hash value to short circuit heavier comparison */
- if ((hashvalue == e->h) && (h->fn_eq(k, e->k))) return e->v;
+ if ((hashvalue == e->h) && (h->eqfn(k, e->k))) return e->v;
e = e->next;
}
return NULL;
}
/*****************************************************************************/
-void * /* returns value associated with key --- modified by majstor */
+void * /* returns value associated with key */
hashtable_remove(struct hashtable *h, void *k)
{
/* TODO: consider compacting the table when the load factor drops enough,
* or provide a 'compact' method. */
struct entry *e;
- struct entry **pE;
void *v;
- unsigned int hashvalue, index;
+ e = hashtable_remove_static(h,k);
+ v = e->v;
+ freekey(e->k);
+ free(e);
+ return v;
+}
+struct entry * /* returns hash table entry associated with key */
+hashtable_remove_static(struct hashtable *h, void *k)
+{
+ struct entry *e;
+ struct entry **pE;
+ unsigned int hashvalue, index;
hashvalue = hashtable_hash(h,k);
index = indexFor(h->tablelength,hashvalue);
pE = &(h->table[index]);
@@ -206,14 +236,11 @@ hashtable_remove(struct hashtable *h, void *k)
while (NULL != e)
{
/* Check hash value to short circuit heavier comparison */
- if ((hashvalue == e->h) && (h->fn_eq(k, e->k)))
+ if ((hashvalue == e->h) && (h->eqfn(k, e->k)))
{
*pE = e->next;
h->entrycount--;
- v = e->v;
- if (h->fn_free_k) h->fn_free_k(e->k);
- h->fn_free(e);
- return v;
+ return e->v;
}
pE = &(e->next);
e = e->next;
@@ -222,44 +249,55 @@ hashtable_remove(struct hashtable *h, void *k)
}
/*****************************************************************************/
-/* destroy --- modified by majstor */
+/* destroy */
void
-hashtable_destroy(struct hashtable *h)
+hashtable_destroy(struct hashtable *h, int free_values)
{
unsigned int i;
struct entry *e, *f;
struct entry **table = h->table;
-
- for (i = 0; i < h->tablelength; i++)
+ if (free_values)
{
- e = table[i];
- while (NULL != e)
- { f = e; e = e->next; if (h->fn_free_k) h->fn_free_k(f->k); if (h->fn_free_v) h->fn_free_v(f->v); h->fn_free(f); }
+ for (i = 0; i < h->tablelength; i++)
+ {
+ e = table[i];
+ while (NULL != e)
+ { f = e; e = e->next; freekey(f->k); free(f->v); free(f); }
+ }
+ }
+ else
+ {
+ for (i = 0; i < h->tablelength; i++)
+ {
+ e = table[i];
+ while (NULL != e)
+ { f = e; e = e->next; freekey(f->k); free(f); }
+ }
}
- h->fn_free(h->table);
- h->fn_free(h);
+ free(h->table);
+ free(h);
}
/*
* Copyright (c) 2002, Christopher Clark
* All rights reserved.
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
- *
+ *
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
- *
+ *
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- *
+ *
* * Neither the name of the original author; nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
- *
- *
+ *
+ *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
diff --git a/ecp/src/htable/hashtable.h b/ecp/src/htable/hashtable.h
index 046f740..a0b1949 100755
--- a/ecp/src/htable/hashtable.h
+++ b/ecp/src/htable/hashtable.h
@@ -3,8 +3,6 @@
#ifndef __HASHTABLE_CWC22_H__
#define __HASHTABLE_CWC22_H__
-#include <stdlib.h>
-
struct hashtable;
/* Example of use:
@@ -21,12 +19,12 @@ struct hashtable;
* v = (struct some_value *) malloc(sizeof(struct some_value));
*
* (initialise k and v to suitable values)
- *
+ *
* if (! hashtable_insert(h,k,v) )
* { exit(-1); }
*
* if (NULL == (found = hashtable_search(h,k) ))
- * { printf("Not found!"); }
+ * { printf("not found!"); }
*
* if (NULL == (found = hashtable_remove(h,k) ))
* { printf("Not found\n"); }
@@ -35,7 +33,7 @@ struct hashtable;
/* Macros may be used to define type-safe(r) hashtable access functions, with
* methods specialized to take known key and value types as parameters.
- *
+ *
* Example:
*
* Insert this at the start of your file:
@@ -63,30 +61,61 @@ struct hashtable;
*
*/
+/*****************************************************************************/
+struct entry
+{
+ void *k, *v;
+ unsigned int h;
+ struct entry *next;
+};
+
/*****************************************************************************
* create_hashtable
-
+
* @name create_hashtable
* @param minsize minimum initial size of hashtable
- * @param hash_fn function for hashing keys
- * @param eq_fn function for determining key equality
- * @param malloc_fn function malloc
- * @param realloc_fn function realloc
- * @param free_fn function free
- * @return hashtable created
+ * @param hashfunction function for hashing keys
+ * @param key_eq_fn function for determining key equality
+ * @return newly created hashtable or NULL on failure
*/
struct hashtable *
-create_hashtable(unsigned int minsize,
- unsigned int (*hash_fn) (void*),
- int (*eq_fn) (void*,void*),
- void *(*malloc_fn) (size_t),
- void *(*realloc_fn) (void*,size_t),
- void (*free_fn) (void*));
+hashtable_create(unsigned int minsize,
+ unsigned int (*hashfunction) (void*),
+ int (*key_eq_fn) (void*,void*));
+
+void
+hashtable_create_static(struct hashtable *h, struct entry **t,
+ unsigned int size,
+ unsigned int (*hashf) (void*),
+ int (*eqf) (void*,void*));
+
+unsigned int
+hashtable_prime_size(unsigned int minsize);
+
+/*****************************************************************************/
+/* hashtable_iterator_key
+ * - return the key of the (key,value) pair at the current position */
+
+extern inline void *
+hashtable_entry_key(struct entry *e)
+{
+ return e->v;
+}
+
+/*****************************************************************************/
+/* hashtable_iterator_value
+ * - return the value of the (key,value) pair at the current position */
+
+extern inline void *
+hashtable_entry_value(struct entry *e)
+{
+ return e->v;
+}
/*****************************************************************************
* hashtable_insert
-
+
* @name hashtable_insert
* @param h the hashtable to insert into
* @param k the key - hashtable claims ownership and will free on removal
@@ -103,9 +132,12 @@ create_hashtable(unsigned int minsize,
* If in doubt, remove before insert.
*/
-int
+int
hashtable_insert(struct hashtable *h, void *k, void *v);
+void
+hashtable_insert_static(struct hashtable *h, struct entry *e, void *k, void *v);
+
#define DEFINE_HASHTABLE_INSERT(fnname, keytype, valuetype) \
int fnname (struct hashtable *h, keytype *k, valuetype *v) \
{ \
@@ -114,7 +146,7 @@ int fnname (struct hashtable *h, keytype *k, valuetype *v) \
/*****************************************************************************
* hashtable_search
-
+
* @name hashtable_search
* @param h the hashtable to search
* @param k the key to search for - does not claim ownership
@@ -132,7 +164,7 @@ valuetype * fnname (struct hashtable *h, keytype *k) \
/*****************************************************************************
* hashtable_remove
-
+
* @name hashtable_remove
* @param h the hashtable to remove the item from
* @param k the key to search for - does not claim ownership
@@ -142,16 +174,19 @@ valuetype * fnname (struct hashtable *h, keytype *k) \
void * /* returns value */
hashtable_remove(struct hashtable *h, void *k);
+struct entry * /* returns hash table entry */
+hashtable_remove_static(struct hashtable *h, void *k);
+
#define DEFINE_HASHTABLE_REMOVE(fnname, keytype, valuetype) \
-valuetype * fnname (struct hashtable *h, keytype *k, void (*f) (void *)) \
+valuetype * fnname (struct hashtable *h, keytype *k) \
{ \
- return (valuetype *) (hashtable_remove(h,k,f)); \
+ return (valuetype *) (hashtable_remove(h,k)); \
}
/*****************************************************************************
* hashtable_count
-
+
* @name hashtable_count
* @param h the hashtable
* @return the number of items stored in the hashtable
@@ -162,36 +197,37 @@ hashtable_count(struct hashtable *h);
/*****************************************************************************
* hashtable_destroy
-
+
* @name hashtable_destroy
* @param h the hashtable
+ * @param free_values whether to call 'free' on the remaining values
*/
void
-hashtable_destroy(struct hashtable *h);
+hashtable_destroy(struct hashtable *h, int free_values);
#endif /* __HASHTABLE_CWC22_H__ */
/*
* Copyright (c) 2002, Christopher Clark
* All rights reserved.
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
- *
+ *
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
- *
+ *
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- *
+ *
* * Neither the name of the original author; nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
- *
- *
+ *
+ *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
diff --git a/ecp/src/htable/hashtable_itr.c b/ecp/src/htable/hashtable_itr.c
index 3ee8fe2..51ce0d2 100755
--- a/ecp/src/htable/hashtable_itr.c
+++ b/ecp/src/htable/hashtable_itr.c
@@ -34,6 +34,7 @@ hashtable_iterator(struct hashtable_itr *itr, struct hashtable *h)
/*****************************************************************************/
/* key - return the key of the (key,value) pair at the current position */
/* value - return the value of the (key,value) pair at the current position */
+/* entry - return the hash table entry at the current position */
void *
hashtable_iterator_key(struct hashtable_itr *i)
@@ -43,6 +44,10 @@ void *
hashtable_iterator_value(struct hashtable_itr *i)
{ return i->e->v; }
+struct entry *
+hashtable_iterator_entry(struct hashtable_itr *i)
+{ return i->e; }
+
/*****************************************************************************/
/* advance - advance the iterator to the next element
* returns zero if advanced to end of table */
@@ -90,11 +95,24 @@ hashtable_iterator_advance(struct hashtable_itr *itr)
* element.
* If you want the value, read it before you remove:
* beware memory leaks if you don't.
- * Returns zero if end of iteration. --- modified by majstor */
+ * Returns zero if end of iteration. */
int
hashtable_iterator_remove(struct hashtable_itr *itr)
{
+ struct entry *e;
+ int ret;
+
+ e = hashtable_iterator_entry(itr);
+ ret = hashtable_iterator_remove_static(itr);
+ freekey(e->k);
+ free(e);
+ return ret;
+}
+
+int
+hashtable_iterator_remove_static(struct hashtable_itr *itr)
+{
struct entry *remember_e, *remember_parent;
int ret;
@@ -110,14 +128,11 @@ hashtable_iterator_remove(struct hashtable_itr *itr)
/* itr->e is now outside the hashtable */
remember_e = itr->e;
itr->h->entrycount--;
- if (itr->h->fn_free_k) itr->h->fn_free_k(remember_e->k);
- if (itr->h->fn_free_v) itr->h->fn_free_v(remember_e->v);
/* Advance the iterator, correcting the parent */
remember_parent = itr->parent;
ret = hashtable_iterator_advance(itr);
if (itr->parent == remember_e) { itr->parent = remember_parent; }
- itr->h->fn_free(remember_e);
return ret;
}
@@ -128,7 +143,7 @@ hashtable_iterator_search(struct hashtable_itr *itr, void *k)
struct entry *e, *parent;
unsigned int hashvalue, index;
struct hashtable *h = itr->h;
-
+
hashvalue = hashtable_hash(h,k);
index = indexFor(h->tablelength,hashvalue);
@@ -137,7 +152,7 @@ hashtable_iterator_search(struct hashtable_itr *itr, void *k)
while (NULL != e)
{
/* Check hash value to short circuit heavier comparison */
- if ((hashvalue == e->h) && (h->fn_eq(k, e->k)))
+ if ((hashvalue == e->h) && (h->eqfn(k, e->k)))
{
itr->index = index;
itr->e = e;
@@ -151,7 +166,6 @@ hashtable_iterator_search(struct hashtable_itr *itr, void *k)
return 0;
}
-
/*****************************************************************************/
int /* returns zero if not found */
hashtable_iterator_search_next(struct hashtable_itr *itr, void *k)
@@ -167,7 +181,7 @@ hashtable_iterator_search_next(struct hashtable_itr *itr, void *k)
while (NULL != e)
{
/* Check hash value to short circuit heavier comparison */
- if ((hashvalue == e->h) && (h->fn_eq(k, e->k)))
+ if ((hashvalue == e->h) && (h->eqfn(k, e->k)))
{
itr->parent = parent;
itr->e = e;
@@ -183,23 +197,23 @@ hashtable_iterator_search_next(struct hashtable_itr *itr, void *k)
/*
* Copyright (c) 2002, 2004, Christopher Clark
* All rights reserved.
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
- *
+ *
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
- *
+ *
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- *
+ *
* * Neither the name of the original author; nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
- *
- *
+ *
+ *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
diff --git a/ecp/src/htable/hashtable_itr.h b/ecp/src/htable/hashtable_itr.h
index 658ae83..47b29cc 100755
--- a/ecp/src/htable/hashtable_itr.h
+++ b/ecp/src/htable/hashtable_itr.h
@@ -26,7 +26,7 @@ hashtable_iterator(struct hashtable_itr *, struct hashtable *h);
/*****************************************************************************/
/* hashtable_iterator_key
- * - return the value of the (key,value) pair at the current position */
+ * - return the key of the (key,value) pair at the current position */
extern inline void *
hashtable_iterator_key(struct hashtable_itr *i)
@@ -35,7 +35,8 @@ hashtable_iterator_key(struct hashtable_itr *i)
}
/*****************************************************************************/
-/* value - return the value of the (key,value) pair at the current position */
+/* hashtable_iterator_value
+ * - return the value of the (key,value) pair at the current position */
extern inline void *
hashtable_iterator_value(struct hashtable_itr *i)
@@ -44,6 +45,16 @@ hashtable_iterator_value(struct hashtable_itr *i)
}
/*****************************************************************************/
+/* hashtable_iterator_entry
+ * - return the hash table entry at the current position */
+
+extern inline struct entry *
+hashtable_iterator_entry(struct hashtable_itr *i)
+{
+ return i->e;
+}
+
+/*****************************************************************************/
/* advance - advance the iterator to the next element
* returns zero if advanced to end of table */
@@ -73,7 +84,7 @@ int fnname (struct hashtable_itr *i, keytype *k) \
}
/*****************************************************************************/
-/* search next - advance the iterator to point to the entry
+/* search next - advance the iterator to point to next entry
* matching the supplied key.
* returns zero if not found. */
int
@@ -90,23 +101,23 @@ int fnname (struct hashtable_itr *i, keytype *k) \
/*
* Copyright (c) 2002, 2004, Christopher Clark
* All rights reserved.
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
- *
+ *
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
- *
+ *
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- *
+ *
* * Neither the name of the original author; nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
- *
- *
+ *
+ *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
diff --git a/ecp/src/htable/hashtable_private.h b/ecp/src/htable/hashtable_private.h
index 2330b00..bdddfc7 100755
--- a/ecp/src/htable/hashtable_private.h
+++ b/ecp/src/htable/hashtable_private.h
@@ -5,27 +5,13 @@
#include "hashtable.h"
-/*****************************************************************************/
-struct entry
-{
- void *k, *v;
- unsigned int h;
- struct entry *next;
-};
-
struct hashtable {
unsigned int tablelength;
struct entry **table;
unsigned int entrycount;
unsigned int loadlimit;
- unsigned int primeindex;
- unsigned int (*fn_hash) (void *k);
- int (*fn_eq) (void *k1, void *k2);
- void *(*fn_malloc) (size_t);
- void *(*fn_realloc) (void *,size_t);
- void (*fn_free) (void *);
- void (*fn_free_k) (void *);
- void (*fn_free_v) (void *);
+ unsigned int (*hashfn) (void *k);
+ int (*eqfn) (void *k1, void *k2);
};
/*****************************************************************************/
@@ -48,28 +34,35 @@ indexFor(unsigned int tablelength, unsigned int hashvalue)
}
*/
+/*****************************************************************************/
+/* #define freekey(X) free(X) */
+#define freekey(X) ;
+
+
+/*****************************************************************************/
+
#endif /* __HASHTABLE_PRIVATE_CWC22_H__*/
/*
* Copyright (c) 2002, Christopher Clark
* All rights reserved.
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
- *
+ *
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
- *
+ *
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- *
+ *
* * Neither the name of the original author; nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
- *
- *
+ *
+ *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
diff --git a/ecp/src/htable/htable.c b/ecp/src/htable/htable.c
index a46291b..1fa79a3 100644
--- a/ecp/src/htable/htable.c
+++ b/ecp/src/htable/htable.c
@@ -5,11 +5,14 @@
#include "hashtable.h"
void *ecp_ht_create(ECPContext *ctx) {
- return create_hashtable(1000, (unsigned int (*)(void *))ecp_cr_dh_pub_hash_fn, (int (*)(void *, void *))ecp_cr_dh_pub_hash_eq, NULL, NULL, NULL);
+ printf("NEFORE CREATE\n");
+ void *r = hashtable_create(1000, (unsigned int (*)(void *))ecp_cr_dh_pub_hash_fn, (int (*)(void *, void *))ecp_cr_dh_pub_hash_eq);
+ printf("AFTER CREATE\n");
+ return r;
}
void ecp_ht_destroy(void *h) {
- hashtable_destroy(h);
+ hashtable_destroy(h, 0);
}
int ecp_ht_insert(void *h, unsigned char *k, ECPConnection *v) {