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 864841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:32:43+00:00 2026-05-15T09:32:43+00:00

I am working on a piece of 3D software that has sometimes has to

  • 0

I am working on a piece of 3D software that has sometimes has to perform intersections between massive numbers of curves (sometimes ~100,000). The most natural way to do this is to do an N^2 bounding box check, and then those curves whose bounding boxes overlap get intersected.

I heard good things about octrees, so I decided to try implementing one to see if I would get improved performance.

Here’s my design:
Each octree node is implemented as a class with a list of subnodes and an ordered list of object indices.

When an object is being added, it’s added to the lowest node that entirely contains the object, or some of that node’s children if the object doesn’t fill all of the children.

Now, what I want to do is retrieve all objects that share a tree node with a given object. To do this, I traverse all tree nodes, and if they contain the given index, I add all of their other indices to an ordered list.

This is efficient because the indices within each node are already ordered, so finding out if each index is already in the list is fast. However, the list ends up having to be resized, and this takes up most of the time in the algorithm. So what I need is some kind of tree-like data structure that will allow me to efficiently add ordered data, and also be efficient in memory.

Any suggestions?

  • 1 1 Answer
  • 3 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-15T09:32:44+00:00Added an answer on May 15, 2026 at 9:32 am

    SortedDictionary (.NET 2+) or SortedSet (.NET 4 only) is probably what you want. They are tree structures.

    SortedList is a dumb class which is no different from List structurally.

    However, it is still not entirely clear to me why you need this list as sorted.
    Maybe if you could elaborate on this matter we could find a solution where you don’t need sorting at all. For example a simple HashSet could do. It is faster at both lookups and insertions than SortedList or any of the tree structures if hashing is done properly.

    Ok, now when it is clear to me that you wanted sorted lists merging, I can try to write an implementation.

    At first, I implemented merging using SortedDictionary to store heads of all the arrays. At each iteration I removed the smallest element from the dictionary and added the next one from the same array. Performance tests showed that overhead of SortedDictionary is huge, so that it is almost impossible to make it faster than simple concatenation+sorting. It even struggles to match SortedList performance on small tests.

    Then I replaced SortedDictionary with custom-made binary heap implementation. Performance improvement was tremendous (more than 6 times). This Heap implementation even manages to beat .Distinct() (which is usually the fastest) in some tests.

    Here is my code:

    class Heap<T>
    {
        public Heap(int limit, IComparer<T> comparer)
        {
            this.comparer = comparer;
            data = new T[limit];
        }
    
        int count = 0;
        T[] data;
    
        public void Add(T t)
        {
            data[count++] = t;
            promote(count-1);
        }
    
        IComparer<T> comparer;
    
        public int Count { get { return count; } }
    
        public T Pop()
        {
            T result = data[0];
            fill(0);
            return result;
        }
    
        bool less(T a, T b)
        {
            return comparer.Compare(a,b)<0;
        }
    
        void fill(int index)
        {
            int child1 = index*2+1;
            int child2 = index*2+2;
            if(child1 >= Count)
            {
                data[index] = data[--count];
                if(index!=count)
                    promote(index);
            }
            else
            {
                int bestChild = child1;
                if(child2 < Count && less(data[child2], data[child1]))
                {
                    bestChild = child2;
                }
    
                data[index] = data[bestChild];
                fill(bestChild);
            }
        }
    
        void promote(int index)
        {
            if(index==0)
                return;
            int parent = (index-1)/2;
            if(less(data[index], data[parent]))
            {
                T tmp = data[parent];
                data[parent] = data[index];
                data[index] = tmp;
                promote(parent);
            }
        }
    }
    
    struct ArrayCursor<T>
    {
        public T [] Array {get;set;}
        public int Index {get;set;}
        public bool Finished {get{return Array.Length == Index;}}
        public T Value{get{return Array[Index];}}
    }
    
    class ArrayComparer<T> : IComparer<ArrayCursor<T>>
    {
        IComparer<T> comparer;
        public ArrayComparer(IComparer<T> comparer)
        {
            this.comparer = comparer;
        }
    
        public int Compare (ArrayCursor<T> a, ArrayCursor<T> b)
        {
            return comparer.Compare(a.Value, b.Value);
        }
    }
    
    static class HeapMerger
    {
        public static IEnumerable<T> MergeUnique<T>(this T[][] arrays)
        {
            bool first = true;
            T last = default(T);
            IEqualityComparer<T> eq = EqualityComparer<T>.Default;
            foreach(T i in Merge(arrays))
                if(first || !eq.Equals(last,i))
                {
                    yield return i;
                    last = i;
                    first = false;
                }
        }
    
        public static IEnumerable<T> Merge<T>(this T[][] arrays)
        {
            var map = new Heap<ArrayCursor<T>>(arrays.Length, new ArrayComparer<T>(Comparer<T>.Default));
    
            Action<ArrayCursor<T>> tryAdd = (a)=>
            {
                if(!a.Finished)
                    map.Add(a);
            };
    
            for(int i=0;i<arrays.Length;i++)
                tryAdd(new ArrayCursor<T>{Array=arrays[i], Index=0});
    
            while(map.Count>0)
            {
                ArrayCursor<T> lowest = map.Pop();
                yield return lowest.Value;
                lowest.Index++;
                tryAdd(lowest);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a piece of software that would work well with an iTunes
Currently I am working on a piece of software, which is currently written 100%
I'm working on a piece of software that creates a contract and captures the
I'm working on a piece of scientific software that is very cpu-intensive (its proc
I am working on a piece of software that needs to call a family
I am working on a piece of software that effectively needs to talk to
Folks, I'm working on a little piece of rich client software that I'd like
I'm working on a CGI script that is called from a piece of software
I'm working on a piece of software that monitors other processes' system calls using
I'm currently working on a piece of software designed in WPF that connects to

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.