/// <summary> /// Inserts a collection of elements into this deque. /// </summary> /// <param name="index">The index at which the collection is inserted.</param> /// <param name="collection">The collection of elements to insert.</param> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index to an insertion point for the source.</exception> public void InsertRange(Int32 index, IEnumerable <T> collection) { CheckNewIndexArgument(Count, index); IReadOnlyCollection <T> source = DequeHelpers.ReifyCollection(collection); Int32 collectionCount = source.Count; // Overflow-safe check for "Count + collectionCount > Capacity" if (collectionCount > Capacity - Count) { Capacity = checked (Count + collectionCount); } if (collectionCount == 0) { return; } DoInsertRange(index, source); }
/// <summary> /// Initializes a new instance of the <see cref="Deque<T>"/> class with the elements from the specified collection. /// </summary> /// <param name="collection">The collection. May not be <c>null</c>.</param> public Deque(IEnumerable <T> collection) { if (collection == null) { throw new ArgumentNullException(nameof(collection)); } IReadOnlyCollection <T> source = DequeHelpers.ReifyCollection(collection); Int32 count = source.Count; if (count > 0) { _buffer = new T[count]; DoInsertRange(0, source); } else { _buffer = new T[DefaultCapacity]; } }