扁平化三维阵列的容量为对象的一维数组 [英] Flatten a volume of a 3D array into a 1D array of objects

查看:280
本文介绍了扁平化三维阵列的容量为对象的一维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给我的类型,其长度和宽度是一致的,但深度为锯齿状的3D图:

Give I have a 3D map of type whose the length and width are uniform but the depth is jagged:

public class Map<T>
{
    T[,][] map;
    ...
}

这是返回所有类型的由一个2D区域限定的体积中存在的对象和所有深度的一维阵列区域内的最佳方式。例如,我可能有一个数组符号替代如下:

What is the best way to return a 1D array of all of the objects of type that exist within a volume defined by a 2D area and all of depth within that area. For example I might have an array notation override as follows:

public IEnumerable<T> this[Rectangle area]
{
    get {...}
}

或只是

public IEnumerable<T> this[int x, int y, int width, int length]
{
    get {...}
}

我诚实地希望一个快速的LINQ解决方案,但性能是preferable到解决方案的视觉优雅。返回的扁平阵列内的对象的顺序是不重要的。如果任何人有任何这方面的建议或经验,请分享你的智慧。

I am honestly hoping for an fast LINQ solution but performance is preferable to visual elegance of the solution. The order of the objects within the flattened array that is returned is unimportant. If anyone has any suggestions or experience with this please share your wisdom.

另外,如果有在我手上另一个数据结构,它可以执行,我不知道同样的功能,我会很乐意使用的替代。

Alternatively if there is another data structure at my disposal that can perform the same function that I am unaware of, I will gladly use that instead.

如果一些关于我的问题是不清楚的请咨询了解更多详情。

If something about my question is unclear please ask for further details.

推荐答案

这可能工作以及(不含LINQ)

This might work as well (without Linq)

    public IEnumerable<T> this[int x, int y, int width, int length]
    {
        get
        {
            for (int i = 0; i < length; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    for (int k = 0; k < map[x + i, y + j].Length; k++)
                    {
                        yield return map[x + i, y + j][k];
                    }
                }
            }
        }
    }

这篇关于扁平化三维阵列的容量为对象的一维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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