This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Add Sort(...) extension methods for Span<T> #16986
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
15a6395
Add span sort code. Missing throwhelper stuff.
nietras 88810dc
Add exception string resources.
nietras cd2f547
Add ThrowHelper stuff
nietras 011c259
Address ahsonkhan feedback.
nietras c7b9eea
Update to latest corefx changes, mostly cleanup.
nietras bb844ac
Update MemoryExtensions
nietras 9f420a9
Remove AggressiveInlining attributes
nietras a7f6ddc
Add license to Common
nietras File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
|
||
#if !netstandard | ||
using Internal.Runtime.CompilerServices; | ||
#endif | ||
|
||
namespace System | ||
{ | ||
internal static partial class SpanSortHelpersCommon | ||
{ | ||
// This is the threshold where Introspective sort switches to Insertion sort. | ||
// Empirically, 16 seems to speed up most cases without slowing down others, at least for integers. | ||
// Large value types may benefit from a smaller number. | ||
internal const int IntrosortSizeThreshold = 16; | ||
|
||
internal static int FloorLog2PlusOne(int n) | ||
{ | ||
Debug.Assert(n >= 2); | ||
int result = 2; | ||
n >>= 2; | ||
while (n > 0) | ||
{ | ||
++result; | ||
n >>= 1; | ||
} | ||
return result; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal static void Swap<T>(ref T items, int i, int j) | ||
{ | ||
Debug.Assert(i != j); | ||
Swap(ref Unsafe.Add(ref items, i), ref Unsafe.Add(ref items, j)); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal static void Swap<T>(ref T a, ref T b) | ||
{ | ||
T temp = a; | ||
a = b; | ||
b = temp; | ||
} | ||
|
||
// This started out with just LessThan. | ||
// However, due to bogus comparers, comparables etc. | ||
// we need to preserve semantics completely to get same result. | ||
internal interface IDirectComparer<in T> | ||
{ | ||
bool GreaterThan(T x, T y); | ||
bool LessThan(T x, T y); | ||
} | ||
|
||
// | ||
// Type specific DirectComparer(s) to ensure optimal code-gen | ||
// | ||
internal struct SByteDirectComparer : IDirectComparer<sbyte> | ||
{ | ||
public bool GreaterThan(sbyte x, sbyte y) => x > y; | ||
public bool LessThan(sbyte x, sbyte y) => x < y; | ||
} | ||
internal struct ByteDirectComparer : IDirectComparer<byte> | ||
{ | ||
public bool GreaterThan(byte x, byte y) => x > y; | ||
public bool LessThan(byte x, byte y) => x < y; | ||
} | ||
internal struct Int16DirectComparer : IDirectComparer<short> | ||
{ | ||
public bool GreaterThan(short x, short y) => x > y; | ||
public bool LessThan(short x, short y) => x < y; | ||
} | ||
internal struct UInt16DirectComparer : IDirectComparer<ushort> | ||
{ | ||
public bool GreaterThan(ushort x, ushort y) => x > y; | ||
public bool LessThan(ushort x, ushort y) => x < y; | ||
} | ||
internal struct Int32DirectComparer : IDirectComparer<int> | ||
{ | ||
public bool GreaterThan(int x, int y) => x > y; | ||
public bool LessThan(int x, int y) => x < y; | ||
} | ||
internal struct UInt32DirectComparer : IDirectComparer<uint> | ||
{ | ||
public bool GreaterThan(uint x, uint y) => x > y; | ||
public bool LessThan(uint x, uint y) => x < y; | ||
} | ||
internal struct Int64DirectComparer : IDirectComparer<long> | ||
{ | ||
public bool GreaterThan(long x, long y) => x > y; | ||
public bool LessThan(long x, long y) => x < y; | ||
} | ||
internal struct UInt64DirectComparer : IDirectComparer<ulong> | ||
{ | ||
public bool GreaterThan(ulong x, ulong y) => x > y; | ||
public bool LessThan(ulong x, ulong y) => x < y; | ||
} | ||
internal struct SingleDirectComparer : IDirectComparer<float> | ||
{ | ||
public bool GreaterThan(float x, float y) => x > y; | ||
public bool LessThan(float x, float y) => x < y; | ||
} | ||
internal struct DoubleDirectComparer : IDirectComparer<double> | ||
{ | ||
public bool GreaterThan(double x, double y) => x > y; | ||
public bool LessThan(double x, double y) => x < y; | ||
} | ||
|
||
internal interface IIsNaN<T> | ||
{ | ||
bool IsNaN(T value); | ||
} | ||
internal struct SingleIsNaN : IIsNaN<float> | ||
{ | ||
public bool IsNaN(float value) => float.IsNaN(value); | ||
} | ||
internal struct DoubleIsNaN : IIsNaN<double> | ||
{ | ||
public bool IsNaN(double value) => double.IsNaN(value); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use default implementation syntax? If yes perhaps we could get more short source code.