Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中高效地将一维数组复制到三维数组?_C#_Arrays - Fatal编程技术网

如何在C#中高效地将一维数组复制到三维数组?

如何在C#中高效地将一维数组复制到三维数组?,c#,arrays,C#,Arrays,我有一个线性阵列,我需要将其重塑为一堆二维数据。在这种特殊情况下,堆栈只包含一个元素,因此输出应该是具有维度(高度、宽度、1)的数组 这与a有关,我问的是在另一个方向(3D到1D)的相同操作 将此数据映射到3D阵列的最快方法是什么? 我计划采取以下方法: public static byte[, ,] ToBuffer3D<TDepth>(this byte[] buffer, int w, int h) { byte[, ,] buff3D = ne

我有一个线性阵列,我需要将其重塑为一堆二维数据。在这种特殊情况下,堆栈只包含一个元素,因此输出应该是具有维度(高度、宽度、1)的数组

这与a有关,我问的是在另一个方向(3D到1D)的相同操作

将此数据映射到3D阵列的最快方法是什么?

我计划采取以下方法:

    public static byte[, ,] ToBuffer3D<TDepth>(this byte[] buffer, int w, int h)
    {
        byte[, ,] buff3D = new byte[h, w, 1];

        for (int i = 0; i < buffer.Length; i++)
        {
            buff3D[(int)Math.Floor(i / (double)w), i % w, 0] = buffer[i];
        }

        return buff3D;
    }
公共静态字节[,]ToBuffer3D(此字节[]缓冲区,int w,int h)
{
字节[,]buff3D=新字节[h,w,1];
for(int i=0;i

但似乎可以利用数据已经存储在内存中的方式一次复制多个元素。是否有其他映射方法可用于C#?

这可能会更快一些:

public static byte[,,] ToBuffer3Da(this byte[] buffer, int w, int h)
{
    byte[,,] buff3D = new byte[h, w, 1];
    Buffer.BlockCopy(buffer, 0, buff3D, 0, h*w);
    return buff3D;
}

如果您使用下面定义的基类和实现,那么您有一个同时支持线性和维度索引的类。不需要转换或复制

这样用,

var matrix = new DecomposedMatrix<byte>(10, 10, 10)

foreach(var b int matrix)
{
    ...
}

for (var i = 0; i < matrix.Count; i++)
{
    ...
    var item = matrix[i];
    ...
}

for (var x = 0; x < matrix.H; x++)
for (var y = 0; y < matrix.W; y++)
for (var z = 0; z < matrix.D; z++)
{
    ...
    var item = matrix[x, y, z];
    ...
}
var矩阵=新的分解矩阵(10,10,10)
foreach(var b int矩阵)
{
...
}
对于(var i=0;i
课程如下

public abstract class DecomposedMatrix
{
    private readonly int h;
    private readonly int w;
    private readonly int d;

    protected DecomposedMatrix(int h, int w, int d)
    {
        this.h = h;
        this.w = w;
        this.d = d;
    }

    public int W
    {
        get
        {
            return this.w;
        }
    }

    public int D
    {
        get
        {
            return this.d;
        }
    }

    public int H
    {
        get
        {
            return this.h;
        }
    }

    protected int DereferenceCoordinates(int x, int y, int z)
    {
        if (x >= this.H || y >= this.W || z >= this.D)
        {
            throw new IndexOutOfRangeException();
        }

        if (x < 0 || y < 0 || z < 0)
        {
            throw new IndexOutOfRangeException();
        }

        return z + (y * this.D) + (x * this.W * this.D);
    }
}
公共抽象类分解矩阵
{
私有只读inth;
私有只读int w;
私有只读int d;
受保护分解矩阵(inth,intw,intd)
{
这个,h=h;
这个.w=w;
这个。d=d;
}
公共int W
{
得到
{
把这个还给我;
}
}
公共int D
{
得到
{
把这个还给我;
}
}
公共int H
{
得到
{
把这个还给我;
}
}
受保护的整数解引用坐标(整数x、整数y、整数z)
{
如果(x>=this.H | | y>=this.W | | z>=this.D)
{
抛出新的IndexOutOfRangeException();
}
if(x<0 | | y<0 | | z<0)
{
抛出新的IndexOutOfRangeException();
}
返回z+(y*this.D)+(x*this.W*this.D);
}
}
以及实施情况

public class DecomposedMatrix<T> : DecomposedMatrix, IReadOnlyList<T>
{
    private readonly IList<T> data;

    public DecomposedMatrix(int h, int w, int d)
            : base(h, w, d)
    {
        this.data = new T[h * w * d];
    }

    public T this[int index]
    {
        get
        {
            return this.data[index];
        }

        set
        {
            this.data[index] = value;
        }
    }

    public T this[int x, int y, int z]
    {
        get
        {
            return this.data[this.DereferenceCoordinates(x, y, z)];
        }

        set
        {
            this.data[this.DereferenceCoordinates(x, y, z)] = value;
        }
    }

    public int Count
    {
        get
        {
            return this.data.Count;
        }
    }

    public IEnumerator<T> GetEnumerator()
    {
        return this.data.GetEnumerator();
    }

    public IEnumerator IEnumerable.GetEnumerator()
    {
        return this.data.GetEnumerator();
    }
}
公共类DecomposedMatrix:DecomposedMatrix,IReadOnlyList
{
私有只读IList数据;
公共分解矩阵(inth,intw,intd)
:底座(高、宽、高)
{
this.data=新的T[h*w*d];
}
公共T此[int索引]
{
得到
{
返回此.data[索引];
}
设置
{
此。数据[索引]=值;
}
}
公共T这个[intx,inty,intz]
{
得到
{
返回此.data[此.取消引用坐标(x,y,z)];
}
设置
{
这个数据[这个参考坐标(x,y,z)]=值;
}
}
公共整数计数
{
得到
{
返回此.data.Count;
}
}
公共IEnumerator GetEnumerator()
{
返回此.data.GetEnumerator();
}
公共IEnumerator IEnumerable.GetEnumerator()
{
返回此.data.GetEnumerator();
}
}

如果它走一条路,它就会走另一条路。执行与前面问题相同的操作,只需反转@jodrell的方向可能会重复一个锯齿状结构提高内存使用率而不是性能如果另一个API是.NET,则无法避免复制数据。在安全C#中,您不能像在C中一样将某些内容强制转换为其他内容。不过,我不知道不安全代码。@Hogan MD数组比基于0的锯齿数组慢。