|
| 1 | +// Copyright (c) libplctag.NET contributors |
| 2 | +// https://github.com/libplctag/libplctag.NET |
| 3 | +// |
| 4 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 7 | + |
| 8 | +using System; |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.Text; |
| 11 | + |
| 12 | +namespace libplctag.DataTypes.Extensions |
| 13 | +{ |
| 14 | + [Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")] |
| 15 | + public static class ArrayExtensions |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Extension method to flatten a 2D array to a 1D array |
| 19 | + /// </summary> |
| 20 | + /// <typeparam name="T">Array Type</typeparam> |
| 21 | + /// <param name="input">2D array to be flattened</param> |
| 22 | + /// <returns>1D array</returns> |
| 23 | + public static T[] To1DArray<T>(this T[,] input) |
| 24 | + { |
| 25 | + // Step 1: get total size of 2D array, and allocate 1D array. |
| 26 | + int size = input.Length; |
| 27 | + T[] result = new T[size]; |
| 28 | + |
| 29 | + // Step 2: copy 2D array elements into a 1D array. |
| 30 | + int write = 0; |
| 31 | + for (int i = 0; i <= input.GetUpperBound(0); i++) |
| 32 | + { |
| 33 | + for (int z = 0; z <= input.GetUpperBound(1); z++) |
| 34 | + { |
| 35 | + result[write++] = input[i, z]; |
| 36 | + } |
| 37 | + } |
| 38 | + // Step 3: return the new array. |
| 39 | + return result; |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Extension method to flatten a 3D array to a 1D array |
| 44 | + /// </summary> |
| 45 | + /// <typeparam name="T">Array Type</typeparam> |
| 46 | + /// <param name="input">3D array to be flattened</param> |
| 47 | + /// <returns>1D array</returns> |
| 48 | + public static T[] To1DArray<T>(this T[,,] input) |
| 49 | + { |
| 50 | + // Step 1: get total size of 3D array, and allocate 1D array. |
| 51 | + int size = input.Length; |
| 52 | + T[] result = new T[size]; |
| 53 | + |
| 54 | + // Step 2: copy 3D array elements into a 1D array. |
| 55 | + int write = 0; |
| 56 | + for (int i = 0; i <= input.GetUpperBound(0); i++) |
| 57 | + { |
| 58 | + for (int j = 0; j <= input.GetUpperBound(1); j++) |
| 59 | + { |
| 60 | + for (int k = 0; k < input.GetUpperBound(2); k++) |
| 61 | + { |
| 62 | + result[write++] = input[i, j, k]; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + // Step 3: return the new array. |
| 67 | + return result; |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Extension method to reshape a 1D array into a 2D array |
| 72 | + /// </summary> |
| 73 | + /// <typeparam name="T">Array Type</typeparam> |
| 74 | + /// <param name="input">1D array to be reshaped</param> |
| 75 | + /// <param name="height">Desired height (first index) of 2D array</param> |
| 76 | + /// <param name="width">Desired width (second index) of 2D array</param> |
| 77 | + /// <returns>2D array</returns> |
| 78 | + public static T[,] To2DArray<T>(this T[] input, int height, int width) |
| 79 | + { |
| 80 | + T[,] output = new T[height, width]; |
| 81 | + |
| 82 | + for (int i = 0; i < height; i++) |
| 83 | + { |
| 84 | + for (int j = 0; j < width; j++) |
| 85 | + { |
| 86 | + output[i, j] = input[i * width + j]; |
| 87 | + } |
| 88 | + } |
| 89 | + return output; |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Extension method to reshape a 1D array into a 3D array |
| 94 | + /// </summary> |
| 95 | + /// <typeparam name="T">Array Type</typeparam> |
| 96 | + /// <param name="input">1D array to be reshaped</param> |
| 97 | + /// <param name="height">Desired height (first index) of 3D array</param> |
| 98 | + /// <param name="width">Desired width (second index) of 3D array</param> |
| 99 | + /// <param name="length">Desired length (third index) of 3D array</param> |
| 100 | + /// <returns>#D array</returns> |
| 101 | + public static T[,,] To3DArray<T>(this T[] input, int height, int width, int length) |
| 102 | + { |
| 103 | + T[,,] output = new T[height, width, length]; |
| 104 | + |
| 105 | + for (int i = 0; i < height; i++) |
| 106 | + { |
| 107 | + for (int j = 0; j < width; j++) |
| 108 | + { |
| 109 | + for (int k = 0; k < length; k++) |
| 110 | + { |
| 111 | + output[i, j, k] = input[i * height * width + j * width + k]; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + return output; |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | +} |
0 commit comments