namespace Thor.Shared.Visit; public class Grid { public string Identifier { get; set; } public string Name { get; set; } protected HashSet Titles { get; set; } = new(); protected HashSet 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 GetItems(IEnumerable ids) { var lst = ids.ToHashSet(); foreach (var i in GetItems() .Where( t => lst.Contains(t.Identifier))) { yield return i; } } public IEnumerable GetItems() { return Titles.Union(Verifications).OrderBy(x => x.OrderId); } public IEnumerable GetChildren(IEnumerable parentsid) { var lst = parentsid.ToHashSet(); foreach (var i in Titles .Where( i => lst.Contains(i.ParentId))) { yield return i; } } public IEnumerable GetChildren(string id) { List toExplore = new(); HashSet 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); } }