public DictionaryTree(Func <T, Y> getKey, T root = default, DictionaryTree <T, Y> parent = null) : base() { this.getKey = getKey; this.Data = root; this.Parent = parent; }
public virtual DictionaryTree <T, Y> New(T data) { Y key = getKey(data); var newDictionary = new DictionaryTree <T, Y>(this.getKey, data, this); this.Add(key, newDictionary); return(newDictionary); }
public void Add(DictionaryTree <T, Y> dictionaryTree) { if (dictionaryTree.Parent != this) { throw new ArgumentException("Not in the same parent"); } this.Add(getKey(dictionaryTree.Data), dictionaryTree); }
public static DictionaryTree <Group, Y> ToDictionaryTree <Y>(this DataServiceQuery <Group> groupsDb, Func <Group, Y> getKey, Guid rootGroupId) where Y : class { var groups = groupsDb.ToList(); var childsGroups = groups.Where(g => g.ParentId == rootGroupId).ToList(); var root = new DictionaryTree <Group, Y>(getKey, null); groups.CastListToDictionaryTree(root, childsGroups); return(root); }
private static void CastListToDictionaryTree <Y>(this List <Group> groups, DictionaryTree <Group, Y> rootDictionary, List <Group> rootGroups) where Y : class { foreach (var rootGroup in rootGroups) { var newRootDictionary = rootDictionary.New(rootGroup); var newRootGroups = groups.Where(g => g.ParentId == rootGroup.Id).ToList(); CastListToDictionaryTree(groups, newRootDictionary, newRootGroups); } }
public DictionaryTree <T, Y> this[Y[] keys] { get { DictionaryTree <T, Y> lastDictionary = this; foreach (var key in keys) { if (lastDictionary.ContainsKey(key)) { lastDictionary = lastDictionary[key]; } else { return(null); } } return(lastDictionary); } }