Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement API for working with graph-like relations #35

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PropertyGroup>
<RepositoryUrl>https://github.com/kysect/Kysect.CommonLib</RepositoryUrl>
<PackageProjectUrl>https://github.com/kysect/Kysect.CommonLib</PackageProjectUrl>
<Version>0.1.16</Version>
<Version>0.1.17</Version>
</PropertyGroup>

<!-- Code configuration -->
Expand Down
20 changes: 20 additions & 0 deletions Sources/Kysect.CommonLib/Graphs/GraphBuildResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Kysect.CommonLib.Graphs;

public class GraphBuildResult<TKey, T> where TKey : notnull
{
public GraphBuildResult(IReadOnlyCollection<GraphNode<TKey, T>> roots)
{
Roots = roots;
}

public IReadOnlyCollection<GraphNode<TKey, T>> Roots { get; }

public GraphNode<TKey, T> GetValue(TKey id)
{
GraphNode<TKey, T>? found = Roots
.Select(root => root.Find(id))
.FirstOrDefault(value => value is not null);

return found ?? throw new ArgumentException($"Work item with id {id} was not found");
}
}
52 changes: 52 additions & 0 deletions Sources/Kysect.CommonLib/Graphs/GraphBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Kysect.CommonLib.BaseTypes.Extensions;

namespace Kysect.CommonLib.Graphs;

public static class GraphBuilder
{
public static GraphBuildResult<TKey, T> Build<TKey, T>(
IReadOnlyCollection<TKey> nodes,
IReadOnlyCollection<GraphLink<TKey>> links,
GraphValueResolver<TKey, T> resolver)
where TKey : notnull
{
nodes.ThrowIfNull();
links.ThrowIfNull();
resolver.ThrowIfNull();

var targetNodes = new HashSet<TKey>(links.Select(l => l.To));

var rootNodes = nodes
.Where(l => !targetNodes.Contains(l))
.ToList();

ILookup<TKey, TKey> nodeLinks = links.ToLookup(l => l.From, l => l.To);

var result = new List<GraphNode<TKey, T>>();

foreach (TKey rootNode in rootNodes)
result.Add(BuildNode(rootNode, nodeLinks, resolver));

return new GraphBuildResult<TKey, T>(result);
}

private static GraphNode<TKey, T> BuildNode<TKey, T>(TKey id, ILookup<TKey, TKey> nodeLinks, IGraphValueResolver<TKey, T> resolver) where TKey : notnull
{
IReadOnlyCollection<GraphNode<TKey, T>> children = nodeLinks.Contains(id)
? BuildChildren(nodeLinks[id], nodeLinks, resolver)
: Array.Empty<GraphNode<TKey, T>>();

return new GraphNode<TKey, T>(id, resolver.Resolve(id), children);
}

private static IReadOnlyCollection<GraphNode<TKey, T>> BuildChildren<TKey, T>(
IEnumerable<TKey> identifiers,
ILookup<TKey, TKey> nodeLinks,
IGraphValueResolver<TKey, T> resolver)
where TKey : notnull
{
return identifiers
.Select(childId => BuildNode(childId, nodeLinks, resolver))
.ToList();
}
}
12 changes: 12 additions & 0 deletions Sources/Kysect.CommonLib/Graphs/GraphLink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Diagnostics.Contracts;

namespace Kysect.CommonLib.Graphs;

public record struct GraphLink<TKey>(TKey From, TKey To)
{
[Pure]
public readonly GraphLink<TKey> Reversed()
{
return new GraphLink<TKey>(From: To, To: From);
}
}
40 changes: 40 additions & 0 deletions Sources/Kysect.CommonLib/Graphs/GraphNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace Kysect.CommonLib.Graphs;

public class GraphNode<TKey, T> where TKey : notnull
{
public TKey Id { get; }
public T Value { get; }
public IReadOnlyCollection<GraphNode<TKey, T>> DirectChildren { get; }

public GraphNode(TKey id, T value, IReadOnlyCollection<GraphNode<TKey, T>> directChildren)
{
Id = id;
Value = value;
DirectChildren = directChildren;
}

public IEnumerable<GraphNode<TKey, T>> EnumerateChildren()
{
if (!DirectChildren.Any())
return Array.Empty<GraphNode<TKey, T>>();

return DirectChildren
.Concat(DirectChildren.SelectMany(c => c.EnumerateChildren()))
.ToList();
}

public GraphNode<TKey, T>? Find(TKey id)
{
if (Id.Equals(id))
return this;

return DirectChildren
.Select(node => node.Find(id))
.FirstOrDefault(founded => founded is not null);
}

public override string ToString()
{
return $"Node {Value}, Direct children count: {DirectChildren.Count}";
}
}
21 changes: 21 additions & 0 deletions Sources/Kysect.CommonLib/Graphs/GraphPath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Kysect.CommonLib.Graphs;

public class GraphPath<T>
{
public GraphPath(IReadOnlyCollection<T> elements)
{
Elements = elements;
}

public static GraphPath<T> Empty { get; } = new GraphPath<T>(Array.Empty<T>());

public IReadOnlyCollection<T> Elements { get; }

public GraphPath<T> AppendToStart(T value)
{
var result = new List<T>();
result.Add(value);
result.AddRange(Elements);
return new GraphPath<T>(result);
}
}
19 changes: 19 additions & 0 deletions Sources/Kysect.CommonLib/Graphs/GraphValueResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Kysect.CommonLib.Graphs;

public class GraphValueResolver<TKey, T> : IGraphValueResolver<TKey, T>
{
private readonly Dictionary<TKey, T> _map;

public GraphValueResolver(IReadOnlyCollection<T> values, Func<T, TKey> selector)
{
_map = values.ToDictionary(selector, v => v);
}

public T Resolve(TKey id)
{
if (_map.TryGetValue(id, out T? result))
return result;

throw new ArgumentException($"Graph node with id {id} was not found");
}
}
9 changes: 9 additions & 0 deletions Sources/Kysect.CommonLib/Graphs/GraphValueResolverCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Kysect.CommonLib.Graphs;

public static class GraphValueResolverCreator
{
public static GraphValueResolver<TKey, T> Create<TKey, T>(IReadOnlyCollection<T> values, Func<T, TKey> selector)
{
return new GraphValueResolver<TKey, T>(values, selector);
}
}
6 changes: 6 additions & 0 deletions Sources/Kysect.CommonLib/Graphs/IGraphValueResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Kysect.CommonLib.Graphs;

public interface IGraphValueResolver<TKey, T>
{
T Resolve(TKey id);
}