Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What data structure to use in my example

I'd like to create a component, which consist from a board and its surrounding corner. The size of the board (and therefore also of the border) is defined at run-time. Some examples (board is bright and border is dark): alt text http://img340.imageshack.us/img340/3862/examplegw.png

The board consists of objects of the type BoardCell and the border consists of objects of the type BorderCell. Data structure for the board is BoardCell[,] - a simple two-dimensional array.

How can I represent the border? I started with something like this:

public BorderCell TopLeft       // top left corner cell
public BorderCell TopRight      // top right corner cell
public BorderCell BottomRight   // bottom right corner cell
public BorderCell BottomLeft    // bottom left corner cell
public BorderCell[] Top         // top border (without corners)
public BorderCell[] Bottom      // bottom border (without corners)
public BorderCell[] Left        // left border (without corners)
public BorderCell[] Right       // right border (without corners)

I don't like this representation of the border, can you suggest something better?

Additional: I'd like to have a method SetSomethingForTheCell on the border object:

public void SetSomethingForTheCell(...)

but with my current data structure I don't know what to pass as a parameter.

like image 990
sventevit Avatar asked Jun 29 '10 11:06

sventevit


People also ask

What is data structure with real life example?

To store a set of fixed key words which are referenced very frequently. To store the customer order information in a drive-in burger place. (Customers keep on coming and they have to get their correct food at the payment/food collection window.) To store the genealogy information of biological species.

What are the 4 data structures?

When we think of data structures, there are generally four forms: Linear: arrays, lists. Tree: binary, heaps, space partitioning etc. Hash: distributed hash table, hash tree etc.


2 Answers

Since it's really trivial to detect whether a cell is part of the border or not, just store the cells once and test for border membership when required.

A simple method to test whether a cell is in the border:

// assuming that the array is in row-major order...
public static bool IsInBorder(this BoardCell[,] board, int x, int y) {
    return x == board.GetLowerBound(1) || x == board.GetUpperBound(1) ||
           y == board.GetLowerBound(0) || y == board.GetUpperBound(0);
}
like image 197
Christian Hayter Avatar answered Nov 14 '22 23:11

Christian Hayter


I would be tempted to define a common interface or base class for BoardCell and BorderCell (i.e. Cell?) and keep them in a (bigger) two dimensional array. This way you can easily address every cell, and it will be rather easy to determine if the address is a border or board.

like image 24
Grzenio Avatar answered Nov 14 '22 21:11

Grzenio