Fast way to update grids

John Kessenich jk at hpfelg.HP.COM
Tue Feb 2 03:16:37 AEST 1988



    Use an array (it is the fastest know data structure for linear 
    traversal in an aribitrary direction), but take advantage of C's
    pointer arithmetic.  Example:

    int a[16][16];

    #define NO   1
    #define SO  -1
    #define EA  16
    #define WE -16

    Then if you want to know about a[x][y] neighbors, look at

    b = &a[x][y];
    b + NO;
    b + SO;
    b + EA;
    b + WE;

    If you are going through the array in some order, the address step 
    may not even be necessary.

    Also, for addressing, subscript ranges equal to powers of two will
    yield faster results.  Why?  They are compiled as << instead of *.

--------------
John Kessenich



More information about the Comp.lang.c mailing list