< 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()
 52419        {
 52420            array = new T[size];
 201603621            for (var i = 0; i < array.Length; i++)
 100749422                array[i] = new T();
 52423        }
 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()
 19634        {
 19635            array = new T[width, height];
 1608436            for (var i = 0; i < array.GetLength(0); i++)
 196092837                for (var j = 0; j < array.GetLength(1); j++)
 97261838                    array[i, j] = new T();
 19639        }
 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()
 53850        {
 53851            array = new T[width][];
 4598452            for (var i = 0; i < array.Length; i++)
 2245453            {
 2245454                array[i] = new T[height];
 717240855                for (var j = 0; j < array[i].Length; j++)
 356375056                    array[i][j] = new T();
 2245457            }
 53858        }
 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)
 668        {
 669            if (source.Length != target.Length)
 270                throw new ArraysWithDifferentSizesException();
 71
 193672            for (var i = 0; i < target.Length; i++)
 96473                target[i] = source[i];
 474        }
 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)
 684        {
 685            if (source.Length != target.GetLength(1) * target.GetLength(0))
 286                throw new ArraysWithDifferentSizesException();
 87
 196488            for (var x = 0; x < target.GetLength(0); x++)
 50754089                for (var y = 0; y < target.GetLength(1); y++)
 25279290                    target[x, y] = source[y * target.GetLength(0) + x];
 491        }
 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)
 506101        {
 33404102            for (var i = 0; i < target.Length; i++)
 16198103            {
 16198104                if (source.Length != target.Length * target[i].Length)
 2105                    throw new ArraysWithDifferentSizesException();
 106
 1535120107                for (var j = 0; j < target[i].Length; j++)
 751364108                    target[i][j] = source[j * target.Length + i];
 16196109            }
 504110        }
 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)
 8121        {
 8122            if (source.GetLength(0) != target.GetLength(0) || source.GetLength(1) != target.GetLength(1))
 6123                throw new ArraysWithDifferentSizesException();
 124
 3460125            for (var i = 0; i < target.Length; i++)
 1728126            {
 127                // if (source.GetLength(0) * source.GetLength(1) != target.GetLength(0) * target.GetLength(1))
 128                //     throw new ArraysWithDifferentSizesException();
 129
 65664130                for (var x = 0; x < target.GetLength(0); x++)
 3048192131                    for (var y = 0; y < target.GetLength(1); y++)
 1492992132                        target[x, y] = source[x, y];
 1728133            }
 2134        }
 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)
 8144        {
 8145            if (source.GetLength(0) != target.GetLength(0))
 4146                throw new ArraysWithDifferentSizesException();
 147
 56148            for (var i = 0; i < target.Length; i++)
 26149            {
 26150                if (source[i].Length != target[i].Length)
 2151                    throw new ArraysWithDifferentSizesException();
 152
 4320153                for (var j = 0; j < target[i].Length; j++)
 2136154                    target[i][j] = source[i][j];
 24155            }
 2156        }
 157    }
 158}