public void Execute()
        {
            var length = IndexedSourceBuffer.Length;

            if (length == 0)
            {
                return;
            }

            var segmentCount = (length + (SegmentWidth - 1)) / SegmentWidth;
            var segmentIndex = stackalloc int[segmentCount];

            var lastSharedIndex = -1;
            var lastSharedValue = default(T);

            for (int sortIndex = 0; sortIndex < length; sortIndex++)
            {
                // find next best
                int bestSegmentIndex       = -1;
                IndexedValue <T> bestValue = default(IndexedValue <T>);

                for (int i = 0; i < segmentCount; i++)
                {
                    var startIndex    = i * SegmentWidth;
                    var offset        = segmentIndex[i];
                    var segmentLength = ((length - startIndex) < SegmentWidth) ? (length - startIndex) : SegmentWidth;
                    if (offset == segmentLength)
                    {
                        continue;
                    }

                    var nextValue = IndexedSourceBuffer[startIndex + offset];
                    if (bestSegmentIndex != -1)
                    {
                        if (nextValue.CompareTo(bestValue) > 0)
                        {
                            continue;
                        }
                    }

                    bestValue        = nextValue;
                    bestSegmentIndex = i;
                }

                segmentIndex[bestSegmentIndex]++;
                SourceIndexBySortedSourceIndex[sortIndex] = bestValue.Index;

                if ((lastSharedIndex != -1) && (bestValue.Value.CompareTo(lastSharedValue) == 0))
                {
                    SharedIndexCountsBySharedIndex[lastSharedIndex]++;
                }
                else
                {
                    lastSharedIndex++;
                    lastSharedValue = bestValue.Value;

                    SortedSourceIndexBySharedIndex.Add(sortIndex);
                    SharedIndexCountsBySharedIndex.Add(1);
                }

                SharedIndicesBySourceIndex[bestValue.Index] = lastSharedIndex;
            }
        }
Beispiel #2
0
 public int CompareTo(IndexedValue <T> other) => Value.CompareTo(other.Value);