Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need a cache friendly data structure to store the neighbors of a letter in a 2d-array

Assume the following is a 2d array that we are operating on

a b c d
e f g h
i j k l
m n o p

The surrounding neighbor of 'f' are [a b c e g i j k]. I'm trying to create a cache friendly data structure to store the neighbor of a node. Right now I have something like so

struct Neighbor{
   size_t neighborPosition[8][2];
   size_t size;
};

typedef size_t Position[2];
typedef Neighbor** NeighborTable;

Note that the max neighbor a node could have is 8. Anyone have any advice? I need the structure to be a constant time neighbor lookup so I will be pre-calculating the neighbor of each node.

like image 971
dchhetri Avatar asked Feb 12 '12 22:02

dchhetri


1 Answers

Every cell has the same neighbors, in terms of their relative location, except for the edge cells. But if you add a border (an extra row & column at the start and end), and fill it with a value that lets you know it is a border, the you don't need any data structure at all to identify neighbors.

like image 71
Scott Hunter Avatar answered Sep 19 '22 19:09

Scott Hunter