Skip to content

Commit

Permalink
Optional Comparison: reduce code coupling
Browse files Browse the repository at this point in the history
  • Loading branch information
andreise committed Jan 12, 2025
1 parent 528396b commit f1c4d07
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace System;

Expand All @@ -13,20 +12,8 @@ internal InternalComparer(IComparer<T> comparer)
=>
this.comparer = comparer;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Compare(Optional<T> x, Optional<T> y)
{
if (x.hasValue != y.hasValue)
{
return x.hasValue ? ComparisonResult.GreaterThan : ComparisonResult.LessThan;
}

if (x.hasValue)
{
return ComparisonResult.Normalize(comparer.Compare(x.value, y.value));
}

return ComparisonResult.EqualTo;
}
=>
x.InternalCompareTo(y, comparer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace System;

partial struct Optional<T>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal int InternalCompareTo(Optional<T> other, IComparer<T> comparer)
{
if (hasValue != other.hasValue)
{
return hasValue ? ComparisonResult.GreaterThan : ComparisonResult.LessThan;
}

if (hasValue)
{
return ComparisonResult.Normalize(comparer.Compare(value, other.value));
}

return ComparisonResult.EqualTo;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
namespace System;
using System.Collections.Generic;

namespace System;

partial class Optional
{
// TODO: Add the tests and open the method
internal static int Compare<T>(Optional<T> left, Optional<T> right)
where T : IComparable<T>
=>
OptionalComparer<T>.Default.Compare(left, right);
left.InternalCompareTo(right, Comparer<T>.Default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace System;
internal sealed class OptionalComparer<T> : IComparer<Optional<T>>
where T : IComparable<T>
{
private readonly Optional<T>.InternalComparer comparer;
private readonly IComparer<T> comparer;

private OptionalComparer(IComparer<T> comparer)
=>
this.comparer = new(comparer);
this.comparer = comparer;

public static OptionalComparer<T> Create(IComparer<T>? comparer)
=>
Expand All @@ -24,7 +24,7 @@ public static OptionalComparer<T> Create()

public int Compare(Optional<T> x, Optional<T> y)
=>
comparer.Compare(x, y);
x.InternalCompareTo(y, comparer);

private static class InnerDefault
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
namespace System;
using System.Collections.Generic;

namespace System;

partial class OptionalExtensions
{
// TODO: Add the tests and open the method
internal static int CompareTo<T>(this Optional<T> optional, Optional<T> other)
where T : IComparable<T>
=>
OptionalComparer<T>.Default.Compare(optional, other);
optional.InternalCompareTo(other, Comparer<T>.Default);
}

0 comments on commit f1c4d07

Please sign in to comment.