Guest User

Untitled

a guest
Jan 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Unity.Collections.LowLevel.Unsafe;
  4. using System.Collections;
  5.  
  6. namespace Unity.Collections
  7. {
  8. public struct NativeArray2D<T> : IDisposable, IEnumerable<T> where T : unmanaged
  9. {
  10. public NativeArray<T> Internal;
  11. private readonly int _yLength;
  12. private readonly int _xLength;
  13.  
  14. public NativeArray2D(int x, int y, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory)
  15. {
  16. Internal = new NativeArray<T>(x * y, allocator);
  17. _yLength = y;
  18. _xLength = x;
  19. }
  20.  
  21. public T this[int i] => Internal[i];
  22.  
  23. public T this[int x, int y]
  24. {
  25. get => Internal[CalculateIndex(x, y)];
  26. set => Internal[CalculateIndex(x, y)] = value;
  27. }
  28.  
  29. public int CalculateIndex(int x, int y)
  30. {
  31. return (x * _yLength) + y;
  32. }
  33.  
  34. public unsafe ref T ByRef(int x, int y)
  35. {
  36. var ptr = Internal.GetUnsafePtr();
  37. var idx = CalculateIndex(x,y);
  38. return ref ((T*)ptr)[idx];
  39. }
  40.  
  41. public int GetLength(int index)
  42. {
  43. switch (index)
  44. {
  45. case 0: return _xLength;
  46. case 1: return _yLength;
  47. }
  48. throw new ArgumentOutOfRangeException($"The dimension with Length index '{index}' doesn't exist");
  49. }
  50.  
  51. public int Length => Internal.Length;
  52. public void Dispose() => Internal.Dispose();
  53. public IEnumerator<T> GetEnumerator() => Internal.GetEnumerator();
  54. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  55. }
  56. }
Add Comment
Please, Sign In to add comment