< Summary

Information
Class: SwiftCollections.Dimensions.SwiftShortArray2D
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Dimension/Default/SwiftShortArray2D.cs
Line coverage
100%
Covered lines: 16
Uncovered lines: 0
Coverable lines: 16
Total lines: 94
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
Scale(...)100%22100%
Normalize(...)100%88100%

File(s)

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Dimension/Default/SwiftShortArray2D.cs

#LineLine coverage
 1//=======================================================================
 2// SwiftShortArray2D.cs
 3//=======================================================================
 4// MIT License, Copyright (c) 2024-present David Oravsky (mrdav30)
 5// See LICENSE file in the project root for full license information.
 6//=======================================================================
 7
 8using System;
 9using System.Text.Json.Serialization;
 10using Chronicler;
 11using Chronicler.Serialization;
 12using MemoryPack;
 13
 14namespace SwiftCollections.Dimensions;
 15
 16/// <summary>
 17/// Represents a 2D array specifically designed to handle short integer values.
 18/// </summary>
 19[Serializable]
 20[JsonConverter(typeof(StateJsonConverterFactory))]
 21[MemoryPackable]
 22public partial class SwiftShortArray2D : SwiftArray2D<short>, IStateBacked<Array2DState<short>>
 23{
 24    #region Constructors
 25
 26    /// <summary>
 27    /// Initializes a new instance of the SwiftShortArray2D class.
 28    /// </summary>
 229    public SwiftShortArray2D() { }
 30
 31    /// <summary>
 32    /// Initializes a new instance of the SwiftShortArray2D class with the specified dimensions.
 33    /// </summary>
 34    /// <param name="width">The number of columns in the two-dimensional array. Must be greater than zero.</param>
 35    /// <param name="height">The number of rows in the two-dimensional array. Must be greater than zero.</param>
 236    public SwiftShortArray2D(int width, int height) : base(width, height) { }
 37
 38    /// <summary>
 39    /// Initializes a new instance of the SwiftShortArray2D class with the specified dimensions and default value.
 40    /// </summary>
 41    /// <param name="width">The number of columns in the two-dimensional array. Must be greater than zero.</param>
 42    /// <param name="height">The number of rows in the two-dimensional array. Must be greater than zero.</param>
 43    /// <param name="defaultValue">The value to assign to each element in the array upon initialization.</param>
 844    public SwiftShortArray2D(int width, int height, short defaultValue) : base(width, height, defaultValue) { }
 45
 46    /// <summary>
 47    /// Initializes a new instance of the SwiftShortArray2D class using the specified two-dimensional array of short
 48    /// values.
 49    /// </summary>
 50    /// <param name="source">A two-dimensional array of short values that provides the initial data for the array.</para
 451    public SwiftShortArray2D(short[,] source) : base(source) { }
 52
 53    /// <summary>
 54    /// Initializes a new instance of the SwiftShortArray2D class using the specified array state.
 55    /// </summary>
 56    /// <param name="state">The state object that provides the initial data and configuration for the two-dimensional sh
 57    [MemoryPackConstructor]
 458    public SwiftShortArray2D(Array2DState<short> state) : base(state) { }
 59
 60    #endregion
 61
 62    #region Collection Management
 63
 64    /// <summary>
 65    /// Scales all elements in the array by the specified factor.
 66    /// </summary>
 67    public void Scale(short factor)
 68    {
 3069        for (int i = 0; i < InnerArray.Length; i++)
 1370            InnerArray[i] = (short)(InnerArray[i] * factor);
 271    }
 72
 73    /// <summary>
 74    /// Normalizes all elements in the array to a specified range.
 75    /// </summary>
 76    public void Normalize(short min, short max)
 77    {
 378        short currentMin = short.MaxValue;
 379        short currentMax = short.MinValue;
 80
 81        // Find current min and max
 4082        foreach (var value in InnerArray)
 83        {
 2084            if (value < currentMin) currentMin = value;
 2685            if (value > currentMax) currentMax = value;
 86        }
 87
 88        // Normalize values
 4089        for (int i = 0; i < InnerArray.Length; i++)
 1790            InnerArray[i] = (short)(((InnerArray[i] - currentMin) / (float)(currentMax - currentMin)) * (max - min) + mi
 391    }
 92
 93    #endregion
 94}