-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from koculu/71-enhancement-irefcomparer-extens…
…ions Add utility class and extension methods to simplify dealing with IRefComparer.
- Loading branch information
Showing
4 changed files
with
87 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Tenray.ZoneTree.Comparers; | ||
|
||
public sealed class ComparerFromRefComparer<TKey> : IComparer<TKey> | ||
{ | ||
public readonly IRefComparer<TKey> RefComparer; | ||
|
||
public ComparerFromRefComparer(IRefComparer<TKey> refComparer) | ||
{ | ||
RefComparer = refComparer; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public int Compare(TKey x, TKey y) | ||
{ | ||
return RefComparer.Compare(x, y); | ||
} | ||
} |
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,66 @@ | ||
using System.Runtime.CompilerServices; | ||
using Tenray.ZoneTree.Comparers; | ||
|
||
namespace Tenray.ZoneTree.Comparers; | ||
|
||
public static class RefComparerExtensions | ||
{ | ||
public static IComparer<TKey> ToComparer<TKey>(this IRefComparer<TKey> refComparer) | ||
{ | ||
return new ComparerFromRefComparer<TKey>(refComparer); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool AreEqual<TKey>( | ||
this IRefComparer<TKey> refComparer, | ||
in TKey a, | ||
in TKey b) | ||
{ | ||
return refComparer.Compare(a, b) == 0; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool AreNotEqual<TKey>( | ||
this IRefComparer<TKey> refComparer, | ||
in TKey a, | ||
in TKey b) | ||
{ | ||
return refComparer.Compare(a, b) != 0; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool ALessThanB<TKey>( | ||
this IRefComparer<TKey> refComparer, | ||
in TKey a, | ||
in TKey b) | ||
{ | ||
return refComparer.Compare(a, b) < 0; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool ALessOrEqualToB<TKey>( | ||
this IRefComparer<TKey> refComparer, | ||
in TKey a, | ||
in TKey b) | ||
{ | ||
return refComparer.Compare(a, b) <= 0; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool AGreaterThanB<TKey>( | ||
this IRefComparer<TKey> refComparer, | ||
in TKey a, | ||
in TKey b) | ||
{ | ||
return refComparer.Compare(a, b) > 0; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool AGreaterOrEqualToB<TKey>( | ||
this IRefComparer<TKey> refComparer, | ||
in TKey a, | ||
in TKey b) | ||
{ | ||
return refComparer.Compare(a, b) >= 0; | ||
} | ||
} |
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