Example #1
0
        public void ConcatenateSmallRopesToRope()
        {
            StringBuilder b    = new StringBuilder();
            Rope <char>   rope = new Rope <char>();

            for (int i = 1; i <= 1000; i++)
            {
                b.Append(i.ToString());
                b.Append(' ');
                rope.AddRange(CharRope.Create(i.ToString() + " "));
            }
            Assert.AreEqual(b.ToString(), rope.ToString());
        }
Example #2
0
        public void ConcatenateSmallRopesToRopeBackwards()
        {
            StringBuilder b    = new StringBuilder();
            Rope <char>   rope = new Rope <char>();

            for (int i = 1; i <= 1000; i++)
            {
                b.Append(i.ToString());
                b.Append(' ');
            }
            for (int i = 1000; i >= 1; i--)
            {
                rope.InsertRange(0, CharRope.Create(i.ToString() + " "));
            }
            Assert.AreEqual(b.ToString(), rope.ToString());
        }
Example #3
0
        public void ConcatenateSmallRopesByInsertionInMiddle()
        {
            StringBuilder b    = new StringBuilder();
            Rope <char>   rope = new Rope <char>();

            for (int i = 1; i <= 1000; i++)
            {
                b.Append(i.ToString("d3"));
                b.Append(' ');
            }
            int middle = 0;

            for (int i = 1; i <= 500; i++)
            {
                rope.InsertRange(middle, CharRope.Create(i.ToString("d3") + " "));
                middle += 4;
                rope.InsertRange(middle, CharRope.Create((1001 - i).ToString("d3") + " "));
            }
            Assert.AreEqual(b.ToString(), rope.ToString());
        }
Example #4
0
        /// <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));
            if (input is Rope<T> inputRope)
            {
                // clone ropes instead of copying them
                inputRope.Root.Publish();
                Root = inputRope.Root;
            }
            else
            {
                // ReSharper disable ConditionIsAlwaysTrueOrFalse
                // ReSharper disable ExpressionIsAlwaysNull
                // ReSharper disable SuspiciousTypeConversion.Global
#pragma warning disable IDE0019 // Use pattern matching
                var text = input as string;
#pragma warning restore IDE0019 // Use pattern matching
                // ReSharper restore SuspiciousTypeConversion.Global
                if (text != null)
                // ReSharper restore ConditionIsAlwaysTrueOrFalse
                // ReSharper restore ExpressionIsAlwaysNull
                // ReSharper disable HeuristicUnreachableCode
                {
                    // if a string is IEnumerable<T>, then T must be char
                    ((Rope<char>)(object)this).Root = CharRope.InitFromString(text);
                }
                // ReSharper restore HeuristicUnreachableCode
                else
                {
                    var arr = ToArray(input);
                    Root = RopeNode<T>.CreateFromArray(arr, 0, arr.Length);
                }
            }
            Root.CheckInvariants();
        }