< Summary - ReFlex - Library

Information
Class: ReFlex.Core.Common.Components.ArrayUtils
Assembly: ReFlex.Core.Common
File(s): D:\a\reflex\reflex\library\src\Core\Common\Components\ArrayUtils.cs
Line coverage
100%
Covered lines: 63
Uncovered lines: 0
Coverable lines: 63
Total lines: 158
Line coverage: 100%
Branch coverage
100%
Covered branches: 44
Total branches: 44
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
InitializeArray(...)100%22100%
InitializeArray(...)100%44100%
InitializeArray(...)100%44100%
ReferencingArrays(...)100%44100%
ReferencingArrays(...)100%66100%
ReferencingArrays(...)100%66100%
ReferencingArrays(...)100%1010100%
ReferencingArrays(...)100%88100%

File(s)

D:\a\reflex\reflex\library\src\Core\Common\Components\ArrayUtils.cs

#LineLine coverage
 1using System;
 2using ReFlex.Core.Common.Exceptions;
 3
 4namespace ReFlex.Core.Common.Components
 5{
 6    /// <summary>
 7    /// Helps to initialize and referencing arrays.
 8    /// </summary>
 9    public static class ArrayUtils
 10    {
 11        /// <summary>
 12        /// Initializes the array.
 13        /// </summary>
 14        /// <typeparam name="T"></typeparam>
 15        /// <param name="array">The array.</param>
 16        /// <param name="size">The size.</param>
 17        public static void InitializeArray<T>(out T[] array, int size)
 18            where T : new()
 1219        {
 1220            array = new T[size];
 22010221            for (var i = 0; i < array.Length; i++)
 11003922                array[i] = new T();
 1223        }
 24
 25        /// <summary>
 26        /// Initializes a multidimensional array.
 27        /// </summary>
 28        /// <typeparam name="T">The inner value type of the array.</typeparam>
 29        /// <param name="array">The array.</param>
 30        /// <param name="width">The width.</param>
 31        /// <param name="height">The height.</param>
 32        public static void InitializeArray<T>(out T[,] array, int width, int height)
 33            where T : new()
 1134        {
 1135            array = new T[width, height];
 408836            for (var i = 0; i < array.GetLength(0); i++)
 95464637                for (var j = 0; j < array.GetLength(1); j++)
 47529038                    array[i, j] = new T();
 1139        }
 40
 41        /// <summary>
 42        /// Initializes a jagged array.
 43        /// </summary>
 44        /// <typeparam name="T">The inner value type of the array.</typeparam>
 45        /// <param name="array">The array.</param>
 46        /// <param name="width">The width.</param>
 47        /// <param name="height">The height.</param>
 48        public static void InitializeArray<T>(out T[][] array, int width, int height)
 49            where T : new()
 1150        {
 1151            array = new T[width][];
 680852            for (var i = 0; i < array.Length; i++)
 339353            {
 339354                array[i] = new T[height];
 309509055                for (var j = 0; j < array[i].Length; j++)
 154415256                    array[i][j] = new T();
 339357            }
 1158        }
 59
 60        /// <summary>
 61        /// Referencing the inner values in the arrays.
 62        /// </summary>
 63        /// <typeparam name="T">The inner value type of both arrays.</typeparam>
 64        /// <param name="source">The source.</param>
 65        /// <param name="target">The target.</param>
 66        /// <exception cref="ArraysWithDifferentSizesException"></exception>
 67        public static void ReferencingArrays<T>(T[] source, T[] target)
 368        {
 369            if (source.Length != target.Length)
 170                throw new ArraysWithDifferentSizesException();
 71
 157472            for (var i = 0; i < target.Length; i++)
 78573                target[i] = source[i];
 274        }
 75
 76        /// <summary>
 77        /// Referencing the inner values in the arrays.
 78        /// </summary>
 79        /// <typeparam name="T">The inner value type of both arrays.</typeparam>
 80        /// <param name="source">The source.</param>
 81        /// <param name="target">The target.</param>
 82        /// <exception cref="ArraysWithDifferentSizesException"></exception>
 83        public static void ReferencingArrays<T>(T[] source, T[,] target)
 384        {
 385            if (source.Length != target.GetLength(1) * target.GetLength(0))
 186                throw new ArraysWithDifferentSizesException();
 87
 35488            for (var x = 0; x < target.GetLength(0); x++)
 7232089                for (var y = 0; y < target.GetLength(1); y++)
 3598590                    target[x, y] = source[y * target.GetLength(0) + x];
 291        }
 92
 93        /// <summary>
 94        /// Referencing the inner values in the arrays.
 95        /// </summary>
 96        /// <typeparam name="T">The inner value type of both arrays.</typeparam>
 97        /// <param name="source">The one dimensional array.</param>
 98        /// <param name="target">The two dimensional array.</param>
 99        /// <exception cref="ArraysWithDifferentSizesException"></exception>
 100        public static void ReferencingArrays<T>(T[] source, T[][] target)
 3101        {
 970102            for (var i = 0; i < target.Length; i++)
 483103            {
 483104                if (source.Length != target.Length * target[i].Length)
 1105                    throw new ArraysWithDifferentSizesException();
 106
 145088107                for (var j = 0; j < target[i].Length; j++)
 72062108                    target[i][j] = source[j * target.Length + i];
 482109            }
 2110        }
 111
 112        /// <summary>
 113        /// RReferencing the inner values in the arrays.
 114        /// </summary>
 115        /// <typeparam name="T">The inner value type of both arrays.</typeparam>
 116        /// <param name="source">The source.</param>
 117        /// <param name="target">The target.</param>
 118        /// <exception cref="ArraysWithDifferentSizesException"></exception>
 119        [Obsolete("Warning: Referencing 2d arrays is extremely slow")]
 120        public static void ReferencingArrays<T>(T[,] source, T[,] target)
 4121        {
 4122            if (source.GetLength(0) != target.GetLength(0) || source.GetLength(1) != target.GetLength(1))
 3123                throw new ArraysWithDifferentSizesException();
 124
 436125            for (var i = 0; i < target.Length; i++)
 217126            {
 127                // if (source.GetLength(0) * source.GetLength(1) != target.GetLength(0) * target.GetLength(1))
 128                //     throw new ArraysWithDifferentSizesException();
 129
 13888130                for (var x = 0; x < target.GetLength(0); x++)
 107632131                    for (var y = 0; y < target.GetLength(1); y++)
 47089132                        target[x, y] = source[x, y];
 217133            }
 1134        }
 135
 136        /// <summary>
 137        /// RReferencing the inner values in the arrays.
 138        /// </summary>
 139        /// <typeparam name="T">The inner value type of both arrays.</typeparam>
 140        /// <param name="source">The source.</param>
 141        /// <param name="target">The target.</param>
 142        /// <exception cref="ArraysWithDifferentSizesException"></exception>
 143        public static void ReferencingArrays<T>(T[][] source, T[][] target)
 4144        {
 4145            if (source.GetLength(0) != target.GetLength(0))
 2146                throw new ArraysWithDifferentSizesException();
 147
 266148            for (var i = 0; i < target.Length; i++)
 132149            {
 132150                if (source[i].Length != target[i].Length)
 1151                    throw new ArraysWithDifferentSizesException();
 152
 79910153                for (var j = 0; j < target[i].Length; j++)
 39824154                    target[i][j] = source[i][j];
 131155            }
 1156        }
 157    }
 158}