| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Text.Json.Serialization; |
| | | 6 | | |
| | | 7 | | namespace SwiftCollections.Dimensions; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents a generic, flattened 3D array with efficient indexing and resizing capabilities. |
| | | 11 | | /// Optimized for use in performance-critical applications like game grids. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <typeparam name="T">The type of elements in the 3D array.</typeparam> |
| | | 14 | | [Serializable] |
| | | 15 | | [JsonConverter(typeof(SwiftStateJsonConverterFactory))] |
| | | 16 | | [MemoryPackable] |
| | | 17 | | public partial class SwiftArray3D<T> : IEnumerable<T>, IEnumerable |
| | | 18 | | { |
| | | 19 | | #region Fields |
| | | 20 | | |
| | | 21 | | private T[] _innerArray; |
| | | 22 | | |
| | | 23 | | private int _width; |
| | | 24 | | |
| | | 25 | | private int _height; |
| | | 26 | | |
| | | 27 | | private int _depth; |
| | | 28 | | |
| | | 29 | | #endregion |
| | | 30 | | |
| | | 31 | | #region Constructors |
| | | 32 | | |
| | 0 | 33 | | public SwiftArray3D() : this(0, 0, 0) { } |
| | | 34 | | |
| | 19 | 35 | | public SwiftArray3D(int width, int height, int length) |
| | 19 | 36 | | { |
| | 19 | 37 | | Initialize(width, height, length); |
| | 19 | 38 | | } |
| | | 39 | | |
| | 1 | 40 | | public SwiftArray3D(int width, int height, int length, T defaultValue) : this(width, height, length) |
| | 1 | 41 | | { |
| | 1 | 42 | | Fill(defaultValue); |
| | 1 | 43 | | } |
| | | 44 | | |
| | | 45 | | [MemoryPackConstructor] |
| | 2 | 46 | | public SwiftArray3D(Array3DState<T> state) |
| | 2 | 47 | | { |
| | 2 | 48 | | State = state; |
| | 2 | 49 | | } |
| | | 50 | | |
| | | 51 | | #endregion |
| | | 52 | | |
| | | 53 | | #region Properties |
| | | 54 | | |
| | | 55 | | |
| | | 56 | | [JsonIgnore] |
| | | 57 | | [MemoryPackIgnore] |
| | 2378114 | 58 | | public int Width => _width; |
| | | 59 | | |
| | | 60 | | [JsonIgnore] |
| | | 61 | | [MemoryPackIgnore] |
| | 4756034 | 62 | | public int Height => _height; |
| | | 63 | | |
| | | 64 | | [JsonIgnore] |
| | | 65 | | [MemoryPackIgnore] |
| | 7134266 | 66 | | public int Depth => _depth; |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Total size of the array. |
| | | 70 | | /// </summary> |
| | | 71 | | [JsonIgnore] |
| | | 72 | | [MemoryPackIgnore] |
| | 0 | 73 | | public int Size => _width * _height * _depth; |
| | | 74 | | |
| | | 75 | | /// <inheritdoc cref="Array.Length" /> |
| | | 76 | | [JsonIgnore] |
| | | 77 | | [MemoryPackIgnore] |
| | 2 | 78 | | public int Length => _innerArray.Length; |
| | | 79 | | |
| | | 80 | | [JsonIgnore] |
| | | 81 | | [MemoryPackIgnore] |
| | | 82 | | public T this[int x, int y, int z] |
| | | 83 | | { |
| | | 84 | | get |
| | 1251123 | 85 | | { |
| | 1251123 | 86 | | ValidateIndex(x, y, z); |
| | 1251119 | 87 | | return _innerArray[GetIndex(x, y, z)]; |
| | 1251119 | 88 | | } |
| | | 89 | | set |
| | 1126014 | 90 | | { |
| | 1126014 | 91 | | ValidateIndex(x, y, z); |
| | 1126014 | 92 | | _innerArray[GetIndex(x, y, z)] = value; |
| | 1126014 | 93 | | } |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | [JsonInclude] |
| | | 97 | | [MemoryPackInclude] |
| | | 98 | | public Array3DState<T> State |
| | | 99 | | { |
| | | 100 | | get |
| | 2 | 101 | | { |
| | 2 | 102 | | var data = new T[_innerArray.Length]; |
| | 2 | 103 | | Array.Copy(_innerArray, data, data.Length); |
| | | 104 | | |
| | 2 | 105 | | return new Array3DState<T>( |
| | 2 | 106 | | _width, |
| | 2 | 107 | | _height, |
| | 2 | 108 | | _depth, |
| | 2 | 109 | | data |
| | 2 | 110 | | ); |
| | 2 | 111 | | } |
| | | 112 | | |
| | | 113 | | internal set |
| | 2 | 114 | | { |
| | 2 | 115 | | _width = value.Width; |
| | 2 | 116 | | _height = value.Height; |
| | 2 | 117 | | _depth = value.Depth; |
| | | 118 | | |
| | 2 | 119 | | _innerArray = new T[value.Data.Length]; |
| | 2 | 120 | | Array.Copy(value.Data, _innerArray, value.Data.Length); |
| | 2 | 121 | | } |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | #endregion |
| | | 125 | | |
| | | 126 | | #region Methods |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Initializes the 3D array with the specified dimensions. |
| | | 130 | | /// </summary> |
| | | 131 | | private void Initialize(int width, int height, int depth) |
| | 19 | 132 | | { |
| | 19 | 133 | | _width = width; |
| | 19 | 134 | | _height = height; |
| | 19 | 135 | | _depth = depth; |
| | 19 | 136 | | _innerArray = new T[width * height * depth]; |
| | 19 | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Resizes the 3D array to the specified dimensions. |
| | | 141 | | /// Retains existing data where possible. |
| | | 142 | | /// </summary> |
| | | 143 | | public void Resize(int newWidth, int newHeight, int newDepth) |
| | 5 | 144 | | { |
| | 5 | 145 | | var newArray = new T[newWidth * newHeight * newDepth]; |
| | | 146 | | |
| | 5 | 147 | | int minWidth = Math.Min(Width, newWidth); |
| | 5 | 148 | | int minHeight = Math.Min(Height, newHeight); |
| | 5 | 149 | | int minDepth = Math.Min(Depth, newDepth); |
| | | 150 | | |
| | 32 | 151 | | for (int x = 0; x < minWidth; x++) |
| | 11 | 152 | | { |
| | 88 | 153 | | for (int y = 0; y < minHeight; y++) |
| | 33 | 154 | | { |
| | 280 | 155 | | for (int z = 0; z < minDepth; z++) |
| | 107 | 156 | | { |
| | 107 | 157 | | int srcIndex = GetIndex(x, y, z); |
| | 107 | 158 | | int dstIndex = x * (newHeight * newDepth) + y * newDepth + z; |
| | 107 | 159 | | newArray[dstIndex] = _innerArray[srcIndex]; |
| | 107 | 160 | | } |
| | 33 | 161 | | } |
| | 11 | 162 | | } |
| | | 163 | | |
| | 5 | 164 | | _innerArray = newArray; |
| | 5 | 165 | | _width = newWidth; |
| | 5 | 166 | | _height = newHeight; |
| | 5 | 167 | | _depth = newDepth; |
| | 5 | 168 | | } |
| | | 169 | | |
| | | 170 | | /// <summary> |
| | | 171 | | /// Shifts the elements in the array by the specified offsets along each axis. |
| | | 172 | | /// </summary> |
| | | 173 | | /// <param name="xOffset">The offset to apply along the X-axis.</param> |
| | | 174 | | /// <param name="yOffset">The offset to apply along the Y-axis.</param> |
| | | 175 | | /// <param name="zOffset">The offset to apply along the Z-axis.</param> |
| | | 176 | | /// <param name="wrap"> |
| | | 177 | | /// Specifies whether to wrap elements that exceed the array's boundaries. |
| | | 178 | | /// If <c>true</c>, values wrap around to the other side of the array. |
| | | 179 | | /// If <c>false</c>, values that exceed boundaries are discarded. |
| | | 180 | | /// </param> |
| | | 181 | | /// <remarks> |
| | | 182 | | /// - Wrapping behavior ensures that no data is lost during shifts. |
| | | 183 | | /// - Non-wrapping behavior discards elements that move out of bounds. |
| | | 184 | | /// </remarks> |
| | | 185 | | public void Shift(int xOffset, int yOffset, int zOffset, bool wrap = true) |
| | 5 | 186 | | { |
| | 5 | 187 | | var newArray = new T[Width * Height * Depth]; |
| | | 188 | | |
| | 46 | 189 | | for (int x = 0; x < Width; x++) |
| | 18 | 190 | | { |
| | 180 | 191 | | for (int y = 0; y < Height; y++) |
| | 72 | 192 | | { |
| | 792 | 193 | | for (int z = 0; z < Depth; z++) |
| | 324 | 194 | | { |
| | 324 | 195 | | int newX = wrap ? (x + xOffset + Width) % Width : x + xOffset; |
| | 324 | 196 | | int newY = wrap ? (y + yOffset + Height) % Height : y + yOffset; |
| | 324 | 197 | | int newZ = wrap ? (z + zOffset + Depth) % Depth : z + zOffset; |
| | | 198 | | |
| | 324 | 199 | | if (IsValidIndex(newX, newY, newZ)) |
| | 301 | 200 | | { |
| | 301 | 201 | | int srcIndex = GetIndex(x, y, z); |
| | 301 | 202 | | int dstIndex = GetIndex(newX, newY, newZ); |
| | 301 | 203 | | newArray[dstIndex] = _innerArray[srcIndex]; |
| | 301 | 204 | | } |
| | 324 | 205 | | } |
| | 72 | 206 | | } |
| | 18 | 207 | | } |
| | | 208 | | |
| | 5 | 209 | | _innerArray = newArray; |
| | 5 | 210 | | } |
| | | 211 | | |
| | | 212 | | /// <summary> |
| | | 213 | | /// Clears all elements in the array. |
| | | 214 | | /// </summary> |
| | 1 | 215 | | public void Clear() => Array.Clear(_innerArray, 0, _innerArray.Length); |
| | | 216 | | |
| | | 217 | | /// <summary> |
| | | 218 | | /// Fills the entire array with the specified value. |
| | | 219 | | /// </summary> |
| | | 220 | | public void Fill(T value) |
| | 4 | 221 | | { |
| | 224 | 222 | | for (int i = 0; i < _innerArray.Length; i++) |
| | 108 | 223 | | _innerArray[i] = value; |
| | 4 | 224 | | } |
| | | 225 | | |
| | | 226 | | public virtual int GetIndex(int x, int y, int z) |
| | 2377842 | 227 | | { |
| | 2377842 | 228 | | return x * (Height * Depth) + y * Depth + z; |
| | 2377842 | 229 | | } |
| | | 230 | | |
| | | 231 | | /// <summary> |
| | | 232 | | /// Validates the specified indices. |
| | | 233 | | /// Throws an exception if the indices are out of bounds. |
| | | 234 | | /// </summary> |
| | | 235 | | public virtual void ValidateIndex(int x, int y, int z) |
| | 2377137 | 236 | | { |
| | 2377137 | 237 | | if (!IsValidIndex(x, y, z)) |
| | 4 | 238 | | throw new IndexOutOfRangeException($"Invalid index ({x}, {y}, {z}) for dimensions ({Width}, {Height}, {Depth |
| | 2377133 | 239 | | } |
| | | 240 | | |
| | | 241 | | /// <summary> |
| | | 242 | | /// Checks if the specified indices are within bounds. |
| | | 243 | | /// </summary> |
| | | 244 | | public virtual bool IsValidIndex(int x, int y, int z) => |
| | 2377464 | 245 | | x >= 0 && x < Width && y >= 0 && y < Height && z >= 0 && z < Depth; |
| | | 246 | | |
| | | 247 | | #endregion |
| | | 248 | | |
| | | 249 | | #region IEnumerator Implementation |
| | | 250 | | |
| | | 251 | | /// <summary> |
| | | 252 | | /// Returns an enumerator that iterates through all elements in the 3D array. |
| | | 253 | | /// </summary> |
| | | 254 | | /// <returns>An enumerator for the 3D array.</returns> |
| | | 255 | | public IEnumerator<T> GetEnumerator() |
| | 5 | 256 | | { |
| | 36 | 257 | | for (int x = 0; x < Width; x++) |
| | 13 | 258 | | { |
| | 96 | 259 | | for (int y = 0; y < Height; y++) |
| | 35 | 260 | | { |
| | 264 | 261 | | for (int z = 0; z < Depth; z++) |
| | 97 | 262 | | yield return this[x, y, z]; |
| | 35 | 263 | | } |
| | 13 | 264 | | } |
| | 5 | 265 | | } |
| | | 266 | | |
| | | 267 | | /// <summary> |
| | | 268 | | /// Returns an enumerator that iterates through all elements in the 3D array (non-generic). |
| | | 269 | | /// </summary> |
| | | 270 | | /// <returns>An enumerator for the 3D array.</returns> |
| | 0 | 271 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 272 | | |
| | | 273 | | |
| | | 274 | | #endregion |
| | | 275 | | } |