Exemplo n.º 1
0
        public static IEnumerable <IEnumerable <TSource> > Rows <TSource>(this TSource[,] self)
        {
            Check.Self(self);

            int rows = self.GetLength(0);
            int cols = self.GetLength(1);

            return(CreateRowsIterator(self, rows, cols));
        }
Exemplo n.º 2
0
 public static void Populate <TSource>(this TSource[,] source, TSource value)
 {
     for (var i = 0; i < source.GetLength(0); i++)
     {
         for (var j = 0; j < source.GetLength(1); j++)
         {
             source[i, j] = value;
         }
     }
 }
Exemplo n.º 3
0
 public static IEnumerable <TResult> ToQueryable <TSource, TResult>(this TSource[,] source, Func <int, int, TSource, TResult> conversionFunc)
 {
     for (int y = 0; y < source.GetLength(0); ++y)
     {
         for (int x = 0; x < source.GetLength(1); ++x)
         {
             yield return(conversionFunc(x, y, source[y, x]));
         }
     }
 }
 public static TResult[,] MapArray2d <TSource, TResult>(this TSource[,] source, Func <TSource, TResult> mapper)
 {
     TResult[,] result = new TResult[source.GetLength(0), source.GetLength(1)];
     for (int i = 0; i < source.GetLength(0); i++)
     {
         for (int j = 0; j < source.GetLength(1); j++)
         {
             result[i, j] = mapper(source[i, j]);
         }
     }
     return(result);
 }
Exemplo n.º 5
0
 /// <summary>
 /// 将二维数组中的每个元素投影到新二维数组。
 /// </summary>
 /// <typeparam name="TSource">source 的元素类型。</typeparam>
 /// <typeparam name="TResult">selector 返回的值的类型</typeparam>
 /// <param name="source">一个二维数组,要对该二维数组调用转换函数</param>
 /// <param name="selector">应用于每个元素的转换函数</param>
 /// <returns></returns>
 public static TResult[,] Select <TSource, TResult>(this TSource[,] source, Func <TSource, TResult> selector)
 {
     TResult[,] result = new TResult[source.GetLength(0), source.GetLength(1)];
     for (int x = 0; x < source.GetLength(0); x++)
     {
         for (int y = 0; y < source.GetLength(1); y++)
         {
             result[x, y] = selector(source[x, y]);
         }
     }
     return(result);
 }
Exemplo n.º 6
0
    public static TResult[,] Select <TSource, TResult>(this TSource[,] sourceMatrix, Func <TSource, TResult> selector)
    {
        var map = new TResult[sourceMatrix.GetLength(0), sourceMatrix.GetLength(1)];

        foreach (var(x, y, value) in EveryPointIn(sourceMatrix))
        {
            var newValue = selector(value);
            map[x, y] = newValue;
        }

        return(map);
    }
    public static TResult[,] ConvertAll <TSource, TResult>(this TSource[,] source, Func <TSource, TResult> projection)
    {
        var result = new TResult[source.GetLength(0), source.GetLength(1)];

        for (int x = 0; x < source.GetLength(0); x++)
        {
            for (int y = 0; y < source.GetLength(1); y++)
            {
                result[x, y] = projection(source[x, y]);
            }
        }
        return(result);
    }
Exemplo n.º 8
0
        public static TResult[,] TwoDimSelect <TSource, TResult>(this TSource[,] array, Func <TSource, TResult> selector)
        {
            var result = new TResult[array.GetLength(0), array.GetLength(1)];

            for (var x = 0; x < array.GetLength(0); x++)
            {
                for (var y = 0; y < array.GetLength(1); y++)
                {
                    result[x, y] = selector(array[x, y]);
                }
            }

            return(result);
        }
        /// <summary>
        /// Populates every element of the two dimension array with <paramref name="value" />.
        /// </summary>
        /// <typeparam name="TSource">Array element type.</typeparam>
        /// <param name="source">The array to populate.</param>
        /// <param name="value">The value to populate the array with.</param>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="source" /> is null.</exception>
        public static void Populate <TSource>(this TSource[,] source, TSource value)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            for (var i = 0; i < source.GetLength(0); i++)
            {
                for (var j = 0; j < source.GetLength(1); j++)
                {
                    source[i, j] = value;
                }
            }
        }
Exemplo n.º 10
0
        public static TResult[,] Cast <TSource, TResult>(this TSource[,] self, Func <TSource, TResult> cast)
        {
            var m      = self.GetLength(0);
            var n      = self.GetLength(1);
            var matrix = new TResult[m, n];

            for (var i = 0; i < m; i++)
            {
                for (var j = 0; j < n; j++)
                {
                    matrix[i, j] = cast(self[i, j]);
                }
            }
            return(matrix);
        }
Exemplo n.º 11
0
        /// <summary>シーケンスの各要素を新しいフォームに射影します。</summary>
        /// <param name="source">変換関数を呼び出す対象となる値のシーケンス。</param>
        /// <param name="selector">各要素に適用する変換関数。</param>
        /// <exception cref="ArgumentNullException"></exception>
        public static IEnumerable <TResult> Select <TSource, TResult>(this TSource[,] source, Func <TSource, int, int, TResult> selector)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (selector == null)
            {
                throw new ArgumentNullException(nameof(selector));
            }

            return(from x in Enumerable.Range(0, source.GetLength(0))
                   from y in Enumerable.Range(0, source.GetLength(1))
                   select selector(source[x, y], x, y));
        }
Exemplo n.º 12
0
        /// <summary>
        /// Elementwise application of a function
        /// </summary>
        /// <param name="input">Data</param>
        /// <param name="func">The function</param>
        /// <returns>Result</returns>
        public static TTarget[,] Map <TSource, TTarget>(this TSource[,] input, System.Func <TSource, TTarget> func)
        {
            int w = input.GetLength(0);
            int h = input.GetLength(1);

            TTarget[,] output = new TTarget[w, h];

            for (int x = 0; x < w; x++)
            {
                for (int y = 0; y < h; y++)
                {
                    output[x, y] = func(input[x, y]);
                }
            }
            return(output);
        }
Exemplo n.º 13
0
        //2D array map function
        public static TDest[,] Map <TSource, TDest>(TSource[,] source, Func <TSource, TDest> func)
        {
            int width  = source.GetLength(0);
            int height = source.GetLength(1);

            TDest[,] result = new TDest[width, height];

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    result[i, j] = func(source[i, j]);
                }
            }

            return(result);
        }
        /// <summary>
        /// Gets the number of rows in <paramref name="source" />.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
        /// <param name="source">The two dimension array to get the row count on.</param>
        /// <returns>The number of rows in <paramref name="source" />.</returns>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="source" /> is null.</exception>
        public static int GetRowCount <TSource>(this TSource[,] source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            return(source.GetLength(1));
        }
Exemplo n.º 15
0
        public static TResult[ , ] MapLoop <TSource, TResult>(
            this TSource [ , ] src
            , Func <TSource, TResult> fn)
        {
            int row = src.GetLength(0);
            int col = src.GetLength(1);

            TResult[,] result = new TResult[row, col];

            for (int j = 0; j < row; j++)
            {
                for (int i = 0; i < col; i++)
                {
                    result[j, i] = fn(src [j, i]);
                }
            }
            return(result);
        }
    public static TResult[,] ConvertAll <TSource, TResult>(this TSource[,] source, Func <TSource, TResult> projection)
    {
        if (null == source)
        {
            throw new ArgumentNullException("source");
        }
        if (null == projection)
        {
            throw new ArgumentNullException("projection");
        }
        var result = new TResult[source.GetLength(0), source.GetLength(1)];

        for (int x = 0; x < source.GetLength(0); x++)
        {
            for (int y = 0; y < source.GetLength(1); y++)
            {
                result[x, y] = projection(source[x, y]);
            }
        }
        return(result);
    }
Exemplo n.º 17
0
        public static IEnumerable <TResult> SelectMany <TSource, TResult>(this TSource[,] source, Func <TSource, int, int, IEnumerable <TResult> > selector)
        {
            if (source == null || selector == null)
            {
                throw new ArgumentNullException();
            }

            int rows = source.GetLength(0);
            int cols = source.GetLength(1);


            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    foreach (TResult result in selector(source[i, j], i, j))
                    {
                        yield return(result);
                    }
                }
            }
        }
Exemplo n.º 18
0
        public static TResult[,] Select <TSource, TResult>(this TSource[,] source, Func <TSource, int, int, TResult> selector)
        {
            if (source == null || selector == null)
            {
                throw new ArgumentNullException();
            }

            int rows = source.GetLength(0);
            int cols = source.GetLength(1);

            TResult[,] tab = new TResult[rows, cols];

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    tab[i, j] = selector(source[i, j], i, j);
                }
            }

            return(tab);
        }
Exemplo n.º 19
0
        public static TSource[][] ToMultiArray <TSource>(this TSource[,] source)
        {
            if (source == null)
            {
                throw new ArgumentNullException();
            }

            int rows = source.GetLength(0);
            int cols = source.GetLength(1);

            TSource[][] tab = new TSource[rows][];

            for (int i = 0; i < rows; i++)
            {
                tab[i] = new TSource[cols];
                for (int j = 0; j < cols; j++)
                {
                    tab[i][j] = source[i, j];
                }
            }

            return(tab);
        }
Exemplo n.º 20
0
 public static int GetColumnCount <TSource>(this TSource[,] source)
 {
     return(source.GetLength(0));
 }
Exemplo n.º 21
0
 public static int GetRowCount <TSource>(this TSource[,] source)
 {
     return(source.GetLength(1));
 }