Merge pull request #592 from ennorehling/develop

smaller optimizations
This commit is contained in:
Enno Rehling 2016-10-05 12:31:33 +02:00 committed by GitHub
commit f9486b6c13
3 changed files with 62 additions and 10 deletions

View File

@ -137,16 +137,6 @@ int NMRTimeout(void)
return config_get_int("nmr.timeout", 0);
}
race_t old_race(const struct race * rc)
{
race_t i;
// TODO: this sucks so bad!
for (i = 0; i != MAXRACES; ++i) {
if (get_race(i) == rc) return i;
}
return NORACE;
}
helpmode helpmodes[] = {
{ "all", HELP_ALL }
,

View File

@ -86,6 +86,50 @@ const struct race *get_race(race_t rt) {
return rc_find(name);
}
typedef struct xref {
race_t id;
const race *rc;
} rc_xref;
int cmp_xref(const void *a, const void *b)
{
const rc_xref *l = (const rc_xref *)a;
const rc_xref *r = (const rc_xref *)b;
if (l->rc<r->rc) return -1;
if (l->rc>r->rc) return 1;
return 0;
}
static rc_xref *xrefs;
race_t old_race(const struct race * rc)
{
static int cache;
int i, l, r;
if (rc_changed(&cache)) {
if (!xrefs) {
xrefs = malloc(sizeof(rc_xref) * MAXRACES);
}
for (i = 0; i != MAXRACES; ++i) {
xrefs[i].rc = get_race(i);
xrefs[i].id = (race_t)i;
}
qsort(xrefs, MAXRACES, sizeof(rc_xref), cmp_xref);
}
l=0; r=MAXRACES-1;
while (l<=r) {
int m = (l+r)/2;
if (rc<xrefs[m].rc) {
r = m-1;
} else if (rc>xrefs[m].rc) {
l = m+1;
} else {
return (race_t)xrefs[m].id;
}
}
return NORACE;
}
race_list *get_familiarraces(void)
{
static int init = 0;
@ -126,6 +170,8 @@ void free_races(void) {
while (races) {
race * rc = races->next;
free_params(&races->parameters);
free(xrefs);
xrefs = 0;
free(races->_name);
free(races->def_damage);
free(races);

View File

@ -66,10 +66,26 @@ static void test_race_get(CuTest *tc) {
test_cleanup();
}
static void test_old_race(CuTest *tc)
{
race * rc1, *rc2;
test_setup();
test_create_race("dwarf");
rc1 = test_create_race("elf");
rc2 = test_create_race("onkel");
CuAssertIntEquals(tc, RC_ELF, old_race(rc1));
CuAssertIntEquals(tc, NORACE, old_race(rc2));
rc2 = test_create_race("human");
CuAssertIntEquals(tc, RC_ELF, old_race(rc1));
CuAssertIntEquals(tc, RC_HUMAN, old_race(rc2));
test_cleanup();
}
CuSuite *get_race_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_race_get);
SUITE_ADD_TEST(suite, test_old_race);
SUITE_ADD_TEST(suite, test_rc_name);
SUITE_ADD_TEST(suite, test_rc_defaults);
SUITE_ADD_TEST(suite, test_rc_find);