public static void ThrowIf_Null_InvalidLength <T>(int minLength, int maxLength, params IEnumerable <T>[] collections) { ThrowUtils.ThrowIf_Negative(minLength, maxLength); ThrowUtils.ThrowIf_IntervalInvalid(minLength, maxLength, true); if (collections == null) { return; } for (int i = 0; i < collections.Length; i++) { IEnumerable <T> c = collections[i]; if (c == null) { throw new ArgumentNullException($"Argument {i + 1} of {collections.Length} cannot have NULL value"); } else { int count = c.Count(); if (count > maxLength || count < minLength) { throw new ArgumentException($"Argument {i + 1} of {collections.Length} have invalid length: {count}"); } } } }
public static double?[] ToNullable(this double[] sourceArray, bool[] nullMask) { ThrowUtils.ThrowIf_True(nullMask.Length != sourceArray.Length, "Cannot convert to nullable array because nullMask.Length != sourceArray.Length"); double?[] nullableArray = new double?[sourceArray.Length]; for (int i = 0; i < nullableArray.Length; i++) { nullableArray[i] = nullMask[i] ? null : (double?)sourceArray[i]; } return(nullableArray); }
public static List <int> CreateListByRange(int from, int step, int count) { ThrowUtils.ThrowIf_Negative(count); List <int> collection = new List <int>(); for (int i = 0; i < count; i++) { collection.Add(from + step * i); } return(collection); }
public static List <T> CreateList <T>(T element, int count) { ThrowUtils.ThrowIf_Negative(count); List <T> collection = new List <T>(); for (int i = 0; i < count; i++) { collection.Add(element); } return(collection); }
//public static T CreateJaggedArray<T>(params int[] lengths) //{ // var type = typeof(T); // var elementType = getRootElementType(); // var depth = getDepth(); // var reverseLengths = lengths.Reverse().ToArray(); // Array array = Array.CreateInstance(elementType, reverseLengths[0]); // for (int i = 1; i < depth; i++) // { // var length = reverseLengths[i]; // var tmpArray = Array.CreateInstance(array.GetType(), length); // for (int k = 0; k < length; k++) // { // var tmp = // Array.Copy(array, tmpArray, 1); // length.SetValue(Array.Copy(array, tmpArray)) // } // } // Type getRootElementType() // { // var t = type; // while (t.GetElementType().IsArray) // { // t = t.GetElementType(); // } // return t.GetElementType(); // } // int getDepth() // { // var i = 1; // var t = type; // while (t.GetElementType().IsArray) // { // t = t.GetElementType(); // i++; // } // return i; // } //} #endregion #region ##### List ##### public static List <T> CreateList <T>(int count, T initial, Func <T, T> factory) { ThrowUtils.ThrowIf_Negative(count); List <T> collection = new List <T>(); var last = initial; for (int i = 0; i < count; i++) { var current = factory(last); collection.Add(current); last = current; } return(collection); }
public static List <T> Merge <T>(Func <T[], T> mergeFunc, params IList <T>[] collections) { if (collections.Length == 0) { return(new List <T>()); } int count = collections[0].Count; ThrowUtils.ThrowIf_True(collections.Any(c => c.Count != count)); T[] tmp = new T[collections.Length]; List <T> result = new List <T>(count); for (int i = 0; i < count; i++) { for (int j = 0; j < collections.Length; j++) { tmp[j] = collections[j][i]; } result.Add(mergeFunc(tmp)); } return(result); }