Jeśli możesz uczynić strukturę niezmienną, być może uratujesz się od konieczności wykonywania wielu kopii. Eric Lippert ma około great posts about immutable structures.
Edit: Ponownie, nie wiem, czy będzie to zwiększyć wydajność w swojej sprawie, ale tutaj jest przykład możliwej konstrukcji z niezmiennych obiektów:
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);
}
}
}
}
Teraz będzie można użyć to w następujący sposób:
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];
Znowu, ja nie wiem, czy to jest właściwe w danej sytuacji, czy też daje wzrost wydajności, ponieważ masz poziom zadnie gdy uzyskanie wartości. Istnieje jednak kilka sposobów, aby to poprawić, ale zależy to od tego, co naprawdę trzeba zrobić.
Powodzenia.
Jak reprent macierze dokładnie? Zgadnij, jak? Jeśli elementy są typami odniesienia, po prostu kopiujesz wskaźnik (4/8 bajtów), który jest dość szybki. Jest tylko 4 (listy) przedmiotów, prawda? –
Dodałem przykład. W mojej strukturze '_leafX' będzie w węźle podrzędnym, z' void CopyInto (ref T [,], Point p) ' – dlras2
Innymi słowy: kopiujesz elementy macierzy do innej macierzy? Możesz użyć 'Array.CopyTo' lub' Buffer.BlockCopy' (kopiowanie zajmie tylko kilka μs). Ponadto, jeśli zmodyfikujesz swój projekt, więc nie jest to 'tablica [64]', ale 'tablica [64 * 64]' i używaj modulo ('%') do sprawdzania wierszy, będzie jeszcze szybciej. Nie wiesz, gdzie jest twoje wąskie gardło wydajności. Zależy to od tego, jak często uzyskujesz dostęp do elementów matryc w porównaniu do częstotliwości ich kopiowania. –