暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using Unity.Collections;
  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;
  5. using Unity.Collections.LowLevel.Unsafe;
  6. using System.Collections.Generic;
  7. using Unity.Burst;
  8. namespace UnityEngine.U2D.Common.UTess
  9. {
  10. /// <summary>
  11. /// Array. Used within UTess and constrained to
  12. /// 1. Auto-resizes upto the Max count with a smaller initial count.
  13. /// 2. Only be used within the created thread. Read 1.
  14. /// 3. Read/Write access are all fast-paths.
  15. /// 4. Mostly used with Temp Alloc within UTess ontext.
  16. /// </summary>
  17. /// <typeparam name="T"></typeparam>
  18. [StructLayout(LayoutKind.Sequential)]
  19. [DebuggerDisplay("Length = {Length}")]
  20. [DebuggerTypeProxy(typeof(ArrayDebugView<>))]
  21. internal unsafe struct Array<T> : IDisposable where T : struct
  22. {
  23. internal NativeArray<T> m_Array;
  24. internal int m_MaxSize;
  25. internal Allocator m_AllocLabel;
  26. internal NativeArrayOptions m_Options;
  27. public Array(int length, int maxSize, Allocator allocMode, NativeArrayOptions options)
  28. {
  29. m_Array = new NativeArray<T>(length, allocMode, options);
  30. m_AllocLabel = allocMode;
  31. m_Options = options;
  32. m_MaxSize = maxSize;
  33. }
  34. private void ResizeIfRequired(int index)
  35. {
  36. if (index >= m_MaxSize || index < 0)
  37. throw new IndexOutOfRangeException(
  38. $"Trying to access beyond allowed size. {index} is out of range of '{m_MaxSize}' MaxSize.");
  39. if (index < m_Array.Length)
  40. return;
  41. int requiredSize = Length;
  42. while (requiredSize <= index)
  43. requiredSize = requiredSize * 2;
  44. requiredSize = requiredSize > m_MaxSize ? m_MaxSize : requiredSize;
  45. var copyArray = new NativeArray<T>(requiredSize, m_AllocLabel, m_Options);
  46. NativeArray<T>.Copy(m_Array, copyArray, Length);
  47. m_Array.Dispose();
  48. m_Array = copyArray;
  49. }
  50. public unsafe T this[int index]
  51. {
  52. get
  53. {
  54. return m_Array[index];
  55. }
  56. set
  57. {
  58. ResizeIfRequired(index);
  59. m_Array[index] = value;
  60. }
  61. }
  62. public bool IsCreated => m_Array.IsCreated;
  63. public int Length => (m_MaxSize != 0) ? m_Array.Length : 0;
  64. public int MaxSize => m_MaxSize;
  65. public void Dispose()
  66. {
  67. m_Array.Dispose();
  68. m_MaxSize = 0;
  69. }
  70. public void* UnsafePtr
  71. {
  72. get
  73. {
  74. return m_Array.GetUnsafePtr();
  75. }
  76. }
  77. public void* UnsafeReadOnlyPtr
  78. {
  79. get
  80. {
  81. return m_Array.GetUnsafeReadOnlyPtr();
  82. }
  83. }
  84. // Should only ever be used for Debugging.
  85. public void CopyTo(T[] array)
  86. {
  87. m_Array.CopyTo(array);
  88. }
  89. }
  90. /// <summary>
  91. /// DebuggerTypeProxy for <see cref="Array{T}"/>
  92. /// </summary>
  93. internal sealed class ArrayDebugView<T> where T : struct
  94. {
  95. private Array<T> array;
  96. public ArrayDebugView(Array<T> array)
  97. {
  98. this.array = array;
  99. }
  100. public T[] Items
  101. {
  102. get
  103. {
  104. var ret = new T[array.Length];
  105. array.CopyTo(ret);
  106. return ret;
  107. }
  108. }
  109. }
  110. }