xNativeList.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Runtime.InteropServices;
  5. using System;
  6. using Unity.Collections;
  7. using Unity.Collections.LowLevel.Unsafe;
  8. namespace Ximmerse.XR.Collections
  9. {
  10. struct InternalPointer
  11. {
  12. public IntPtr m_Pointer;
  13. }
  14. /// <summary>
  15. /// Native list which compares to Unity.Collections.NativeList, this type is able to be used as generic type to Unity's native array or native list collection types.
  16. ///
  17. /// Make sure Dispose() is called properly.
  18. ///
  19. /// Note: foreach() has GC problem due to type casting, using for() in most case.
  20. ///
  21. /// </summary>
  22. /// <typeparam name="T"></typeparam>
  23. internal unsafe struct xNativeList<T> : IDisposable, IEnumerable<T>, IList<T>, IReadOnlyList<T> where T : unmanaged
  24. {
  25. const int kDefaultIncrementElement = 8;
  26. private const string kErrorMsgFormat01 = "Invalid index: {0} of accessing array length = {1}";
  27. private const string kErrorMsg02 = "PENativeList is not created !";
  28. /// <summary>
  29. /// Length of int, should be 4 in x64 platform.
  30. /// </summary>
  31. static readonly int kLengthInt = Marshal.SizeOf<int>();
  32. /// <summary>
  33. /// Double kLengthInt.
  34. /// </summary>
  35. static readonly int kLengthTwoInt = 2 * Marshal.SizeOf<int>();
  36. /// <summary>
  37. /// Capacity of the list
  38. /// </summary>
  39. public int Capacity
  40. {
  41. get
  42. {
  43. CheckSafety();
  44. return Marshal.ReadInt32(m_InternalPointer->m_Pointer);
  45. }
  46. set
  47. {
  48. CheckSafety();
  49. Marshal.WriteInt32(m_InternalPointer->m_Pointer, value);
  50. }
  51. }
  52. /// <summary>
  53. /// Element length of the list
  54. /// </summary>
  55. public int Length
  56. {
  57. get
  58. {
  59. CheckSafety();
  60. return Marshal.ReadInt32(m_InternalPointer->m_Pointer, kLengthInt);
  61. }
  62. private set
  63. {
  64. CheckSafety();
  65. Marshal.WriteInt32(m_InternalPointer->m_Pointer, kLengthInt, value);
  66. }
  67. }
  68. int m_ElementSize;
  69. /// <summary>
  70. /// Element size in memory bytes.
  71. /// </summary>
  72. public int ElementSize
  73. {
  74. get
  75. {
  76. if (m_ElementSize == 0)
  77. {
  78. m_ElementSize = Marshal.SizeOf<T>();
  79. }
  80. return m_ElementSize;
  81. }
  82. }
  83. InternalPointer* m_InternalPointer;
  84. /// <summary>
  85. /// Gets the pointer of the list memory.
  86. /// First int(32bits, or 4 bytes) = capacity.
  87. /// Second int(32bits, or 4 bytes) = length.
  88. /// </summary>
  89. public IntPtr Pointer
  90. {
  91. get
  92. {
  93. CheckSafety();
  94. return m_InternalPointer->m_Pointer;
  95. }
  96. }
  97. /// <summary>
  98. /// Gets the data pointer of the list. The capacity / length is skipped .
  99. /// The pointer should be used for reading or modifying data only.
  100. /// DO NOT INSERT/DELETE DATA BY THIS POINTER !
  101. /// </summary>
  102. public IntPtr ReadOnlyDataPointer
  103. {
  104. get
  105. {
  106. CheckSafety();
  107. return IntPtr.Add(m_InternalPointer->m_Pointer, kLengthTwoInt);
  108. }
  109. }
  110. bool m_IsCreated;
  111. /// <summary>
  112. /// If the native list has been allocated.
  113. /// </summary>
  114. public bool IsCreated
  115. {
  116. get => m_IsCreated;
  117. }
  118. /// <summary>
  119. /// Element count of the list
  120. /// </summary>
  121. public int Count => Length;
  122. /// <summary>
  123. /// Is the list readonly ?
  124. /// </summary>
  125. public bool IsReadOnly => false;
  126. /// <summary>
  127. /// Create a new PENativeList with initial capacity.
  128. /// </summary>
  129. /// <param name="Capacity"></param>
  130. /// <returns></returns>
  131. public static xNativeList<T> Create(int Capacity)
  132. {
  133. xNativeList<T> list = default;
  134. list.m_ElementSize = Marshal.SizeOf<T>();
  135. int sizeOfInterPtr = Marshal.SizeOf<InternalPointer>();
  136. list.m_InternalPointer = (InternalPointer*)Marshal.AllocHGlobal(sizeOfInterPtr).ToPointer();
  137. list.m_InternalPointer->m_Pointer = Marshal.AllocHGlobal(list.m_ElementSize * Capacity + kLengthTwoInt); //[capacity] | [length] | [content]
  138. Marshal.WriteInt32(list.m_InternalPointer->m_Pointer, Capacity);
  139. Marshal.WriteInt32(list.m_InternalPointer->m_Pointer, kLengthInt, 0);
  140. list.m_IsCreated = true;
  141. return list;
  142. }
  143. /// <summary>
  144. /// Create a new PENativeList by given array
  145. /// </summary>
  146. /// <param name="nativeArray"></param>
  147. /// <returns></returns>
  148. public static xNativeList<T> Create(NativeArray<T> nativeArray)
  149. {
  150. xNativeList<T> list = default;
  151. list.m_ElementSize = Marshal.SizeOf<T>();
  152. int lengthInByte = list.m_ElementSize * nativeArray.Length;
  153. list.m_InternalPointer = (InternalPointer*)Marshal.AllocHGlobal(Marshal.SizeOf<InternalPointer>()).ToPointer();
  154. list.m_InternalPointer->m_Pointer = Marshal.AllocHGlobal(lengthInByte + kLengthTwoInt); //[capacity] | [length] | [content]
  155. Buffer.MemoryCopy(nativeArray.GetUnsafePtr(), IntPtr.Add(list.m_InternalPointer->m_Pointer, kLengthTwoInt).ToPointer(), lengthInByte, lengthInByte);
  156. Marshal.WriteInt32(list.m_InternalPointer->m_Pointer, nativeArray.Length);//capacity
  157. Marshal.WriteInt32(IntPtr.Add(list.m_InternalPointer->m_Pointer, kLengthInt), nativeArray.Length);//length
  158. list.m_IsCreated = true;
  159. return list;
  160. }
  161. /// <summary>
  162. /// Create a new PENativeList by given array
  163. /// </summary>
  164. /// <param name="array"></param>
  165. /// <returns></returns>
  166. public static xNativeList<T> Create(T[] array)
  167. {
  168. xNativeList<T> list = default;
  169. list.m_ElementSize = Marshal.SizeOf<T>();
  170. int lengthInByte = list.m_ElementSize * array.Length;
  171. list.m_InternalPointer = (InternalPointer*)Marshal.AllocHGlobal(Marshal.SizeOf<InternalPointer>()).ToPointer();
  172. list.m_InternalPointer->m_Pointer = Marshal.AllocHGlobal(lengthInByte + kLengthTwoInt); //[capacity] | [length] | [content]
  173. IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement<T>(array, 0);
  174. Buffer.MemoryCopy(ptr.ToPointer(), IntPtr.Add(list.m_InternalPointer->m_Pointer, kLengthTwoInt).ToPointer(), lengthInByte, lengthInByte);
  175. Marshal.WriteInt32(list.m_InternalPointer->m_Pointer, array.Length);//capacity
  176. Marshal.WriteInt32(IntPtr.Add(list.m_InternalPointer->m_Pointer, kLengthInt), array.Length);//length
  177. list.m_IsCreated = true;
  178. return list;
  179. }
  180. /// <summary>
  181. /// Allocates by another native list.
  182. /// </summary>
  183. /// <param name="source"></param>
  184. /// <returns></returns>
  185. public static xNativeList<T> Create(xNativeList<T> source)
  186. {
  187. xNativeList<T> list = default;
  188. list.m_ElementSize = Marshal.SizeOf<T>();
  189. int lengthInByte = list.m_ElementSize * source.Length;
  190. list.m_InternalPointer = (InternalPointer*)Marshal.AllocHGlobal(Marshal.SizeOf<InternalPointer>()).ToPointer();
  191. list.m_InternalPointer->m_Pointer = Marshal.AllocHGlobal(lengthInByte + kLengthTwoInt); //[capacity] | [length] | [content]
  192. //从source pointer执行 memory copy:
  193. IntPtr ptr_source = source.m_InternalPointer->m_Pointer;
  194. Buffer.MemoryCopy(ptr_source.ToPointer(), (list.m_InternalPointer->m_Pointer).ToPointer(), lengthInByte + kLengthTwoInt, lengthInByte + kLengthTwoInt);
  195. list.m_IsCreated = true;
  196. return list;
  197. }
  198. /// <summary>
  199. /// Gets/sets array element.
  200. /// </summary>
  201. /// <param name="index"></param>
  202. /// <returns></returns>
  203. public T this[int index]
  204. {
  205. get
  206. {
  207. CheckSafety();
  208. int length = this.Length;
  209. if (index < 0 || index >= length)
  210. {
  211. throw new ArgumentOutOfRangeException(string.Format(kErrorMsgFormat01, index, length));
  212. }
  213. T val = Marshal.PtrToStructure<T>(IntPtr.Add(m_InternalPointer->m_Pointer, kLengthTwoInt + index * ElementSize));
  214. return val;
  215. }
  216. set
  217. {
  218. CheckSafety();
  219. int length = this.Length;
  220. if (index < 0 || index >= length)
  221. {
  222. throw new ArgumentOutOfRangeException(string.Format(kErrorMsgFormat01, index, length));
  223. }
  224. T* p = &value;
  225. Buffer.MemoryCopy(p, IntPtr.Add(m_InternalPointer->m_Pointer, kLengthTwoInt + index * ElementSize).ToPointer(), ElementSize, ElementSize);
  226. }
  227. }
  228. /// <summary>
  229. /// Appends a value to the list.
  230. /// </summary>
  231. /// <param name="value"></param>
  232. public void Add(T value)
  233. {
  234. CheckSafety();
  235. int length = this.Length;
  236. int capacity = this.Capacity;
  237. if (length >= capacity)
  238. {
  239. Reallocate(capacity + kDefaultIncrementElement);
  240. }
  241. T* p = &value;
  242. Buffer.MemoryCopy(p, IntPtr.Add(m_InternalPointer->m_Pointer, kLengthTwoInt + length * ElementSize).ToPointer(), ElementSize, ElementSize);
  243. Length = length + 1;
  244. }
  245. /// <summary>
  246. /// Add multiple elements.
  247. /// </summary>
  248. /// <param name="values"></param>
  249. public void AddRange(T[] values)
  250. {
  251. CheckSafety();
  252. int length = this.Length;
  253. int capacity = this.Capacity;
  254. if (length + values.Length >= capacity)
  255. {
  256. Reallocate(capacity + values.Length);
  257. }
  258. fixed (void* ptr = values)
  259. {
  260. Buffer.MemoryCopy(ptr, IntPtr.Add(m_InternalPointer->m_Pointer, kLengthTwoInt + length * ElementSize).ToPointer(), ElementSize * values.Length, ElementSize * values.Length);
  261. Length = length + values.Length;
  262. }
  263. }
  264. /// <summary>
  265. /// Add multiple elements.
  266. /// </summary>
  267. /// <param name="array"></param>
  268. public void AddRange(NativeArray<T> array)
  269. {
  270. void* src = array.GetUnsafePtr();
  271. if (this.Length + array.Length > this.Capacity)
  272. {
  273. Reallocate(this.Length + array.Length);
  274. }
  275. int currentLength = this.Length;
  276. void* dst = IntPtr.Add(this.m_InternalPointer->m_Pointer, kLengthTwoInt + currentLength * ElementSize).ToPointer();
  277. Buffer.MemoryCopy(src, dst, array.Length * ElementSize, array.Length * ElementSize);
  278. this.Length = array.Length + currentLength;
  279. }
  280. /// <summary>
  281. /// Remove element at index.
  282. /// Performance tips : remove from the last index has best performance than remove from the head.
  283. /// </summary>
  284. /// <param name="index"></param>
  285. public void RemoveAt(int index)
  286. {
  287. CheckSafety();
  288. int length = this.Length;
  289. if (index < 0 || index >= length)
  290. {
  291. throw new ArgumentOutOfRangeException(string.Format(kErrorMsgFormat01, index, length));
  292. }
  293. if (index == length - 1)
  294. {
  295. Length = length - 1;
  296. }
  297. else
  298. {
  299. //preservedLength 是要保留的内容长度
  300. int preservedLength = length - index;
  301. IntPtr slice = GetSlice(index + 1, preservedLength);
  302. Buffer.MemoryCopy(
  303. slice.ToPointer(),
  304. IntPtr.Add(m_InternalPointer->m_Pointer, kLengthTwoInt + index * ElementSize).ToPointer(),
  305. ElementSize * preservedLength,
  306. ElementSize * preservedLength);
  307. Marshal.FreeHGlobal(slice);
  308. Length = length - 1;
  309. }
  310. }
  311. /// <summary>
  312. /// Removes element
  313. /// </summary>
  314. /// <param name="value"></param>
  315. public bool Remove(T value)
  316. {
  317. CheckSafety();
  318. int length = this.Length;
  319. for (int i = length - 1; i >= 0; i--)
  320. {
  321. if (this[i].Equals(value))
  322. {
  323. RemoveAt(i);
  324. return true;
  325. }
  326. }
  327. return false;
  328. }
  329. /// <summary>
  330. /// Convert the native list to native array.
  331. /// </summary>
  332. /// <returns></returns>
  333. public NativeArray<T> ToArray(Allocator allocator)
  334. {
  335. NativeArray<T> array = new NativeArray<T>(this.Length, allocator);
  336. xNativeList<T> list = this;
  337. for (int i = 0, iMax = list.Count; i < iMax; i++)
  338. {
  339. array[i] = list[i];
  340. }
  341. return array;
  342. }
  343. /// <summary>
  344. /// Clears all element
  345. /// </summary>
  346. public void Clear()
  347. {
  348. CheckSafety();
  349. Length = 0;
  350. }
  351. /// <summary>
  352. /// Reallocate a longer memory, the exists content is copied to new allocated memory area.List length remains unchanged.
  353. /// Content length remains unchanged , while the list's capacity is extended.
  354. /// </summary>
  355. /// <param name="length">The element length.</param>
  356. internal void Reallocate(int length)
  357. {
  358. IntPtr newBufferPtr = Marshal.AllocHGlobal(kLengthTwoInt + length * ElementSize);
  359. Buffer.MemoryCopy(m_InternalPointer->m_Pointer.ToPointer(), newBufferPtr.ToPointer(), kLengthTwoInt + ElementSize * Length, kLengthTwoInt + ElementSize * Length);
  360. Marshal.FreeHGlobal(m_InternalPointer->m_Pointer);
  361. m_InternalPointer->m_Pointer = newBufferPtr;
  362. Capacity = length;
  363. }
  364. /// <summary>
  365. /// Gets a slice inside the list.
  366. /// </summary>
  367. /// <param name="index"></param>
  368. /// <param name="length"></param>
  369. /// <returns></returns>
  370. IntPtr GetSlice(int index, int length)
  371. {
  372. IntPtr slicePtr = Marshal.AllocHGlobal(length * ElementSize);
  373. IntPtr src = IntPtr.Add(m_InternalPointer->m_Pointer, kLengthTwoInt + index * ElementSize);
  374. Buffer.MemoryCopy(src.ToPointer(), slicePtr.ToPointer(), ElementSize * length, ElementSize * length);
  375. return slicePtr;
  376. }
  377. /// <summary>
  378. /// Gets the element's data pointer at the index
  379. /// </summary>
  380. /// <param name="index"></param>
  381. /// <param name="elementPtr"></param>
  382. /// <returns></returns>
  383. public unsafe bool GetElementPtr(int index, out T* elementPtr)
  384. {
  385. elementPtr = default;
  386. if (this.Length <= index)
  387. {
  388. return false;
  389. }
  390. var ptr = IntPtr.Add(m_InternalPointer->m_Pointer, kLengthTwoInt + m_ElementSize * index);
  391. elementPtr = (T*)ptr.ToPointer();
  392. return true;
  393. }
  394. /// <summary>
  395. /// Dispose the native list and deallocate the memory.
  396. /// </summary>
  397. public void Dispose()
  398. {
  399. if (IsCreated)
  400. {
  401. Marshal.FreeHGlobal(this.m_InternalPointer->m_Pointer);
  402. Marshal.FreeHGlobal(new IntPtr(m_InternalPointer));
  403. m_IsCreated = false;
  404. }
  405. }
  406. /// <summary>
  407. /// Gets enumerator.
  408. /// </summary>
  409. /// <returns></returns>
  410. public IEnumerator<T> GetEnumerator()
  411. {
  412. CheckSafety();
  413. return new NativeListEnumerator<T>(this);
  414. }
  415. IEnumerator IEnumerable.GetEnumerator()
  416. {
  417. CheckSafety();
  418. return new NativeListEnumerator<T>(this);
  419. }
  420. /// <summary>
  421. /// Find index of the element
  422. /// </summary>
  423. /// <param name="item"></param>
  424. /// <returns></returns>
  425. public int IndexOf(T item)
  426. {
  427. CheckSafety();
  428. int length = this.Length;
  429. for (int i = 0, imax = length; i < imax; i++)
  430. {
  431. if (this[i].Equals(item))
  432. {
  433. return i;
  434. }
  435. }
  436. return -1;
  437. }
  438. /// <summary>
  439. /// Inserts item at the index.
  440. /// </summary>
  441. /// <param name="index"></param>
  442. /// <param name="item"></param>
  443. public void Insert(int index, T item)
  444. {
  445. CheckSafety();
  446. int length = this.Length;
  447. int capacity = this.Capacity;
  448. if (length >= capacity)
  449. {
  450. Reallocate(capacity + kDefaultIncrementElement);
  451. }
  452. //先将 index 以后的元素后移一位:
  453. int preservedLength = length - index;
  454. IntPtr slice = GetSlice(index, preservedLength);
  455. Buffer.MemoryCopy(
  456. slice.ToPointer(),
  457. IntPtr.Add(m_InternalPointer->m_Pointer, kLengthTwoInt + (index + 1) * ElementSize).ToPointer(),
  458. ElementSize * preservedLength,
  459. ElementSize * preservedLength);
  460. Marshal.FreeHGlobal(slice);
  461. //然后将新的内容copy到 index 对应的位置
  462. T* p = &item;
  463. Buffer.MemoryCopy(p, IntPtr.Add(m_InternalPointer->m_Pointer, kLengthTwoInt + index * ElementSize).ToPointer(), ElementSize, ElementSize);
  464. Length = length + 1;
  465. }
  466. /// <summary>
  467. /// Inserts items at the index.
  468. /// </summary>
  469. /// <param name="index"></param>
  470. /// <param name="items"></param>
  471. public void InsertValues(int index, T[] items)
  472. {
  473. CheckSafety();
  474. int length = this.Length;
  475. int capacity = this.Capacity;
  476. if (length + items.Length >= capacity)
  477. {
  478. Reallocate(capacity +
  479. items.Length + kDefaultIncrementElement);
  480. }
  481. //先将 index 以后的元素后移N位:
  482. int preservedLength = length - index;
  483. IntPtr slice = GetSlice(index, preservedLength);
  484. Buffer.MemoryCopy(
  485. slice.ToPointer(),
  486. IntPtr.Add(m_InternalPointer->m_Pointer, kLengthTwoInt + (index + items.Length) * ElementSize).ToPointer(),
  487. ElementSize * preservedLength,
  488. ElementSize * preservedLength);
  489. Marshal.FreeHGlobal(slice);
  490. //然后将新的内容copy到 index 对应的位置
  491. IntPtr srcPtr = Marshal.UnsafeAddrOfPinnedArrayElement<T>(items, 0);
  492. Buffer.MemoryCopy(srcPtr.ToPointer(), IntPtr.Add(m_InternalPointer->m_Pointer, kLengthTwoInt + index * ElementSize).ToPointer(), ElementSize * items.Length, ElementSize * items.Length);
  493. Length = length + items.Length;
  494. }
  495. /// <summary>
  496. /// Inserts items at the index.
  497. /// </summary>
  498. /// <param name="index"></param>
  499. /// <param name="item"></param>
  500. public void InsertValues(int index, NativeArray<T> items)
  501. {
  502. CheckSafety();
  503. int length = this.Length;
  504. int capacity = this.Capacity;
  505. if (length + items.Length >= capacity)
  506. {
  507. Reallocate(capacity +
  508. items.Length + kDefaultIncrementElement);
  509. }
  510. //先将 index 以后的元素后移N位:
  511. int preservedLength = length - index;
  512. IntPtr slice = GetSlice(index, preservedLength);
  513. Buffer.MemoryCopy(
  514. slice.ToPointer(),
  515. IntPtr.Add(m_InternalPointer->m_Pointer, kLengthTwoInt + (index + items.Length) * ElementSize).ToPointer(),
  516. ElementSize * preservedLength,
  517. ElementSize * preservedLength);
  518. Marshal.FreeHGlobal(slice);
  519. //然后将新的内容copy到 index 对应的位置
  520. void* srcPtr = items.GetUnsafePtr();
  521. Buffer.MemoryCopy(srcPtr, IntPtr.Add(m_InternalPointer->m_Pointer, kLengthTwoInt + index * ElementSize).ToPointer(), ElementSize * items.Length, ElementSize * items.Length);
  522. Length = length + items.Length;
  523. }
  524. /// <summary>
  525. /// If the item presents in the list
  526. /// </summary>
  527. /// <param name="item"></param>
  528. /// <returns></returns>
  529. public bool Contains(T item)
  530. {
  531. CheckSafety();
  532. int length = this.Length;
  533. for (int i = 0, imax = length; i < imax; i++)
  534. {
  535. if (this[i].Equals(item))
  536. {
  537. return true;
  538. }
  539. }
  540. return false;
  541. }
  542. /// <summary>
  543. /// Copy content to array
  544. /// </summary>
  545. /// <param name="array"></param>
  546. /// <param name="arrayIndex"></param>
  547. public void CopyTo(T[] array, int arrayIndex)
  548. {
  549. CheckSafety();
  550. int length = this.Length;
  551. if ((arrayIndex + length) > array.Length)
  552. {
  553. throw new ArgumentOutOfRangeException(string.Format("ArrayIndex + m_Length: {0} GE array length :{1}", arrayIndex + length, array.Length));
  554. }
  555. if (length == 0)
  556. {
  557. return;//nothing to copy ...
  558. }
  559. fixed (void* dst = array)
  560. {
  561. IntPtr dstPtr = arrayIndex > 0 ? IntPtr.Add(new IntPtr(dst), ElementSize * arrayIndex) : new IntPtr(dst);
  562. Buffer.MemoryCopy(IntPtr.Add(m_InternalPointer->m_Pointer, kLengthTwoInt).ToPointer(), dstPtr.ToPointer(), ElementSize * length, ElementSize * length);
  563. }
  564. }
  565. /// <summary>
  566. /// Copy content to array
  567. /// </summary>
  568. /// <param name="nativeArray"></param>
  569. /// <param name="arrayIndex"></param>
  570. public void CopyTo(NativeArray<T> nativeArray, int arrayIndex)
  571. {
  572. CheckSafety();
  573. int length = this.Length;
  574. if ((arrayIndex + length) > nativeArray.Length)
  575. {
  576. throw new ArgumentOutOfRangeException(string.Format("ArrayIndex + m_Length: {0} GE array length :{1}", arrayIndex + length, nativeArray.Length));
  577. }
  578. if (length == 0)
  579. {
  580. return;//nothing to copy ...
  581. }
  582. void* ptr = nativeArray.GetUnsafePtr();
  583. Buffer.MemoryCopy(IntPtr.Add(m_InternalPointer->m_Pointer, kLengthTwoInt).ToPointer(), ptr, ElementSize * length, ElementSize * length);
  584. }
  585. private void CheckSafety()
  586. {
  587. if (!m_IsCreated)
  588. {
  589. throw new InvalidOperationException(kErrorMsg02);
  590. }
  591. }
  592. /// <summary>
  593. /// Enumerator for PENativeList
  594. /// </summary>
  595. internal struct NativeListEnumerator<T> : IEnumerator<T> where T : unmanaged
  596. {
  597. xNativeList<T> m_list;
  598. int m_index;
  599. public NativeListEnumerator(xNativeList<T> lst)
  600. {
  601. m_list = lst;
  602. m_index = -1;
  603. }
  604. public T Current => m_list[m_index];
  605. object IEnumerator.Current => m_list[m_index];
  606. public void Dispose()
  607. {
  608. }
  609. public bool MoveNext()
  610. {
  611. m_index++;
  612. int max = m_list.Length;
  613. if (m_index < max)
  614. {
  615. return true;
  616. }
  617. else return false;
  618. }
  619. public void Reset()
  620. {
  621. m_index = -1;
  622. }
  623. }
  624. }
  625. }