From dd1d4771adb3583403e0fe90e09eaa79a9ff2e88 Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Fri, 20 Apr 2012 13:57:22 +0200 Subject: hst: Calculate cell position instead of caching Its easy to calculate the index from pointer offset, so we can avoid an initialization loop. --- hst.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'hst.c') diff --git a/hst.c b/hst.c index 63516c7..e6628b1 100644 --- a/hst.c +++ b/hst.c @@ -1,5 +1,6 @@ #include #include +#include #ifndef INTERVAL #define INTERVAL 8 @@ -7,7 +8,6 @@ #define BLOCKS (256 / INTERVAL) struct cell { - int i, j, k; int count; struct cell *max; }; @@ -37,14 +37,25 @@ compare_cell(gconstpointer a, gconstpointer b) return cell1->count - cell2->count; } +static void +calculate_index(struct hst *hst, struct cell *cell, int *i, int *j, int *k) +{ + size_t offset = cell - &hst->cells[0][0][0]; + + *i = offset / (BLOCKS*BLOCKS); + *j = (offset - *i * (BLOCKS*BLOCKS)) / BLOCKS; + *k = (offset - *i * (BLOCKS*BLOCKS) - *j * BLOCKS); +} + static void add_neighbors(struct hst *hst, struct cell *cell) { - int i, j, k; + int i, j, k, ci, cj, ck; - for (i = MAX(cell->i - 1, 0); i < MIN(cell->i + 1, BLOCKS); ++i) { - for (j = MAX(cell->j - 1, 0); j < MIN(cell->j + 1, BLOCKS); ++j) { - for (k = MAX(cell->k - 1, 0); k < MIN(cell->k + 1, BLOCKS); ++k) { + calculate_index(hst, cell, &ci, &cj, &ck); + for (i = MAX(ci - 1, 0); i < MIN(ci + 1, BLOCKS); ++i) { + for (j = MAX(cj - 1, 0); j < MIN(cj + 1, BLOCKS); ++j) { + for (k = MAX(ck - 1, 0); k < MIN(ck + 1, BLOCKS); ++k) { if (hst->cells[i][j][k].count <= cell->count && hst->cells[i][j][k].max == NULL) { hst->cells[i][j][k].max = cell->max; @@ -68,6 +79,7 @@ main(int argc, char *argv[]) struct cell *cell; g_type_init(); + memset(&hst, 0, sizeof hst); if (argc < 2) return 1; @@ -76,19 +88,6 @@ main(int argc, char *argv[]) if (!image) return 1; - hst.list = NULL; - for (i = 0; i < BLOCKS; ++i) { - for (j = 0; j < BLOCKS; ++j) { - for (k = 0; k < BLOCKS; ++k) { - hst.cells[i][j][k].i = i; - hst.cells[i][j][k].j = j; - hst.cells[i][j][k].k = k; - hst.cells[i][j][k].count = 0; - hst.cells[i][j][k].max = NULL; - } - } - } - pixbuf_get(image, &row, &width, &height, &nch, &stride); for (y = 0; y < height; ++y, row += stride) for (x = 0, p = row; x < width; ++x, p += nch) @@ -114,9 +113,10 @@ main(int argc, char *argv[]) for (x = 0, p = row; x < width; ++x, p += nch) { cell = &hst.cells[p[0] / INTERVAL][p[1] / INTERVAL][p[2] / INTERVAL]; - p[0] = cell->max->i * INTERVAL; - p[1] = cell->max->j * INTERVAL; - p[2] = cell->max->k * INTERVAL; + calculate_index(&hst, cell->max, &i, &j, &k); + p[0] = i * INTERVAL; + p[1] = j * INTERVAL; + p[2] = k * INTERVAL; } } -- cgit