/// <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) { int[][] numArray1 = (object)hid1 == null ? (int[][])null : hid1._nodes; int[][] numArray2 = (object)hid2 == null ? (int[][])null : hid2._nodes; if (numArray1 == null && numArray2 == null) { return(0); } if (numArray1 == null) { return(-1); } if (numArray2 == null) { return(1); } int num1 = Math.Min(numArray1.Length, numArray2.Length); for (int index = 0; index < num1; ++index) { int num2 = HierarchyId.CompareIntArrays(numArray1[index], numArray2[index]); if (num2 != 0) { return(num2); } } if (hid1._nodes.Length > num1) { return(1); } return(hid2._nodes.Length > num1 ? -1 : 0); }
/// <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 (this._nodes == null) { return(new HierarchyId((string)null)); } if (child1 != (HierarchyId)null && ((int)child1.GetLevel() != (int)this.GetLevel() + 1 || !child1.IsDescendantOf(this))) { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "HierarchyId.GetDescendant failed because '{0}' must be a child of 'this'. '{0}' was '{1}' and 'this' was '{2}'.", (object)nameof(child1), (object)child1, (object)this.ToString()), nameof(child1)); } if (child2 != (HierarchyId)null && ((int)child2.GetLevel() != (int)this.GetLevel() + 1 || !child2.IsDescendantOf(this))) { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "HierarchyId.GetDescendant failed because '{0}' must be a child of 'this'. '{0}' was '{1}' and 'this' was '{2}'.", (object)nameof(child2), (object)child1, (object)this.ToString()), nameof(child2)); } if (child1 == (HierarchyId)null && child2 == (HierarchyId)null) { return(new HierarchyId(this.ToString() + (object)1 + "/")); } if (child1 == (HierarchyId)null) { HierarchyId hierarchyId = new HierarchyId(child2.ToString()); int[] numArray = ((IEnumerable <int[]>)hierarchyId._nodes).Last <int[]>(); --numArray[numArray.Length - 1]; return(new HierarchyId("/" + string.Join("/", ((IEnumerable <int[]>)hierarchyId._nodes).Select <int[], string>(new Func <int[], string>(HierarchyId.IntArrayToStirng))) + "/")); } if (child2 == (HierarchyId)null) { HierarchyId hierarchyId = new HierarchyId(child1.ToString()); int[] numArray = ((IEnumerable <int[]>)hierarchyId._nodes).Last <int[]>(); ++numArray[numArray.Length - 1]; return(new HierarchyId("/" + string.Join("/", ((IEnumerable <int[]>)hierarchyId._nodes).Select <int[], string>(new Func <int[], string>(HierarchyId.IntArrayToStirng))) + "/")); } int[] array1 = ((IEnumerable <int[]>)child1._nodes).Last <int[]>(); int[] array2 = ((IEnumerable <int[]>)child2._nodes).Last <int[]>(); if (HierarchyId.CompareIntArrays(array1, array2) >= 0) { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "HierarchyId.GetDescendant failed because 'child1' must be less than 'child2'. 'child1' was '{0}' and 'child2' was '{1}'.", (object)child1, (object)child2), nameof(child1)); } int index = 0; while (index < array1.Length && array1[index] >= array2[index]) { ++index; } int[] array = ((IEnumerable <int>)array1).Take <int>(index + 1).ToArray <int>(); if (array[index] + 1 < array2[index]) { ++array[index]; } else { array = ((IEnumerable <int>)array).Concat <int>((IEnumerable <int>) new int[1] { 1 }).ToArray <int>(); } return(new HierarchyId("/" + string.Join("/", ((IEnumerable <int[]>) this._nodes).Select <int[], string>(new Func <int[], string>(HierarchyId.IntArrayToStirng))) + "/" + HierarchyId.IntArrayToStirng((IEnumerable <int>)array) + "/")); }
/// <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 == (HierarchyId)null) { return(true); } if (this._nodes == null || (int)parent.GetLevel() > (int)this.GetLevel()) { return(false); } for (int index = 0; index < (int)parent.GetLevel(); ++index) { if (HierarchyId.CompareIntArrays(this._nodes[index], parent._nodes[index]) != 0) { return(false); } } return(true); }