/// <summary> /// adds elements from <paramref name="sourceList"/> /// in range [<paramref name="startIndex"/>,<paramref name="endIndex"/>) /// to end of <paramref name="destinationList"/>. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="destinationList"></param> /// <param name="startIndex"></param> /// <param name="endIndex"></param> /// <param name="sourceList"></param> public static void AddRange <T>( this List <T> destinationList, int startIndex, int endIndex, IList <T> sourceList) { ListUtils.AssertRangeIndicesValid(startIndex, endIndex, sourceList); int numberOfItemsToBeAddedToList = (endIndex - startIndex); int numberOfItemsInListAfterAdd = destinationList.Count + numberOfItemsToBeAddedToList; // number of items in list after add is greater than current list capacity, // and doubling list capacity fails to allocate enough space for added items if (destinationList.Capacity * 2 < numberOfItemsInListAfterAdd) { // manually allocate additional memory to list destinationList.Capacity = numberOfItemsInListAfterAdd; } // add items from sourceList in range [beginIndex - endIndex) to destinationList for (int i = startIndex; i < endIndex; i++) { T sourceListItem = sourceList[i]; destinationList.Add(sourceListItem); } }
/// <summary> /// represents an <paramref name="array"/> range of /// [<paramref name="startIndex"/>, <paramref name="endIndex"/>). /// </summary> /// <param name="list"></param> /// <param name="startIndex"></param> /// <param name="endIndex"></param> /// <exception cref="IndexOutOfRangeException"> /// <seealso cref="ListUtils.AssertRangeIndicesValid{T}(int, int, IList{T})"/> /// </exception> public ArrayRange(T[] array, int startIndex, int endIndex) { ListUtils.AssertRangeIndicesValid(startIndex, endIndex, array); this.startIndex = startIndex; this.endIndex = endIndex; this.subArray = array.GetRange(startIndex, endIndex); }
/// <summary> /// returns subarray of <paramref name="sourceArray"/> containing items in range /// [<paramref name="startIndex"/>, <paramref name="endIndex"/>). /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sourceArray"></param> /// <param name="startIndex"></param> /// <param name="endIndex"></param> /// <returns> /// subarray of <paramref name="sourceArray"/> containing items in range /// [<paramref name="startIndex"/>, <paramref name="endIndex"/>) /// </returns> /// <exception cref="IndexOutOfRangeException"> /// <seealso cref="ListUtils.AssertRangeIndicesValid{T}(int, int, IList{T})"/> /// </exception> public static T[] Subarray <T>(this T[] sourceArray, int startIndex, int endIndex) { ListUtils.AssertRangeIndicesValid(startIndex, endIndex, sourceArray); T[] subarray; // allocate memory for subArray int subarrayLength = endIndex - startIndex; subarray = new T[subarrayLength]; // copy element from sourceArray into subArray Array.Copy(sourceArray, startIndex, subarray, 0, subarrayLength); return(subarray); }
/// <summary> /// returns a subarray containing elements from <paramref name="sourceList"/> in /// range [<paramref name="startIndex"/>, <paramref name="endIndex"/>). /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sourceList"></param> /// <param name="startIndex"></param> /// <param name="endIndex"></param> /// <returns> /// subarray containing elements from <paramref name="sourceList"/> in /// range [<paramref name="startIndex"/>, <paramref name="endIndex"/>). /// </returns> /// <exception cref="IndexOutOfRangeException"> /// <seealso cref="ListUtils.AssertRangeIndicesValid{T}(int, int, IList{T})"/> /// </exception> public static T[] GetRange <T>( this IList <T> sourceList, int startIndex, int endIndex) { ListUtils.AssertRangeIndicesValid(startIndex, endIndex, sourceList); int numberOfItemsInRange = endIndex - startIndex; T[] resultArray = new T[numberOfItemsInRange]; // copy elements in range [startIndex, endIndex) in sourceList to resultArray for (int i = startIndex; i < endIndex; i++) { T sourceListItem = sourceList[i]; int resultArrayIndex = i - startIndex; resultArray[resultArrayIndex] = sourceListItem; } return(resultArray); }