Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 948705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:17:11+00:00 2026-05-15T23:17:11+00:00

I’m writing a quadtree-like data structure which contains matrices of generic objects T .

  • 0

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);
  • 1 1 Answer
  • 6 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:17:11+00:00Added an answer on May 15, 2026 at 11:17 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.