public HierarchyId GetReparentedValue(HierarchyId oldRoot, HierarchyId newRoot) { if (oldRoot == null || newRoot == null) { return(new HierarchyId(null)); } if (!IsDescendantOf(oldRoot)) { throw new ArgumentException( string.Format(CultureInfo.InvariantCulture, GetReparentedValueOldRootExceptionMessage, oldRoot, ToString()), "oldRoot"); } StringBuilder sb = new StringBuilder(); sb.Append(PathSeparator); foreach (var node in newRoot._nodes) { sb.Append(IntArrayToStirng(node)); sb.Append(PathSeparator); } foreach (var node in _nodes.Skip(oldRoot.GetLevel())) { sb.Append(IntArrayToStirng(node)); sb.Append(PathSeparator); } return(new HierarchyId(sb.ToString())); }
/// <summary> /// Returns true if this is a descendant of parent. /// </summary> /// <returns>True if this is a descendant of parent.</returns> /// <param name="parent">parent</param> public bool IsDescendantOf(HierarchyId parent) { if (parent == null) { return(true); } if (_nodes == null || parent.GetLevel() > GetLevel()) { return(false); } for (int i = 0; i < parent.GetLevel(); i++) { int cmp = CompareIntArrays(_nodes[i], parent._nodes[i]); if (cmp != 0) { return(false); } } return(true); }
/// <summary> /// Returns a child node of the parent. /// </summary> /// <param name="child1"> null or the hierarchyid of a child of the current node. </param> /// <param name="child2"> null or the hierarchyid of a child of the current node. </param> /// <returns> /// Returns one child node that is a descendant of the parent. /// If parent is null, returns null. /// If parent is not null, and both child1 and child2 are null, returns a child of parent. /// If parent and child1 are not null, and child2 is null, returns a child of parent greater than child1. /// If parent and child2 are not null and child1 is null, returns a child of parent less than child2. /// If parent, child1, and child2 are not null, returns a child of parent greater than child1 and less than child2. /// If child1 is not null and not a child of parent, an exception is raised. /// If child2 is not null and not a child of parent, an exception is raised. /// If child1 >= child2, an exception is raised. /// </returns> public HierarchyId GetDescendant(HierarchyId child1, HierarchyId child2) { if (_nodes == null) { return(new HierarchyId(null)); } if (child1 != null && (child1.GetLevel() != GetLevel() + 1 || !child1.IsDescendantOf(this))) { throw new ArgumentException( string.Format(CultureInfo.InvariantCulture, GetDescendantMostBeChildExceptionMessage, "child1", child1, ToString()), "child1"); } if (child2 != null && (child2.GetLevel() != GetLevel() + 1 || !child2.IsDescendantOf(this))) { throw new ArgumentException( string.Format(CultureInfo.InvariantCulture, GetDescendantMostBeChildExceptionMessage, "child2", child1, ToString()), "child2"); } if (child1 == null && child2 == null) { return(new HierarchyId(ToString() + 1 + PathSeparator)); } string hierarchyStr; if (child1 == null) { var result = new HierarchyId(child2.ToString()); var lastNode = result._nodes.Last(); //decrease the last part of the last node of the 1nd child lastNode[lastNode.Length - 1]--; hierarchyStr = PathSeparator + string.Join(PathSeparator, result._nodes.Select(IntArrayToStirng)) + PathSeparator; return(new HierarchyId(hierarchyStr)); } if (child2 == null) { var result = new HierarchyId(child1.ToString()); var lastNode = result._nodes.Last(); //increase the last part of the last node of the 2nd child lastNode[lastNode.Length - 1]++; hierarchyStr = PathSeparator + string.Join(PathSeparator, result._nodes.Select(IntArrayToStirng)) + PathSeparator; return(new HierarchyId(hierarchyStr)); } var child1LastNode = child1._nodes.Last(); var child2LastNode = child2._nodes.Last(); var cmp = CompareIntArrays(child1LastNode, child2LastNode); if (cmp >= 0) { throw new ArgumentException( string.Format(CultureInfo.InvariantCulture, GetDescendantChild1MustLessThanChild2ExceptionMessage, child1, child2), "child1"); } int firstDiffrenceIdx = 0; for (; firstDiffrenceIdx < child1LastNode.Length; firstDiffrenceIdx++) { if (child1LastNode[firstDiffrenceIdx] < child2LastNode[firstDiffrenceIdx]) { break; } } child1LastNode = child1LastNode.Take(firstDiffrenceIdx + 1).ToArray(); if (child1LastNode[firstDiffrenceIdx] + 1 < child2LastNode[firstDiffrenceIdx]) { child1LastNode[firstDiffrenceIdx]++; } else { child1LastNode = child1LastNode.Concat(new[] { 1 }).ToArray(); } hierarchyStr = PathSeparator + string.Join(PathSeparator, _nodes.Select(IntArrayToStirng)) + PathSeparator + IntArrayToStirng(child1LastNode) + PathSeparator; return(new HierarchyId(hierarchyStr)); }