gpt4 book ai didi

c# - 有效地将对象矩阵复制到更大的对象矩阵

转载 作者:太空狗 更新时间:2023-10-29 23:48:33 27 4
gpt4 key购买 nike

我正在编写一个类似四叉树的数据结构,其中包含通用对象矩阵 T。如果四个子节点都包含 T 的定义矩阵,我将把它们聚合成一个更大的矩阵,然后删除子节点。有没有比遍历每个引用并复制它更有效的方法呢?我可以复制内存块吗?


示例:

T[,] _leaf1 = new T[64,64];
T[,] _leaf2 = new T[64,64];
T[,] _leaf3 = new T[64,64];
T[,] _leaf4 = new T[64,64];

// Populate leafs

T[,] _root = new T[128,128];

CopyInto(ref _root, ref _leaf1, 64, 64);
CopyInto(ref _root, ref _leaf2, 0, 64);
CopyInto(ref _root, ref _leaf3, 0, 0);
CopyInto(ref _root, ref _leaf4, 64, 0);

最佳答案

如果您可以使结构不可变,您也许可以避免制作大量副本。 Eric Lippert 有一些 great posts about immutable structures .

编辑:同样,我不知道它是否会提高您的性能,但这里有一个可能的不可变对象(immutable对象)设计示例:

abstract class QuadTree<T>
{
public QuadTree(int width, int height)
{
this.Width = width;
this.Heigth = heigth;
}

public int Width { get; private set; }
public int Height { get; private set; }

public abstract T Get(int x, int y);
}

class MatrixQuadTree<T> : QuadTree<T>
{
private readonly T[,] matrix;

public QuadTree(T[,] matrix, int width, int heigth)
: base(width, heigth)
{
this.matrix = matrix;
}

public override T Get(int x, int y)
{
return this.matrix[x, y];
}
}

class CompositeQuadTree<T> : QuadTree<T>
{
private readonly QuadTree<T> topLeft;
private readonly QuadTree<T> topRight;
private readonly QuadTree<T> bottomLeft;
private readonly QuadTree<T> bottomRight;

public CompositeQuadTree(QuadTree<T> topLeft,
QuadTree<T> topRight, QuadTree<T> bottomLeft,
QuadTree<T> bottomRight)
: base(topLeft.Width + topRight.Width,
topLeft.Height + bottomLeft.Heigth)
{
// TODO: Do proper checks.
if (this.Width != topLeft.Width + bottomRight.Width)
throw Exception();

this.topLeft = topLeft;
this.topRight = topRight;
this.bottomLeft = bottomLeft;
this.bottomRight = bottomRight;
}

public override T Get(int x, int y)
{
if (x <= this.topLeft.Width)
{
if (y <= this.topLeft.Width)
{
return this.topLeft.Get(x, y);
}
else
{
return this.topLeft.Get(x, y + this.topLeft.Heigth);
}
}
else
{
if (y <= this.topLeft.Width)
{
return this.topRight.Get(x + this.topLeft.Width, y);
}
else
{
return this.topRight.Get(x + this.topLeft.Width,
y + this.topLeft.Heigth);
}
}
}
}

现在您可以按如下方式使用它:

T[,] _leaf1 = new T[64,64];
T[,] _leaf2 = new T[64,64];
T[,] _leaf3 = new T[64,64];
T[,] _leaf4 = new T[64,64];

// Populate leafs

QuadTree<T> l1 = new MatrixQuadTree<T>(_leaf1,64,64);
QuadTree<T> l2 = new MatrixQuadTree<T>(_leaf2,64,64);
QuadTree<T> l3 = new MatrixQuadTree<T>(_leaf3,64,64);
QuadTree<T> l4 = new MatrixQuadTree<T>(_leaf4,64,64);

// Instead of copying, you can no do this:
QuadTree<T> c = CompositeQuadTree<T>(l1,l2,l3,l4);

// And you can even make composites, of other composites:
QuadTree<T> c2 = CompositeQuadTree<T>(c,c,c,c);

// And you can read a value as follows:
T value = c2[30, 50];

同样,我不知道它是否适合您的情况,或者它是否可以提高性能,因为您在获取值(value)时有一定程度的间接性。然而,有几种方法可以改善这一点,但这取决于您真正需要做什么。

祝你好运。

关于c# - 有效地将对象矩阵复制到更大的对象矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3302467/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com