Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiently copying a matrix of objects to a larger matrix of objects

Tags:

c#

I'm writing a quadtree-like data structure which contains matrices of generic objects T. If four subnodes all contain defined matrices of T, I'm going to aggregate them into a single, larger matrix, then delete the subnodes. Is there a more efficient way to do this than looping through every reference and copying it over? Can I copy chunks of memory instead?


Example:

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);
like image 700
dlras2 Avatar asked Nov 15 '22 09:11

dlras2


1 Answers

If you can make the structure immutable, you can perhaps save yourself from having to make lots of copies. Eric Lippert has some great posts about immutable structures.

Edit: Again, I don't know if it will improve performance in your case, but here is an example of a possible design with immutable objects:

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);
            }
        }
    }
}

Now you would be able to use it as follows:

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];

Again, I don't know if it is appropriate in your situation or whether it gives an performance improvement, because you have a level of indirection when getting the value. However, there are several ways to improve this, but it depends on what you really need to do.

Good luck.

like image 175
Steven Avatar answered Dec 20 '22 02:12

Steven