Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
        public HierarchyId GetReparentedValue(HierarchyId oldRoot, HierarchyId newRoot)
        {
            if (oldRoot == (HierarchyId)null || newRoot == (HierarchyId)null)
            {
                return(new HierarchyId((string)null));
            }
            if (!this.IsDescendantOf(oldRoot))
            {
                throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "HierarchyId.GetReparentedValue failed because 'oldRoot' was not an ancestor node of 'this'.  'oldRoot' was '{0}', and 'this' was '{1}'.", (object)oldRoot, (object)this.ToString()), nameof(oldRoot));
            }
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("/");
            foreach (int[] node in newRoot._nodes)
            {
                stringBuilder.Append(HierarchyId.IntArrayToStirng((IEnumerable <int>)node));
                stringBuilder.Append("/");
            }
            foreach (int[] numArray in ((IEnumerable <int[]>) this._nodes).Skip <int[]>((int)oldRoot.GetLevel()))
            {
                stringBuilder.Append(HierarchyId.IntArrayToStirng((IEnumerable <int>)numArray));
                stringBuilder.Append("/");
            }
            return(new HierarchyId(stringBuilder.ToString()));
        }
Exemplo n.º 3
0
 /// <summary>Implementation of IComparable.CompareTo()</summary>
 /// <param name="obj"> The object to compare to </param>
 /// <returns> 0 if the HierarchyIds are "equal" (i.e., have the same _hierarchyId value) </returns>
 public int CompareTo(object obj)
 {
     if (obj as HierarchyId != (HierarchyId)null)
     {
         return(HierarchyId.Compare(this, (HierarchyId)obj));
     }
     return(-1);
 }
Exemplo n.º 4
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 &gt;= 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) + "/"));
        }
Exemplo n.º 5
0
 /// <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);
 }
Exemplo n.º 6
0
 public abstract HierarchyId GetReparentedValue(
     HierarchyId oldRoot,
     HierarchyId newRoot);
Exemplo n.º 7
0
 /// <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);
Exemplo n.º 8
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 &gt;= child2, an exception is raised.
 /// </returns>
 public abstract HierarchyId GetDescendant(HierarchyId child1, HierarchyId child2);
Exemplo n.º 9
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);
 }
Exemplo n.º 10
0
 /// <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> true if the two HierarchyIds are equal, false otherwise </returns>
 public static bool operator ==(HierarchyId hid1, HierarchyId hid2)
 {
     return(HierarchyId.Compare(hid1, hid2) == 0);
 }