1
0
Fork 0
mirror of https://github.com/VSadov/Satori.git synced 2025-06-10 01:50:53 +09:00

IntroSort minor cleanup, remove unnecessary 'hi' (#35663)

* IntroSort minor cleanup, remove unnecessary 'hi'

* Apply suggestions from code review

Co-authored-by: nietras <nietras@users.noreply.github.com>

Co-authored-by: Stephen Toub <stoub@microsoft.com>
This commit is contained in:
nietras 2020-05-01 15:30:46 +02:00 committed by GitHub
parent ac6fac8979
commit d119c05139
Signed by: github
GPG key ID: 4AEE18F83AFDEB23

View file

@ -141,45 +141,42 @@ namespace System.Collections.Generic
Debug.Assert(depthLimit >= 0);
Debug.Assert(comparer != null);
int hi = keys.Length - 1;
while (hi > 0)
int partitionSize = keys.Length;
while (partitionSize > 1)
{
int partitionSize = hi + 1;
if (partitionSize <= Array.IntrosortSizeThreshold)
{
Debug.Assert(partitionSize >= 2);
if (partitionSize == 2)
{
SwapIfGreater(keys, comparer, 0, hi);
SwapIfGreater(keys, comparer, 0, 1);
return;
}
if (partitionSize == 3)
{
SwapIfGreater(keys, comparer, 0, hi - 1);
SwapIfGreater(keys, comparer, 0, hi);
SwapIfGreater(keys, comparer, hi - 1, hi);
SwapIfGreater(keys, comparer, 0, 1);
SwapIfGreater(keys, comparer, 0, 2);
SwapIfGreater(keys, comparer, 1, 2);
return;
}
InsertionSort(keys.Slice(0, hi+1), comparer);
InsertionSort(keys.Slice(0, partitionSize), comparer);
return;
}
if (depthLimit == 0)
{
HeapSort(keys.Slice(0, hi+1), comparer);
HeapSort(keys.Slice(0, partitionSize), comparer);
return;
}
depthLimit--;
int p = PickPivotAndPartition(keys.Slice(0, hi+1), comparer);
int p = PickPivotAndPartition(keys.Slice(0, partitionSize), comparer);
// Note we've already partitioned around the pivot and do not have to move the pivot again.
IntroSort(keys[(p+1)..(hi+1)], depthLimit, comparer);
hi = p - 1;
IntroSort(keys[(p+1)..partitionSize], depthLimit, comparer);
partitionSize = p;
}
}
@ -404,14 +401,11 @@ namespace System.Collections.Generic
Debug.Assert(!keys.IsEmpty);
Debug.Assert(depthLimit >= 0);
int hi = keys.Length - 1;
while (hi > 0)
int partitionSize = keys.Length;
while (partitionSize > 1)
{
int partitionSize = hi + 1;
if (partitionSize <= Array.IntrosortSizeThreshold)
{
Debug.Assert(partitionSize >= 2);
if (partitionSize == 2)
{
@ -446,7 +440,7 @@ namespace System.Collections.Generic
// Note we've already partitioned around the pivot and do not have to move the pivot again.
IntroSort(keys[(p+1)..partitionSize], depthLimit);
hi = p - 1;
partitionSize = p;
}
}
@ -632,26 +626,23 @@ namespace System.Collections.Generic
Debug.Assert(depthLimit >= 0);
Debug.Assert(comparer != null);
int hi = keys.Length - 1;
while (hi > 0)
int partitionSize = keys.Length;
while (partitionSize > 1)
{
int partitionSize = hi + 1;
if (partitionSize <= Array.IntrosortSizeThreshold)
{
Debug.Assert(partitionSize >= 2);
if (partitionSize == 2)
{
SwapIfGreaterWithValues(keys, values, comparer, 0, hi);
SwapIfGreaterWithValues(keys, values, comparer, 0, 1);
return;
}
if (partitionSize == 3)
{
SwapIfGreaterWithValues(keys, values, comparer, 0, hi - 1);
SwapIfGreaterWithValues(keys, values, comparer, 0, hi);
SwapIfGreaterWithValues(keys, values, comparer, hi - 1, hi);
SwapIfGreaterWithValues(keys, values, comparer, 0, 1);
SwapIfGreaterWithValues(keys, values, comparer, 0, 2);
SwapIfGreaterWithValues(keys, values, comparer, 1, 2);
return;
}
@ -670,7 +661,7 @@ namespace System.Collections.Generic
// Note we've already partitioned around the pivot and do not have to move the pivot again.
IntroSort(keys[(p+1)..partitionSize], values[(p+1)..partitionSize], depthLimit, comparer);
hi = p - 1;
partitionSize = p;
}
}
@ -856,26 +847,23 @@ namespace System.Collections.Generic
Debug.Assert(values.Length == keys.Length);
Debug.Assert(depthLimit >= 0);
int hi = keys.Length - 1;
while (hi > 0)
int partitionSize = keys.Length;
while (partitionSize > 1)
{
int partitionSize = hi + 1;
if (partitionSize <= Array.IntrosortSizeThreshold)
{
Debug.Assert(partitionSize >= 2);
if (partitionSize == 2)
{
SwapIfGreaterWithValues(keys, values, 0, hi);
SwapIfGreaterWithValues(keys, values, 0, 1);
return;
}
if (partitionSize == 3)
{
SwapIfGreaterWithValues(keys, values, 0, hi - 1);
SwapIfGreaterWithValues(keys, values, 0, hi);
SwapIfGreaterWithValues(keys, values, hi - 1, hi);
SwapIfGreaterWithValues(keys, values, 0, 1);
SwapIfGreaterWithValues(keys, values, 0, 2);
SwapIfGreaterWithValues(keys, values, 1, 2);
return;
}
@ -894,7 +882,7 @@ namespace System.Collections.Generic
// Note we've already partitioned around the pivot and do not have to move the pivot again.
IntroSort(keys[(p+1)..partitionSize], values[(p+1)..partitionSize], depthLimit);
hi = p - 1;
partitionSize = p;
}
}