BEPUphysics.BroadPhaseSystems.SortAndSweep.SortAndSweep1D.MergeSections C# (CSharp) Method

MergeSections() public method

public MergeSections ( int a, int b ) : void
a int
b int
return void
        void MergeSections(int a, int b)
        {
            int intervalLength = entries.Count / sortSegmentCount;
            //'a' is known to be less than b, which means it cannot be the last section.
            int aStart = intervalLength * a;
            int aEnd = intervalLength * (a + 1);
            int bStart = intervalLength * b;
            int bEnd;
            int length;
            if (b == sortSegmentCount - 1)
            {
                bEnd = entries.Count;
                length = intervalLength + entries.Count - bStart;
            }
            else
            {
                bEnd = intervalLength * (b + 1);
                length = intervalLength * 2;
            }

            int aIndex = aStart, bIndex = bStart;
            int i = 0;
            while (i < length)
            {
                //Compute the location in the buffer array to put the minimum element.
                int bufferIndex;
                if (i >= intervalLength) //a length is intervalLength.
                    bufferIndex = bStart + i - intervalLength;
                else
                    bufferIndex = aStart + i;

                if (aIndex < aEnd && bIndex < bEnd)
                {
                    //Compare the element at a to the one at b.
                    if (entries.Elements[aIndex].boundingBox.Min.X < entries.Elements[bIndex].boundingBox.Min.X)
                    {
                        //a was the minimum element.  Put it into the buffer and increment the considered a index.
                        backbuffer.Elements[bufferIndex] = entries.Elements[aIndex++];
                    }
                    else
                    {
                        //b was the minimum element.  Put it into the buffer and increment the considered b index.
                        backbuffer.Elements[bufferIndex] = entries.Elements[bIndex++];
                    }
                }
                else if (aIndex < aEnd)
                {
                    //B is at the max, so just use a.
                    backbuffer.Elements[bufferIndex] = entries.Elements[aIndex++];
                }
                else
                {
                    //A is at the max, so just use b.
                    backbuffer.Elements[bufferIndex] = entries.Elements[bIndex++];
                }
                i++;
            }
        }
    }