using Hast.Common.Interfaces;
using Hast.Common.Models;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Hast.Common.Extensions;
///
/// Extension for ordering collections.
///
public static class RequirementExtensions
{
///
/// Returns a new collection where the items required by others are guaranteed to be prior to the ones requiring
/// them. This way their load order will satisfy their requirements. Beside this, the order of items is an undefined
/// implementation detail. If there are no requirements the order is kept.
///
/// The original collection.
/// The type of .
/// The T in .
public static IReadOnlyCollection OrderByRequirements(this IEnumerable source)
where TItem : IRequirement
where TKey : IEquatable
{
var items = source is IList list ? list : source.ToList();
var keys = items.Select(item => item.Name).ToList();
CheckRequirements(items, keys);
var descendantsByAncestor = items
.SelectMany(item => item.Requirements.Select(requirement => new { item.Name, Ancestor = requirement }))
.GroupBy(relationship => relationship.Ancestor)
.ToDictionary(group => group.Key, group => group.Select(relationship => relationship.Name).ToList());
var ancestors = descendantsByAncestor.SelectMany(pair => pair.Value).Distinct().ToHashSet();
var roots = items
.Where(item => !ancestors.Contains(item.Name))
.Select(item => new RequirementTreeNode { Data = item })
.ToList();
CreateForest(roots, descendantsByAncestor, items.ToDictionary(item => item.Name));
PruneDuplicates(roots, new Dictionary>>());
var results = new List();
BuildResults(results, roots);
return results;
}
private static void CheckRequirements(IList items, IList keys)
where TItem : IRequirement
{
// Look for impossible requirements.
foreach (var item in items)
{
foreach (var requirement in item.Requirements)
{
if (!keys.Contains(requirement))
{
throw new KeyNotFoundException($"The required service \"{requirement}\" was not found!");
}
}
}
}
private static void CreateForest(
IList> parents,
IReadOnlyDictionary> descendantsByAncestor,
IReadOnlyDictionary itemsDictionary)
where TItem : IRequirement
{
if (parents.Count == 0) return;
foreach (var parent in parents)
{
if (descendantsByAncestor.TryGetValue(parent.Data.Name, out var descendants))
{
var subCollection = descendants
.Select(key => new RequirementTreeNode { Data = itemsDictionary[key] });
parent.Children.AddRange(subCollection);
}
CreateForest(parent.Children, descendantsByAncestor, itemsDictionary);
}
}
private static void PruneDuplicates(
IList> parents,
IDictionary>> containersByName)
where TItem : IRequirement
where TKey : IEquatable
{
foreach (var parent in parents)
{
var key = parent.Data.Name;
if (containersByName.TryGetValue(key, out var oldContainer))
{
oldContainer.RemoveAll(item => item.Data.Name.Equals(key));
}
containersByName[key] = parents;
PruneDuplicates(parent.Children, containersByName);
}
}
private static void BuildResults(
ICollection results,
IEnumerable> parents)
where TItem : IRequirement
{
foreach (var parent in parents)
{
results.Add(parent.Data);
BuildResults(results, parent.Children);
}
}
}