version 1.1.1.1, 2012/02/17 15:09:30
|
version 1.1.1.2, 2013/10/14 07:51:14
|
Line 1
|
Line 1
|
/* |
/* |
* Routines to provide a memory-efficient hashtable. |
* Routines to provide a memory-efficient hashtable. |
* |
* |
* Copyright (C) 2007-2009 Wayne Davison | * Copyright (C) 2007-2013 Wayne Davison |
* |
* |
* This program is free software; you can redistribute it and/or modify |
* 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 |
* it under the terms of the GNU General Public License as published by |
Line 23
|
Line 23
|
|
|
struct hashtable *hashtable_create(int size, int key64) |
struct hashtable *hashtable_create(int size, int key64) |
{ |
{ |
|
int req = size; |
struct hashtable *tbl; |
struct hashtable *tbl; |
int node_size = key64 ? sizeof (struct ht_int64_node) |
int node_size = key64 ? sizeof (struct ht_int64_node) |
: sizeof (struct ht_int32_node); |
: sizeof (struct ht_int32_node); |
|
|
/* Pick a power of 2 that can hold the requested size. */ |
/* Pick a power of 2 that can hold the requested size. */ |
if (size & (size-1) || size < 16) { |
if (size & (size-1) || size < 16) { |
int req = size; |
|
size = 16; |
size = 16; |
while (size < req) |
while (size < req) |
size *= 2; |
size *= 2; |
Line 43 struct hashtable *hashtable_create(int size, int key64
|
Line 43 struct hashtable *hashtable_create(int size, int key64
|
tbl->node_size = node_size; |
tbl->node_size = node_size; |
tbl->key64 = key64 ? 1 : 0; |
tbl->key64 = key64 ? 1 : 0; |
|
|
|
if (DEBUG_GTE(HASH, 1)) { |
|
char buf[32]; |
|
if (req != size) |
|
snprintf(buf, sizeof buf, "req: %d, ", req); |
|
else |
|
*buf = '\0'; |
|
rprintf(FINFO, "[%s] created hashtable %lx (%ssize: %d, keys: %d-bit)\n", |
|
who_am_i(), (long)tbl, buf, size, key64 ? 64 : 32); |
|
} |
|
|
return tbl; |
return tbl; |
} |
} |
|
|
void hashtable_destroy(struct hashtable *tbl) |
void hashtable_destroy(struct hashtable *tbl) |
{ |
{ |
|
if (DEBUG_GTE(HASH, 1)) { |
|
rprintf(FINFO, "[%s] destroyed hashtable %lx (size: %d, keys: %d-bit)\n", |
|
who_am_i(), (long)tbl, tbl->size, tbl->key64 ? 64 : 32); |
|
} |
free(tbl->nodes); |
free(tbl->nodes); |
free(tbl); |
free(tbl); |
} |
} |
Line 74 void *hashtable_find(struct hashtable *tbl, int64 key,
|
Line 88 void *hashtable_find(struct hashtable *tbl, int64 key,
|
out_of_memory("hashtable_node"); |
out_of_memory("hashtable_node"); |
tbl->size = size; |
tbl->size = size; |
tbl->entries = 0; |
tbl->entries = 0; |
|
|
|
if (DEBUG_GTE(HASH, 1)) { |
|
rprintf(FINFO, "[%s] growing hashtable %lx (size: %d, keys: %d-bit)\n", |
|
who_am_i(), (long)tbl, size, key64 ? 64 : 32); |
|
} |
|
|
for (i = size / 2; i-- > 0; ) { |
for (i = size / 2; i-- > 0; ) { |
struct ht_int32_node *move_node = HT_NODE(tbl, old_nodes, i); |
struct ht_int32_node *move_node = HT_NODE(tbl, old_nodes, i); |