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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
/*
* Simple (and reasonably efficient -- we don't use large hashes so it doesn't
* really matter that we fix the number of buckets) hash tables, locked to
* strings.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "main/curve.h"
#include "exception.h"
#include "demolib_prefs.h"
#ifndef __linux__
#define strcasecmp stricmp
#endif
#include "hashtable.h"
#if DEMOLIB_MAINLOOP
unsigned int Hashtable::string_hash(const char *str)
{
unsigned int hash = 0;
while (*str) {
char schar = *str++;
#if CASE_INSENSITIVE_HASHES
if (schar > 'a' && schar < 'z') schar ^= 32;
#endif
hash <<= 2;
hash ^= schar;
hash += hash >> 30;
}
return hash;
}
Hashtable::Hashtable()
{
for (int i = 0; i < NUM_BUCKETS; i++) {
this->buckets[i] = NULL;
}
}
/* all the linked lists will be in reverse order ;-) */
Hashtable::Hashtable(Hashtable *h)
{
for (int i = 0; i < NUM_BUCKETS; i++) {
buckets[i] = NULL;
struct linked_list *ptr =
(struct linked_list *)h->buckets[i];
while (ptr) {
struct linked_list *nptr =
(struct linked_list *)malloc(sizeof(struct linked_list));
nptr->key = strdup(ptr->key);
nptr->hash = ptr->hash;
nptr->obj = ptr->obj;
nptr->next = buckets[i];
buckets[i] = nptr;
ptr = ptr->next;
}
}
}
Hashtable::~Hashtable()
{
for (int i = 0; i < NUM_BUCKETS; i++) {
struct linked_list *ptr = buckets[i];
while (ptr) {
struct linked_list *next = ptr->next;
free(ptr->key);
free(ptr);
ptr = next;
}
buckets[i] = NULL;
}
}
void Hashtable::insert(const char * const key, void * const obj)
{
const unsigned int hash = string_hash(key);
const unsigned int partial_hash = hash & BUCKET_MASK;
this->remove(key);
/* insert in the beginning of the list */
struct linked_list *ptr =
(struct linked_list *)malloc(sizeof(struct linked_list));
ptr->key = strdup(key);
ptr->hash = hash;
ptr->obj = obj;
ptr->next = this->buckets[partial_hash];
this->buckets[partial_hash] = ptr;
}
void Hashtable::insert(const char * const key, char * const str)
{
this->insert(key, (void * const)str);
}
/* makes no error if it doesn't exist (make it return a bool?) */
void Hashtable::remove(const char * const key)
{
const unsigned int hash = string_hash(key);
const unsigned int partial_hash = hash & BUCKET_MASK;
struct linked_list *ptr = buckets[partial_hash];
struct linked_list *prev = NULL;
while (ptr) {
if (ptr->hash == hash &&
#if CASE_INSENSITIVE_HASHES
strcasecmp(ptr->key, key) == 0
#else
strcmp(ptr->key, key) == 0
#endif
) {
if (prev == NULL) {
buckets[partial_hash] = ptr->next;
} else {
prev->next = ptr->next;
}
free(ptr->key);
return;
}
prev = ptr;
ptr = ptr->next;
}
}
void *Hashtable::lookup(const char * const key) const
{
const unsigned int hash = string_hash(key);
const unsigned int partial_hash = hash & BUCKET_MASK;
struct linked_list *ptr = buckets[partial_hash];
while (ptr) {
if (ptr->hash == hash &&
#if CASE_INSENSITIVE_HASHES
strcasecmp(ptr->key, key) == 0
#else
strcmp(ptr->key, key) == 0
#endif
) {
return ptr->obj;
}
ptr = ptr->next;
}
return NULL;
}
bool Hashtable::exists(const char * const key) const
{
return (this->lookup(key) != NULL);
}
char *Hashtable::get_str(const char * const key) const
{
char *obj = (char *)(this->lookup(key));
if (obj == NULL)
throw new ValueNotSpecifiedException(key);
return obj;
}
/* might want to check this and get_float() for validity, but okay for now */
int Hashtable::get_int(const char * const key) const
{
const char * const obj = (const char * const)(this->lookup(key));
if (obj == NULL)
throw new ValueNotSpecifiedException(key);
return atoi(obj);
}
float Hashtable::get_float(const char * const key) const
{
const char * const obj = (const char * const)(this->lookup(key));
if (obj == NULL)
throw new ValueNotSpecifiedException(key);
return atof(obj);
}
bool Hashtable::get_bool(const char * const key) const
{
const char * const obj = (const char * const)(this->lookup(key));
if (obj == NULL)
throw new ValueNotSpecifiedException(key);
if (strcmp(obj, "yes") == 0 || strcmp(obj, "true") == 0 ||
strcmp(obj, "on") == 0) {
return true;
}
if (strcmp(obj, "no") == 0 || strcmp(obj, "false") == 0 ||
strcmp(obj, "off") == 0) {
return false;
}
throw new ValueNotSpecifiedException(key, "Illegal boolean value!");
}
/*
* free all objects in hash (not keys or the hash itself, use delete for those)
*/
void Hashtable::destroy_values()
{
for (int i = 0; i < NUM_BUCKETS; i++) {
struct linked_list *ptr = buckets[i];
while (ptr) {
free(ptr->obj);
ptr->obj = NULL;
ptr = ptr->next;
}
}
}
/* partly obsolete now that we have foreach()? */
void Hashtable::finish_curvedata(const float start, const float stop)
{
for (int i = 0; i < NUM_BUCKETS; i++) {
struct linked_list *ptr = buckets[i];
while (ptr) {
Curve *c = (Curve *)ptr->obj;
try {
c->end_curvepoints(start, stop - start);
} catch (FatalException *e) {
/* no such curve */
char buf[256];
sprintf(buf, "Curve `%s' missing!", ptr->key);
throw new FatalException(buf);
}
ptr = ptr->next;
}
}
}
void Hashtable::foreach(hash_callback callback, const void * const user_data)
{
for (int i = 0; i < NUM_BUCKETS; i++) {
struct linked_list *ptr = buckets[i];
while (ptr) {
struct linked_list *next = ptr->next;
(*callback)(ptr->key, ptr->obj, user_data);
ptr = next;
}
}
}
#endif
|