public static long[] Range(long n)
{
long[] r = new long[(int)n];
for (int i = 0; i < r.Length; i++)
r[i] = (long)i;
return r;
}
Vector::Range ( byte n ) : byte[] | |
Vector::Range ( byte a, byte b ) : byte[] | |
Vector::Range ( byte a, byte b, byte stepSize ) : byte[] | |
Vector::Range ( this range ) : byte[] | |
Vector::Range ( this range, byte stepSize ) : byte[] | |
Vector::Range ( decimal n ) : decimal[] | |
Vector::Range ( decimal a, decimal b ) : decimal[] | |
Vector::Range ( decimal a, decimal b, decimal stepSize ) : decimal[] | |
Vector::Range ( double n ) : double[] | |
Vector::Range ( double a, double b ) : double[] | |
Vector::Range ( double a, double b, double stepSize ) : double[] | |
Vector::Range ( this range ) : double[] | |
Vector::Range ( this range, double stepSize ) : double[] | |
Vector::Range ( float n ) : float[] | |
Vector::Range ( float a, float b ) : float[] | |
Vector::Range ( float a, float b, float stepSize ) : float[] | |
Vector::Range ( this range ) : float[] | |
Vector::Range ( this range, float stepSize ) : float[] | |
Vector::Range ( int n ) : int[] | |
Vector::Range ( int a, int b ) : int[] | |
Vector::Range ( int a, int b, double stepSize ) : int[] | |
Vector::Range ( this range ) : int[] | |
Vector::Range ( this range, double stepSize ) : int[] | |
Vector::Range ( long a, long b ) : long[] | |
Vector::Range ( long a, long b, long stepSize ) : long[] | |
Vector::Range ( sbyte n ) : sbyte[] | |
Vector::Range ( sbyte a, sbyte b ) : sbyte[] | |
Vector::Range ( sbyte a, sbyte b, sbyte stepSize ) : sbyte[] | |
Vector::Range ( short n ) : short[] | |
Vector::Range ( short a, short b ) : short[] | |
Vector::Range ( short a, short b, short stepSize ) : short[] | |
Vector::Range ( ulong n ) : ulong[] | |
Vector::Range ( ulong a, ulong b ) : ulong[] | |
Vector::Range ( ulong a, ulong b, ulong stepSize ) : ulong[] | |
Vector::Range ( ushort n ) : ushort[] | |
Vector::Range ( ushort a, ushort b ) : ushort[] | |
Vector::Range ( ushort a, ushort b, ushort stepSize ) : ushort[] |
/// <summary> /// Enumerates all possible value permutations for a given array. /// </summary> /// /// <param name="values">The array whose permutations need to be generated</param>. /// <param name="inPlace"> /// If set to true, the different generated permutations will be stored in /// the same array, thus preserving memory. However, this may prevent the /// samples from being stored in other locations without having to clone /// them. If set to false, a new memory block will be allocated for each /// new object in the sequence.</param> /// /// <example> /// <code> /// // Let's say we would like to generate all possible permutations /// // of the elements (1, 2, 3). In order to enumerate all those /// // permutations, we can use: /// /// int[] values = { 1, 2, 3 }; /// /// foreach (int[] permutation in Combinatorics.Permutations(values)) /// { /// // The permutations will be generated in the following order: /// // /// // { 1, 3, 2 }; /// // { 2, 1, 3 }; /// // { 2, 3, 1 }; /// // { 3, 1, 2 }; /// // { 3, 2, 1 }; /// // /// } /// </code> /// </example> /// public static IEnumerable <T[]> Permutations <T>(T[] values, bool inPlace = false) { T[] current = new T[values.Length]; yield return(inPlace ? values : (T[])values.Clone()); int[] idx = Vector.Range(0, values.Length); int j, l; while (true) { for (j = values.Length - 2; j >= 0; j--) { if (idx[j + 1] > idx[j]) { break; } } if (j == -1) { yield break; } for (l = values.Length - 1; l > j; l--) { if (idx[l] > idx[j]) { break; } } int temp = idx[j]; idx[j] = idx[l]; idx[l] = temp; for (int i = j + 1; i < idx.Length; i++) { if (i > idx.Length - i + j) { break; } temp = idx[i]; idx[i] = idx[idx.Length - i + j]; idx[idx.Length - i + j] = temp; } for (int i = 0; i < values.Length; i++) { current[i] = values[idx[i]]; } yield return(inPlace ? current : (T[])current.Clone()); } }