public static void FindCommonAncestorsWithinListWorks() { // Arrange var list = new List <TestElement>(); list.Add(new TestElement("root", -1)); list.Add(new TestElement("A", 0)); var b0 = new TestElement("B", 0); var b1 = new TestElement("Bchild", 1); var b2 = new TestElement("Bchildchild", 2); list.Add(b0); list.Add(b1); list.Add(b2); var c0 = new TestElement("C", 0); list.Add(c0); var f0 = new TestElement("F", 0); var f1 = new TestElement("Fchild", 1); var f2 = new TestElement("Fchildchild", 2); list.Add(f0); list.Add(f1); list.Add(f2); // Init tree structure: set children and parent properties TreeElement.ListToTree(list); // Single element TestElement[] input = { b1 }; TestElement[] expectedResult = { b1 }; var result = TreeElement.FindCommonAncestorsWithinList(input).ToArray(); Assert.IsTrue(UnityEditor.ArrayUtility.ArrayEquals(expectedResult, result), "Single input should return single output"); // Single sub tree input = new[] { b1, b2 }; expectedResult = new[] { b1 }; result = TreeElement.FindCommonAncestorsWithinList(input).ToArray(); Assert.IsTrue(UnityEditor.ArrayUtility.ArrayEquals(expectedResult, result), "Common ancestor should only be b1 "); // Multiple sub trees input = new[] { b0, b2, f0, f2, c0 }; expectedResult = new[] { b0, f0, c0 }; result = TreeElement.FindCommonAncestorsWithinList(input).ToArray(); Assert.IsTrue(UnityEditor.ArrayUtility.ArrayEquals(expectedResult, result), "Common ancestor should only be b0, f0, c0"); }
/// <summary> /// Removes the given elements /// </summary> /// <param name="elements"></param> public void RemoveElements(IList <T> elements) { foreach (var element in elements) { if (element == this.root) { throw new ArgumentException("It is not allowed to remove the root element"); } } var commonAncestors = TreeElement.FindCommonAncestorsWithinList(elements); foreach (var element in commonAncestors) { element.parent.children.Remove(element); element.parent = null; } TreeElement.TreeToList(this.root, this.data); OnChanged(); }