Thor/Thor.Shared/Grid.cs
2025-02-10 18:38:27 +01:00

101 lines
2.5 KiB
C#

using System.Collections;
namespace Thor.Shared;
public class Grid
{
public string Identifier { get; set; }
public string Name { get; set; }
protected HashSet<ItemTitle> Titles { get; set; } = new();
protected HashSet<ItemVerification> Verifications { get; set; } = new();
public Item? GetItem(string id)
{
var title = Titles.Where(t => t.Identifier == id).FirstOrDefault();
if (title is not null)
return title;
var verification = Verifications.Where(v => v.Identifier == id).FirstOrDefault();
if(verification is not null)
return verification;
return null;
}
public IEnumerable<Item> GetItems(IEnumerable<string> ids)
{
var lst = ids.ToHashSet();
foreach (var i
in GetItems()
.Where(
t => lst.Contains(t.Identifier)))
{
yield return i;
}
}
public IEnumerable<Item> GetItems()
{
return Enumerable.Union<Item>(Titles, Verifications).OrderBy(x => x.OrderId);
}
public IEnumerable<Item> GetChildren(IEnumerable<string> parentsid)
{
var lst = parentsid.ToHashSet();
foreach (var i
in Titles
.Where(
i => lst.Contains(i.ParentId)))
{
yield return i;
}
}
public IEnumerable<Item> GetChildren(string id)
{
List<string> toExplore = new();
HashSet<Item> toReturn = new();
toExplore.Add(id);
while (toExplore.Any())
{
var items = GetItems(toExplore);
toExplore.Clear();
foreach (var item in items)
{
if (toReturn.Contains(item))
continue;
toReturn.Add(item);
if(item is ItemTitle title)
{
var children = GetChildren(title.Identifier);
foreach (var child in children)
if(!toReturn.Contains(child))
toExplore.Add(child.Identifier);
}
}
}
return toReturn;
}
public void AddItem(Item item)
{
if (item is ItemTitle title)
Titles.Add(title);
else if (item is ItemVerification verification)
Verifications.Add(verification);
else throw new NotImplementedException();
}
public void AddItems(params Item[] items)
{
foreach (var i in items)
AddItem(i);
}
}