/// <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("input"); } Rope <T> inputRope = input as Rope <T>; if (inputRope != null) { // clone ropes instead of copying them inputRope.root.Publish(); this.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); this.root = RopeNode <T> .CreateFromArray(arr, 0, arr.Length); } } this.root.CheckInvariants(); }
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()); }
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()); }
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()); }