/// <summary> /// Inserts an item at the specified index, pushing the element at the index forward. /// </summary> /// <param name="index"> The index before which to insert the item. </param> /// <param name="item"> The item to insert. </param> /// <returns> </returns> /// <exception cref="ArgumentOutOfRangeException">Thrown if the index doesn't exist.</exception> public ImmList <T> Insert(int index, T item) { index.CheckIsBetween("index", -Root.Measure - 1, Root.Measure); index = index < 0 ? Root.Measure + index + 1 : index; if (index == Root.Measure) { return(AddLast(item)); } if (index == 0) { return(AddFirst(item)); } var newRoot = Root.Insert(index, item, Lineage.Mutable()); var ret = newRoot.Wrap(); ret[index].AssertEqual(item); ret.Root.Measure.AssertEqual(Root.Measure + 1); return(ret); }