Пример #1
0
        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}");
                    }
                }
            }
        }
Пример #2
0
        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);
        }
Пример #3
0
        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);
        }
Пример #4
0
        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);
        }
Пример #5
0
        //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);
        }
Пример #6
0
        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);
        }