Skip to content

Commit

Permalink
Merge pull request #72 from koculu/71-enhancement-irefcomparer-extens…
Browse files Browse the repository at this point in the history
…ions

Add utility class and extension methods to simplify dealing with IRefComparer.
  • Loading branch information
koculu authored Aug 16, 2024
2 parents dc4f6bf + b63f18d commit 9b45449
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/ZoneTree/Comparers/ComparerFromRefComparer.cs
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);
}
}
1 change: 0 additions & 1 deletion src/ZoneTree/Comparers/IRefComparer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace Tenray.ZoneTree.Comparers;


/// <summary>
/// Defines a method that a type implements to compare two objects.
/// </summary>
Expand Down
66 changes: 66 additions & 0 deletions src/ZoneTree/Comparers/RefComparerExtensions.cs
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;
}
}
4 changes: 2 additions & 2 deletions src/ZoneTree/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<Authors>Ahmed Yasin Koculu</Authors>
<PackageId>ZoneTree</PackageId>
<Title>ZoneTree</Title>
<ProductVersion>1.7.4.0</ProductVersion>
<Version>1.7.4.0</Version>
<ProductVersion>1.7.5.0</ProductVersion>
<Version>1.7.5.0</Version>
<Authors>Ahmed Yasin Koculu</Authors>
<AssemblyTitle>ZoneTree</AssemblyTitle>
<Description>ZoneTree is a persistent, high-performance, transactional, ACID-compliant ordered key-value database for NET. It can operate in memory or on local/cloud storage.</Description>
Expand Down

0 comments on commit 9b45449

Please sign in to comment.