यदि आप संरचना को अपरिवर्तनीय बना सकते हैं, तो आप शायद खुद को बहुत सारी प्रतियां बनाने से बचा सकते हैं। एरिक लिपर्ट में कुछ great posts about immutable structures है।
संपादित करें: फिर से, मैं अगर यह आपके मामले में प्रदर्शन में सुधार होगा पता नहीं है, लेकिन यहाँ अपरिवर्तनीय वस्तुओं के साथ एक संभव डिजाइन का एक उदाहरण है:
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];
फिर, अगर यह अपनी स्थिति में उचित है या क्या यह एक प्रदर्शन में सुधार देता है, क्योंकि आप अविवेक का एक स्तर है जब मूल्य मिल रहा है मैं नहीं जानता। हालांकि, इसे सुधारने के कई तरीके हैं, लेकिन यह इस बात पर निर्भर करता है कि आपको वास्तव में क्या करना है।
शुभकामनाएं।
कैसे आप मैट्रिक्स बिल्कुल reprent करते हैं? कुल कैसे? यदि आइटम संदर्भ प्रकार हैं, तो आप केवल एक पॉइंटर (4/8 बाइट्स) की प्रतिलिपि बना रहे हैं, जो कि बहुत तेज है। वहाँ केवल 4 (सूचियों) आइटम हैं, है ना? –
मैंने एक उदाहरण जोड़ा। मेरी संरचना में, '_leafX' एक बच्चे नोड में होगा, 'शून्य कॉपीइंटो (रेफरी टी [,], प्वाइंट पी)' – dlras2
के साथ तो दूसरे शब्दों में: आप किसी अन्य मैट्रिक्स में मैट्रिक्स के तत्वों की प्रतिलिपि बना रहे हैं? आप 'Array.CopyTo' या 'Buffer.BlockCopy' का उपयोग कर सकते हैं (प्रतिलिपि केवल कई μs ले जाएगा)। साथ ही, यदि आप अपने डिज़ाइन को संशोधित करते हैं तो यह 'सरणी [64] 'नहीं है, लेकिन' सरणी [64 * 64]' है और पंक्तियों की जांच के लिए मॉड्यूलो ('%') का उपयोग करें, यह और भी तेज़ होगा। सुनिश्चित नहीं है कि आपकी प्रदर्शन बाधा कहां है। यह निर्भर करता है कि आप कितनी बार मैट्रिक्स बनाम तत्वों तक पहुंचते हैं। आप कितनी बार प्रतिलिपि बनाते हैं। –