[Freeciv-Dev] Re: (PR#3444) index_to_map_pos macro
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Gregory Berkolaiko wrote:
> The macro inverts the action of map_inx: extracts the map coordinates from
> the index. Also added are macros CHECK_INDEX (similar to CHECK_MAP_POS)
> and MAX_INDEX for sanity checking and allocations.
>
> I intend to adapt the PF code to use this and map_inx code shortly, so
> please commit.
> Index: common/map.h
> ===================================================================
> RCS file: /home/freeciv/CVS/freeciv/common/map.h,v
> retrieving revision 1.135
> diff -u -r1.135 map.h
> --- common/map.h 2003/02/10 21:43:41 1.135
> +++ common/map.h 2003/02/16 16:35:04
> @@ -222,6 +222,21 @@
> #define map_inx(x,y) \
> (CHECK_MAP_POS((x),(y)), (x)+(y)*map.xsize)
>
> +/* Maximum value of index (for sanity checks and allocations) */
> +#define MAX_INDEX map.xsize * map.ysize
> +
> +#ifdef DEBUG
> +#define CHECK_INDEX(index) assert((index) >= 0 && (index) < MAX_INDEX)
> +#else
> +#define CHECK_INDEX(index) ((void)0)
> +#endif
> +
> +/* index_to_map_pos(int, int *, int *) inverts map_inx */
> +#define index_to_map_pos(index, x, y) \
> +( CHECK_INDEX(index), \
> + *(x) = (index) % map.xsize, \
> + *(y) = (index) / map.ysize)
> +
> #define DIRSTEP(dest_x, dest_y, dir) \
> ( (dest_x) = DIR_DX[(dir)], \
> (dest_y) = DIR_DY[(dir)])
The preferred form for these conversion functions is (dest, source) -
like memcpy, strcpy, etc. I also prefer to call the values map_x, map_y
- to indicate that they are map positions (which will become relevant
shortly).
In the patch below, I also renamed MAX_INDEX as MAX_MAP_INDEX and moved
CHECK_INDEX up next to CHECK_MAP_POS.
Note, Ross has a form of this that will work on non-normal (e.g.,
negative) index positions. We could use that form instead, although it
is slower. If we don't do that, we could probably optimize it as:
map_y = index / map.xsize
map_x = index - map_y * map.xsize;
which is likely to be faster than using %. But I'd rather see the
interface be implemented first - we can work on the implementation later.
jason
Index: common/map.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/map.h,v
retrieving revision 1.135
diff -u -r1.135 map.h
--- common/map.h 2003/02/10 21:43:41 1.135
+++ common/map.h 2003/02/16 22:37:54
@@ -203,10 +203,15 @@
void initialize_move_costs(void);
void reset_move_costs(int x, int y);
+/* Maximum value of index (for sanity checks and allocations) */
+#define MAX_MAP_INDEX map.xsize * map.ysize
+
#ifdef DEBUG
#define CHECK_MAP_POS(x,y) assert(is_normal_map_pos((x),(y)))
+#define CHECK_INDEX(index) assert((index) >= 0 && (index) < MAX_MAP_INDEX)
#else
#define CHECK_MAP_POS(x,y) ((void)0)
+#define CHECK_INDEX(index) ((void)0)
#endif
#define map_adjust_x(X) \
@@ -221,6 +226,12 @@
#define map_inx(x,y) \
(CHECK_MAP_POS((x),(y)), (x)+(y)*map.xsize)
+
+/* index_to_map_pos(int *, int *, int) inverts map_inx */
+#define index_to_map_pos(pmap_x, pmap_y, index) \
+ (CHECK_INDEX(index), \
+ *(pmap_x) = (index) % map.xsize, \
+ *(pmap_y) = (index) / map.ysize)
#define DIRSTEP(dest_x, dest_y, dir) \
( (dest_x) = DIR_DX[(dir)], \
|
|