From 79a8e30ea5800dc8606bc83d29b68918ade8831d Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Fri, 10 Feb 2012 19:17:37 +0100 Subject: [PATCH] Patterns: Use unsigned where appropriate --- pattern.c | 6 +++--- pattern.h | 4 ++-- patternscan/patternscan.c | 6 +++--- patternsp.c | 42 +++++++++++++++++++++--------------------- patternsp.h | 14 +++++++------- 5 files changed, 36 insertions(+), 36 deletions(-) diff --git a/pattern.c b/pattern.c index 12f6d77..45712a0 100644 --- a/pattern.c +++ b/pattern.c @@ -405,10 +405,10 @@ pattern_match_spatial_outer(struct pattern_config *pc, pattern_spec ps, static enum stone bt_white[4] = { S_NONE, S_WHITE, S_BLACK, S_OFFBOARD }; enum stone (*bt)[4] = m->color == S_WHITE ? &bt_white : &bt_black; - for (int d = BOARD_SPATHASH_MAXD + 1; d <= pc->spat_max; d++) { + for (unsigned int d = BOARD_SPATHASH_MAXD + 1; d <= pc->spat_max; d++) { /* Recompute missing outer circles: * Go through all points in given distance. */ - for (int j = ptind[d]; j < ptind[d + 1]; j++) { + for (unsigned int j = ptind[d]; j < ptind[d + 1]; j++) { ptcoords_at(x, y, m->coord, b, j); h ^= pthashes[0][j][(*bt)[board_atxy(b, x, y)]]; } @@ -515,7 +515,7 @@ pattern_match(struct pattern_config *pc, pattern_spec ps, } if (PS_ANY(BORDER)) { - int bdist = coord_edge_distance(m->coord, b); + unsigned int bdist = coord_edge_distance(m->coord, b); if (bdist <= pc->bdist_max) { f->id = FEAT_BORDER; f->payload = bdist; diff --git a/pattern.h b/pattern.h index 661afc3..ebde884 100644 --- a/pattern.h +++ b/pattern.h @@ -104,12 +104,12 @@ struct pattern { struct spatial_dict; struct pattern_config { /* FEAT_BORDER: Generate features only up to this board distance. */ - int bdist_max; + unsigned int bdist_max; /* FEAT_SPATIAL: Generate patterns only for these sizes (gridcular). * TODO: Special-case high values to match larger areas or the * whole board. */ - int spat_min, spat_max; + unsigned int spat_min, spat_max; /* Produce only a single spatial feature per pattern, corresponding * to the largest matched spatial pattern. */ bool spat_largest; diff --git a/patternscan/patternscan.c b/patternscan/patternscan.c index c750f39..db837b5 100644 --- a/patternscan/patternscan.c +++ b/patternscan/patternscan.c @@ -46,7 +46,7 @@ struct patternscan { /* Book-keeping of spatial occurence count. */ int gameno; - int nscounts; + unsigned int nscounts; int *scounts; int *sgameno; }; @@ -63,7 +63,7 @@ process_pattern(struct patternscan *ps, struct board *b, struct move *m, char ** int dmax = s.dist; for (int d = ps->pat.pc.spat_min; d <= dmax; d++) { s.dist = d; - int sid = spatial_dict_put(ps->pat.pc.spat_dict, &s, spatial_hash(0, &s)); + unsigned int sid = spatial_dict_put(ps->pat.pc.spat_dict, &s, spatial_hash(0, &s)); assert(sid > 0); #define SCOUNTS_ALLOC 1048576 // Allocate space in 1M*4 blocks. if (sid >= ps->nscounts) { @@ -189,7 +189,7 @@ patternscan_done(struct engine *e) if (newfile) spatial_dict_writeinfo(ps->pat.pc.spat_dict, f); - for (int i = ps->loaded_spatials; i < ps->pat.pc.spat_dict->nspatials; i++) { + for (unsigned int i = ps->loaded_spatials; i < ps->pat.pc.spat_dict->nspatials; i++) { /* By default, threshold is 0 and condition is always true. */ assert(i < ps->nscounts && ps->scounts[i] > 0); if (ps->scounts[i] >= ps->spat_threshold) diff --git a/patternsp.c b/patternsp.c index 80e3971..25182d0 100644 --- a/patternsp.c +++ b/patternsp.c @@ -19,7 +19,7 @@ struct ptcoord ptcoords[MAX_PATTERN_AREA]; /* For each radius, starting index in ptcoords[]. */ -int ptind[MAX_PATTERN_DIST + 2]; +unsigned int ptind[MAX_PATTERN_DIST + 2]; /* ptcoords[], ptind[] setup */ static void @@ -141,10 +141,10 @@ spatial_init(void) } inline hash_t -spatial_hash(int rotation, struct spatial *s) +spatial_hash(unsigned int rotation, struct spatial *s) { hash_t h = 0; - for (int i = 0; i < ptind[s->dist + 1]; i++) { + for (unsigned int i = 0; i < ptind[s->dist + 1]; i++) { h ^= pthashes[rotation][i][spatial_point_at(*s, i)]; } return h & spatial_hash_mask; @@ -154,7 +154,7 @@ char * spatial2str(struct spatial *s) { static char buf[1024]; - for (int i = 0; i < ptind[s->dist + 1]; i++) { + for (unsigned int i = 0; i < ptind[s->dist + 1]; i++) { buf[i] = stone2char(spatial_point_at(*s, i)); } buf[ptind[s->dist + 1]] = 0; @@ -174,7 +174,7 @@ spatial_from_board(struct pattern_config *pc, struct spatial *s, enum stone (*bt)[4] = m->color == S_WHITE ? &bt_white : &bt_black; memset(s, 0, sizeof(*s)); - for (int j = 0; j < ptind[pc->spat_max + 1]; j++) { + for (unsigned int j = 0; j < ptind[pc->spat_max + 1]; j++) { ptcoords_at(x, y, m->coord, b, j); s->points[j / 4] |= (*bt)[board_atxy(b, x, y)] << ((j % 4) * 2); } @@ -194,11 +194,11 @@ spatial_cmp(struct spatial *s1, struct spatial *s2) * foolproof to just check if the sets of rotation hashes are the * same for both. */ hash_t s1r[PTH__ROTATIONS]; - for (int r = 0; r < PTH__ROTATIONS; r++) + for (unsigned int r = 0; r < PTH__ROTATIONS; r++) s1r[r] = spatial_hash(r, s1); - for (int r = 0; r < PTH__ROTATIONS; r++) { + for (unsigned int r = 0; r < PTH__ROTATIONS; r++) { hash_t s2r = spatial_hash(r, s2); - for (int p = 0; p < PTH__ROTATIONS; p++) + for (unsigned int p = 0; p < PTH__ROTATIONS; p++) if (s2r == s1r[p]) goto found_rot; /* Rotation hash s2r does not correspond to s1r. */ @@ -258,7 +258,7 @@ spatial_dict_read(struct spatial_dict *dict, char *buf, bool hash) /* Load the stone configuration. */ struct spatial s = { .dist = radius }; - int sl = 0; + unsigned int sl = 0; while (!isspace(*bufp)) { s.points[sl / 4] |= char2stone(*bufp++) << ((sl % 4)*2); sl++; @@ -278,18 +278,18 @@ spatial_dict_read(struct spatial_dict *dict, char *buf, bool hash) /* Add to specified hash places. */ if (hash) - for (int r = 0; r < PTH__ROTATIONS; r++) + for (unsigned int r = 0; r < PTH__ROTATIONS; r++) spatial_dict_addh(dict, spatial_hash(r, &s), id); } void -spatial_write(struct spatial_dict *dict, struct spatial *s, int id, FILE *f) +spatial_write(struct spatial_dict *dict, struct spatial *s, unsigned int id, FILE *f) { fprintf(f, "%d %d ", id, s->dist); fputs(spatial2str(s), f); - for (int r = 0; r < PTH__ROTATIONS; r++) { + for (unsigned int r = 0; r < PTH__ROTATIONS; r++) { hash_t rhash = spatial_hash(r, s); - int id2 = dict->hash[rhash]; + unsigned int id2 = dict->hash[rhash]; if (id2 != id) { /* This hash does not belong to us. Decide whether * we or the current owner is better owner. */ @@ -336,9 +336,9 @@ spatial_dict_writeinfo(struct spatial_dict *dict, FILE *f) * of external tools, Pachi never interprets it itself. */ fprintf(f, "# Pachi spatial patterns dictionary v1.0 maxdist %d\n", MAX_PATTERN_DIST); - for (int d = 0; d <= MAX_PATTERN_DIST; d++) { + for (unsigned int d = 0; d <= MAX_PATTERN_DIST; d++) { fprintf(f, "# Point order: d=%d ", d); - for (int j = ptind[d]; j < ptind[d + 1]; j++) { + for (unsigned int j = ptind[d]; j < ptind[d + 1]; j++) { fprintf(f, "%d,%d ", ptcoords[j].x, ptcoords[j].y); } fprintf(f, "\n"); @@ -381,12 +381,12 @@ spatial_dict_init(bool will_append, bool hash) return dict; } -int +unsigned int spatial_dict_put(struct spatial_dict *dict, struct spatial *s, hash_t h) { /* We avoid spatial_dict_get() here, since we want to ignore radius * differences - we have custom collision detection. */ - int id = dict->hash[h]; + unsigned int id = dict->hash[h]; if (id > 0) { /* Is this the same or isomorphous spatial? */ if (spatial_cmp(s, &dict->spatials[id])) @@ -394,9 +394,9 @@ spatial_dict_put(struct spatial_dict *dict, struct spatial *s, hash_t h) /* Look a bit harder - perhaps one of our rotations still * points at the correct spatial. */ - for (int r = 0; r < PTH__ROTATIONS; r++) { + for (unsigned int r = 0; r < PTH__ROTATIONS; r++) { hash_t rhash = spatial_hash(r, s); - int rid = dict->hash[rhash]; + unsigned int rid = dict->hash[rhash]; /* No match means we definitely aren't stored yet. */ if (!rid) break; @@ -420,14 +420,14 @@ spatial_dict_put(struct spatial_dict *dict, struct spatial *s, hash_t h) id = spatial_dict_addc(dict, s); if (DEBUGL(4)) { fprintf(stderr, "new spat %d(%d) %s <%"PRIhash"> ", id, s->dist, spatial2str(s), h); - for (int r = 0; r < 8; r++) + for (unsigned int r = 0; r < 8; r++) fprintf(stderr,"[%"PRIhash"] ", spatial_hash(r, s)); fprintf(stderr, "\n"); } /* Store new pattern in the hash. */ hash_store: - for (int r = 0; r < PTH__ROTATIONS; r++) + for (unsigned int r = 0; r < PTH__ROTATIONS; r++) spatial_dict_addh(dict, spatial_hash(r, s), id); return id; diff --git a/patternsp.h b/patternsp.h index 944d8d9..6ed1201 100644 --- a/patternsp.h +++ b/patternsp.h @@ -52,12 +52,12 @@ struct spatial { /* Gridcular radius of matched pattern. */ - char dist; + unsigned char dist; /* The points; each point is two bits, corresponding * to {enum stone}. Points are ordered in gridcular-defined * spiral from middle to the edge; the dictionary file has * a comment describing the ordering at the top. */ - char points[MAX_PATTERN_AREA / 4]; + unsigned char points[MAX_PATTERN_AREA / 4]; #define spatial_point_at(s, i) (((s).points[(i) / 4] >> (((i) % 4) * 2)) & 3) }; @@ -67,7 +67,7 @@ struct pattern_config; void spatial_from_board(struct pattern_config *pc, struct spatial *s, struct board *b, struct move *m); /* Compute hash of given spatial pattern. */ -hash_t spatial_hash(int rotation, struct spatial *s); +hash_t spatial_hash(unsigned int rotation, struct spatial *s); /* Convert given spatial pattern to string. */ char *spatial2str(struct spatial *s); @@ -76,7 +76,7 @@ char *spatial2str(struct spatial *s); * coordinates relative to pattern center). */ struct ptcoord { short x, y; } ptcoords[MAX_PATTERN_AREA]; /* For each radius, starting index in ptcoords[]. */ -int ptind[MAX_PATTERN_DIST + 2]; +unsigned int ptind[MAX_PATTERN_DIST + 2]; /* Zobrist hashes used for points in patterns. */ #define PTH__ROTATIONS 8 @@ -93,7 +93,7 @@ hash_t pthashes[PTH__ROTATIONS][MAX_PATTERN_AREA][S_MAX]; /* Two ways of lookup: (i) by index (ii) by hash of the configuration. */ struct spatial_dict { /* Indexed base store */ - int nspatials; /* Number of records. */ + unsigned int nspatials; /* Number of records. */ struct spatial *spatials; /* Actual records. */ /* Hashed access; all isomorphous configurations @@ -121,7 +121,7 @@ static unsigned int spatial_dict_get(struct spatial_dict *dict, int dist, hash_t /* Store specified spatial pattern in the dictionary if it is not known yet. * Returns pattern id. Note that the pattern is NOT written to the underlying * file automatically. */ -int spatial_dict_put(struct spatial_dict *dict, struct spatial *s, hash_t); +unsigned int spatial_dict_put(struct spatial_dict *dict, struct spatial *s, hash_t); /* Readds given rotation of given pattern to the hash. This is useful only * if you want to tweak hash priority of various patterns. */ @@ -144,7 +144,7 @@ extern const char *spatial_dict_filename; void spatial_dict_writeinfo(struct spatial_dict *dict, FILE *f); /* Append specified spatial pattern to the given file. */ -void spatial_write(struct spatial_dict *dict, struct spatial *s, int id, FILE *f); +void spatial_write(struct spatial_dict *dict, struct spatial *s, unsigned int id, FILE *f); static inline unsigned int -- 2.11.4.GIT