| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | |
| | | 5 | | namespace SwiftCollections.Dimensions; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Represents a 2D array specifically designed to handle short integer values. |
| | | 9 | | /// </summary> |
| | | 10 | | [Serializable] |
| | | 11 | | [JsonConverter(typeof(SwiftStateJsonConverterFactory))] |
| | | 12 | | [MemoryPackable] |
| | | 13 | | public partial class SwiftShortArray2D : SwiftArray2D<short> |
| | | 14 | | { |
| | | 15 | | #region Constructors |
| | | 16 | | |
| | 0 | 17 | | public SwiftShortArray2D() { } |
| | | 18 | | |
| | 3 | 19 | | public SwiftShortArray2D(int width, int height) : base(width, height) { } |
| | | 20 | | |
| | 12 | 21 | | public SwiftShortArray2D(int width, int height, short defaultValue) : base(width, height, defaultValue) { } |
| | | 22 | | |
| | 6 | 23 | | public SwiftShortArray2D(short[,] source) : base(source) { } |
| | | 24 | | |
| | | 25 | | [MemoryPackConstructor] |
| | 6 | 26 | | public SwiftShortArray2D(Array2DState<short> state) : base(state) { } |
| | | 27 | | |
| | | 28 | | #endregion |
| | | 29 | | |
| | | 30 | | #region Collection Management |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Scales all elements in the array by the specified factor. |
| | | 34 | | /// </summary> |
| | | 35 | | public void Scale(short factor) |
| | 2 | 36 | | { |
| | 30 | 37 | | for (int i = 0; i < InnerArray.Length; i++) |
| | 13 | 38 | | InnerArray[i] = (short)(InnerArray[i] * factor); |
| | 2 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Normalizes all elements in the array to a specified range. |
| | | 43 | | /// </summary> |
| | | 44 | | public void Normalize(short min, short max) |
| | 3 | 45 | | { |
| | 3 | 46 | | short currentMin = short.MaxValue; |
| | 3 | 47 | | short currentMax = short.MinValue; |
| | | 48 | | |
| | | 49 | | // Find current min and max |
| | 43 | 50 | | foreach (var value in InnerArray) |
| | 17 | 51 | | { |
| | 20 | 52 | | if (value < currentMin) currentMin = value; |
| | 26 | 53 | | if (value > currentMax) currentMax = value; |
| | 17 | 54 | | } |
| | | 55 | | |
| | | 56 | | // Normalize values |
| | 40 | 57 | | for (int i = 0; i < InnerArray.Length; i++) |
| | 17 | 58 | | InnerArray[i] = (short)(((InnerArray[i] - currentMin) / (float)(currentMax - currentMin)) * (max - min) + mi |
| | 3 | 59 | | } |
| | | 60 | | |
| | | 61 | | #endregion |
| | | 62 | | } |