| | | 1 | | using Chronicler; |
| | | 2 | | using MemoryPack; |
| | | 3 | | using System; |
| | | 4 | | using System.Collections; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Text.Json.Serialization; |
| | | 7 | | |
| | | 8 | | namespace SwiftCollections.Dimensions; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Represents a generic, flattened 3D array with efficient indexing and resizing capabilities. |
| | | 12 | | /// Optimized for use in performance-critical applications like game grids. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <typeparam name="T">The type of elements in the 3D array.</typeparam> |
| | | 15 | | [Serializable] |
| | | 16 | | [JsonConverter(typeof(StateJsonConverterFactory))] |
| | | 17 | | [MemoryPackable] |
| | | 18 | | public partial class SwiftArray3D<T> : IStateBacked<Array3DState<T>>, IEnumerable<T>, IEnumerable |
| | | 19 | | { |
| | | 20 | | #region Fields |
| | | 21 | | |
| | | 22 | | private T[] _innerArray; |
| | | 23 | | |
| | | 24 | | private int _width; |
| | | 25 | | |
| | | 26 | | private int _height; |
| | | 27 | | |
| | | 28 | | private int _depth; |
| | | 29 | | |
| | | 30 | | #endregion |
| | | 31 | | |
| | | 32 | | #region Constructors |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Initializes a new instance of the SwiftArray3D class with zero dimensions. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <remarks> |
| | | 38 | | /// This constructor creates an empty three-dimensional array. |
| | | 39 | | /// Use this overload when you intend to set the dimensions later or create an empty array. |
| | | 40 | | /// </remarks> |
| | 0 | 41 | | public SwiftArray3D() : this(0, 0, 0) { } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Initializes a new instance of the SwiftArray3D class with the specified dimensions. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="width">The number of elements in the first dimension. Must be greater than zero.</param> |
| | | 47 | | /// <param name="height">The number of elements in the second dimension. Must be greater than zero.</param> |
| | | 48 | | /// <param name="depth">The number of elements in the third dimension. Must be greater than zero.</param> |
| | 20 | 49 | | public SwiftArray3D(int width, int height, int depth) |
| | | 50 | | { |
| | 20 | 51 | | _width = width; |
| | 20 | 52 | | _height = height; |
| | 20 | 53 | | _depth = depth; |
| | 20 | 54 | | _innerArray = new T[width * height * depth]; |
| | 20 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Initializes a new instance of the SwiftArray3D class with the specified dimensions and fills all elements with |
| | | 59 | | /// the provided default value. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="width">The number of elements in the first dimension. Must be greater than zero.</param> |
| | | 62 | | /// <param name="height">The number of elements in the second dimension. Must be greater than zero.</param> |
| | | 63 | | /// <param name="depth">The number of elements in the third dimension. Must be greater than zero.</param> |
| | | 64 | | /// <param name="defaultValue">The value to assign to each element in the array upon initialization.</param> |
| | 1 | 65 | | public SwiftArray3D(int width, int height, int depth, T defaultValue) : this(width, height, depth) |
| | | 66 | | { |
| | 1 | 67 | | Fill(defaultValue); |
| | 1 | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Initializes a new instance of the SwiftArray3D class with the specified array state. |
| | | 72 | | /// </summary> |
| | | 73 | | /// <param name="state"> |
| | | 74 | | /// The state object that encapsulates the underlying data and configuration for the three-dimensional array. |
| | | 75 | | /// Cannot be null. |
| | | 76 | | /// </param> |
| | | 77 | | [MemoryPackConstructor] |
| | 2 | 78 | | public SwiftArray3D(Array3DState<T> state) |
| | | 79 | | { |
| | 2 | 80 | | State = state; |
| | 2 | 81 | | _innerArray ??= Array.Empty<T>(); // Ensure _innerArray is initialized to avoid null reference issues. |
| | 2 | 82 | | } |
| | | 83 | | |
| | | 84 | | #endregion |
| | | 85 | | |
| | | 86 | | #region Properties |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Gets the width of the object. |
| | | 90 | | /// </summary> |
| | | 91 | | [JsonIgnore] |
| | | 92 | | [MemoryPackIgnore] |
| | 2377224 | 93 | | public int Width => _width; |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Gets the height value associated with the current instance. |
| | | 97 | | /// </summary> |
| | | 98 | | [JsonIgnore] |
| | | 99 | | [MemoryPackIgnore] |
| | 4755270 | 100 | | public int Height => _height; |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Gets the current depth value for this instance. |
| | | 104 | | /// </summary> |
| | | 105 | | [JsonIgnore] |
| | | 106 | | [MemoryPackIgnore] |
| | 7133811 | 107 | | public int Depth => _depth; |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Total size of the array. |
| | | 111 | | /// </summary> |
| | | 112 | | [JsonIgnore] |
| | | 113 | | [MemoryPackIgnore] |
| | 0 | 114 | | public int Size => _width * _height * _depth; |
| | | 115 | | |
| | | 116 | | /// <inheritdoc cref="Array.Length" /> |
| | | 117 | | [JsonIgnore] |
| | | 118 | | [MemoryPackIgnore] |
| | 2 | 119 | | public int Length => _innerArray.Length; |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Gets or sets the element at the specified three-dimensional indices. |
| | | 123 | | /// </summary> |
| | | 124 | | /// <remarks>An exception is thrown if any index is outside the valid range for its dimension.</remarks> |
| | | 125 | | /// <param name="x">The zero-based index along the first dimension.</param> |
| | | 126 | | /// <param name="y">The zero-based index along the second dimension.</param> |
| | | 127 | | /// <param name="z">The zero-based index along the third dimension.</param> |
| | | 128 | | /// <returns>The element located at the specified indices.</returns> |
| | | 129 | | [JsonIgnore] |
| | | 130 | | [MemoryPackIgnore] |
| | | 131 | | public T this[int x, int y, int z] |
| | | 132 | | { |
| | | 133 | | get |
| | | 134 | | { |
| | 1251125 | 135 | | ValidateIndex(x, y, z); |
| | 1251121 | 136 | | return _innerArray[GetIndex(x, y, z)]; |
| | | 137 | | } |
| | | 138 | | set |
| | | 139 | | { |
| | 1126015 | 140 | | ValidateIndex(x, y, z); |
| | 1126015 | 141 | | _innerArray[GetIndex(x, y, z)] = value; |
| | 1126015 | 142 | | } |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Gets or sets the complete state of the 3D array, including its dimensions and data contents. |
| | | 147 | | /// </summary> |
| | | 148 | | /// <remarks> |
| | | 149 | | /// Setting this property replaces the current array's dimensions and data with those from the specified state. |
| | | 150 | | /// Getting this property returns a snapshot of the current array state. |
| | | 151 | | /// This property is intended for serialization and deserialization scenarios. |
| | | 152 | | /// </remarks> |
| | | 153 | | [JsonInclude] |
| | | 154 | | [MemoryPackInclude] |
| | | 155 | | public Array3DState<T> State |
| | | 156 | | { |
| | | 157 | | get |
| | | 158 | | { |
| | 2 | 159 | | var data = new T[_innerArray.Length]; |
| | 2 | 160 | | Array.Copy(_innerArray, data, data.Length); |
| | | 161 | | |
| | 2 | 162 | | return new Array3DState<T>( |
| | 2 | 163 | | _width, |
| | 2 | 164 | | _height, |
| | 2 | 165 | | _depth, |
| | 2 | 166 | | data |
| | 2 | 167 | | ); |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | internal set |
| | | 171 | | { |
| | 2 | 172 | | _width = value.Width; |
| | 2 | 173 | | _height = value.Height; |
| | 2 | 174 | | _depth = value.Depth; |
| | | 175 | | |
| | 2 | 176 | | _innerArray = new T[value.Data.Length]; |
| | 2 | 177 | | Array.Copy(value.Data, _innerArray, value.Data.Length); |
| | 2 | 178 | | } |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | #endregion |
| | | 182 | | |
| | | 183 | | #region Methods |
| | | 184 | | |
| | | 185 | | /// <summary> |
| | | 186 | | /// Resizes the 3D array to the specified dimensions. |
| | | 187 | | /// Retains existing data where possible. |
| | | 188 | | /// </summary> |
| | | 189 | | public void Resize(int newWidth, int newHeight, int newDepth) |
| | | 190 | | { |
| | 5 | 191 | | var newArray = new T[newWidth * newHeight * newDepth]; |
| | | 192 | | |
| | 5 | 193 | | int minWidth = Math.Min(Width, newWidth); |
| | 5 | 194 | | int minHeight = Math.Min(Height, newHeight); |
| | 5 | 195 | | int minDepth = Math.Min(Depth, newDepth); |
| | | 196 | | |
| | 32 | 197 | | for (int x = 0; x < minWidth; x++) |
| | | 198 | | { |
| | 88 | 199 | | for (int y = 0; y < minHeight; y++) |
| | | 200 | | { |
| | 280 | 201 | | for (int z = 0; z < minDepth; z++) |
| | | 202 | | { |
| | 107 | 203 | | int srcIndex = GetIndex(x, y, z); |
| | 107 | 204 | | int dstIndex = x * (newHeight * newDepth) + y * newDepth + z; |
| | 107 | 205 | | newArray[dstIndex] = _innerArray[srcIndex]; |
| | | 206 | | } |
| | | 207 | | } |
| | | 208 | | } |
| | | 209 | | |
| | 5 | 210 | | _innerArray = newArray; |
| | 5 | 211 | | _width = newWidth; |
| | 5 | 212 | | _height = newHeight; |
| | 5 | 213 | | _depth = newDepth; |
| | 5 | 214 | | } |
| | | 215 | | |
| | | 216 | | /// <summary> |
| | | 217 | | /// Shifts the elements in the array by the specified offsets along each axis. |
| | | 218 | | /// </summary> |
| | | 219 | | /// <param name="xOffset">The offset to apply along the X-axis.</param> |
| | | 220 | | /// <param name="yOffset">The offset to apply along the Y-axis.</param> |
| | | 221 | | /// <param name="zOffset">The offset to apply along the Z-axis.</param> |
| | | 222 | | /// <param name="wrap"> |
| | | 223 | | /// Specifies whether to wrap elements that exceed the array's boundaries. |
| | | 224 | | /// If <c>true</c>, values wrap around to the other side of the array. |
| | | 225 | | /// If <c>false</c>, values that exceed boundaries are discarded. |
| | | 226 | | /// </param> |
| | | 227 | | /// <remarks> |
| | | 228 | | /// - Wrapping behavior ensures that no data is lost during shifts. |
| | | 229 | | /// - Non-wrapping behavior discards elements that move out of bounds. |
| | | 230 | | /// </remarks> |
| | | 231 | | public void Shift(int xOffset, int yOffset, int zOffset, bool wrap = true) |
| | | 232 | | { |
| | 6 | 233 | | var newArray = new T[Width * Height * Depth]; |
| | | 234 | | |
| | 6 | 235 | | if (wrap) |
| | 5 | 236 | | ShiftWrapped(newArray, xOffset, yOffset, zOffset); |
| | | 237 | | else |
| | 1 | 238 | | ShiftClamped(newArray, xOffset, yOffset, zOffset); |
| | | 239 | | |
| | 6 | 240 | | _innerArray = newArray; |
| | 6 | 241 | | } |
| | | 242 | | |
| | | 243 | | private void ShiftWrapped(T[] newArray, int xOffset, int yOffset, int zOffset) |
| | | 244 | | { |
| | 5 | 245 | | int normalizedXOffset = NormalizeShift(xOffset, Width); |
| | 5 | 246 | | int normalizedYOffset = NormalizeShift(yOffset, Height); |
| | 5 | 247 | | int normalizedZOffset = NormalizeShift(zOffset, Depth); |
| | | 248 | | |
| | 46 | 249 | | for (int x = 0; x < Width; x++) |
| | | 250 | | { |
| | 18 | 251 | | int newX = WrapForwardIndex(x, normalizedXOffset, Width); |
| | 180 | 252 | | for (int y = 0; y < Height; y++) |
| | | 253 | | { |
| | 72 | 254 | | int newY = WrapForwardIndex(y, normalizedYOffset, Height); |
| | 792 | 255 | | for (int z = 0; z < Depth; z++) |
| | | 256 | | { |
| | 324 | 257 | | int newZ = WrapForwardIndex(z, normalizedZOffset, Depth); |
| | 324 | 258 | | newArray[GetIndex(newX, newY, newZ)] = _innerArray[GetIndex(x, y, z)]; |
| | | 259 | | } |
| | | 260 | | } |
| | | 261 | | } |
| | 5 | 262 | | } |
| | | 263 | | |
| | | 264 | | private void ShiftClamped(T[] newArray, int xOffset, int yOffset, int zOffset) |
| | | 265 | | { |
| | 1 | 266 | | GetShiftRange(Width, xOffset, out int xStart, out int xEnd); |
| | 1 | 267 | | GetShiftRange(Height, yOffset, out int yStart, out int yEnd); |
| | 1 | 268 | | GetShiftRange(Depth, zOffset, out int zStart, out int zEnd); |
| | | 269 | | |
| | 6 | 270 | | for (int x = xStart; x < xEnd; x++) |
| | | 271 | | { |
| | 2 | 272 | | int newX = x + xOffset; |
| | 12 | 273 | | for (int y = yStart; y < yEnd; y++) |
| | | 274 | | { |
| | 4 | 275 | | int newY = y + yOffset; |
| | 16 | 276 | | for (int z = zStart; z < zEnd; z++) |
| | | 277 | | { |
| | 4 | 278 | | int newZ = z + zOffset; |
| | 4 | 279 | | newArray[GetIndex(newX, newY, newZ)] = _innerArray[GetIndex(x, y, z)]; |
| | | 280 | | } |
| | | 281 | | } |
| | | 282 | | } |
| | 1 | 283 | | } |
| | | 284 | | |
| | | 285 | | /// <summary> |
| | | 286 | | /// Clears all elements in the array. |
| | | 287 | | /// </summary> |
| | 1 | 288 | | public void Clear() => Array.Clear(_innerArray, 0, _innerArray.Length); |
| | | 289 | | |
| | | 290 | | /// <summary> |
| | | 291 | | /// Fills the entire array with the specified value. |
| | | 292 | | /// </summary> |
| | | 293 | | public void Fill(T value) |
| | | 294 | | { |
| | 224 | 295 | | for (int i = 0; i < _innerArray.Length; i++) |
| | 108 | 296 | | _innerArray[i] = value; |
| | 4 | 297 | | } |
| | | 298 | | |
| | | 299 | | /// <summary> |
| | | 300 | | /// Calculates the one-dimensional array index corresponding to the specified three-dimensional coordinates. |
| | | 301 | | /// </summary> |
| | | 302 | | /// <remarks> |
| | | 303 | | /// Use this method to map three-dimensional coordinates to a linear array index when working with flattened 3D data |
| | | 304 | | /// The valid ranges for x, y, and z depend on the dimensions of the underlying data structure. |
| | | 305 | | /// </remarks> |
| | | 306 | | /// <param name="x">The zero-based X coordinate to convert.</param> |
| | | 307 | | /// <param name="y">The zero-based Y coordinate to convert.</param> |
| | | 308 | | /// <param name="z">The zero-based Z coordinate to convert.</param> |
| | | 309 | | /// <returns>The zero-based index in the underlying one-dimensional array that corresponds to the specified (x, y, z |
| | | 310 | | public virtual int GetIndex(int x, int y, int z) |
| | | 311 | | { |
| | 2377899 | 312 | | return x * (Height * Depth) + y * Depth + z; |
| | | 313 | | } |
| | | 314 | | |
| | | 315 | | /// <summary> |
| | | 316 | | /// Validates the specified indices. |
| | | 317 | | /// Throws an exception if the indices are out of bounds. |
| | | 318 | | /// </summary> |
| | | 319 | | public virtual void ValidateIndex(int x, int y, int z) |
| | | 320 | | { |
| | 2377140 | 321 | | if (!IsValidIndex(x, y, z)) |
| | 4 | 322 | | throw new IndexOutOfRangeException($"Invalid index ({x}, {y}, {z}) for dimensions ({Width}, {Height}, {Depth |
| | 2377136 | 323 | | } |
| | | 324 | | |
| | | 325 | | private static int NormalizeShift(int shift, int length) |
| | | 326 | | { |
| | 15 | 327 | | if (length == 0) |
| | 0 | 328 | | return 0; |
| | | 329 | | |
| | 15 | 330 | | int normalized = shift % length; |
| | 15 | 331 | | return normalized < 0 ? normalized + length : normalized; |
| | | 332 | | } |
| | | 333 | | |
| | | 334 | | private static int WrapForwardIndex(int index, int normalizedShift, int length) |
| | | 335 | | { |
| | 414 | 336 | | int shifted = index + normalizedShift; |
| | 414 | 337 | | return shifted >= length ? shifted - length : shifted; |
| | | 338 | | } |
| | | 339 | | |
| | | 340 | | private static void GetShiftRange(int length, int shift, out int start, out int end) |
| | | 341 | | { |
| | 3 | 342 | | long startValue = shift < 0 ? -(long)shift : 0L; |
| | 3 | 343 | | long endValue = shift > 0 ? (long)length - shift : length; |
| | | 344 | | |
| | 3 | 345 | | start = (int)Math.Min(startValue, length); |
| | 3 | 346 | | end = (int)Math.Max(0L, Math.Min(endValue, length)); |
| | 3 | 347 | | if (end < start) |
| | 0 | 348 | | end = start; |
| | 3 | 349 | | } |
| | | 350 | | |
| | | 351 | | /// <summary> |
| | | 352 | | /// Checks if the specified indices are within bounds. |
| | | 353 | | /// </summary> |
| | | 354 | | public virtual bool IsValidIndex(int x, int y, int z) => |
| | 2377143 | 355 | | x >= 0 && x < Width && y >= 0 && y < Height && z >= 0 && z < Depth; |
| | | 356 | | |
| | | 357 | | #endregion |
| | | 358 | | |
| | | 359 | | #region IEnumerator Implementation |
| | | 360 | | |
| | | 361 | | /// <summary> |
| | | 362 | | /// Returns an enumerator that iterates through all elements in the 3D array. |
| | | 363 | | /// </summary> |
| | | 364 | | /// <returns>An enumerator for the 3D array.</returns> |
| | | 365 | | public IEnumerator<T> GetEnumerator() |
| | | 366 | | { |
| | 36 | 367 | | for (int x = 0; x < Width; x++) |
| | | 368 | | { |
| | 96 | 369 | | for (int y = 0; y < Height; y++) |
| | | 370 | | { |
| | 264 | 371 | | for (int z = 0; z < Depth; z++) |
| | 97 | 372 | | yield return this[x, y, z]; |
| | | 373 | | } |
| | | 374 | | } |
| | 5 | 375 | | } |
| | | 376 | | |
| | | 377 | | /// <summary> |
| | | 378 | | /// Returns an enumerator that iterates through all elements in the 3D array (non-generic). |
| | | 379 | | /// </summary> |
| | | 380 | | /// <returns>An enumerator for the 3D array.</returns> |
| | 0 | 381 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 382 | | |
| | | 383 | | |
| | | 384 | | #endregion |
| | | 385 | | } |