Skip to content

Commit

Permalink
compilation fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mazharenko committed Dec 18, 2024
1 parent d602136 commit c045e7d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
7 changes: 0 additions & 7 deletions src/aoc/Common/Bfs/Bfs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ public class AdhocAdjacency<T>(Func<T, IEnumerable<(T newState, int weight)>> fu
public IEnumerable<(T newState, int weight)> GetAdjacent(T pos) => function(pos);
}

public delegate bool Target<in T>(T state); // ITarget?

public static class Targets
{
public static Target<T> Value<T>(T value)
=> state => Equals(value, state);
}

public static BfsBuilder<T> StartWith<T>(T start)
{
Expand Down
11 changes: 0 additions & 11 deletions src/aoc/Common/Bfs/Folder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,6 @@ public class AdhocFold<T, TRes>(Func<TRes, Path<T>, (TRes, TraversalResult)> fun
=> function(acc, path);
}

public class SearchFold<T>(Bfs.Common.Target<T> target) : IFold<T, Result<T>>
{
public (Result<T>, TraversalResult) Fold(Result<T> acc, Path<T> path)
{
var (current, _) = path.PathList;
if (target(current.Item))
return (new Found<T>(path), TraversalResult.Interrupt);
return (new NotFound<T>(), TraversalResult.Continue);
}
}

public enum TraversalResult
{
Continue,
Expand Down
22 changes: 22 additions & 0 deletions src/aoc/Common/Bfs/Search.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// ReSharper disable once CheckNamespace

namespace aoc.Common.BfsImpl;

public delegate bool Target<in T>(T state); // ITarget?

public static class Targets
{
public static Target<T> Value<T>(T value)
=> state => Equals(value, state);
}

public class SearchFold<T>(Target<T> target) : IFold<T, Result<T>>
{
public (Result<T>, TraversalResult) Fold(Result<T> acc, Path<T> path)
{
var (current, _) = path.PathList;
if (target(current.Item))
return (new Found<T>(path), TraversalResult.Interrupt);
return (new NotFound<T>(), TraversalResult.Continue);
}
}
14 changes: 13 additions & 1 deletion src/aoc/Common/L.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class L
public static L<T> Create<T>(IList<T> values) => L<T>.Create(values);
}

public record L<T> : IEnumerable<T> // todo: Equality?
public record L<T> : IEnumerable<T>
{
private readonly T? value;
private readonly L<T>? tail;
Expand Down Expand Up @@ -61,6 +61,7 @@ public L<T> Append(L<T> newTail)
}

public T Head => empty ? throw new InvalidOperationException("List is empty") : value!;
public L<T>? Tail => tail;

public bool IsEmpty => empty;

Expand All @@ -71,4 +72,15 @@ public void Deconstruct(out T head, out L<T>? tail1)
tail1 = tail;
}

public virtual bool Equals(L<T>? other)
{
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;
return EqualityComparer<T?>.Default.Equals(value, other.value) && Equals(tail, other.tail) && empty == other.empty;
}

public override int GetHashCode()
{
return HashCode.Combine(value, tail, empty);
}
}

0 comments on commit c045e7d

Please sign in to comment.