/// <summary> /// 从当前数组的指定索引开始截取到指定索引结束的一部分。 /// 如果 <paramref name="startIndex"/> 或 <paramref name="endIndex"/> /// 小于 <c>0</c>,那么表示从数组末尾向前计算的位置。 /// </summary> /// <typeparam name="T">数组中元素的类型。</typeparam> /// <param name="array">要截取的数组。</param> /// <param name="startIndex">要截取的起始索引。</param> /// <param name="endIndex">要截取的结束索引,但不包括该位置的元素。</param> /// <returns>截取得到的数组。如果 <paramref name="startIndex"/> 等于数组的长度或大于等于 /// <paramref name="endIndex"/> ,则为空数组。</returns> /// <exception cref="System.ArgumentNullException"> /// <paramref name="array"/> 为 <c>null</c>。</exception> /// <exception cref="System.ArgumentOutOfRangeException"> /// <paramref name="startIndex"/> 或 <paramref name="endIndex"/> /// 指示的位置不在此数组中。</exception> public static T[] Slice <T>(this T[] array, int startIndex, int endIndex) { if (array == null) { throw ExceptionHelper.ArgumentNull("array"); } if (startIndex < -array.Length || startIndex > array.Length) { throw ExceptionHelper.ArgumentOutOfRange("startIndex"); } if (endIndex < -array.Length || endIndex > array.Length) { throw ExceptionHelper.ArgumentOutOfRange("endIndex"); } Contract.Ensures(Contract.Result <T[]>() != null); if (startIndex == endIndex) { return(Empty <T>()); } if (startIndex < 0) { startIndex += array.Length; } if (endIndex < 0) { endIndex += array.Length; } if (startIndex < endIndex) { return(SubarrayInternal(array, startIndex, endIndex)); } return(Empty <T>()); }
public static T[,] Random <T>(this T[,] array) { if (array == null) { throw ExceptionHelper.ArgumentNull("array"); } Contract.Ensures(Contract.Result <T[, ]>() != null); int w = array.GetLength(1); int idx = array.Length; for (int i = array.GetLength(0) - 1; i >= 0; i--) { for (int j = w - 1; j >= 0; j--) { int r = RandomExt.Next(idx--); int x = r % w; int y = r / w; if (y != i || x != j) { T temp = array[i, j]; array[i, j] = array[y, x]; array[y, x] = temp; } } } return(array); }
/// <summary> /// 从当前数组的指定索引开始截取指定长度的一部分。 /// 如果 <paramref name="startIndex"/> 小于 <c>0</c>, /// 那么表示从字符串结束位置向前计算的位置。 /// </summary> /// <typeparam name="T">数组中元素的类型。</typeparam> /// <param name="array">要截取的数组。</param> /// <param name="startIndex">要截取的起始索引。</param> /// <param name="length">要截取的数组元素个数。</param> /// <returns>截取得到的数组。</returns> /// <exception cref="System.ArgumentNullException"> /// <paramref name="array"/> 为 <c>null</c>。</exception> /// <exception cref="System.ArgumentOutOfRangeException"> /// <paramref name="startIndex"/> 小于负的此数组的长度。</exception> /// <exception cref="System.ArgumentOutOfRangeException"> /// <paramref name="length"/> 小于 <c>0</c>。</exception> /// <exception cref="System.ArgumentOutOfRangeException"> /// <paramref name="startIndex"/> 加 <paramref name="length"/> /// 之和指示的位置不在此数组中。</exception> public static T[] Subarray <T>(this T[] array, int startIndex, int length) { if (array == null) { throw ExceptionHelper.ArgumentNull("array"); } if (startIndex < -array.Length) { throw ExceptionHelper.ArgumentOutOfRange("startIndex"); } if (length < 0 || startIndex + length > array.Length) { throw ExceptionHelper.InvalidOffsetLength(); } Contract.Ensures(Contract.Result <T[]>() != null); if (length == 0) { return(Empty <T>()); } if (startIndex < 0) { startIndex += array.Length; } return(SubarrayInternal(array, startIndex, startIndex + length)); }
/// <summary> /// 将数组使用指定的值填充。 /// </summary> /// <typeparam name="T">数组中元素的类型。</typeparam> /// <param name="array">要填充的数组。</param> /// <param name="value">要填充数组的值。</param> /// <returns>填充完毕的数组。</returns> /// <exception cref="System.ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception> public static T[] Fill <T>(this T[] array, T value) { if (array == null) { throw ExceptionHelper.ArgumentNull("array"); } Contract.Ensures(Contract.Result <T[]>() != null); return(FillInternal(array, value, 0, array.Length)); }
/// <summary> /// 将数组使用指定的值填充。 /// </summary> /// <param name="array">要填充的数组。</param> /// <param name="value">要填充数组的值。</param> /// <returns>填充完毕的数组。</returns> /// <exception cref="System.ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception> /// <overloads> /// <summary> /// 将数组使用指定的值填充。 /// </summary> /// </overloads> public static Array Fill(this Array array, object value) { if (array == null) { throw ExceptionHelper.ArgumentNull("array"); } Contract.Ensures(Contract.Result <Array>() != null); return(FillInternal(array, value, 0, array.Length)); }
/// <summary> /// 将数组从指定的索引位置开始使用指定的值填充。 /// </summary> /// <param name="array">要填充的数组。</param> /// <param name="value">要填充数组的值。</param> /// <param name="startIndex">要开始填充的起始索引。</param> /// <returns>填充完毕的数组。</returns> /// <exception cref="System.ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception> /// <exception cref="System.ArgumentOutOfRangeException"><paramref name="startIndex"/> /// 小于零或大于等于数组的长度。</exception> public static Array Fill(this Array array, object value, int startIndex) { if (array == null) { throw ExceptionHelper.ArgumentNull("array"); } if (startIndex < 0 || startIndex >= array.Length) { throw ExceptionHelper.ArgumentOutOfRange("startIndex"); } Contract.Ensures(Contract.Result <Array>() != null); return(FillInternal(array, value, startIndex, array.Length - startIndex)); }
/// <summary> /// 将数组使用指定的值填充。 /// </summary> /// <typeparam name="T">数组中元素的类型。</typeparam> /// <param name="array">要填充的数组。</param> /// <param name="value">要填充数组的值。</param> /// <returns>填充完毕的数组。</returns> /// <exception cref="System.ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception> public static T[][] Fill <T>(this T[][] array, T value) { if (array == null) { throw ExceptionHelper.ArgumentNull("array"); } Contract.Ensures(Contract.Result <T[][]>() != null); for (int i = array.Length - 1; i >= 0; i--) { for (int j = array[i].Length - 1; j >= 0; j--) { array[i][j] = value; } } return(array); }
/// <summary> /// 将数组从指定的索引位置开始使用指定的函数的返回值填充。 /// </summary> /// <typeparam name="T">数组中元素的类型。</typeparam> /// <param name="array">要填充的数组。</param> /// <param name="value">返回要填充数组的值的函数,其参数是当前填充到的索引。</param> /// <param name="startIndex">要开始填充的起始索引。</param> /// <returns>填充完毕的数组。</returns> /// <exception cref="System.ArgumentOutOfRangeException"><paramref name="startIndex"/> /// 小于零或大于等于数组的长度。</exception> public static T[] Fill <T>(this T[] array, Func <int, T> value, int startIndex) { if (array == null) { throw ExceptionHelper.ArgumentNull("array"); } if (value == null) { throw ExceptionHelper.ArgumentNull("value"); } if (startIndex < 0 || startIndex >= array.Length) { throw ExceptionHelper.ArgumentOutOfRange("startIndex"); } Contract.Ensures(Contract.Result <T[]>() != null); return(FillInternal(array, value, startIndex, array.Length - startIndex)); }
/// <summary> /// 从当前数组的指定索引开始截取一部分。 /// 如果 <paramref name="startIndex"/> 小于 <c>0</c>, /// 那么表示从字符串结束位置向前计算的位置。 /// </summary> /// <typeparam name="T">数组中元素的类型。</typeparam> /// <param name="startIndex">要截取的起始索引。</param> /// <param name="array">要截取的数组。</param> /// <returns>截取得到的数组。</returns> /// <exception cref="System.ArgumentNullException"> /// <paramref name="array"/> 为 <c>null</c>。</exception> /// <exception cref="System.ArgumentOutOfRangeException"> /// <paramref name="startIndex"/> 小于负的数组的长度。</exception> /// <exception cref="System.ArgumentOutOfRangeException"> /// <paramref name="startIndex"/> 大于数组的长度。</exception> /// <overloads> /// <summary> /// 从当前数组的指定索引开始截取一部分。 /// </summary> /// </overloads> public static T[] Subarray <T>(this T[] array, int startIndex) { if (array == null) { throw ExceptionHelper.ArgumentNull("array"); } if (startIndex < -array.Length || startIndex > array.Length) { throw ExceptionHelper.ArgumentOutOfRange("startIndex"); } Contract.Ensures(Contract.Result <T[]>() != null); if (startIndex == array.Length) { return(Empty <T>()); } return(Subarray(array, startIndex, array.Length - startIndex)); }
/// <summary> /// 初始化方法字典。 /// </summary> /// <param name="methods">方法列表。</param> private void InitMethods(Delegate[] methods) { methodDict = new Dictionary <Type, TDelegate>(methods.Length); for (int i = 0; i < methods.Length; i++) { if (methods[i] == null) { throw ExceptionHelper.ArgumentNull("methods[" + i + "]"); } TDelegate dlg = methods[i].Wrap <TDelegate>(); if (dlg == null) { throw ExceptionHelper.DelegateCompatible("methods[" + i + "]", typeof(TDelegate)); } methodDict.Add(methods[i].GetType().GetMethod("Invoke").GetParameters()[keyIndex].ParameterType, dlg); } }
public static T[, ,] Fill <T>(this T[, ,] array, T value) { if (array == null) { throw ExceptionHelper.ArgumentNull("array"); } Contract.Ensures(Contract.Result <T[, , ]>() != null); for (int i = 0; i < array.GetLength(0); i++) { for (int j = 0; j < array.GetLength(1); j++) { for (int k = 0; k < array.GetLength(2); k++) { array[i, j, k] = value; } } } return(array); }
/// <summary> /// 将数组进行随机排序。 /// </summary> /// <typeparam name="T">数组中元素的类型。</typeparam> /// <param name="array">要进行随机排序的数组。</param> /// <returns>已完成随机排序的数组。</returns> /// <remarks>应保证每个元素出现在每个位置的概率基本相同。 /// 采用下面的代码进行测试: /// <code>int size = 10; /// int[] arr = new int[size]; /// int[,] cnt = new int[size, size]; /// for (int i = 0; i < 200; i++) /// { /// arr.Fill(n => n).Random(); /// for (int j = 0; j < size; j++) cnt[j, arr[j]]++; /// } /// for (int i = 0; i < size; i++) /// { /// for (int j = 0; j < size; j++) /// Console.Write("{0} ", cnt[i, j]); /// Console.WriteLine(); /// }</code> /// </remarks> /// <overloads> /// <summary> /// 将数组进行随机排序。 /// </summary> /// </overloads> public static T[] Random <T>(this T[] array) { if (array == null) { throw ExceptionHelper.ArgumentNull("array"); } Contract.Ensures(Contract.Result <T[]>() != null); for (int i = array.Length - 1; i > 0; i--) { int j = RandomExt.Next(i + 1); if (j != i) { T temp = array[i]; array[i] = array[j]; array[j] = temp; } } return(array); }
/// <summary> /// 将数组使用指定的函数的返回值填充。 /// </summary> /// <typeparam name="T">数组中元素的类型。</typeparam> /// <param name="array">要填充的数组。</param> /// <param name="value">返回要填充数组指定索引的值的函数,其参数是当前填充到的索引。</param> /// <returns>填充完毕的数组。</returns> /// <exception cref="System.ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception> /// <exception cref="System.ArgumentNullException"><paramref name="value"/> 为 <c>null</c>。</exception> public static T[][] Fill <T>(this T[][] array, Func <int, int, T> value) { if (array == null) { throw ExceptionHelper.ArgumentNull("array"); } if (value == null) { throw ExceptionHelper.ArgumentNull("value"); } Contract.Ensures(Contract.Result <T[][]>() != null); for (int i = 0; i < array.Length; i++) { for (int j = 0; j < array[i].Length; j++) { array[i][j] = value(i, j); } } return(array); }
/// <summary> /// 将数组从指定的索引位置开始使用指定的函数的返回值填充指定的长度。 /// </summary> /// <param name="array">要填充的数组。</param> /// <param name="value">返回要填充数组指定索引的值的函数,其参数是当前填充到的索引。</param> /// <param name="startIndex">要开始填充的起始索引。</param> /// <param name="length">要填充的数组元素个数。</param> /// <returns>填充完毕的数组。</returns> /// <exception cref="System.ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception> /// <exception cref="System.ArgumentNullException"><paramref name="value"/> 为 <c>null</c>。</exception> /// <exception cref="System.ArgumentOutOfRangeException"><paramref name="startIndex"/> /// 加 <paramref name="length"/> 之和大于数组的长度。</exception> /// <exception cref="System.ArgumentOutOfRangeException"><paramref name="startIndex"/> /// 或 <paramref name="length"/> 小于零。</exception> public static Array Fill(this Array array, Func <int, object> value, int startIndex, int length) { if (array == null) { throw ExceptionHelper.ArgumentNull("array"); } if (value == null) { throw ExceptionHelper.ArgumentNull("value"); } if (startIndex < 0) { throw ExceptionHelper.ArgumentOutOfRange("startIndex"); } if (length < 0 || startIndex + length > array.Length) { throw ExceptionHelper.ArgumentOutOfRange("length"); } Contract.Ensures(Contract.Result <Array>() != null); return(FillInternal(array, value, startIndex, length)); }
/// <summary> /// 从当前数组的右端截取一部分。 /// </summary> /// <typeparam name="T">数组中元素的类型。</typeparam> /// <param name="array">从该数组返回其最右端截取的部分。</param> /// <param name="length">要截取的元素个数。 /// 如果为 <c>0</c>,则返回空数组。如果大于或等于 <paramref name="array"/> 的长度, /// 则返回整个数组的一个浅拷贝。</param> /// <returns>从指定数组的右端截取的部分。</returns> /// <exception cref="System.ArgumentNullException"> /// <paramref name="array"/> 为 <c>null</c>。</exception> /// <exception cref="System.ArgumentOutOfRangeException"> /// <paramref name="length"/> 小于 <c>0</c>。</exception> public static T[] Right <T>(this T[] array, int length) { if (array == null) { throw ExceptionHelper.ArgumentNull("array"); } if (length < 0) { throw ExceptionHelper.ArgumentOutOfRange("length"); } Contract.Ensures(Contract.Result <T[]>() != null); if (length == 0) { return(Empty <T>()); } if (length > array.Length) { length = array.Length; } return(SubarrayInternal(array, array.Length - length, length)); }