From c521ccf993106289a36cd2b598059bc4ed340039 Mon Sep 17 00:00:00 2001 From: Lee Salzman Date: Fri, 12 Sep 2008 09:17:33 +0000 Subject: [PATCH] hashtable cleanup git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@2544 e016b1c5-ed68-44aa-a2f7-31cac88cffde --- source/src/command.cpp | 12 ++++++------ source/src/rendermodel.cpp | 2 +- source/src/tools.h | 30 +++++++++++++----------------- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/source/src/command.cpp b/source/src/command.cpp index 7c6cbf91..7eaf6ec1 100644 --- a/source/src/command.cpp +++ b/source/src/command.cpp @@ -55,7 +55,7 @@ ident *newident(const char *name) if(!id) { ident init(ID_ALIAS, newstring(name), newstring(""), persistidents, IEXC_CORE); - id = idents->access(init.name, &init); + id = &idents->access(init.name, init); } return id; } @@ -85,7 +85,7 @@ void alias(const char *name, const char *action) if(!b) { ident b(ID_ALIAS, newstring(name), newstring(action), persistidents, execcontext); - idents->access(b.name, &b); + idents->access(b.name, b); } else if(b->type==ID_ALIAS) { @@ -104,7 +104,7 @@ int variable(const char *name, int minval, int cur, int maxval, int *storage, vo { if(!idents) idents = new hashtable; ident v(ID_VAR, name, minval, maxval, storage, fun, persist, IEXC_CORE); - idents->access(name, &v); + idents->access(name, v); return cur; } @@ -112,7 +112,7 @@ float fvariable(const char *name, float cur, float *storage, void (*fun)(), bool { if(!idents) idents = new hashtable; ident v(ID_FVAR, name, storage, fun, persist, IEXC_CORE); - idents->access(name, &v); + idents->access(name, v); return cur; } @@ -120,7 +120,7 @@ char *svariable(const char *name, const char *cur, char **storage, void (*fun)() { if(!idents) idents = new hashtable; ident v(ID_SVAR, name, storage, fun, persist, IEXC_CORE); - idents->access(name, &v); + idents->access(name, v); return newstring(cur); } @@ -151,7 +151,7 @@ bool addcommand(const char *name, void (*fun)(), int narg) { if(!idents) idents = new hashtable; ident c(ID_COMMAND, name, fun, narg, IEXC_CORE); - idents->access(name, &c); + idents->access(name, c); return false; } diff --git a/source/src/rendermodel.cpp b/source/src/rendermodel.cpp index bc9660ca..ba634fc1 100644 --- a/source/src/rendermodel.cpp +++ b/source/src/rendermodel.cpp @@ -107,7 +107,7 @@ model *loadmodel(const char *name, int i) } } loadingmodel = NULL; - mdllookup.access(m->name(), &m); + mdllookup.access(m->name(), m); } if(mapmodels.inrange(i) && !mapmodels[i].m) mapmodels[i].m = m; return m; diff --git a/source/src/tools.h b/source/src/tools.h index 9c697166..cbce2142 100644 --- a/source/src/tools.h +++ b/source/src/tools.h @@ -430,35 +430,31 @@ template struct hashtable return c; } - chain *find(const K &key, bool doinsert) - { - uint h = hthash(key)&(size-1); - for(chain *c = table[h]; c; c = c->next) - { - if(htcmp(key, c->key)) return c; - } - if(doinsert) return insert(key, h); - return NULL; - } + #define HTFIND(success, fail) \ + uint h = hthash(key)&(size-1); \ + for(chain *c = table[h]; c; c = c->next) \ + { \ + if(htcmp(key, c->key)) return (success); \ + } \ + return (fail); - T *access(const K &key, const T *data = NULL) + T *access(const K &key) { - chain *c = find(key, data != NULL); - if(data) c->data = *data; - if(c) return &c->data; - return NULL; + HTFIND(&c->data, NULL); } T &access(const K &key, const T &data) { - return *access(key, &data); + HTFIND(c->data, insert(key, h)->data = data); } T &operator[](const K &key) { - return find(key, true)->data; + HTFIND(c->data, insert(key, h)->data); } + #undef HTFIND + bool remove(const K &key) { uint h = hthash(key)&(size-1);