示例#1
0
文件: Rope.cs 项目: arkanoid1/Yanitta
        /// <summary>
        /// Creates a rope from the specified input.
        /// This operation runs in O(N).
        /// </summary>
        /// <exception cref="ArgumentNullException">input is null.</exception>
        public Rope(IEnumerable <T> input)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }
            Rope <T> inputRope = input as Rope <T>;

            if (inputRope != null)
            {
                // clone ropes instead of copying them
                inputRope.root.Publish();
                root = inputRope.root;
            }
            else
            {
                string text = input as string;
                if (text != null)
                {
                    // if a string is IEnumerable<T>, then T must be char
                    ((Rope <char>)(object) this).root = CharRope.InitFromString(text);
                }
                else
                {
                    T[] arr = ToArray(input);
                    root = RopeNode <T> .CreateFromArray(arr, 0, arr.Length);
                }
            }
            root.CheckInvariants();
        }
示例#2
0
文件: Rope.cs 项目: arkanoid1/Yanitta
        /// <summary>
        /// Creates a rope from a part of the array.
        /// This operation runs in O(N).
        /// </summary>
        /// <exception cref="ArgumentNullException">input is null.</exception>
        public Rope(T[] array, int arrayIndex, int count)
        {
            VerifyArrayWithRange(array, arrayIndex, count);
            root = RopeNode <T> .CreateFromArray(array, arrayIndex, count);

            root.CheckInvariants();
        }
示例#3
0
        internal void CheckInvariants()
        {
            if (height == 0)
            {
                Debug.Assert(left == null && right == null);
                if (contents == null)
                {
                    Debug.Assert(this is FunctionNode <T>);
                    Debug.Assert(length > 0);
                    Debug.Assert(isShared);
                }
                else
                {
                    Debug.Assert(contents != null && contents.Length == NodeSize);
                    Debug.Assert(length >= 0 && length <= NodeSize);
                }
            }
            else
            {
                Debug.Assert(left != null && right != null);
                Debug.Assert(contents == null);
                Debug.Assert(length == left.length + right.length);
                Debug.Assert(height == 1 + Math.Max(left.height, right.height));
                Debug.Assert(Math.Abs(this.Balance) <= 1);

                // this is an additional invariant that forces the tree to combine small leafs to prevent excessive memory usage:
                Debug.Assert(length > NodeSize);
                // note that this invariant ensures that all nodes except for the empty rope's single node have at least length 1

                if (isShared)
                {
                    Debug.Assert(left.isShared && right.isShared);
                }
                left.CheckInvariants();
                right.CheckInvariants();
            }
        }
示例#4
0
 /// <summary>
 /// Creates a new rope representing the empty string.
 /// </summary>
 public Rope()
 {
     // we'll construct the empty rope as a clone of an imaginary static empty rope
     this.root = RopeNode <T> .emptyRopeNode;
     root.CheckInvariants();
 }
示例#5
0
        internal void OnChanged()
        {
            lastUsedNodeStack = null;

            root.CheckInvariants();
        }
示例#6
0
 internal Rope(RopeNode <T> root)
 {
     this.root = root;
     root.CheckInvariants();
 }