暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FixedList.tt 51KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. <#/*THIS IS A T4 FILE - see t4_text_templating.md for what it is and how to run codegen*/#>
  2. <#@ template debug="True" #>
  3. <#@ output extension=".gen.cs" encoding="utf-8" #>
  4. <#@ assembly name="System.Core" #>
  5. <#@ import namespace="System.Globalization" #>
  6. <#@ import namespace="System.Security.Cryptography" #>
  7. //------------------------------------------------------------------------------
  8. // <auto-generated>
  9. // This code was generated by a tool.
  10. //
  11. // TextTransform Samples/Packages/com.unity.collections/Unity.Collections/FixedList.tt
  12. //
  13. // Changes to this file may cause incorrect behavior and will be lost if
  14. // the code is regenerated.
  15. // </auto-generated>
  16. //------------------------------------------------------------------------------
  17. using System.Collections.Generic;
  18. using System.Collections;
  19. using System.Diagnostics;
  20. using System.Runtime.CompilerServices;
  21. using System.Runtime.InteropServices;
  22. using System;
  23. using Unity.Collections.LowLevel.Unsafe;
  24. using Unity.Mathematics;
  25. using UnityEngine.Internal;
  26. using UnityEngine;
  27. #if UNITY_PROPERTIES_EXISTS
  28. using Unity.Properties;
  29. #endif
  30. namespace Unity.Collections
  31. {
  32. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(FixedBytes30) })]
  33. [Serializable]
  34. internal struct FixedList<T,U>
  35. : INativeList<T>
  36. where T : unmanaged
  37. where U : unmanaged
  38. {
  39. [SerializeField] internal ushort length;
  40. [SerializeField] internal U buffer;
  41. /// <summary>
  42. /// The current number of items in this list.
  43. /// </summary>
  44. /// <value>The current number of items in this list.</value>
  45. [CreateProperty]
  46. public int Length
  47. {
  48. get => length;
  49. set
  50. {
  51. FixedList.CheckResize<U,T>(value);
  52. length = (ushort)value;
  53. }
  54. }
  55. /// <summary>
  56. /// A property in order to display items in the Entity Inspector.
  57. /// </summary>
  58. [CreateProperty] IEnumerable<T> Elements => this.ToArray();
  59. /// <summary>
  60. /// Whether the list is empty.
  61. /// </summary>
  62. /// <value>True if this string has no characters or if the container has not been constructed.</value>
  63. public bool IsEmpty => Length == 0;
  64. internal int LengthInBytes => Length * UnsafeUtility.SizeOf<T>();
  65. unsafe internal byte* Buffer
  66. {
  67. get
  68. {
  69. fixed(U* u = &buffer)
  70. return (byte*)u + FixedList.PaddingBytes<T>();
  71. }
  72. }
  73. /// <summary>
  74. /// The number of elements that can fit in this list.
  75. /// </summary>
  76. /// <value>The number of elements that can fit in this list.</value>
  77. /// <remarks>The capacity of a FixedList cannot be changed. The setter is included only for conformity with <see cref="INativeList{T}"/>.</remarks>
  78. /// <exception cref="ArgumentOutOfRangeException">Thrown if the new value does not match the current capacity.</exception>
  79. public int Capacity
  80. {
  81. get
  82. {
  83. return FixedList.Capacity<U,T>();
  84. }
  85. set
  86. {
  87. CollectionHelper.CheckCapacityInRange(value, Length);
  88. }
  89. }
  90. /// <summary>
  91. /// The element at a given index.
  92. /// </summary>
  93. /// <param name="index">An index.</param>
  94. /// <value>The value to store at the index.</value>
  95. /// <exception cref="IndexOutOfRangeException">Thrown if the index is out of bounds.</exception>
  96. public T this[int index]
  97. {
  98. get
  99. {
  100. CollectionHelper.CheckIndexInRange(index, length);
  101. unsafe
  102. {
  103. return UnsafeUtility.ReadArrayElement<T>(Buffer, CollectionHelper.AssumePositive(index));
  104. }
  105. }
  106. set
  107. {
  108. CollectionHelper.CheckIndexInRange(index, length);
  109. unsafe
  110. {
  111. UnsafeUtility.WriteArrayElement<T>(Buffer, CollectionHelper.AssumePositive(index), value);
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// Returns the element at a given index.
  117. /// </summary>
  118. /// <param name="index">An index.</param>
  119. /// <returns>A reference to the element at the index.</returns>
  120. public ref T ElementAt(int index)
  121. {
  122. CollectionHelper.CheckIndexInRange(index, length);
  123. unsafe
  124. {
  125. return ref UnsafeUtility.ArrayElementAsRef<T>(Buffer, index);
  126. }
  127. }
  128. /// <summary>
  129. /// Returns the hash code of this list.
  130. /// </summary>
  131. /// <remarks>
  132. /// Only the content of the list (the bytes of the elements) are included in the hash. Any bytes beyond the length are not part of the hash.</remarks>
  133. /// <returns>The hash code of this list.</returns>
  134. public override int GetHashCode()
  135. {
  136. unsafe
  137. {
  138. return (int)CollectionHelper.Hash(Buffer, LengthInBytes);
  139. }
  140. }
  141. /// <summary>
  142. /// Appends an element to the end of this list. Increments the length by 1.
  143. /// </summary>
  144. /// <remarks>The same as <see cref="AddNoResize"/> (because a fixed list is never resized).</remarks>
  145. /// <param name="item">The element to append at the end of the list.</param>
  146. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  147. public void Add(in T item)
  148. {
  149. this[Length++] = item;
  150. }
  151. /// <summary>
  152. /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements.
  153. /// </summary>
  154. /// <remarks>The same as <see cref="AddRangeNoResize"/>. Remember that a fixed list is never resized.</remarks>
  155. /// <param name="ptr">A buffer.</param>
  156. /// <param name="length">The number of elements from the buffer to append.</param>
  157. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  158. public unsafe void AddRange(void* ptr, int length)
  159. {
  160. T* data = (T*)ptr;
  161. for (var i = 0; i < length; ++i)
  162. {
  163. this[Length++] = data[i];
  164. }
  165. }
  166. /// <summary>
  167. /// Appends an element to the end of this list. Increments the length by 1.
  168. /// </summary>
  169. /// <remarks>The same as <see cref="Add"/>. Included only for consistency with the other list types.</remarks>
  170. /// <param name="item">The element to append at the end of the list.</param>
  171. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  172. public void AddNoResize(in T item) => Add(item);
  173. /// <summary>
  174. /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements.
  175. /// </summary>
  176. /// <remarks>The same as <see cref="AddRange"/>. Included only for consistency with the other list types.</remarks>
  177. /// <param name="ptr">A buffer.</param>
  178. /// <param name="length">The number of elements from the buffer to append.</param>
  179. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  180. public unsafe void AddRangeNoResize(void* ptr, int length) => AddRange(ptr, length);
  181. /// <summary>
  182. /// Sets the length to 0.
  183. /// </summary>
  184. /// <remarks> Does *not* zero out the bytes.</remarks>
  185. public void Clear()
  186. {
  187. Length = 0;
  188. }
  189. /// <summary>
  190. /// Shifts elements toward the end of this list, increasing its length.
  191. /// </summary>
  192. /// <remarks>
  193. /// Right-shifts elements in the list so as to create 'free' slots at the beginning or in the middle.
  194. ///
  195. /// The length is increased by `end - begin`.
  196. ///
  197. /// If `end` equals `begin`, the method does nothing.
  198. ///
  199. /// The element at index `begin` will be copied to index `end`, the element at index `begin + 1` will be copied to `end + 1`, and so forth.
  200. ///
  201. /// The indexes `begin` up to `end` are not cleared: they will contain whatever values they held prior.
  202. /// </remarks>
  203. /// <param name="begin">The index of the first element that will be shifted up.</param>
  204. /// <param name="end">The index where the first shifted element will end up.</param>
  205. /// <exception cref="IndexOutOfRangeException">Thrown if the new length exceeds the capacity.</exception>
  206. public void InsertRangeWithBeginEnd(int begin, int end)
  207. {
  208. int items = end - begin;
  209. if(items < 1)
  210. return;
  211. int itemsToCopy = length - begin;
  212. Length += items;
  213. if(itemsToCopy < 1)
  214. return;
  215. int bytesToCopy = itemsToCopy * UnsafeUtility.SizeOf<T>();
  216. unsafe
  217. {
  218. byte *b = Buffer;
  219. byte *dest = b + end * UnsafeUtility.SizeOf<T>();
  220. byte *src = b + begin * UnsafeUtility.SizeOf<T>();
  221. UnsafeUtility.MemMove(dest, src, bytesToCopy);
  222. }
  223. }
  224. /// <summary>
  225. /// Inserts a single element at an index. Increments the length by 1.
  226. /// </summary>
  227. /// <param name="index">The index at which to insert the element.</param>
  228. /// <param name="item">The element to insert.</param>
  229. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  230. public void Insert(int index, in T item)
  231. {
  232. InsertRangeWithBeginEnd(index, index+1);
  233. this[index] = item;
  234. }
  235. /// <summary>
  236. /// Copies the last element of this list to an index. Decrements the length by 1.
  237. /// </summary>
  238. /// <remarks>Useful as a cheap way to remove elements from a list when you don't care about preserving order.</remarks>
  239. /// <param name="index">The index to overwrite with the last element.</param>
  240. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  241. public void RemoveAtSwapBack(int index)
  242. {
  243. RemoveRangeSwapBack(index, 1);
  244. }
  245. /// <summary>
  246. /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*.
  247. /// </summary>
  248. /// <remarks>
  249. /// Copies the last `count`-numbered elements to the range starting at `index`.
  250. ///
  251. /// Useful as a cheap way to remove elements from a list when you don't care about preserving order.
  252. ///
  253. /// Does nothing if the count is less than 1.
  254. /// </remarks>
  255. /// <param name="index">The first index of the destination range.</param>
  256. /// <param name="count">The number of elements to copy and the amount by which to decrement the length.</param>
  257. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  258. public void RemoveRangeSwapBack(int index, int count)
  259. {
  260. if (count > 0)
  261. {
  262. int copyFrom = math.max(Length - count, index + count);
  263. unsafe
  264. {
  265. var sizeOf = UnsafeUtility.SizeOf<T>();
  266. void* dst = Buffer + index * sizeOf;
  267. void* src = Buffer + copyFrom * sizeOf;
  268. UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf);
  269. }
  270. Length -= count;
  271. }
  272. }
  273. /// <summary>
  274. /// Truncates the list by replacing the item at the specified index range with the items from the end the list. The list
  275. /// is shortened by number of elements in range.
  276. /// </summary>
  277. /// <param name="begin">The first index of the item to remove.</param>
  278. /// <param name="end">The index past-the-last item to remove.</param>
  279. /// <exception cref="ArgumentException">Thrown if end argument is less than begin argument.</exception>
  280. /// <exception cref="ArgumentOutOfRangeException">Thrown if begin or end arguments are not positive or out of bounds.</exception>
  281. [Obsolete("RemoveRangeSwapBackWithBeginEnd(begin, end) is deprecated, use RemoveRangeSwapBack(index, count) instead. (RemovedAfter 2021-06-02)", false)]
  282. public void RemoveRangeSwapBackWithBeginEnd(int begin, int end) => RemoveRangeSwapBack(begin, end - begin);
  283. /// <summary>
  284. /// Removes the element at an index. Shifts everything above the index down by one and decrements the length by 1.
  285. /// </summary>
  286. /// <param name="index">The index of the element to remove.</param>
  287. /// <remarks>
  288. /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove an element.
  289. /// </remarks>
  290. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  291. public void RemoveAt(int index)
  292. {
  293. RemoveRange(index, 1);
  294. }
  295. /// <summary>
  296. /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*.
  297. /// </summary>
  298. /// <remarks>
  299. /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove elements.
  300. /// </remarks>
  301. /// <param name="index">The first index of the range to remove.</param>
  302. /// <param name="count">The number of elements to remove.</param>
  303. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  304. public void RemoveRange(int index, int count)
  305. {
  306. if (count > 0)
  307. {
  308. int copyFrom = math.min(index + count, Length);
  309. unsafe
  310. {
  311. var sizeOf = UnsafeUtility.SizeOf<T>();
  312. void* dst = Buffer + index * sizeOf;
  313. void* src = Buffer + copyFrom * sizeOf;
  314. UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf);
  315. }
  316. Length -= count;
  317. }
  318. }
  319. /// <summary>
  320. /// Truncates the list by removing the items at the specified index range, and shifting all remaining items to replace removed items. The list
  321. /// is shortened by number of elements in range.
  322. /// </summary>
  323. /// <param name="begin">The first index of the item to remove.</param>
  324. /// <param name="end">The index past-the-last item to remove.</param>
  325. /// <remarks>
  326. /// This method of removing item(s) is useful only in case when list is ordered and user wants to preserve order
  327. /// in list after removal In majority of cases is not important and user should use more performant `RemoveRangeSwapBackWithBeginEnd`.
  328. /// </remarks>
  329. /// <exception cref="ArgumentException">Thrown if end argument is less than begin argument.</exception>
  330. /// <exception cref="ArgumentOutOfRangeException">Thrown if begin or end arguments are not positive or out of bounds.</exception>
  331. [Obsolete("RemoveRangeWithBeginEnd(begin, end) is deprecated, use RemoveRange(index, count) instead. (RemovedAfter 2021-06-02)", false)]
  332. public void RemoveRangeWithBeginEnd(int begin, int end) => RemoveRange(begin, end - begin);
  333. /// <summary>
  334. /// Returns a managed array that is a copy of this list.
  335. /// </summary>
  336. /// <returns>A managed array that is a copy of this list.</returns>
  337. [NotBurstCompatible]
  338. public T[] ToArray()
  339. {
  340. var result = new T[Length];
  341. unsafe
  342. {
  343. byte* s = Buffer;
  344. fixed(T* d = result)
  345. UnsafeUtility.MemCpy(d, s, LengthInBytes);
  346. }
  347. return result;
  348. }
  349. /// <summary>
  350. /// Returns an array that is a copy of this list.
  351. /// </summary>
  352. /// <param name="allocator">The allocator to use.</param>
  353. /// <returns>An array that is a copy of this list.</returns>
  354. public NativeArray<T> ToNativeArray(AllocatorManager.AllocatorHandle allocator)
  355. {
  356. unsafe
  357. {
  358. var copy = CollectionHelper.CreateNativeArray<T>(Length, allocator, NativeArrayOptions.UninitializedMemory);
  359. UnsafeUtility.MemCpy(copy.GetUnsafePtr(), Buffer, LengthInBytes);
  360. return copy;
  361. }
  362. }
  363. }
  364. [BurstCompatible]
  365. struct FixedList
  366. {
  367. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
  368. internal static int PaddingBytes<T>() where T : struct
  369. {
  370. return math.max(0, math.min(6, (1 << math.tzcnt(UnsafeUtility.SizeOf<T>())) - 2));
  371. }
  372. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  373. internal static int StorageBytes<BUFFER,T>() where BUFFER : struct where T : struct
  374. {
  375. return UnsafeUtility.SizeOf<BUFFER>() - PaddingBytes<T>();
  376. }
  377. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  378. internal static int Capacity<BUFFER,T>() where BUFFER : struct where T : struct
  379. {
  380. return StorageBytes<BUFFER,T>() / UnsafeUtility.SizeOf<T>();
  381. }
  382. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  383. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
  384. internal static void CheckResize<BUFFER,T>(int newLength) where BUFFER : struct where T : struct
  385. {
  386. var Capacity = Capacity<BUFFER,T>();
  387. if (newLength < 0 || newLength > Capacity)
  388. throw new IndexOutOfRangeException($"NewLength {newLength} is out of range of '{Capacity}' Capacity.");
  389. }
  390. }
  391. <#
  392. var SIZES = new int[]{32,64,128,512,4096};
  393. for(var size = 0; size < 5; ++size)
  394. {
  395. var BYTES = SIZES[size];
  396. var BUFFER_BYTES = BYTES - 2;
  397. var TYPENAME = String.Format("FixedList{0}Bytes", BYTES);
  398. var OLD_TYPENAME = String.Format("FixedList{0}", BYTES);
  399. #>
  400. [Obsolete("Renamed to <#=TYPENAME#><T> (UnityUpgradable) -> <#=TYPENAME#><T>", true)]
  401. public struct <#=OLD_TYPENAME#><T> where T : unmanaged {}
  402. /// <summary>
  403. /// An unmanaged, resizable list whose content is all stored directly in the <#=BYTES#>-byte struct. Useful for small lists.
  404. /// </summary>
  405. /// <typeparam name="T">The type of the elements.</typeparam>
  406. [Serializable]
  407. [DebuggerTypeProxy(typeof(<#=TYPENAME#>DebugView<>))]
  408. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
  409. public struct <#=TYPENAME#><T>
  410. : INativeList<T>
  411. , IEnumerable<T> // Used by collection initializers.
  412. <#
  413. foreach(var OTHERBYTES in SIZES)
  414. {
  415. var OTHERTYPENAME = String.Format("FixedList{0}Bytes", OTHERBYTES);
  416. WriteLine(" , IEquatable<{0}<T>>", OTHERTYPENAME);
  417. WriteLine(" , IComparable<{0}<T>>", OTHERTYPENAME);
  418. }
  419. #> where T : unmanaged
  420. {
  421. [SerializeField] internal ushort length;
  422. [SerializeField] internal FixedBytes<#=BUFFER_BYTES#> buffer;
  423. /// <summary>
  424. /// The current number of items in this list.
  425. /// </summary>
  426. /// <value>The current number of items in this list.</value>
  427. [CreateProperty]
  428. public int Length
  429. {
  430. get => length;
  431. set
  432. {
  433. FixedList.CheckResize<FixedBytes<#=BUFFER_BYTES#>,T>(value);
  434. length = (ushort)value;
  435. }
  436. }
  437. /// <summary>
  438. /// A property in order to display items in the Entity Inspector.
  439. /// </summary>
  440. [CreateProperty] IEnumerable<T> Elements => this.ToArray();
  441. /// <summary>
  442. /// Whether this list is empty.
  443. /// </summary>
  444. /// <value>True if this string has no characters or if the container has not been constructed.</value>
  445. public bool IsEmpty => Length == 0;
  446. internal int LengthInBytes => Length * UnsafeUtility.SizeOf<T>();
  447. unsafe internal byte* Buffer
  448. {
  449. get
  450. {
  451. fixed(byte* b = &buffer.offset0000.byte0000)
  452. return b + FixedList.PaddingBytes<T>();
  453. }
  454. }
  455. /// <summary>
  456. /// The number of elements that can fit in this list.
  457. /// </summary>
  458. /// <value>The number of elements that can fit in this list.</value>
  459. /// <remarks>The capacity of a FixedList cannot be changed. The setter is included only for conformity with <see cref="INativeList{T}"/>.</remarks>
  460. /// <exception cref="ArgumentOutOfRangeException">Thrown if the new value does not match the current capacity.</exception>
  461. public int Capacity
  462. {
  463. get
  464. {
  465. return FixedList.Capacity<FixedBytes<#=BUFFER_BYTES#>,T>();
  466. }
  467. set
  468. {
  469. CollectionHelper.CheckCapacityInRange(value, Length);
  470. }
  471. }
  472. /// <summary>
  473. /// The element at a given index.
  474. /// </summary>
  475. /// <param name="index">An index.</param>
  476. /// <value>The value to store at the index.</value>
  477. /// <exception cref="IndexOutOfRangeException">Thrown if the index is out of bounds.</exception>
  478. public T this[int index]
  479. {
  480. get
  481. {
  482. CollectionHelper.CheckIndexInRange(index, length);
  483. unsafe
  484. {
  485. return UnsafeUtility.ReadArrayElement<T>(Buffer, CollectionHelper.AssumePositive(index));
  486. }
  487. }
  488. set
  489. {
  490. CollectionHelper.CheckIndexInRange(index, length);
  491. unsafe
  492. {
  493. UnsafeUtility.WriteArrayElement<T>(Buffer, CollectionHelper.AssumePositive(index), value);
  494. }
  495. }
  496. }
  497. /// <summary>
  498. /// Returns the element at a given index.
  499. /// </summary>
  500. /// <param name="index">An index.</param>
  501. /// <returns>The list element at the index.</returns>
  502. public ref T ElementAt(int index)
  503. {
  504. CollectionHelper.CheckIndexInRange(index, length);
  505. unsafe
  506. {
  507. return ref UnsafeUtility.ArrayElementAsRef<T>(Buffer, index);
  508. }
  509. }
  510. /// <summary>
  511. /// Returns the hash code of this list.
  512. /// </summary>
  513. /// <remarks>
  514. /// Only the content of the list (the bytes of the elements) are included in the hash. Any bytes beyond the length are not part of the hash.</remarks>
  515. /// <returns>The hash code of this list.</returns>
  516. public override int GetHashCode()
  517. {
  518. unsafe
  519. {
  520. return (int)CollectionHelper.Hash(Buffer, LengthInBytes);
  521. }
  522. }
  523. /// <summary>
  524. /// Appends an element to the end of this list. Increments the length by 1.
  525. /// </summary>
  526. /// <remarks>The same as <see cref="AddNoResize"/>. Remember that a fixed list is never resized.</remarks>
  527. /// <param name="item">The element to append at the end of the list.</param>
  528. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  529. public void Add(in T item)
  530. {
  531. this[Length++] = item;
  532. }
  533. /// <summary>
  534. /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements.
  535. /// </summary>
  536. /// <remarks>The same as <see cref="AddRangeNoResize"/>. Remember that a fixed list is never resized.</remarks>
  537. /// <param name="ptr">A buffer.</param>
  538. /// <param name="length">The number of elements from the buffer to append.</param>
  539. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  540. public unsafe void AddRange(void* ptr, int length)
  541. {
  542. T* data = (T*)ptr;
  543. for (var i = 0; i < length; ++i)
  544. {
  545. this[Length++] = data[i];
  546. }
  547. }
  548. /// <summary>
  549. /// Appends an element to the end of this list. Increments the length by 1.
  550. /// </summary>
  551. /// <remarks>The same as <see cref="Add"/>. Included only for consistency with the other list types.</remarks>
  552. /// <param name="item">The element to append at the end of the list.</param>
  553. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  554. public void AddNoResize(in T item) => Add(item);
  555. /// <summary>
  556. /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements.
  557. /// </summary>
  558. /// <remarks>The same as <see cref="AddRange"/>. Included only for consistency with the other list types.</remarks>
  559. /// <param name="ptr">A buffer.</param>
  560. /// <param name="length">The number of elements from the buffer to append.</param>
  561. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  562. public unsafe void AddRangeNoResize(void* ptr, int length) => AddRange(ptr, length);
  563. /// <summary>
  564. /// Sets the length to 0.
  565. /// </summary>
  566. /// <remarks> Does *not* zero out the bytes.</remarks>
  567. public void Clear()
  568. {
  569. Length = 0;
  570. }
  571. /// <summary>
  572. /// Shifts elements toward the end of this list, increasing its length.
  573. /// </summary>
  574. /// <remarks>
  575. /// Right-shifts elements in the list so as to create 'free' slots at the beginning or in the middle.
  576. ///
  577. /// The length is increased by `end - begin`.
  578. ///
  579. /// If `end` equals `begin`, the method does nothing.
  580. ///
  581. /// The element at index `begin` will be copied to index `end`, the element at index `begin + 1` will be copied to `end + 1`, and so forth.
  582. ///
  583. /// The indexes `begin` up to `end` are not cleared: they will contain whatever values they held prior.
  584. /// </remarks>
  585. /// <param name="begin">The index of the first element that will be shifted up.</param>
  586. /// <param name="end">The index where the first shifted element will end up.</param>
  587. /// <exception cref="IndexOutOfRangeException">Thrown if the new length exceeds the capacity.</exception>
  588. public void InsertRangeWithBeginEnd(int begin, int end)
  589. {
  590. int items = end - begin;
  591. if(items < 1)
  592. return;
  593. int itemsToCopy = length - begin;
  594. Length += items;
  595. if(itemsToCopy < 1)
  596. return;
  597. int bytesToCopy = itemsToCopy * UnsafeUtility.SizeOf<T>();
  598. unsafe
  599. {
  600. byte *b = Buffer;
  601. byte *dest = b + end * UnsafeUtility.SizeOf<T>();
  602. byte *src = b + begin * UnsafeUtility.SizeOf<T>();
  603. UnsafeUtility.MemMove(dest, src, bytesToCopy);
  604. }
  605. }
  606. /// <summary>
  607. /// Inserts a single element at an index. Increments the length by 1.
  608. /// </summary>
  609. /// <param name="index">The index at which to insert the element.</param>
  610. /// <param name="item">The element to insert.</param>
  611. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  612. public void Insert(int index, in T item)
  613. {
  614. InsertRangeWithBeginEnd(index, index+1);
  615. this[index] = item;
  616. }
  617. /// <summary>
  618. /// Copies the last element of this list to an index. Decrements the length by 1.
  619. /// </summary>
  620. /// <remarks>Useful as a cheap way to remove elements from a list when you don't care about preserving order.</remarks>
  621. /// <param name="index">The index to overwrite with the last element.</param>
  622. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  623. public void RemoveAtSwapBack(int index)
  624. {
  625. RemoveRangeSwapBack(index, 1);
  626. }
  627. /// <summary>
  628. /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*.
  629. /// </summary>
  630. /// <remarks>
  631. /// Copies the last `count`-numbered elements to the range starting at `index`.
  632. ///
  633. /// Useful as a cheap way to remove elements from a list when you don't care about preserving order.
  634. ///
  635. /// Does nothing if the count is less than 1.
  636. /// </remarks>
  637. /// <param name="index">The first index of the destination range.</param>
  638. /// <param name="count">The number of elements to copy and the amount by which to decrement the length.</param>
  639. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  640. public void RemoveRangeSwapBack(int index, int count)
  641. {
  642. if (count > 0)
  643. {
  644. int copyFrom = math.max(Length - count, index + count);
  645. unsafe
  646. {
  647. var sizeOf = UnsafeUtility.SizeOf<T>();
  648. void* dst = Buffer + index * sizeOf;
  649. void* src = Buffer + copyFrom * sizeOf;
  650. UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf);
  651. }
  652. Length -= count;
  653. }
  654. }
  655. /// <summary>
  656. /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*.
  657. /// </summary>
  658. /// <param name="begin">The first index of the item to remove.</param>
  659. /// <param name="end">The index past-the-last item to remove.</param>
  660. /// <exception cref="ArgumentException">Thrown if end argument is less than begin argument.</exception>
  661. /// <exception cref="ArgumentOutOfRangeException">Thrown if begin or end arguments are not positive or out of bounds.</exception>
  662. [Obsolete("RemoveRangeSwapBackWithBeginEnd(begin, end) is deprecated, use RemoveRangeSwapBack(index, count) instead. (RemovedAfter 2021-06-02)", false)]
  663. public void RemoveRangeSwapBackWithBeginEnd(int begin, int end) => RemoveRangeSwapBack(begin, end - begin);
  664. /// <summary>
  665. /// Removes the element at an index. Shifts everything above the index down by one and decrements the length by 1.
  666. /// </summary>
  667. /// <param name="index">The index of the element to remove.</param>
  668. /// <remarks>
  669. /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove an element.
  670. /// </remarks>
  671. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  672. public void RemoveAt(int index)
  673. {
  674. RemoveRange(index, 1);
  675. }
  676. /// <summary>
  677. /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*.
  678. /// </summary>
  679. /// <remarks>
  680. /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove elements.
  681. /// </remarks>
  682. /// <param name="index">The first index of the range to remove.</param>
  683. /// <param name="count">The number of elements to remove.</param>
  684. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  685. public void RemoveRange(int index, int count)
  686. {
  687. if (count > 0)
  688. {
  689. int copyFrom = math.min(index + count, Length);
  690. unsafe
  691. {
  692. var sizeOf = UnsafeUtility.SizeOf<T>();
  693. void* dst = Buffer + index * sizeOf;
  694. void* src = Buffer + copyFrom * sizeOf;
  695. UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf);
  696. }
  697. Length -= count;
  698. }
  699. }
  700. /// <summary>
  701. /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*.
  702. /// </summary>
  703. /// <param name="begin">The first index of the item to remove.</param>
  704. /// <param name="end">The index past-the-last item to remove.</param>
  705. /// <remarks>
  706. /// This method of removing item(s) is useful only in case when list is ordered and user wants to preserve order
  707. /// in list after removal In majority of cases is not important and user should use more performant `RemoveRangeSwapBackWithBeginEnd`.
  708. /// </remarks>
  709. /// <exception cref="ArgumentException">Thrown if end argument is less than begin argument.</exception>
  710. /// <exception cref="ArgumentOutOfRangeException">Thrown if begin or end arguments are not positive or out of bounds.</exception>
  711. [Obsolete("RemoveRangeWithBeginEnd(begin, end) is deprecated, use RemoveRange(index, count) instead. (RemovedAfter 2021-06-02)", false)]
  712. public void RemoveRangeWithBeginEnd(int begin, int end) => RemoveRange(begin, end - begin);
  713. /// <summary>
  714. /// Returns a managed array that is a copy of this list.
  715. /// </summary>
  716. /// <returns>A managed array that is a copy of this list.</returns>
  717. [NotBurstCompatible]
  718. public T[] ToArray()
  719. {
  720. var result = new T[Length];
  721. unsafe
  722. {
  723. byte* s = Buffer;
  724. fixed(T* d = result)
  725. UnsafeUtility.MemCpy(d, s, LengthInBytes);
  726. }
  727. return result;
  728. }
  729. /// <summary>
  730. /// Returns an array that is a copy of this list.
  731. /// </summary>
  732. /// <param name="allocator">The allocator to use.</param>
  733. /// <returns>An array that is a copy of this list.</returns>
  734. public NativeArray<T> ToNativeArray(AllocatorManager.AllocatorHandle allocator)
  735. {
  736. unsafe
  737. {
  738. var copy = CollectionHelper.CreateNativeArray<T>(Length, allocator, NativeArrayOptions.UninitializedMemory);
  739. UnsafeUtility.MemCpy(copy.GetUnsafePtr(), Buffer, LengthInBytes);
  740. return copy;
  741. }
  742. }
  743. <#
  744. foreach(var OTHERBYTES in SIZES)
  745. {
  746. var OTHER_BUFFER_BYTES = OTHERBYTES - 2;
  747. var OTHERTYPENAME = String.Format("FixedList{0}Bytes", OTHERBYTES);
  748. #>
  749. /// <summary>
  750. /// Returns true if two lists are equal.
  751. /// </summary>
  752. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  753. /// <param name="a">The first list to compare for equality.</param>
  754. /// <param name="b">The second list to compare for equality.</param>
  755. /// <returns>True if the two lists are equal.</returns>
  756. public static bool operator ==(in <#=TYPENAME#><T> a, in <#=OTHERTYPENAME#><T> b)
  757. {
  758. unsafe
  759. {
  760. if(a.length != b.length)
  761. return false;
  762. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  763. }
  764. }
  765. /// <summary>
  766. /// Returns true if two lists are unequal.
  767. /// </summary>
  768. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  769. /// <param name="a">The first list to compare for inequality.</param>
  770. /// <param name="b">The second list to compare for inequality.</param>
  771. /// <returns>True if the two lists are unequal.</returns>
  772. public static bool operator !=(in <#=TYPENAME#><T> a, in <#=OTHERTYPENAME#><T> b)
  773. {
  774. return !(a == b);
  775. }
  776. /// <summary>
  777. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  778. /// </summary>
  779. /// <param name="other">A list to to compare with.</param>
  780. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  781. ///
  782. /// 0 denotes that both lists should have the same position in a sort.
  783. /// -1 denotes that this list should precede the other list in a sort.
  784. /// +1 denotes that this list should follow the other list in a sort.
  785. /// </returns>
  786. public int CompareTo(<#=OTHERTYPENAME#><T> other)
  787. {
  788. unsafe
  789. {
  790. fixed(byte* a = &buffer.offset0000.byte0000)
  791. {
  792. byte* b = &other.buffer.offset0000.byte0000;
  793. var aa = a + FixedList.PaddingBytes<T>();
  794. var bb = b + FixedList.PaddingBytes<T>();
  795. var mini = math.min(Length, other.Length);
  796. for(var i = 0; i < mini; ++i)
  797. {
  798. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  799. if(j != 0)
  800. return j;
  801. }
  802. return Length.CompareTo(other.Length);
  803. }
  804. }
  805. }
  806. /// <summary>
  807. /// Returns true if this list and another list are equal.
  808. /// </summary>
  809. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  810. /// <param name="other">The list to compare for equality.</param>
  811. /// <returns>True if the two lists are equal.</returns>
  812. public bool Equals(<#=OTHERTYPENAME#><T> other)
  813. {
  814. return CompareTo(other) == 0;
  815. }
  816. <#
  817. if(BYTES != OTHERBYTES)
  818. {
  819. #>
  820. /// <summary>
  821. /// Initializes and returns an instance of <#=TYPENAME#> with content copied from another list.
  822. /// </summary>
  823. /// <param name="other">The list to copy.</param>
  824. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of <#=TYPENAME#>&lt;T&gt;.</exception>
  825. public <#=TYPENAME#>(in <#=OTHERTYPENAME#><T> other)
  826. {
  827. this = default;
  828. var error = Initialize(other);
  829. if(error != 0)
  830. FixedList.CheckResize<FixedBytes<#=BUFFER_BYTES#>,T>(other.Length);
  831. }
  832. /// <summary>
  833. /// Initializes an instance of <#=TYPENAME#> with content copied from another list.
  834. /// </summary>
  835. /// <param name="other">The list to copy.</param>
  836. /// <returns>zero on success, or non-zero on error.</returns>
  837. internal int Initialize(in <#=OTHERTYPENAME#><T> other)
  838. {
  839. if(other.Length > Capacity)
  840. return (int)CopyError.Truncation;
  841. length = other.length;
  842. buffer = new FixedBytes<#=BUFFER_BYTES#>();
  843. unsafe
  844. {
  845. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  846. }
  847. return 0;
  848. }
  849. /// <summary>
  850. /// Returns a new list that is a copy of another list.
  851. /// </summary>
  852. /// <param name="other">The list to copy.</param>
  853. /// <returns>A new list that is a copy of the other.</returns>
  854. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of <#=TYPENAME#>&lt;T&gt;.</exception>
  855. public static implicit operator <#=TYPENAME#><T>(in <#=OTHERTYPENAME#><T> other)
  856. {
  857. return new <#=TYPENAME#><T>(other);
  858. }
  859. <#
  860. }
  861. }
  862. #>
  863. /// <summary>
  864. /// Returns true if the list is equal to an object.
  865. /// </summary>
  866. /// <remarks>Two lists are equal if their length and bytes are equal.
  867. ///
  868. /// A FixedList*N*&lt;T&gt; can only be equal to another FixedList*N*&lt;T&gt; with the same *N* and T.
  869. /// </remarks>
  870. /// <param name="obj">An object to compare for equality.</param>
  871. /// <returns>True if the list is equal to the object.</returns>
  872. [NotBurstCompatible]
  873. public override bool Equals(object obj)
  874. {
  875. <#
  876. foreach(var OTHERBYTES in SIZES)
  877. {
  878. var OTHERTYPENAME = String.Format("FixedList{0}Bytes", OTHERBYTES);
  879. WriteLine(" if(obj is {0}<T> a{0}) return Equals(a{0});", OTHERTYPENAME);
  880. }
  881. #>
  882. return false;
  883. }
  884. /// <summary>
  885. /// An enumerator over the elements of a <#=TYPENAME#>&lt;T&gt;.
  886. /// </summary>
  887. /// <remarks>
  888. /// In an enumerator's initial state, `Current` cannot be read. The first <see cref="MoveNext"/> call advances the enumerator to the first element.
  889. /// </remarks>
  890. public struct Enumerator : IEnumerator<T>
  891. {
  892. <#=TYPENAME#><T> m_List;
  893. int m_Index;
  894. /// <summary>
  895. /// Initializes and returns an instance of <#=TYPENAME#>&lt;T&gt;.
  896. /// </summary>
  897. /// <param name="list">The list for which to create an enumerator.</param>
  898. public Enumerator(ref <#=TYPENAME#><T> list)
  899. {
  900. m_List = list;
  901. m_Index = -1;
  902. }
  903. /// <summary>
  904. /// Does nothing.
  905. /// </summary>
  906. public void Dispose()
  907. {
  908. }
  909. /// <summary>
  910. /// Advances the enumerator to the next element.
  911. /// </summary>
  912. /// <returns>True if <see cref="Current"/> is valid to read after the call.</returns>
  913. public bool MoveNext()
  914. {
  915. m_Index++;
  916. return m_Index < m_List.Length;
  917. }
  918. /// <summary>
  919. /// Resets the enumerator to its initial state.
  920. /// </summary>
  921. public void Reset()
  922. {
  923. m_Index = -1;
  924. }
  925. /// <summary>
  926. /// The current element.
  927. /// </summary>
  928. /// <value>The current element.</value>
  929. public T Current => m_List[m_Index]; // Let <#=TYPENAME#><T> indexer check for out of range.
  930. object IEnumerator.Current => Current;
  931. }
  932. /// <summary>
  933. /// Returns an enumerator for iterating over the elements of this list.
  934. /// </summary>
  935. /// <returns>An enumerator for iterating over the elements of this list.</returns>
  936. public Enumerator GetEnumerator()
  937. {
  938. return new Enumerator(ref this);
  939. }
  940. /// <summary>
  941. /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
  942. /// </summary>
  943. /// <returns>Nothing because it always throws <see cref="NotImplementedException"/>.</returns>
  944. /// <exception cref="NotImplementedException">Method is not implemented.</exception>
  945. IEnumerator IEnumerable.GetEnumerator()
  946. {
  947. throw new NotImplementedException();
  948. }
  949. /// <summary>
  950. /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
  951. /// </summary>
  952. /// <returns>Nothing because it always throws <see cref="NotImplementedException"/>.</returns>
  953. /// <exception cref="NotImplementedException">Method is not implemented.</exception>
  954. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  955. {
  956. throw new NotImplementedException();
  957. }
  958. }
  959. /// <summary>
  960. /// Provides extension methods for <#=TYPENAME#>.
  961. /// </summary>
  962. [BurstCompatible]
  963. public unsafe static class <#=TYPENAME#>Extensions
  964. {
  965. /// <summary>
  966. /// Finds the index of the first occurrence of a particular value in this list.
  967. /// </summary>
  968. /// <typeparam name="T">The type of elements in this list.</typeparam>
  969. /// <typeparam name="U">The value type.</typeparam>
  970. /// <param name="list">The list to search.</param>
  971. /// <param name="value">The value to locate.</param>
  972. /// <returns>The index of the first occurrence of the value. Returns -1 if no occurrence is found.</returns>
  973. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  974. public static int IndexOf<T, U>(this ref <#=TYPENAME#><T> list, U value) where T : unmanaged, IEquatable<U>
  975. {
  976. return NativeArrayExtensions.IndexOf<T, U>(list.Buffer, list.Length, value);
  977. }
  978. /// <summary>
  979. /// Returns true if a particular value is present in this list.
  980. /// </summary>
  981. /// <typeparam name="T">The type of elements in this list.</typeparam>
  982. /// <typeparam name="U">The value type.</typeparam>
  983. /// <param name="list">The list to search.</param>
  984. /// <param name="value">The value to locate.</param>
  985. /// <returns>True if the value is present in this list.</returns>
  986. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  987. public static bool Contains<T, U>(this ref <#=TYPENAME#><T> list, U value) where T : unmanaged, IEquatable<U>
  988. {
  989. return list.IndexOf(value) != -1;
  990. }
  991. /// <summary>
  992. /// Removes the first occurrence of a particular value in this list.
  993. /// </summary>
  994. /// <remarks>
  995. /// If a value is removed, all elements after it are shifted down by one, and the list's length is decremented by one.
  996. ///
  997. /// If you don't need to preserve the order of the remaining elements, <see cref="Unity.Collections.<#=TYPENAME#>Extensions.RemoveSwapBack{T, U}"/> is a cheaper alternative.
  998. /// </remarks>
  999. /// <typeparam name="T">The type of elements in this list.</typeparam>
  1000. /// <typeparam name="U">The value type.</typeparam>
  1001. /// <param name="list">The list to search.</param>
  1002. /// <param name="value">The value to locate and remove.</param>
  1003. /// <returns>True if the value was found and removed.</returns>
  1004. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  1005. public static bool Remove<T, U>(this ref <#=TYPENAME#><T> list, U value) where T : unmanaged, IEquatable<U>
  1006. {
  1007. int index = list.IndexOf(value);
  1008. if (index < 0)
  1009. {
  1010. return false;
  1011. }
  1012. list.RemoveAt(index);
  1013. return true;
  1014. }
  1015. /// <summary>
  1016. /// Removes the first occurrence of a particular value in this list.
  1017. /// </summary>
  1018. /// <remarks>
  1019. /// If a value is removed, the last element of the list is copied to overwrite the removed value, and the list's length is decremented by one.
  1020. ///
  1021. /// This is cheaper than <see cref="Remove"/>, but the order of the remaining elements is not preserved.
  1022. /// </remarks>
  1023. /// <typeparam name="T">The type of elements in this list.</typeparam>
  1024. /// <typeparam name="U">The value type.</typeparam>
  1025. /// <param name="list">The list to search.</param>
  1026. /// <param name="value">The value to locate and remove.</param>
  1027. /// <returns>Returns true if the item is removed.</returns>
  1028. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  1029. public static bool RemoveSwapBack<T, U>(this ref <#=TYPENAME#><T> list, U value) where T : unmanaged, IEquatable<U>
  1030. {
  1031. var index = list.IndexOf(value);
  1032. if (index == -1)
  1033. {
  1034. return false;
  1035. }
  1036. list.RemoveAtSwapBack(index);
  1037. return true;
  1038. }
  1039. }
  1040. sealed class <#=TYPENAME#>DebugView<T> where T : unmanaged
  1041. {
  1042. <#=TYPENAME#><T> m_List;
  1043. public <#=TYPENAME#>DebugView(<#=TYPENAME#><T> list)
  1044. {
  1045. m_List = list;
  1046. }
  1047. public T[] Items => m_List.ToArray();
  1048. }
  1049. <#
  1050. }
  1051. #>
  1052. <#
  1053. var TYPES = new string[]{"byte","int","float"};
  1054. var TYPESIZES = new int[]{1,4,4};
  1055. for(var type = 0; type < 3; ++type)
  1056. for(var size = 0; size < 5; ++size)
  1057. {
  1058. var BYTES = SIZES[size];
  1059. var TYPE = TYPES[type];
  1060. var TYPESIZE = TYPESIZES[type];
  1061. var BUFFER_BYTES = BYTES - 2;
  1062. var TYPENAME = String.Format("FixedList{0}{1}", new CultureInfo("en-US").TextInfo.ToTitleCase(TYPE), BYTES);
  1063. var NEW_TYPENAME = $"FixedList{BYTES}Bytes<{TYPE}>";
  1064. #>
  1065. /// <summary>
  1066. /// An unmanaged, resizable list of <#=TYPE#> that does not allocate memory.
  1067. /// It is <#=BYTES#> bytes in size, and contains all the memory it needs.
  1068. /// </summary>
  1069. [Serializable]
  1070. [StructLayout(LayoutKind.Explicit, Size=<#=BYTES#>)]
  1071. [Obsolete("<#=TYPENAME#> is deprecated, please use FixedList<#=BYTES#>Bytes<<#=TYPE#>> instead. (UnityUpgradable) -> <#=NEW_TYPENAME#>", true)]
  1072. public struct <#=TYPENAME#> {}
  1073. [Obsolete("<#=TYPENAME#>DebugView is deprecated. (UnityUpgradable) -> FixedList<#=BYTES#>BytesDebugView<<#=TYPE#>>", true)]
  1074. sealed class <#=TYPENAME#>DebugView
  1075. {
  1076. FixedList<#=BYTES#>Bytes<<#=TYPE#>> m_List;
  1077. public <#=TYPENAME#>DebugView(FixedList<#=BYTES#>Bytes<<#=TYPE#>> list)
  1078. {
  1079. m_List = list;
  1080. }
  1081. public <#=TYPE#>[] Items => m_List.ToArray();
  1082. }
  1083. <#
  1084. }
  1085. #>
  1086. /// <summary>
  1087. /// Provides extension methods for FixedList*N*.
  1088. /// </summary>
  1089. public static class FixedListExtensions
  1090. {
  1091. <#
  1092. for(var size = 0; size < 5; ++size)
  1093. {
  1094. var BYTES = SIZES[size];
  1095. var BUFFER_BYTES = BYTES - 2;
  1096. var TYPENAME = String.Format("FixedList{0}Bytes", BYTES);
  1097. #>
  1098. /// <summary>
  1099. /// Sorts the elements in this list in ascending order.
  1100. /// </summary>
  1101. /// <typeparam name="T">The type of the elements.</typeparam>
  1102. /// <param name="list">The list to sort.</param>
  1103. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
  1104. public static void Sort<T>(this ref <#=TYPENAME#><T> list)
  1105. where T : unmanaged, IComparable<T>
  1106. {
  1107. unsafe
  1108. {
  1109. fixed(byte* b = &list.buffer.offset0000.byte0000)
  1110. {
  1111. var c = b + FixedList.PaddingBytes<T>();
  1112. NativeSortExtension.Sort((T*)c, list.Length);
  1113. }
  1114. }
  1115. }
  1116. /// <summary>
  1117. /// Sorts the elements in this list using a custom comparison.
  1118. /// </summary>
  1119. /// <typeparam name="T">The type of the elements.</typeparam>
  1120. /// <typeparam name="U">The type of the comparer.</typeparam>
  1121. /// <param name="list">The list to sort.</param>
  1122. /// <param name="comp">The comparison function used to determine the relative order of the elements.</param>
  1123. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(NativeSortExtension.DefaultComparer<int>) })]
  1124. public static void Sort<T, U>(this ref <#=TYPENAME#><T> list, U comp)
  1125. where T : unmanaged, IComparable<T>
  1126. where U : IComparer<T>
  1127. {
  1128. unsafe
  1129. {
  1130. fixed(byte* b = &list.buffer.offset0000.byte0000)
  1131. {
  1132. var c = b + FixedList.PaddingBytes<T>();
  1133. NativeSortExtension.Sort((T*)c, list.Length, comp);
  1134. }
  1135. }
  1136. }
  1137. <#
  1138. }
  1139. #>
  1140. }
  1141. }