在我的例子中使用什么数据结构 [英] What data structure to use in my example

查看:107
本文介绍了在我的例子中使用什么数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个组件,其中包括一个板子及其周围的角落。板的大小(因此也是边界)在运行时被定义。一些示例(板是明亮的,边框是黑暗的):
alt text http://img340.imageshack。我们/ img340 / 3862 / examplegw.png

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

该板由类型为BoardCell的对象组成,边框由BorderCell类型的对象组成。板的数据结构是BoardCell [,] - 一个简单的二维数组。

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?

其他:我想在边框对象上设置SetSomethingForTheCell方法:

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.

推荐答案

由于检测单元格是否是边框的一部分真的很简单,只需存储一次单元格并测试

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);
}

这篇关于在我的例子中使用什么数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆