1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
/*
* chocolateboy 2009-02-25
*
* This is a customised version of the pointer table implementation in sv.c
*
* tsee 2009-11-03
*
* - Taken from chocolateboy's B-Hooks-OP-Annotation.
* - Added string-to-PTRV conversion using MurmurHash2.
* - Converted to storing I32s (Class::XSAccessor indexes of the key name storage)
* instead of OP structures (pointers).
* - Plenty of renaming and prefixing with CXSA_.
*/
#include "cxsa_hash_table.h"
#include "MurmurHashNeutral2.h"
#define CXSA_string_hash(str, len) CXSA_MurmurHashNeutral2(str, len, 12345678)
HashTable*
CXSA_HashTable_new(UV size, NV threshold) {
HashTable* table;
if ((size < 2) || (size & (size - 1))) {
croak("invalid hash table size: expected a power of 2 (>= 2), got %u", (unsigned)size);
}
if (!((threshold > 0) && (threshold < 1))) {
croak("invalid threshold: expected 0.0 < threshold < 1.0, got %f", threshold);
}
table = (HashTable*)cxa_zmalloc(sizeof(HashTable));
table->size = size;
table->threshold = threshold;
table->items = 0;
table->array = (HashTableEntry**)cxa_zmalloc(size * sizeof(HashTableEntry*));
return table;
}
HashTableEntry*
CXSA_HashTable_find(HashTable* table, const char* key, STRLEN len) {
HashTableEntry* entry;
UV index = CXSA_string_hash(key, len) & (table->size - 1);
for (entry = table->array[index]; entry; entry = entry->next) {
if (strcmp(entry->key, key) == 0)
break;
}
return entry;
}
/* currently unused */
/*
void *
CXSA_HashTable_delete(HashTable* table, const char* key, STRLEN len) {
HashTableEntry *entry, *prev = NULL;
UV index = CXSA_string_hash(key, len) & (table->size - 1);
void * retval = NULL;
for (entry = table->array[index]; entry; prev = entry, entry = entry->next) {
if (strcmp(entry->key, key) == 0) {
if (prev) {
prev->next = entry->next;
} else {
table->array[index] = entry->next;
}
--(table->items);
retval = entry->value;
Safefree(entry->key);
Safefree(entry);
break;
}
}
return retval;
}
*/
void *
CXSA_HashTable_fetch(HashTable* table, const char* key, STRLEN len) {
HashTableEntry const * const entry = CXSA_HashTable_find(table, key, len);
return entry ? entry->value : NULL;
}
void *
CXSA_HashTable_store(HashTable* table, const char* key, STRLEN len, void * value) {
void * retval = NULL;
HashTableEntry* entry = CXSA_HashTable_find(table, key, len);
if (entry) {
retval = entry->value;
entry->value = value;
} else {
const UV index = CXSA_string_hash(key, len) & (table->size - 1);
entry = (HashTableEntry*)cxa_malloc(sizeof(HashTableEntry));
entry->key = (char*)cxa_malloc( (len+1) );
cxa_memcpy((void*)entry->key, (void*)key, len+1);
/*Copy(key, entry->key, len+1, char);*/
entry->len = len;
entry->value = value;
entry->next = table->array[index];
table->array[index] = entry;
++(table->items);
if (((NV)table->items / (NV)table->size) > table->threshold)
CXSA_HashTable_grow(table);
}
return retval;
}
/* double the size of the array */
void
CXSA_HashTable_grow(HashTable* table) {
HashTableEntry** array = table->array;
const UV oldsize = table->size;
UV newsize = oldsize * 2;
UV i;
array = (HashTableEntry**)cxa_realloc((void*)array, sizeof(HashTableEntry*)*newsize);
cxa_memzero(&array[oldsize], (newsize-oldsize)*sizeof(HashTableEntry*));
/*Renew(array, newsize, HashTableEntry*);
Zero(&array[oldsize], newsize - oldsize, HashTableEntry*);
*/
table->size = newsize;
table->array = array;
for (i = 0; i < oldsize; ++i, ++array) {
HashTableEntry **current_entry_ptr, **entry_ptr, *entry;
if (!*array)
continue;
current_entry_ptr = array + oldsize;
for (entry_ptr = array, entry = *array; entry; entry = *entry_ptr) {
UV index = CXSA_string_hash(entry->key, entry->len) & (newsize - 1);
if (index != i) {
*entry_ptr = entry->next;
entry->next = *current_entry_ptr;
*current_entry_ptr = entry;
continue;
} else {
entry_ptr = &entry->next;
}
}
}
}
void
CXSA_HashTable_clear(HashTable *table, bool do_release_values) {
if (table && table->items) {
HashTableEntry** const array = table->array;
UV riter = table->size - 1;
do {
HashTableEntry* entry = array[riter];
while (entry) {
HashTableEntry* const temp = entry;
entry = entry->next;
if (temp->key)
cxa_free((void*)temp->key);
if (do_release_values) {
cxa_free((void*)temp->value);
}
cxa_free(temp);
}
/* chocolateboy 2008-01-08
*
* make sure we clear the array entry, so that subsequent probes fail
*/
array[riter] = NULL;
} while (riter--);
table->items = 0;
}
}
void
CXSA_HashTable_free(HashTable* table, bool do_release_values) {
if (table) {
CXSA_HashTable_clear(table, do_release_values);
cxa_free(table->array);
cxa_free(table);
}
}
|