Skip to content

Commit 9b45449

Browse files
authored
Merge pull request #72 from koculu/71-enhancement-irefcomparer-extensions
Add utility class and extension methods to simplify dealing with IRefComparer.
2 parents dc4f6bf + b63f18d commit 9b45449

File tree

4 files changed

+87
-3
lines changed

4 files changed

+87
-3
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Runtime.CompilerServices;
2+
3+
namespace Tenray.ZoneTree.Comparers;
4+
5+
public sealed class ComparerFromRefComparer<TKey> : IComparer<TKey>
6+
{
7+
public readonly IRefComparer<TKey> RefComparer;
8+
9+
public ComparerFromRefComparer(IRefComparer<TKey> refComparer)
10+
{
11+
RefComparer = refComparer;
12+
}
13+
14+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
15+
public int Compare(TKey x, TKey y)
16+
{
17+
return RefComparer.Compare(x, y);
18+
}
19+
}

src/ZoneTree/Comparers/IRefComparer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace Tenray.ZoneTree.Comparers;
22

3-
43
/// <summary>
54
/// Defines a method that a type implements to compare two objects.
65
/// </summary>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System.Runtime.CompilerServices;
2+
using Tenray.ZoneTree.Comparers;
3+
4+
namespace Tenray.ZoneTree.Comparers;
5+
6+
public static class RefComparerExtensions
7+
{
8+
public static IComparer<TKey> ToComparer<TKey>(this IRefComparer<TKey> refComparer)
9+
{
10+
return new ComparerFromRefComparer<TKey>(refComparer);
11+
}
12+
13+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
14+
public static bool AreEqual<TKey>(
15+
this IRefComparer<TKey> refComparer,
16+
in TKey a,
17+
in TKey b)
18+
{
19+
return refComparer.Compare(a, b) == 0;
20+
}
21+
22+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
23+
public static bool AreNotEqual<TKey>(
24+
this IRefComparer<TKey> refComparer,
25+
in TKey a,
26+
in TKey b)
27+
{
28+
return refComparer.Compare(a, b) != 0;
29+
}
30+
31+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
32+
public static bool ALessThanB<TKey>(
33+
this IRefComparer<TKey> refComparer,
34+
in TKey a,
35+
in TKey b)
36+
{
37+
return refComparer.Compare(a, b) < 0;
38+
}
39+
40+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
41+
public static bool ALessOrEqualToB<TKey>(
42+
this IRefComparer<TKey> refComparer,
43+
in TKey a,
44+
in TKey b)
45+
{
46+
return refComparer.Compare(a, b) <= 0;
47+
}
48+
49+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
50+
public static bool AGreaterThanB<TKey>(
51+
this IRefComparer<TKey> refComparer,
52+
in TKey a,
53+
in TKey b)
54+
{
55+
return refComparer.Compare(a, b) > 0;
56+
}
57+
58+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
59+
public static bool AGreaterOrEqualToB<TKey>(
60+
this IRefComparer<TKey> refComparer,
61+
in TKey a,
62+
in TKey b)
63+
{
64+
return refComparer.Compare(a, b) >= 0;
65+
}
66+
}

src/ZoneTree/Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<Authors>Ahmed Yasin Koculu</Authors>
66
<PackageId>ZoneTree</PackageId>
77
<Title>ZoneTree</Title>
8-
<ProductVersion>1.7.4.0</ProductVersion>
9-
<Version>1.7.4.0</Version>
8+
<ProductVersion>1.7.5.0</ProductVersion>
9+
<Version>1.7.5.0</Version>
1010
<Authors>Ahmed Yasin Koculu</Authors>
1111
<AssemblyTitle>ZoneTree</AssemblyTitle>
1212
<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>

0 commit comments

Comments
 (0)