Skip to content

Commit

Permalink
Redesign Optional Comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
andreise committed Jan 12, 2025
1 parent ed5cb41 commit c558b42
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 83 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace System;

partial struct Optional<T>
{
internal sealed class InternalComparer : IComparer<Optional<T>>
{
private readonly IComparer<T> comparer;

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 ? GreaterThan : LessThan;
}

if (x.hasValue)
{
return comparer.Compare(x.value, y.value) switch
{
> EqualTo => GreaterThan,
< EqualTo => LessThan,
_ => EqualTo
};
}

return EqualTo;
}

private const int GreaterThan = 1;
private const int EqualTo = default;
private const int LessThan = -1;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
using System.Collections.Generic;

namespace System;
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>
=>
left.InternalCompareTo(right, Comparer<T>.Default);

// TODO: Add the tests and open the method
internal static IComparer<Optional<T>> CreateComparer<T>(IComparer<T> comparer)
=>
new Optional<T>.InternalComparer(comparer ?? throw new ArgumentNullException(nameof(comparer)));
OptionalComparer<T>.Default.Compare(left, right);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace System;

// TODO: Add the tests and open the class
internal sealed class OptionalComparer<T> : IComparer<Optional<T>> where T : IComparable<T>
internal sealed class OptionalComparer<T> : IComparer<Optional<T>>
where T : IComparable<T>
{
private readonly IComparer<T> comparer;
private readonly Optional<T>.InternalComparer comparer;

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

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

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

private static class InnerDefault
{
Expand Down
11 changes: 0 additions & 11 deletions src/core-taggeds-optional/Optional/OptionalExtensions/CompareTo.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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);
}

0 comments on commit c558b42

Please sign in to comment.