Complete.Org: Mailing Lists: Archives: freeciv-dev: October 2001:
[Freeciv-Dev] Re: example patch: [xy]_map_iterate
Home

[Freeciv-Dev] Re: example patch: [xy]_map_iterate

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Cc: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: example patch: [xy]_map_iterate
From: Jason Dorje Short <vze2zq63@xxxxxxxxxxx>
Date: Thu, 04 Oct 2001 07:00:01 -0400
Reply-to: jdorje@xxxxxxxxxxxx

Raimar Falke wrote:
> 
> On Thu, Oct 04, 2001 at 05:57:09AM -0400, Jason Dorje Short wrote:
> > There's a lot of code that can't use whole_map_iterate because the
> > iteration has to happen in a certain order.
> 
> What code? I don't have a good feeling with code that has such
> dependencies. Printing the map?

Printing the map (to some random text file, I presume) is part of it. 
Also saving the game and sending tiles across the network.  These depend
strictly upon the ordering, so are easy to fix using [xy]_map_iterate.

Other examples don't iterate over the whole map...for instance some
iterate over just the poles while others skip border positions.  These
can be done with whole_map_iterate by skipping invalid positions; the
problem here is that the current definition of "valid" is
topology-dependent (it assumes the current wrapping system).  These will
take a little time to figure out.

All in all, there are 30-50 such cases.  Try grepping for
"for.*x.*map.xsize".  Most of them are in the savegame code, which I'm
not particularly familiar with.

> So there are two possible implementations for whole_map_iterate:
> 
>  for(index=0;index<max_index;index++)
>  {
>    int x,y;
>    index_to_map_pos(&x,&y,index);
>    /* BODY */
>  }
> 
> and
> 
>   int x,y;
>   for(y=0;y < map.ysize; y++)
>     for(x=0; x < map.xsize; x++)
>       if(is_normal_map_pos(x,y))
>          /* BODY */
> 
> Correct?

Yes.

Here's are some other possible implementations, basically improvements
on the second one of yours:

  y_map_iterate(y_itr)
    x_map_iterate(x_itr)
      if (is_normal_map_pos(x, y))
        /* BODY */

This one is identical to your second case, but with macros (fully
topology-independent).

  y_map_iterate(y_itr)
    yx_map_iterate(y_itr, x_itr)
      /* BODY */

This is identical to your second case under the current topology, but
possibly faster under arbitrary topologies.  Of course, there's no
yx_map_iterate yet (it iterates over x_itr given y_itr - the
nomenclature is confusing but I can't think of anything better yet).

jason


[Prev in Thread] Current Thread [Next in Thread]