Skip to content

Commit

Permalink
New hash table implementation based on open addressing and linear pro…
Browse files Browse the repository at this point in the history
…bing. Seems significantly faster than existing chained hash table implementation.
  • Loading branch information
fmela committed Jul 30, 2014
1 parent b94394b commit b5a4327
Show file tree
Hide file tree
Showing 6 changed files with 753 additions and 8 deletions.
13 changes: 9 additions & 4 deletions benchmark.c
Expand Up @@ -66,6 +66,7 @@ main(int argc, char **argv)
fprintf(stderr, " w: weight-balanced tree\n");
fprintf(stderr, " S: skiplist\n");
fprintf(stderr, " H: hashtable\n");
fprintf(stderr, " 2: hashtable 2\n");
fprintf(stderr, "input: text file consisting of newline-separated keys\n");
exit(EXIT_FAILURE);
}
Expand Down Expand Up @@ -129,7 +130,7 @@ main(int argc, char **argv)
comp_count, hash_count);
total_comp += comp_count; comp_count = 0;
total_hash += hash_count; hash_count = 0;
if (type != 'H' && type != 'S') {
if (type != 'H' && type != '2' && type != 'S') {
tree_base *tree = dict_private(dct);
printf("insert rotations: %zu\n", tree->rotation_count);
total_rotations += tree->rotation_count;
Expand Down Expand Up @@ -193,7 +194,7 @@ main(int argc, char **argv)
comp_count, hash_count);
total_comp += comp_count; comp_count = 0;
total_hash += hash_count; hash_count = 0;
if (type != 'H' && type != 'S') {
if (type != 'H' && type != '2' && type != 'S') {
tree_base *tree = dict_private(dct);
printf("search rotations: %zu\n", tree->rotation_count);
total_rotations += tree->rotation_count;
Expand Down Expand Up @@ -229,7 +230,7 @@ main(int argc, char **argv)
comp_count, hash_count);
total_comp += comp_count; comp_count = 0;
total_hash += hash_count; hash_count = 0;
if (type != 'H' && type != 'S') {
if (type != 'H' && type != '2' && type != 'S') {
tree_base *tree = dict_private(dct);
printf("remove rotations: %zu\n", tree->rotation_count);
total_rotations += tree->rotation_count;
Expand All @@ -248,7 +249,7 @@ main(int argc, char **argv)
(total.tv_sec * 1000000 + total.tv_usec) * 1e-6,
total_comp, total_hash);

if (type != 'H' && type != 'S') {
if (type != 'H' && type != '2' && type != 'S') {
printf(" total rotations: %zu\n", total_rotations);
}

Expand Down Expand Up @@ -296,6 +297,10 @@ create_dictionary(char type, const char **container_name)
*container_name = "ht";
return hashtable_dict_new(cmp_func, hash_func, key_str_free, HASHTABLE_SIZE);

case '2':
*container_name = "h2";
return hashtable2_dict_new(cmp_func, hash_func, key_str_free, HASHTABLE_SIZE);

default:
quit("type must be one of h, p, r, t, s, w or H");
}
Expand Down
5 changes: 5 additions & 0 deletions demo.c
Expand Up @@ -72,6 +72,11 @@ main(int argc, char **argv)
dict_str_hash,
key_val_free, HSIZE);
break;
case '2':
dct = hashtable2_dict_new((dict_compare_func)strcmp,
dict_str_hash,
key_val_free, HSIZE);
break;
default:
quit("type must be one of h, p, r, t, s, w, or H");
}
Expand Down
1 change: 1 addition & 0 deletions include/dict.h
Expand Up @@ -197,6 +197,7 @@ unsigned dict_str_hash(const void* str);
END_DECL

#include "hashtable.h"
#include "hashtable2.h"
#include "hb_tree.h"
#include "pr_tree.h"
#include "rb_tree.h"
Expand Down
79 changes: 79 additions & 0 deletions include/hashtable2.h
@@ -0,0 +1,79 @@
/*
* libdict -- open-addressing hash-table interface.
*
* Copyright (c) 2001-2014, Farooq Mela
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
*
* 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 A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef _HASHTABLE2_H_
#define _HASHTABLE2_H_

#include "dict.h"

BEGIN_DECL

typedef struct hashtable2 hashtable2;

hashtable2* hashtable2_new(dict_compare_func cmp_func,
dict_hash_func hash_func,
dict_delete_func del_func, unsigned size);
dict* hashtable2_dict_new(dict_compare_func cmp_func,
dict_hash_func hash_func,
dict_delete_func del_func, unsigned size);
size_t hashtable2_free(hashtable2* table);
hashtable2* hashtable2_clone(hashtable2* table,
dict_key_datum_clone_func clone_func);

void** hashtable2_insert(hashtable2* table, void* key, bool* inserted);
void* hashtable2_search(hashtable2* table, const void* key);
bool hashtable2_remove(hashtable2* table, const void* key);
size_t hashtable2_clear(hashtable2* table);
size_t hashtable2_traverse(hashtable2* table, dict_visit_func visit);
size_t hashtable2_count(const hashtable2* table);
size_t hashtable2_size(const hashtable2* table);
size_t hashtable2_slots_used(const hashtable2* table);
bool hashtable2_verify(const hashtable2* table);
bool hashtable2_resize(hashtable2* table, unsigned size);

typedef struct hashtable2_itor hashtable2_itor;

hashtable2_itor* hashtable2_itor_new(hashtable2* table);
dict_itor* hashtable2_dict_itor_new(hashtable2* table);
void hashtable2_itor_free(hashtable2_itor* itor);

bool hashtable2_itor_valid(const hashtable2_itor* itor);
void hashtable2_itor_invalidate(hashtable2_itor* itor);
bool hashtable2_itor_next(hashtable2_itor* itor);
bool hashtable2_itor_prev(hashtable2_itor* itor);
bool hashtable2_itor_nextn(hashtable2_itor* itor, size_t count);
bool hashtable2_itor_prevn(hashtable2_itor* itor, size_t count);
bool hashtable2_itor_first(hashtable2_itor* itor);
bool hashtable2_itor_last(hashtable2_itor* itor);
bool hashtable2_itor_search(hashtable2_itor* itor, const void* key);
const void* hashtable2_itor_key(const hashtable2_itor* itor);
void** hashtable2_itor_data(hashtable2_itor* itor);
bool hashtable2_itor_remove(hashtable2_itor* itor);

END_DECL

#endif /* !_HASHTABLE2_H_ */

0 comments on commit b5a4327

Please sign in to comment.