slightly faster lookup for equipment, probably.

This commit is contained in:
Enno Rehling 2017-09-08 22:24:17 +02:00
parent e7fe3f8292
commit b9b3f0322e
2 changed files with 55 additions and 54 deletions

View File

@ -29,6 +29,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* util includes */ /* util includes */
#include <selist.h> #include <selist.h>
#include <critbit.h>
#include <util/rand.h> #include <util/rand.h>
#include <util/rng.h> #include <util/rng.h>
@ -37,41 +38,35 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
static equipment *equipment_sets; static critbit_tree cb_equipments = { 0 };
equipment *get_or_create_equipment(const char *eqname)
{
equipment **eqp = &equipment_sets;
for (;;) {
struct equipment *eq = *eqp;
int i = eq ? strcmp(eq->name, eqname) : 1;
if (i > 0) {
eq = (equipment *)calloc(1, sizeof(equipment));
eq->name = strdup(eqname);
eq->next = *eqp;
memset(eq->skills, 0, sizeof(eq->skills));
*eqp = eq;
break;
}
else if (i == 0) {
break;
}
eqp = &eq->next;
}
return *eqp;
}
equipment *get_equipment(const char *eqname) equipment *get_equipment(const char *eqname)
{ {
equipment *eq = equipment_sets; const char *match;
for (; eq; eq = eq->next) { equipment *eq = NULL;
int i = strcmp(eq->name, eqname);
if (i == 0) match = cb_find_str(&cb_equipments, eqname);
return eq; if (match) {
else if (i > 0) cb_get_kv(match, &eq, sizeof(eq));
break;
} }
return NULL; return eq;
}
equipment *get_or_create_equipment(const char *eqname)
{
equipment *eq = get_equipment(eqname);
if (!eq) {
size_t len;
char data[64];
eq = (equipment *)calloc(1, sizeof(equipment));
eq->name = strdup(eqname);
len = cb_new_kv(eqname, strlen(eqname), &eq, sizeof(eq), data);
assert(len <= sizeof(data));
cb_insert(&cb_equipments, data, len);
}
return eq;
} }
void equipment_setskill(equipment * eq, skill_t sk, const char *value) void equipment_setskill(equipment * eq, skill_t sk, const char *value)
@ -236,12 +231,8 @@ void free_ls(void *arg) {
free(ls); free(ls);
} }
void equipment_done(void) { static void free_equipment(equipment *eq) {
equipment **eqp = &equipment_sets;
while (*eqp) {
int i; int i;
equipment *eq = *eqp;
*eqp = eq->next;
free(eq->name); free(eq->name);
if (eq->spells) { if (eq->spells) {
selist_foreach(eq->spells, free_ls); selist_foreach(eq->spells, free_ls);
@ -254,9 +245,20 @@ void equipment_done(void) {
eq->items = next; eq->items = next;
} }
/* TODO: subsets, skills */ /* TODO: subsets, skills */
for (i=0;i!=MAXSKILLS;++i) { for (i = 0; i != MAXSKILLS; ++i) {
free(eq->skills[i]); free(eq->skills[i]);
} }
free(eq); }
}
static int free_equipment_cb(const void * match, const void * key, size_t keylen, void *cbdata) {
equipment *eq;
cb_get_kv(match, &eq, sizeof(eq));
free_equipment(eq);
free(eq);
return 0;
}
void equipment_done(void) {
cb_foreach(&cb_equipments, "", 0, free_equipment_cb, 0);
cb_clear(&cb_equipments);
} }

View File

@ -50,7 +50,6 @@ extern "C" {
char *skills[MAXSKILLS]; char *skills[MAXSKILLS];
struct selist *spells; struct selist *spells;
struct subset *subsets; struct subset *subsets;
struct equipment *next;
void(*callback) (const struct equipment *, struct unit *); void(*callback) (const struct equipment *, struct unit *);
} equipment; } equipment;