From ec178e92e2453e6aafc099b2b036a079e2428c5a Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Wed, 17 Sep 2008 19:19:56 +0200 Subject: [PATCH] Board: Iterate only within bounds of the lib[] array using new gi_libs_bound() --- board.c | 12 ++++++------ board.h | 1 + 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/board.c b/board.c index 22a783d..9ac453a 100644 --- a/board.c +++ b/board.c @@ -295,7 +295,7 @@ check_libs_consistency(struct board *board, group_t g) #ifdef DEBUG if (!g) return; struct group *gi = &board_group_info(board, g); - for (int i = 0; i < gi->libs; i++) + for (int i = 0; i < gi_libs_bound(*gi); i++) if (board_at(board, gi->lib[i]) != S_NONE) { fprintf(stderr, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi->lib[i], board), g, coord2sstr(g, board)); assert(0); @@ -340,7 +340,7 @@ board_group_addlib(struct board *board, group_t group, coord_t coord) struct group *gi = &board_group_info(board, group); if (gi->libs < GROUP_KEEP_LIBS) { - for (int i = 0; i < gi->libs; i++) + for (int i = 0; i < gi_libs_bound(*gi); i++) if (gi->lib[i] == coord) return; if (gi->libs == 0) @@ -360,9 +360,9 @@ board_group_rmlib(struct board *board, group_t group, coord_t coord) } struct group *gi = &board_group_info(board, group); - for (int i = 0; i < gi->libs; i++) { - if (gi->lib[i] == coord) { - for (i++; i < gi->libs; i++) + for (int i = 0; i < gi_libs_bound(*gi); i++) { + if (unlikely(gi->lib[i] == coord)) { + for (i++; i < gi_libs_bound(*gi); i++) gi->lib[i - 1] = gi->lib[i]; gi->libs--; @@ -557,7 +557,7 @@ board_play_outside(struct board *board, struct move *m, int f) if (DEBUGL(8)) { struct group *gi = &board_group_info(board, ngroup); fprintf(stderr, "testing captured group %d[%s]: ", ngroup, coord2sstr(ngroup, board)); - for (int i = 0; i < gi->libs; i++) + for (int i = 0; i < gi_libs_bound(*gi); i++) fprintf(stderr, "%s ", coord2sstr(gi->lib[i], board)); fprintf(stderr, "\n"); } diff --git a/board.h b/board.h index fe6c812..81dead8 100644 --- a/board.h +++ b/board.h @@ -124,6 +124,7 @@ struct board { #define board_group_info(b_, g_) ((b_)->gi[(g_)]) #define board_group_captured(b_, g_) (board_group_info(b_, g_).libs == 0) +#define gi_libs_bound(g_) ((g_).libs > GROUP_KEEP_LIBS ? GROUP_KEEP_LIBS : (g_).libs) #define hash_at(b_, coord, color) (b_)->h[((color) == S_BLACK ? board_size2(b_) : 0) + coord_raw(coord)] -- 2.11.4.GIT