From dc6a85a7cb26624bce5d1ef30537d3b340f2e22f Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Fri, 10 Feb 2012 23:01:27 +0100 Subject: [PATCH] Spatial: Deal gracefully with databases of spatials with too large radius This will allow easy experiments with finding the optimal MAX_PATTERN_DIST. --- patternprob.c | 22 +++++++++++++--------- patternsp.c | 8 ++++++++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/patternprob.c b/patternprob.c index 4e0d3c0..8496e54 100644 --- a/patternprob.c +++ b/patternprob.c @@ -61,16 +61,20 @@ pattern_pdict_init(char *filename, struct pattern_config *pc) pb->next = dict->table[spi]; dict->table[spi] = pb; - /* We rehash spatials in the order of loaded patterns. This way - * we make sure that the most popular patterns will be hashed - * last and therefore take priority. */ - if (!sphcachehit[spi]) { - sphcachehit[spi] = 1; - for (int r = 0; r < PTH__ROTATIONS; r++) - sphcache[spi][r] = spatial_hash(r, &pc->spat_dict->spatials[spi]); + /* Some spatials may not have been loaded if they correspond + * to a radius larger than supported. */ + if (pc->spat_dict->spatials[spi].dist > 0) { + /* We rehash spatials in the order of loaded patterns. This way + * we make sure that the most popular patterns will be hashed + * last and therefore take priority. */ + if (!sphcachehit[spi]) { + sphcachehit[spi] = 1; + for (unsigned int r = 0; r < PTH__ROTATIONS; r++) + sphcache[spi][r] = spatial_hash(r, &pc->spat_dict->spatials[spi]); + } + for (unsigned int r = 0; r < PTH__ROTATIONS; r++) + spatial_dict_addh(pc->spat_dict, sphcache[spi][r], spi); } - for (int r = 0; r < PTH__ROTATIONS; r++) - spatial_dict_addh(pc->spat_dict, sphcache[spi][r], spi); i++; } diff --git a/patternsp.c b/patternsp.c index 25182d0..b59956e 100644 --- a/patternsp.c +++ b/patternsp.c @@ -256,6 +256,14 @@ spatial_dict_read(struct spatial_dict *dict, char *buf, bool hash) radius = strtoul(bufp, &bufp, 10); while (isspace(*bufp)) bufp++; + if (radius > MAX_PATTERN_DIST) { + /* Too large spatial, skip. */ + struct spatial s = { .dist = 0 }; + unsigned int id = spatial_dict_addc(dict, &s); + assert(id == index); + return; + } + /* Load the stone configuration. */ struct spatial s = { .dist = radius }; unsigned int sl = 0; -- 2.11.4.GIT