From 2a57df6be92bd119d80361ec2176856ca7fee4da Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Sun, 11 Aug 2024 17:26:52 -0400 Subject: [PATCH] Consolidate duplicate implementations of `compare_integers` Both Inertia and Twiddle previously included static implementations of this exact same function, which was passed to `qsort()` as a comparator. With this change, a single global implementation is provided in misc.c, which will hopefully reduce code duplication going forward. I'm refactoring this in preparation for the upcoming fallback polygon fill function, which I'm about to add. --- devel.but | 10 ++++++++++ inertia.c | 12 ------------ misc.c | 11 +++++++++++ puzzles.h | 3 +++ twiddle.c | 14 +------------- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/devel.but b/devel.but index c201a3e..fd16151 100644 --- a/devel.but +++ b/devel.but @@ -5337,6 +5337,16 @@ equivalent to a bitwise-AND with \cw{~MOD_MASK}. Swap two regions of memory of \cw{size} bytes. The two regions must not overlap. +\S{utils-compare-integers} \cw{compare_integers()} + +\c int compare_integers(const void *av, const void *bv); + +Compare the ints pointed to by \c{av} and \c{bv}. It returns an +integer less than, equal to, or greater than zero if the first +argument is respectively less than, equal to, or greater than the +second. This function is intended to be passed to \c{qsort()} for +sorting ints in ascending order. + \C{writing} How to write a new puzzle This chapter gives a guide to how to actually write a new puzzle: diff --git a/inertia.c b/inertia.c index d120411..fe174f3 100644 --- a/inertia.c +++ b/inertia.c @@ -722,18 +722,6 @@ static int move_goes_to(int w, int h, char *grid, int x, int y, int d) return (y*w+x)*DP1+dr; } -static int compare_integers(const void *av, const void *bv) -{ - const int *a = (const int *)av; - const int *b = (const int *)bv; - if (*a < *b) - return -1; - else if (*a > *b) - return +1; - else - return 0; -} - static char *solve_game(const game_state *state, const game_state *currstate, const char *aux, const char **error) { diff --git a/misc.c b/misc.c index f9eaf5d..8e96d67 100644 --- a/misc.c +++ b/misc.c @@ -351,6 +351,17 @@ void draw_rect_corners(drawing *dr, int cx, int cy, int r, int col) draw_line(dr, cx + r, cy + r, cx + r/2, cy + r, col); } +int compare_integers(const void *av, const void *bv) { + const int *a = (const int *)av; + const int *b = (const int *)bv; + if (*a < *b) + return -1; + else if (*a > *b) + return +1; + else + return 0; +} + char *move_cursor(int button, int *x, int *y, int maxw, int maxh, bool wrap, bool *visible) { diff --git a/puzzles.h b/puzzles.h index 2c52bc6..f1022ea 100644 --- a/puzzles.h +++ b/puzzles.h @@ -454,6 +454,9 @@ char *button2label(int button); * standard per clause 7.26.11.1.) */ void swap_regions(void *av, void *bv, size_t size); +/* comparator for sorting ints with qsort() */ +int compare_integers(const void *av, const void *bv); + /* * dsf.c */ diff --git a/twiddle.c b/twiddle.c index b3aa06f..49d8db7 100644 --- a/twiddle.c +++ b/twiddle.c @@ -532,18 +532,6 @@ static void free_game(game_state *state) sfree(state); } -static int compare_int(const void *av, const void *bv) -{ - const int *a = (const int *)av; - const int *b = (const int *)bv; - if (*a < *b) - return -1; - else if (*a > *b) - return +1; - else - return 0; -} - static char *solve_game(const game_state *state, const game_state *currstate, const char *aux, const char **error) { @@ -758,7 +746,7 @@ static game_state *execute_move(const game_state *from, const char *move) * conveniently being able to get hold of a clean state from * which to practise manoeuvres. */ - qsort(ret->grid, ret->w*ret->h, sizeof(int), compare_int); + qsort(ret->grid, ret->w*ret->h, sizeof(int), compare_integers); for (i = 0; i < ret->w*ret->h; i++) ret->grid[i] &= ~3; ret->used_solve = true;