Si puede hacer que la estructura sea inmutable, puede evitar tener que hacer muchas copias. Eric Lippert tiene algunos great posts about immutable structures.
Editar: Una vez más, no sé si va a mejorar el rendimiento en su caso, pero aquí es un ejemplo de un posible diseño con objetos inmutables:
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);
}
}
}
}
Ahora usted sería capaz de usar es como sigue:
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];
una vez más, no sé si es apropiado en su situación o si se da una mejora en el rendimiento, porque tiene un nivel de indirección al obtener el valor. Sin embargo, hay varias formas de mejorar esto, pero depende de lo que realmente necesite hacer.
Buena suerte.
¿Cómo se reprent las matrices exactamente? Agregue cómo? Si los elementos son tipos de referencia, simplemente está copiando un puntero (4/8 bytes), que si es bastante rápido. Solo hay 4 (listas de) artículos allí, ¿no? –
Agregué un ejemplo. En mi estructura, '_leafX' estaría en un nodo secundario, con' void CopyInto (ref T [,], Point p) ' – dlras2
Entonces, en otras palabras, ¿está copiando los elementos de una matriz en alguna otra matriz? Puede usar 'Array.CopyTo' o' Buffer.BlockCopy' (la copia solo tomará varios μs). Además, si modifica su diseño para que no sea 'matriz [64]', sino 'matriz [64 * 64]' y use módulo ('%') para verificar filas, será aún más rápido. Sin embargo, no estoy seguro de dónde está el cuello de botella de rendimiento. Depende de la frecuencia con la que acceda a los elementos de las matrices frente a la frecuencia con la que copia. –