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> /// Compares two HierarchyIds by their values. /// </summary> /// <param name="hid1"> a HierarchyId to compare </param> /// <param name="hid2"> a HierarchyId to compare </param> /// <returns> /// A 32-bit signed integer that indicates the lexical relationship between the two comparands. /// Value Condition Less than zero: hid1 is less than hid2. /// Zero: hid1 equals hid2. /// Greater than zero: hid1 is greater than hid2. /// </returns> public static int Compare(HierarchyId hid1, HierarchyId hid2) { var nodes1 = (object)hid1 == null ? null : hid1._nodes; var nodes2 = (object)hid2 == null ? null : hid2._nodes; if (nodes1 == null && nodes2 == null) { return(0); } if (nodes1 == null) { return(-1); } if (nodes2 == null) { return(1); } int count = Math.Min(nodes1.Length, nodes2.Length); for (int i = 0; i < count; i++) { var node1 = nodes1[i]; var node2 = nodes2[i]; int cmp = CompareIntArrays(node1, node2); if (cmp != 0) { return(cmp); } } if (hid1._nodes.Length > count) { return(1); } if (hid2._nodes.Length > count) { return(-1); } return(0); }
/// <summary> /// Compares this instance to a given HierarchyId by their values. /// </summary> /// <param name="other"> the HierarchyId to compare against this instance </param> /// <returns> true if this instance is equal to the given HierarchyId, and false otherwise </returns> protected bool Equals(HierarchyId other) { return(this == other); }
/// <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)); }
public abstract HierarchyId GetReparentedValue(HierarchyId oldRoot, HierarchyId newRoot);
/// <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 abstract bool IsDescendantOf(HierarchyId parent);
/// <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 abstract HierarchyId GetDescendant(HierarchyId child1, HierarchyId child2);