public void Populate(IObjectWithPathAndChildren source) { if (name.isNullOrEmpty()) { name = source.name; } var allChilren = source.getAllChildren(); foreach (var item in allChilren) { Int32 ChildrenCount = 0; foreach (var citem in item) { ChildrenCount++; } ChildrenCountRange.Learn(ChildrenCount); if (ChildrenCount == 0) { LeafNodes++; LeafLevelRange.Learn(item.level); } LevelRange.Learn(item.level); } TotalCount = allChilren.Count; }
//public static List<IObjectWithPathAndChildren> getOnlyWithFullBranch(this IEnumerable<IObjectWithPathAndChildren> children, Regex pathRegex, Regex nameRegex = null) //{ // List<IObjectWithPathAndChildren> output = new List<IObjectWithPathAndChildren>(); // foreach (IObjectWithPathAndChildren child in children) // { // if (pathRegex.IsMatch(child.path)) // { // if (nameRegex != null) // { // if (nameRegex.IsMatch(child.name)) output.Add(child); // } // else // { // output.Add(child); // } // } // else { } // } // return output; //} /// <summary> /// Gets all leafs, optionally applies Regex criteria used to child name [[[doesn't work]]] /// </summary> /// <param name="parent">The parent.</param> /// <param name="nameFilter">The name filter.</param> /// <returns></returns> public static List <IObjectWithPathAndChildren> getAllLeafs(this IObjectWithPathAndChildren parent, Regex nameFilter = null, Boolean inverse = false) { List <IObjectWithPathAndChildren> output = new List <IObjectWithPathAndChildren>(); List <IObjectWithPathAndChildren> stack = new List <IObjectWithPathAndChildren>(); stack.Add(parent); while (stack.Any()) { var n_stack = new List <IObjectWithPathAndChildren>(); foreach (IObjectWithPathAndChildren child in stack) { if (child.Any()) { foreach (IObjectWithPathAndChildren c in child) { n_stack.Add(c); } } else { if (nameFilter != null) { if (nameFilter.IsMatch(child.name)) { if (!inverse) { output.Add(child); } } else { if (inverse) { output.Add(child); } } } else { output.Add(child); } } } stack = n_stack; } return(output); }
/// <summary> /// Gets all children. If pathFilder defined, it uses it to select only children with appropriate path /// </summary> /// <param name="parent">The parent.</param> /// <param name="pathFilter">The path filter.</param> /// <param name="inverse">if set to <c>true</c> it inverses the filter</param> /// <param name="orderByPath">if set to <c>true</c> [order by path].</param> /// <param name="recursiveIndex">Index of the recursive calls (if orderByPath is true)</param> /// <param name="recursiveLimit">The recursive calls limit.</param> /// <returns></returns> public static List <IObjectWithPathAndChildren> getAllChildren(this IObjectWithPathAndChildren parent, Regex pathFilter = null, Boolean inverse = false, Boolean orderByPath = false, Int32 recursiveIndex = 1, Int32 recursiveLimit = 500, Boolean includingParent = false) { List <IObjectWithPathAndChildren> output = new List <IObjectWithPathAndChildren>(); List <IObjectWithPathAndChildren> stack = new List <IObjectWithPathAndChildren>(); stack.Add(parent); if (recursiveIndex > recursiveLimit) { #if DEBUG Console.WriteLine("RECURSIVE LIMIT REACHED [" + recursiveIndex + "] in getAllChildren - [" + parent.name + "] (" + parent.GetType().Name + ")"); #endif return(output); } while (stack.Any()) { var n_stack = new List <IObjectWithPathAndChildren>(); foreach (IObjectWithPathAndChildren child in stack) { if (orderByPath) { if (pathFilter != null) { if (pathFilter.IsMatch(child.path)) { if (!inverse) { output.Add(child); } } else { if (inverse) { output.Add(child); } } } else { output.Add(child); } if (child.Any()) { foreach (IObjectWithPathAndChildren c in child) { output.AddRange(getAllChildren(c, pathFilter, inverse, orderByPath, recursiveIndex + 1, recursiveLimit, true)); } } } else { if (pathFilter != null) { if (!child.path.isNullOrEmpty()) { if (pathFilter.IsMatch(child.path)) { if (!inverse) { output.Add(child); } } else { if (inverse) { output.Add(child); } } } } else { output.Add(child); } if (child != null) { if (child.Any()) { foreach (IObjectWithPathAndChildren c in child) { n_stack.Add(c); } } } } } stack = n_stack; } if (!includingParent) { output.Remove(parent); } return(output); }