暫無描述
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.

UnsafeHashMap.cs 54KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Runtime.InteropServices;
  6. using System.Threading;
  7. using Unity.Burst;
  8. using Unity.Mathematics;
  9. using Unity.Jobs;
  10. using Unity.Jobs.LowLevel.Unsafe;
  11. using UnityEngine.Assertions;
  12. namespace Unity.Collections.LowLevel.Unsafe
  13. {
  14. /// <summary>
  15. /// A bucket of key-value pairs. Used as the internal storage for hash maps.
  16. /// </summary>
  17. /// <remarks>Exposed publicly only for advanced use cases.</remarks>
  18. [BurstCompatible]
  19. public unsafe struct UnsafeHashMapBucketData
  20. {
  21. internal UnsafeHashMapBucketData(byte* v, byte* k, byte* n, byte* b, int bcm)
  22. {
  23. values = v;
  24. keys = k;
  25. next = n;
  26. buckets = b;
  27. bucketCapacityMask = bcm;
  28. }
  29. /// <summary>
  30. /// The buffer of values.
  31. /// </summary>
  32. /// <value>The buffer of values.</value>
  33. public readonly byte* values;
  34. /// <summary>
  35. /// The buffer of keys.
  36. /// </summary>
  37. /// <value>The buffer of keys.</value>
  38. public readonly byte* keys;
  39. /// <summary>
  40. /// The next bucket in the chain.
  41. /// </summary>
  42. /// <value>The next bucket in the chain.</value>
  43. public readonly byte* next;
  44. /// <summary>
  45. /// The first bucket in the chain.
  46. /// </summary>
  47. /// <value>The first bucket in the chain.</value>
  48. public readonly byte* buckets;
  49. /// <summary>
  50. /// One less than the bucket capacity.
  51. /// </summary>
  52. /// <value>One less than the bucket capacity.</value>
  53. public readonly int bucketCapacityMask;
  54. }
  55. [StructLayout(LayoutKind.Explicit)]
  56. [BurstCompatible]
  57. internal unsafe struct UnsafeHashMapData
  58. {
  59. [FieldOffset(0)]
  60. internal byte* values;
  61. // 4-byte padding on 32-bit architectures here
  62. [FieldOffset(8)]
  63. internal byte* keys;
  64. // 4-byte padding on 32-bit architectures here
  65. [FieldOffset(16)]
  66. internal byte* next;
  67. // 4-byte padding on 32-bit architectures here
  68. [FieldOffset(24)]
  69. internal byte* buckets;
  70. // 4-byte padding on 32-bit architectures here
  71. [FieldOffset(32)]
  72. internal int keyCapacity;
  73. [FieldOffset(36)]
  74. internal int bucketCapacityMask; // = bucket capacity - 1
  75. [FieldOffset(40)]
  76. internal int allocatedIndexLength;
  77. [FieldOffset(JobsUtility.CacheLineSize < 64 ? 64 : JobsUtility.CacheLineSize)]
  78. internal fixed int firstFreeTLS[JobsUtility.MaxJobThreadCount * IntsPerCacheLine];
  79. // 64 is the cache line size on x86, arm usually has 32 - so it is possible to save some memory there
  80. internal const int IntsPerCacheLine = JobsUtility.CacheLineSize / sizeof(int);
  81. internal static int GetBucketSize(int capacity)
  82. {
  83. return capacity * 2;
  84. }
  85. internal static int GrowCapacity(int capacity)
  86. {
  87. if (capacity == 0)
  88. {
  89. return 1;
  90. }
  91. return capacity * 2;
  92. }
  93. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  94. internal static void AllocateHashMap<TKey, TValue>(int length, int bucketLength, AllocatorManager.AllocatorHandle label,
  95. out UnsafeHashMapData* outBuf)
  96. where TKey : struct
  97. where TValue : struct
  98. {
  99. CollectionHelper.CheckIsUnmanaged<TKey>();
  100. CollectionHelper.CheckIsUnmanaged<TValue>();
  101. UnsafeHashMapData* data = (UnsafeHashMapData*)Memory.Unmanaged.Allocate(sizeof(UnsafeHashMapData), UnsafeUtility.AlignOf<UnsafeHashMapData>(), label);
  102. bucketLength = math.ceilpow2(bucketLength);
  103. data->keyCapacity = length;
  104. data->bucketCapacityMask = bucketLength - 1;
  105. int keyOffset, nextOffset, bucketOffset;
  106. int totalSize = CalculateDataSize<TKey, TValue>(length, bucketLength, out keyOffset, out nextOffset, out bucketOffset);
  107. data->values = (byte*)Memory.Unmanaged.Allocate(totalSize, JobsUtility.CacheLineSize, label);
  108. data->keys = data->values + keyOffset;
  109. data->next = data->values + nextOffset;
  110. data->buckets = data->values + bucketOffset;
  111. outBuf = data;
  112. }
  113. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  114. internal static void ReallocateHashMap<TKey, TValue>(UnsafeHashMapData* data, int newCapacity, int newBucketCapacity, AllocatorManager.AllocatorHandle label)
  115. where TKey : struct
  116. where TValue : struct
  117. {
  118. newBucketCapacity = math.ceilpow2(newBucketCapacity);
  119. if (data->keyCapacity == newCapacity && (data->bucketCapacityMask + 1) == newBucketCapacity)
  120. {
  121. return;
  122. }
  123. CheckHashMapReallocateDoesNotShrink(data, newCapacity);
  124. int keyOffset, nextOffset, bucketOffset;
  125. int totalSize = CalculateDataSize<TKey, TValue>(newCapacity, newBucketCapacity, out keyOffset, out nextOffset, out bucketOffset);
  126. byte* newData = (byte*)Memory.Unmanaged.Allocate(totalSize, JobsUtility.CacheLineSize, label);
  127. byte* newKeys = newData + keyOffset;
  128. byte* newNext = newData + nextOffset;
  129. byte* newBuckets = newData + bucketOffset;
  130. // The items are taken from a free-list and might not be tightly packed, copy all of the old capcity
  131. UnsafeUtility.MemCpy(newData, data->values, data->keyCapacity * UnsafeUtility.SizeOf<TValue>());
  132. UnsafeUtility.MemCpy(newKeys, data->keys, data->keyCapacity * UnsafeUtility.SizeOf<TKey>());
  133. UnsafeUtility.MemCpy(newNext, data->next, data->keyCapacity * UnsafeUtility.SizeOf<int>());
  134. for (int emptyNext = data->keyCapacity; emptyNext < newCapacity; ++emptyNext)
  135. {
  136. ((int*)newNext)[emptyNext] = -1;
  137. }
  138. // re-hash the buckets, first clear the new bucket list, then insert all values from the old list
  139. for (int bucket = 0; bucket < newBucketCapacity; ++bucket)
  140. {
  141. ((int*)newBuckets)[bucket] = -1;
  142. }
  143. for (int bucket = 0; bucket <= data->bucketCapacityMask; ++bucket)
  144. {
  145. int* buckets = (int*)data->buckets;
  146. int* nextPtrs = (int*)newNext;
  147. while (buckets[bucket] >= 0)
  148. {
  149. int curEntry = buckets[bucket];
  150. buckets[bucket] = nextPtrs[curEntry];
  151. int newBucket = UnsafeUtility.ReadArrayElement<TKey>(data->keys, curEntry).GetHashCode() & (newBucketCapacity - 1);
  152. nextPtrs[curEntry] = ((int*)newBuckets)[newBucket];
  153. ((int*)newBuckets)[newBucket] = curEntry;
  154. }
  155. }
  156. Memory.Unmanaged.Free(data->values, label);
  157. if (data->allocatedIndexLength > data->keyCapacity)
  158. {
  159. data->allocatedIndexLength = data->keyCapacity;
  160. }
  161. data->values = newData;
  162. data->keys = newKeys;
  163. data->next = newNext;
  164. data->buckets = newBuckets;
  165. data->keyCapacity = newCapacity;
  166. data->bucketCapacityMask = newBucketCapacity - 1;
  167. }
  168. internal static void DeallocateHashMap(UnsafeHashMapData* data, AllocatorManager.AllocatorHandle allocator)
  169. {
  170. Memory.Unmanaged.Free(data->values, allocator);
  171. Memory.Unmanaged.Free(data, allocator);
  172. }
  173. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  174. internal static int CalculateDataSize<TKey, TValue>(int length, int bucketLength, out int keyOffset, out int nextOffset, out int bucketOffset)
  175. where TKey : struct
  176. where TValue : struct
  177. {
  178. var sizeOfTValue = UnsafeUtility.SizeOf<TValue>();
  179. var sizeOfTKey = UnsafeUtility.SizeOf<TKey>();
  180. var sizeOfInt = UnsafeUtility.SizeOf<int>();
  181. var valuesSize = CollectionHelper.Align(sizeOfTValue * length, JobsUtility.CacheLineSize);
  182. var keysSize = CollectionHelper.Align(sizeOfTKey * length, JobsUtility.CacheLineSize);
  183. var nextSize = CollectionHelper.Align(sizeOfInt * length, JobsUtility.CacheLineSize);
  184. var bucketSize = CollectionHelper.Align(sizeOfInt * bucketLength, JobsUtility.CacheLineSize);
  185. var totalSize = valuesSize + keysSize + nextSize + bucketSize;
  186. keyOffset = 0 + valuesSize;
  187. nextOffset = keyOffset + keysSize;
  188. bucketOffset = nextOffset + nextSize;
  189. return totalSize;
  190. }
  191. internal static bool IsEmpty(UnsafeHashMapData* data)
  192. {
  193. if (data->allocatedIndexLength <= 0)
  194. {
  195. return true;
  196. }
  197. var bucketArray = (int*)data->buckets;
  198. var bucketNext = (int*)data->next;
  199. var capacityMask = data->bucketCapacityMask;
  200. for (int i = 0; i <= capacityMask; ++i)
  201. {
  202. int bucket = bucketArray[i];
  203. if (bucket != -1)
  204. {
  205. return false;
  206. }
  207. }
  208. return true;
  209. }
  210. internal static int GetCount(UnsafeHashMapData* data)
  211. {
  212. if (data->allocatedIndexLength <= 0)
  213. {
  214. return 0;
  215. }
  216. var bucketNext = (int*)data->next;
  217. var freeListSize = 0;
  218. for (int tls = 0; tls < JobsUtility.MaxJobThreadCount; ++tls)
  219. {
  220. for (var freeIdx = data->firstFreeTLS[tls * IntsPerCacheLine]
  221. ; freeIdx >= 0
  222. ; freeIdx = bucketNext[freeIdx]
  223. )
  224. {
  225. ++freeListSize;
  226. }
  227. }
  228. return math.min(data->keyCapacity, data->allocatedIndexLength) - freeListSize;
  229. }
  230. internal static bool MoveNext(UnsafeHashMapData* data, ref int bucketIndex, ref int nextIndex, out int index)
  231. {
  232. var bucketArray = (int*)data->buckets;
  233. var bucketNext = (int*)data->next;
  234. var capacityMask = data->bucketCapacityMask;
  235. if (nextIndex != -1)
  236. {
  237. index = nextIndex;
  238. nextIndex = bucketNext[nextIndex];
  239. return true;
  240. }
  241. for (int i = bucketIndex; i <= capacityMask; ++i)
  242. {
  243. var idx = bucketArray[i];
  244. if (idx != -1)
  245. {
  246. index = idx;
  247. bucketIndex = i + 1;
  248. nextIndex = bucketNext[idx];
  249. return true;
  250. }
  251. }
  252. index = -1;
  253. bucketIndex = capacityMask + 1;
  254. nextIndex = -1;
  255. return false;
  256. }
  257. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
  258. internal static void GetKeyArray<TKey>(UnsafeHashMapData* data, NativeArray<TKey> result)
  259. where TKey : struct
  260. {
  261. var bucketArray = (int*)data->buckets;
  262. var bucketNext = (int*)data->next;
  263. for (int i = 0, count = 0, max = result.Length; i <= data->bucketCapacityMask && count < max; ++i)
  264. {
  265. int bucket = bucketArray[i];
  266. while (bucket != -1)
  267. {
  268. result[count++] = UnsafeUtility.ReadArrayElement<TKey>(data->keys, bucket);
  269. bucket = bucketNext[bucket];
  270. }
  271. }
  272. }
  273. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
  274. internal static void GetValueArray<TValue>(UnsafeHashMapData* data, NativeArray<TValue> result)
  275. where TValue : struct
  276. {
  277. var bucketArray = (int*)data->buckets;
  278. var bucketNext = (int*)data->next;
  279. for (int i = 0, count = 0, max = result.Length, capacityMask = data->bucketCapacityMask
  280. ; i <= capacityMask && count < max
  281. ; ++i
  282. )
  283. {
  284. int bucket = bucketArray[i];
  285. while (bucket != -1)
  286. {
  287. result[count++] = UnsafeUtility.ReadArrayElement<TValue>(data->values, bucket);
  288. bucket = bucketNext[bucket];
  289. }
  290. }
  291. }
  292. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  293. internal static void GetKeyValueArrays<TKey, TValue>(UnsafeHashMapData* data, NativeKeyValueArrays<TKey, TValue> result)
  294. where TKey : struct
  295. where TValue : struct
  296. {
  297. var bucketArray = (int*)data->buckets;
  298. var bucketNext = (int*)data->next;
  299. for (int i = 0, count = 0, max = result.Length, capacityMask = data->bucketCapacityMask
  300. ; i <= capacityMask && count < max
  301. ; ++i
  302. )
  303. {
  304. int bucket = bucketArray[i];
  305. while (bucket != -1)
  306. {
  307. result.Keys[count] = UnsafeUtility.ReadArrayElement<TKey>(data->keys, bucket);
  308. result.Values[count] = UnsafeUtility.ReadArrayElement<TValue>(data->values, bucket);
  309. count++;
  310. bucket = bucketNext[bucket];
  311. }
  312. }
  313. }
  314. internal UnsafeHashMapBucketData GetBucketData()
  315. {
  316. return new UnsafeHashMapBucketData(values, keys, next, buckets, bucketCapacityMask);
  317. }
  318. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  319. static void CheckHashMapReallocateDoesNotShrink(UnsafeHashMapData* data, int newCapacity)
  320. {
  321. if (data->keyCapacity > newCapacity)
  322. throw new Exception("Shrinking a hash map is not supported");
  323. }
  324. }
  325. [NativeContainer]
  326. [BurstCompatible]
  327. internal unsafe struct UnsafeHashMapDataDispose
  328. {
  329. [NativeDisableUnsafePtrRestriction]
  330. internal UnsafeHashMapData* m_Buffer;
  331. internal AllocatorManager.AllocatorHandle m_AllocatorLabel;
  332. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  333. internal AtomicSafetyHandle m_Safety;
  334. #endif
  335. public void Dispose()
  336. {
  337. UnsafeHashMapData.DeallocateHashMap(m_Buffer, m_AllocatorLabel);
  338. }
  339. }
  340. [BurstCompile]
  341. internal unsafe struct UnsafeHashMapDataDisposeJob : IJob
  342. {
  343. internal UnsafeHashMapDataDispose Data;
  344. public void Execute()
  345. {
  346. Data.Dispose();
  347. }
  348. }
  349. [StructLayout(LayoutKind.Sequential)]
  350. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  351. internal struct UnsafeHashMapBase<TKey, TValue>
  352. where TKey : struct, IEquatable<TKey>
  353. where TValue : struct
  354. {
  355. internal static unsafe void Clear(UnsafeHashMapData* data)
  356. {
  357. UnsafeUtility.MemSet(data->buckets, 0xff, (data->bucketCapacityMask + 1) * 4);
  358. UnsafeUtility.MemSet(data->next, 0xff, (data->keyCapacity) * 4);
  359. for (int tls = 0; tls < JobsUtility.MaxJobThreadCount; ++tls)
  360. {
  361. data->firstFreeTLS[tls * UnsafeHashMapData.IntsPerCacheLine] = -1;
  362. }
  363. data->allocatedIndexLength = 0;
  364. }
  365. internal static unsafe int AllocEntry(UnsafeHashMapData* data, int threadIndex)
  366. {
  367. int idx;
  368. int* nextPtrs = (int*)data->next;
  369. do
  370. {
  371. idx = data->firstFreeTLS[threadIndex * UnsafeHashMapData.IntsPerCacheLine];
  372. // Check if this thread has a free entry. Negative value means there is nothing free.
  373. if (idx < 0)
  374. {
  375. // Try to refill local cache. The local cache is a linked list of 16 free entries.
  376. // Indicate to other threads that we are refilling the cache.
  377. // -2 means refilling cache.
  378. // -1 means nothing free on this thread.
  379. Interlocked.Exchange(ref data->firstFreeTLS[threadIndex * UnsafeHashMapData.IntsPerCacheLine], -2);
  380. // If it failed try to get one from the never-allocated array
  381. if (data->allocatedIndexLength < data->keyCapacity)
  382. {
  383. idx = Interlocked.Add(ref data->allocatedIndexLength, 16) - 16;
  384. if (idx < data->keyCapacity - 1)
  385. {
  386. int count = math.min(16, data->keyCapacity - idx);
  387. // Set up a linked list of free entries.
  388. for (int i = 1; i < count; ++i)
  389. {
  390. nextPtrs[idx + i] = idx + i + 1;
  391. }
  392. // Last entry points to null.
  393. nextPtrs[idx + count - 1] = -1;
  394. // The first entry is going to be allocated to someone so it also points to null.
  395. nextPtrs[idx] = -1;
  396. // Set the TLS first free to the head of the list, which is the one after the entry we are returning.
  397. Interlocked.Exchange(ref data->firstFreeTLS[threadIndex * UnsafeHashMapData.IntsPerCacheLine], idx + 1);
  398. return idx;
  399. }
  400. if (idx == data->keyCapacity - 1)
  401. {
  402. // We tried to allocate more entries for this thread but we've already hit the key capacity,
  403. // so we are in fact out of space. Record that this thread has no more entries.
  404. Interlocked.Exchange(ref data->firstFreeTLS[threadIndex * UnsafeHashMapData.IntsPerCacheLine], -1);
  405. return idx;
  406. }
  407. }
  408. // If we reach here, then we couldn't allocate more entries for this thread, so it's completely empty.
  409. Interlocked.Exchange(ref data->firstFreeTLS[threadIndex * UnsafeHashMapData.IntsPerCacheLine], -1);
  410. // Failed to get any, try to get one from another free list
  411. bool again = true;
  412. while (again)
  413. {
  414. again = false;
  415. for (int other = (threadIndex + 1) % JobsUtility.MaxJobThreadCount
  416. ; other != threadIndex
  417. ; other = (other + 1) % JobsUtility.MaxJobThreadCount
  418. )
  419. {
  420. // Attempt to grab a free entry from another thread and switch the other thread's free head
  421. // atomically.
  422. do
  423. {
  424. idx = data->firstFreeTLS[other * UnsafeHashMapData.IntsPerCacheLine];
  425. if (idx < 0)
  426. {
  427. break;
  428. }
  429. }
  430. while (Interlocked.CompareExchange(
  431. ref data->firstFreeTLS[other * UnsafeHashMapData.IntsPerCacheLine]
  432. , nextPtrs[idx]
  433. , idx
  434. ) != idx
  435. );
  436. if (idx == -2)
  437. {
  438. // If the thread was refilling the cache, then try again.
  439. again = true;
  440. }
  441. else if (idx >= 0)
  442. {
  443. // We succeeded in getting an entry from another thread so remove this entry from the
  444. // linked list.
  445. nextPtrs[idx] = -1;
  446. return idx;
  447. }
  448. }
  449. }
  450. ThrowFull();
  451. }
  452. CheckOutOfCapacity(idx, data->keyCapacity);
  453. }
  454. while (Interlocked.CompareExchange(
  455. ref data->firstFreeTLS[threadIndex * UnsafeHashMapData.IntsPerCacheLine]
  456. , nextPtrs[idx]
  457. , idx
  458. ) != idx
  459. );
  460. nextPtrs[idx] = -1;
  461. return idx;
  462. }
  463. internal static unsafe void FreeEntry(UnsafeHashMapData* data, int idx, int threadIndex)
  464. {
  465. int* nextPtrs = (int*)data->next;
  466. int next = -1;
  467. do
  468. {
  469. next = data->firstFreeTLS[threadIndex * UnsafeHashMapData.IntsPerCacheLine];
  470. nextPtrs[idx] = next;
  471. }
  472. while (Interlocked.CompareExchange(
  473. ref data->firstFreeTLS[threadIndex * UnsafeHashMapData.IntsPerCacheLine]
  474. , idx
  475. , next
  476. ) != next
  477. );
  478. }
  479. internal static unsafe bool TryAddAtomic(UnsafeHashMapData* data, TKey key, TValue item, int threadIndex)
  480. {
  481. TValue tempItem;
  482. NativeMultiHashMapIterator<TKey> tempIt;
  483. if (TryGetFirstValueAtomic(data, key, out tempItem, out tempIt))
  484. {
  485. return false;
  486. }
  487. // Allocate an entry from the free list
  488. int idx = AllocEntry(data, threadIndex);
  489. // Write the new value to the entry
  490. UnsafeUtility.WriteArrayElement(data->keys, idx, key);
  491. UnsafeUtility.WriteArrayElement(data->values, idx, item);
  492. int bucket = key.GetHashCode() & data->bucketCapacityMask;
  493. // Add the index to the hash-map
  494. int* buckets = (int*)data->buckets;
  495. // Make the bucket's head idx. If the exchange returns something other than -1, then the bucket had
  496. // a non-null head which means we need to do more checks...
  497. if (Interlocked.CompareExchange(ref buckets[bucket], idx, -1) != -1)
  498. {
  499. int* nextPtrs = (int*)data->next;
  500. int next = -1;
  501. do
  502. {
  503. // Link up this entry with the rest of the bucket under the assumption that this key
  504. // doesn't already exist in the bucket. This assumption could be wrong, which will be
  505. // checked later.
  506. next = buckets[bucket];
  507. nextPtrs[idx] = next;
  508. // If the key already exists then we should free the entry we took earlier.
  509. if (TryGetFirstValueAtomic(data, key, out tempItem, out tempIt))
  510. {
  511. // Put back the entry in the free list if someone else added it while trying to add
  512. FreeEntry(data, idx, threadIndex);
  513. return false;
  514. }
  515. }
  516. while (Interlocked.CompareExchange(ref buckets[bucket], idx, next) != next);
  517. }
  518. return true;
  519. }
  520. internal static unsafe void AddAtomicMulti(UnsafeHashMapData* data, TKey key, TValue item, int threadIndex)
  521. {
  522. // Allocate an entry from the free list
  523. int idx = AllocEntry(data, threadIndex);
  524. // Write the new value to the entry
  525. UnsafeUtility.WriteArrayElement(data->keys, idx, key);
  526. UnsafeUtility.WriteArrayElement(data->values, idx, item);
  527. int bucket = key.GetHashCode() & data->bucketCapacityMask;
  528. // Add the index to the hash-map
  529. int* buckets = (int*)data->buckets;
  530. int nextPtr;
  531. int* nextPtrs = (int*)data->next;
  532. do
  533. {
  534. nextPtr = buckets[bucket];
  535. nextPtrs[idx] = nextPtr;
  536. }
  537. while (Interlocked.CompareExchange(ref buckets[bucket], idx, nextPtr) != nextPtr);
  538. }
  539. internal static unsafe bool TryAdd(UnsafeHashMapData* data, TKey key, TValue item, bool isMultiHashMap, AllocatorManager.AllocatorHandle allocation)
  540. {
  541. TValue tempItem;
  542. NativeMultiHashMapIterator<TKey> tempIt;
  543. if (!isMultiHashMap && TryGetFirstValueAtomic(data, key, out tempItem, out tempIt))
  544. {
  545. return false;
  546. }
  547. // Allocate an entry from the free list
  548. int idx;
  549. int* nextPtrs;
  550. if (data->allocatedIndexLength >= data->keyCapacity && data->firstFreeTLS[0] < 0)
  551. {
  552. for (int tls = 1; tls < JobsUtility.MaxJobThreadCount; ++tls)
  553. {
  554. if (data->firstFreeTLS[tls * UnsafeHashMapData.IntsPerCacheLine] >= 0)
  555. {
  556. idx = data->firstFreeTLS[tls * UnsafeHashMapData.IntsPerCacheLine];
  557. nextPtrs = (int*)data->next;
  558. data->firstFreeTLS[tls * UnsafeHashMapData.IntsPerCacheLine] = nextPtrs[idx];
  559. nextPtrs[idx] = -1;
  560. data->firstFreeTLS[0] = idx;
  561. break;
  562. }
  563. }
  564. if (data->firstFreeTLS[0] < 0)
  565. {
  566. int newCap = UnsafeHashMapData.GrowCapacity(data->keyCapacity);
  567. UnsafeHashMapData.ReallocateHashMap<TKey, TValue>(data, newCap, UnsafeHashMapData.GetBucketSize(newCap), allocation);
  568. }
  569. }
  570. idx = data->firstFreeTLS[0];
  571. if (idx >= 0)
  572. {
  573. data->firstFreeTLS[0] = ((int*)data->next)[idx];
  574. }
  575. else
  576. {
  577. idx = data->allocatedIndexLength++;
  578. }
  579. CheckIndexOutOfBounds(data, idx);
  580. // Write the new value to the entry
  581. UnsafeUtility.WriteArrayElement(data->keys, idx, key);
  582. UnsafeUtility.WriteArrayElement(data->values, idx, item);
  583. int bucket = key.GetHashCode() & data->bucketCapacityMask;
  584. // Add the index to the hash-map
  585. int* buckets = (int*)data->buckets;
  586. nextPtrs = (int*)data->next;
  587. nextPtrs[idx] = buckets[bucket];
  588. buckets[bucket] = idx;
  589. return true;
  590. }
  591. internal static unsafe int Remove(UnsafeHashMapData* data, TKey key, bool isMultiHashMap)
  592. {
  593. if (data->keyCapacity == 0)
  594. {
  595. return 0;
  596. }
  597. var removed = 0;
  598. // First find the slot based on the hash
  599. var buckets = (int*)data->buckets;
  600. var nextPtrs = (int*)data->next;
  601. var bucket = key.GetHashCode() & data->bucketCapacityMask;
  602. var prevEntry = -1;
  603. var entryIdx = buckets[bucket];
  604. while (entryIdx >= 0 && entryIdx < data->keyCapacity)
  605. {
  606. if (UnsafeUtility.ReadArrayElement<TKey>(data->keys, entryIdx).Equals(key))
  607. {
  608. ++removed;
  609. // Found matching element, remove it
  610. if (prevEntry < 0)
  611. {
  612. buckets[bucket] = nextPtrs[entryIdx];
  613. }
  614. else
  615. {
  616. nextPtrs[prevEntry] = nextPtrs[entryIdx];
  617. }
  618. // And free the index
  619. int nextIdx = nextPtrs[entryIdx];
  620. nextPtrs[entryIdx] = data->firstFreeTLS[0];
  621. data->firstFreeTLS[0] = entryIdx;
  622. entryIdx = nextIdx;
  623. // Can only be one hit in regular hashmaps, so return
  624. if (!isMultiHashMap)
  625. {
  626. break;
  627. }
  628. }
  629. else
  630. {
  631. prevEntry = entryIdx;
  632. entryIdx = nextPtrs[entryIdx];
  633. }
  634. }
  635. return removed;
  636. }
  637. internal static unsafe void Remove(UnsafeHashMapData* data, NativeMultiHashMapIterator<TKey> it)
  638. {
  639. // First find the slot based on the hash
  640. int* buckets = (int*)data->buckets;
  641. int* nextPtrs = (int*)data->next;
  642. int bucket = it.key.GetHashCode() & data->bucketCapacityMask;
  643. int entryIdx = buckets[bucket];
  644. if (entryIdx == it.EntryIndex)
  645. {
  646. buckets[bucket] = nextPtrs[entryIdx];
  647. }
  648. else
  649. {
  650. while (entryIdx >= 0 && nextPtrs[entryIdx] != it.EntryIndex)
  651. {
  652. entryIdx = nextPtrs[entryIdx];
  653. }
  654. if (entryIdx < 0)
  655. {
  656. ThrowInvalidIterator();
  657. }
  658. nextPtrs[entryIdx] = nextPtrs[it.EntryIndex];
  659. }
  660. // And free the index
  661. nextPtrs[it.EntryIndex] = data->firstFreeTLS[0];
  662. data->firstFreeTLS[0] = it.EntryIndex;
  663. }
  664. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
  665. internal static unsafe void RemoveKeyValue<TValueEQ>(UnsafeHashMapData* data, TKey key, TValueEQ value)
  666. where TValueEQ : struct, IEquatable<TValueEQ>
  667. {
  668. if (data->keyCapacity == 0)
  669. {
  670. return;
  671. }
  672. var buckets = (int*)data->buckets;
  673. var keyCapacity = (uint)data->keyCapacity;
  674. var prevNextPtr = buckets + (key.GetHashCode() & data->bucketCapacityMask);
  675. var entryIdx = *prevNextPtr;
  676. if ((uint)entryIdx >= keyCapacity)
  677. {
  678. return;
  679. }
  680. var nextPtrs = (int*)data->next;
  681. var keys = data->keys;
  682. var values = data->values;
  683. var firstFreeTLS = data->firstFreeTLS;
  684. do
  685. {
  686. if (UnsafeUtility.ReadArrayElement<TKey>(keys, entryIdx).Equals(key)
  687. && UnsafeUtility.ReadArrayElement<TValueEQ>(values, entryIdx).Equals(value))
  688. {
  689. int nextIdx = nextPtrs[entryIdx];
  690. nextPtrs[entryIdx] = firstFreeTLS[0];
  691. firstFreeTLS[0] = entryIdx;
  692. *prevNextPtr = entryIdx = nextIdx;
  693. }
  694. else
  695. {
  696. prevNextPtr = nextPtrs + entryIdx;
  697. entryIdx = *prevNextPtr;
  698. }
  699. }
  700. while ((uint)entryIdx < keyCapacity);
  701. }
  702. internal static unsafe bool TryGetFirstValueAtomic(UnsafeHashMapData* data, TKey key, out TValue item, out NativeMultiHashMapIterator<TKey> it)
  703. {
  704. it.key = key;
  705. if (data->allocatedIndexLength <= 0)
  706. {
  707. it.EntryIndex = it.NextEntryIndex = -1;
  708. item = default;
  709. return false;
  710. }
  711. // First find the slot based on the hash
  712. int* buckets = (int*)data->buckets;
  713. int bucket = key.GetHashCode() & data->bucketCapacityMask;
  714. it.EntryIndex = it.NextEntryIndex = buckets[bucket];
  715. return TryGetNextValueAtomic(data, out item, ref it);
  716. }
  717. internal static unsafe bool TryGetNextValueAtomic(UnsafeHashMapData* data, out TValue item, ref NativeMultiHashMapIterator<TKey> it)
  718. {
  719. int entryIdx = it.NextEntryIndex;
  720. it.NextEntryIndex = -1;
  721. it.EntryIndex = -1;
  722. item = default;
  723. if (entryIdx < 0 || entryIdx >= data->keyCapacity)
  724. {
  725. return false;
  726. }
  727. int* nextPtrs = (int*)data->next;
  728. while (!UnsafeUtility.ReadArrayElement<TKey>(data->keys, entryIdx).Equals(it.key))
  729. {
  730. entryIdx = nextPtrs[entryIdx];
  731. if (entryIdx < 0 || entryIdx >= data->keyCapacity)
  732. {
  733. return false;
  734. }
  735. }
  736. it.NextEntryIndex = nextPtrs[entryIdx];
  737. it.EntryIndex = entryIdx;
  738. // Read the value
  739. item = UnsafeUtility.ReadArrayElement<TValue>(data->values, entryIdx);
  740. return true;
  741. }
  742. internal static unsafe bool SetValue(UnsafeHashMapData* data, ref NativeMultiHashMapIterator<TKey> it, ref TValue item)
  743. {
  744. int entryIdx = it.EntryIndex;
  745. if (entryIdx < 0 || entryIdx >= data->keyCapacity)
  746. {
  747. return false;
  748. }
  749. UnsafeUtility.WriteArrayElement(data->values, entryIdx, item);
  750. return true;
  751. }
  752. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  753. static void CheckOutOfCapacity(int idx, int keyCapacity)
  754. {
  755. if (idx >= keyCapacity)
  756. {
  757. throw new InvalidOperationException(string.Format("nextPtr idx {0} beyond capacity {1}", idx, keyCapacity));
  758. }
  759. }
  760. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  761. static unsafe void CheckIndexOutOfBounds(UnsafeHashMapData* data, int idx)
  762. {
  763. if (idx < 0 || idx >= data->keyCapacity)
  764. throw new InvalidOperationException("Internal HashMap error");
  765. }
  766. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  767. static void ThrowFull()
  768. {
  769. throw new InvalidOperationException("HashMap is full");
  770. }
  771. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  772. static void ThrowInvalidIterator()
  773. {
  774. throw new InvalidOperationException("Invalid iterator passed to HashMap remove");
  775. }
  776. }
  777. /// <summary>
  778. /// A key-value pair.
  779. /// </summary>
  780. /// <remarks>Used for enumerators.</remarks>
  781. /// <typeparam name="TKey">The type of the keys.</typeparam>
  782. /// <typeparam name="TValue">The type of the values.</typeparam>
  783. [DebuggerDisplay("Key = {Key}, Value = {Value}")]
  784. [BurstCompatible(GenericTypeArguments = new[] {typeof(int), typeof(int)})]
  785. public unsafe struct KeyValue<TKey, TValue>
  786. where TKey : struct, IEquatable<TKey>
  787. where TValue : struct
  788. {
  789. internal UnsafeHashMapData* m_Buffer;
  790. internal int m_Index;
  791. internal int m_Next;
  792. /// <summary>
  793. /// An invalid KeyValue.
  794. /// </summary>
  795. /// <value>In a hash map enumerator's initial state, its <see cref="UnsafeHashMap{TKey,TValue}.Enumerator.Current"/> value is Null.</value>
  796. public static KeyValue<TKey, TValue> Null => new KeyValue<TKey, TValue>{m_Index = -1};
  797. /// <summary>
  798. /// The key.
  799. /// </summary>
  800. /// <value>The key. If this KeyValue is Null, returns the default of TKey.</value>
  801. public TKey Key
  802. {
  803. get
  804. {
  805. if (m_Index != -1)
  806. {
  807. return UnsafeUtility.ReadArrayElement<TKey>(m_Buffer->keys, m_Index);
  808. }
  809. return default;
  810. }
  811. }
  812. /// <summary>
  813. /// Value of key/value pair.
  814. /// </summary>
  815. public ref TValue Value
  816. {
  817. get
  818. {
  819. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  820. if (m_Index == -1)
  821. throw new ArgumentException("must be valid");
  822. #endif
  823. return ref UnsafeUtility.AsRef<TValue>(m_Buffer->values + UnsafeUtility.SizeOf<TValue>() * m_Index);
  824. }
  825. }
  826. /// <summary>
  827. /// Gets the key and the value.
  828. /// </summary>
  829. /// <param name="key">Outputs the key. If this KeyValue is Null, outputs the default of TKey.</param>
  830. /// <param name="value">Outputs the value. If this KeyValue is Null, outputs the default of TValue.</param>
  831. /// <returns>True if the key-value pair is valid.</returns>
  832. public bool GetKeyValue(out TKey key, out TValue value)
  833. {
  834. if (m_Index != -1)
  835. {
  836. key = UnsafeUtility.ReadArrayElement<TKey>(m_Buffer->keys, m_Index);
  837. value = UnsafeUtility.ReadArrayElement<TValue>(m_Buffer->values, m_Index);
  838. return true;
  839. }
  840. key = default;
  841. value = default;
  842. return false;
  843. }
  844. }
  845. internal unsafe struct UnsafeHashMapDataEnumerator
  846. {
  847. [NativeDisableUnsafePtrRestriction]
  848. internal UnsafeHashMapData* m_Buffer;
  849. internal int m_Index;
  850. internal int m_BucketIndex;
  851. internal int m_NextIndex;
  852. internal unsafe UnsafeHashMapDataEnumerator(UnsafeHashMapData* data)
  853. {
  854. m_Buffer = data;
  855. m_Index = -1;
  856. m_BucketIndex = 0;
  857. m_NextIndex = -1;
  858. }
  859. internal bool MoveNext()
  860. {
  861. return UnsafeHashMapData.MoveNext(m_Buffer, ref m_BucketIndex, ref m_NextIndex, out m_Index);
  862. }
  863. internal void Reset()
  864. {
  865. m_Index = -1;
  866. m_BucketIndex = 0;
  867. m_NextIndex = -1;
  868. }
  869. internal KeyValue<TKey, TValue> GetCurrent<TKey, TValue>()
  870. where TKey : struct, IEquatable<TKey>
  871. where TValue : struct
  872. {
  873. return new KeyValue<TKey, TValue> { m_Buffer = m_Buffer, m_Index = m_Index };
  874. }
  875. internal TKey GetCurrentKey<TKey>()
  876. where TKey : struct, IEquatable<TKey>
  877. {
  878. if (m_Index != -1)
  879. {
  880. return UnsafeUtility.ReadArrayElement<TKey>(m_Buffer->keys, m_Index);
  881. }
  882. return default;
  883. }
  884. }
  885. /// <summary>
  886. /// An unordered, expandable associative array.
  887. /// </summary>
  888. /// <typeparam name="TKey">The type of the keys.</typeparam>
  889. /// <typeparam name="TValue">The type of the values.</typeparam>
  890. [StructLayout(LayoutKind.Sequential)]
  891. [DebuggerDisplay("Count = {Count()}, Capacity = {Capacity}, IsCreated = {IsCreated}, IsEmpty = {IsEmpty}")]
  892. [DebuggerTypeProxy(typeof(UnsafeHashMapDebuggerTypeProxy<,>))]
  893. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  894. public unsafe struct UnsafeHashMap<TKey, TValue>
  895. : INativeDisposable
  896. , IEnumerable<KeyValue<TKey, TValue>> // Used by collection initializers.
  897. where TKey : struct, IEquatable<TKey>
  898. where TValue : struct
  899. {
  900. [NativeDisableUnsafePtrRestriction]
  901. internal UnsafeHashMapData* m_Buffer;
  902. internal AllocatorManager.AllocatorHandle m_AllocatorLabel;
  903. /// <summary>
  904. /// Initializes and returns an instance of UnsafeHashMap.
  905. /// </summary>
  906. /// <param name="capacity">The number of key-value pairs that should fit in the initial allocation.</param>
  907. /// <param name="allocator">The allocator to use.</param>
  908. public UnsafeHashMap(int capacity, AllocatorManager.AllocatorHandle allocator)
  909. {
  910. CollectionHelper.CheckIsUnmanaged<TKey>();
  911. CollectionHelper.CheckIsUnmanaged<TValue>();
  912. m_AllocatorLabel = allocator;
  913. // Bucket size if bigger to reduce collisions
  914. UnsafeHashMapData.AllocateHashMap<TKey, TValue>(capacity, capacity * 2, allocator, out m_Buffer);
  915. Clear();
  916. }
  917. /// <summary>
  918. /// Whether this hash map is empty.
  919. /// </summary>
  920. /// <value>True if this hash map is empty or the hash map has not been constructed.</value>
  921. public bool IsEmpty => !IsCreated || UnsafeHashMapData.IsEmpty(m_Buffer);
  922. /// <summary>
  923. /// The current number of key-value pairs in this hash map.
  924. /// </summary>
  925. /// <returns>The current number of key-value pairs in this hash map.</returns>
  926. public int Count() => UnsafeHashMapData.GetCount(m_Buffer);
  927. /// <summary>
  928. /// The number of key-value pairs that fit in the current allocation.
  929. /// </summary>
  930. /// <value>The number of key-value pairs that fit in the current allocation.</value>
  931. /// <param name="value">A new capacity. Must be larger than the current capacity.</param>
  932. /// <exception cref="Exception">Thrown if `value` is less than the current capacity.</exception>
  933. public int Capacity
  934. {
  935. get
  936. {
  937. UnsafeHashMapData* data = m_Buffer;
  938. return data->keyCapacity;
  939. }
  940. set
  941. {
  942. UnsafeHashMapData* data = m_Buffer;
  943. UnsafeHashMapData.ReallocateHashMap<TKey, TValue>(data, value, UnsafeHashMapData.GetBucketSize(value), m_AllocatorLabel);
  944. }
  945. }
  946. /// <summary>
  947. /// Removes all key-value pairs.
  948. /// </summary>
  949. /// <remarks>Does not change the capacity.</remarks>
  950. public void Clear()
  951. {
  952. UnsafeHashMapBase<TKey, TValue>.Clear(m_Buffer);
  953. }
  954. /// <summary>
  955. /// Adds a new key-value pair.
  956. /// </summary>
  957. /// <remarks>If the key is already present, this method returns false without modifying the hash map.</remarks>
  958. /// <param name="key">The key to add.</param>
  959. /// <param name="item">The value to add.</param>
  960. /// <returns>True if the key-value pair was added.</returns>
  961. public bool TryAdd(TKey key, TValue item)
  962. {
  963. return UnsafeHashMapBase<TKey, TValue>.TryAdd(m_Buffer, key, item, false, m_AllocatorLabel);
  964. }
  965. /// <summary>
  966. /// Adds a new key-value pair.
  967. /// </summary>
  968. /// <remarks>If the key is already present, this method throws without modifying the hash map.</remarks>
  969. /// <param name="key">The key to add.</param>
  970. /// <param name="item">The value to add.</param>
  971. /// <exception cref="ArgumentException">Thrown if the key was already present.</exception>
  972. public void Add(TKey key, TValue item)
  973. {
  974. TryAdd(key, item);
  975. }
  976. /// <summary>
  977. /// Removes a key-value pair.
  978. /// </summary>
  979. /// <param name="key">The key to remove.</param>
  980. /// <returns>True if a key-value pair was removed.</returns>
  981. public bool Remove(TKey key)
  982. {
  983. return UnsafeHashMapBase<TKey, TValue>.Remove(m_Buffer, key, false) != 0;
  984. }
  985. /// <summary>
  986. /// Returns the value associated with a key.
  987. /// </summary>
  988. /// <param name="key">The key to look up.</param>
  989. /// <param name="item">Outputs the value associated with the key. Outputs default if the key was not present.</param>
  990. /// <returns>True if the key was present.</returns>
  991. public bool TryGetValue(TKey key, out TValue item)
  992. {
  993. NativeMultiHashMapIterator<TKey> tempIt;
  994. return UnsafeHashMapBase<TKey, TValue>.TryGetFirstValueAtomic(m_Buffer, key, out item, out tempIt);
  995. }
  996. /// <summary>
  997. /// Returns true if a given key is present in this hash map.
  998. /// </summary>
  999. /// <param name="key">The key to look up.</param>
  1000. /// <returns>True if the key was present.</returns>
  1001. public bool ContainsKey(TKey key)
  1002. {
  1003. return UnsafeHashMapBase<TKey, TValue>.TryGetFirstValueAtomic(m_Buffer, key, out var tempValue, out var tempIt);
  1004. }
  1005. /// <summary>
  1006. /// Gets and sets values by key.
  1007. /// </summary>
  1008. /// <remarks>Getting a key that is not present will throw. Setting a key that is not already present will add the key.</remarks>
  1009. /// <param name="key">The key to look up.</param>
  1010. /// <value>The value associated with the key.</value>
  1011. /// <exception cref="ArgumentException">For getting, thrown if the key was not present.</exception>
  1012. public TValue this[TKey key]
  1013. {
  1014. get
  1015. {
  1016. TValue res;
  1017. TryGetValue(key, out res);
  1018. return res;
  1019. }
  1020. set
  1021. {
  1022. if (UnsafeHashMapBase<TKey, TValue>.TryGetFirstValueAtomic(m_Buffer, key, out var item, out var iterator))
  1023. {
  1024. UnsafeHashMapBase<TKey, TValue>.SetValue(m_Buffer, ref iterator, ref value);
  1025. }
  1026. else
  1027. {
  1028. UnsafeHashMapBase<TKey, TValue>.TryAdd(m_Buffer, key, value, false, m_AllocatorLabel);
  1029. }
  1030. }
  1031. }
  1032. /// <summary>
  1033. /// Whether this hash map has been allocated (and not yet deallocated).
  1034. /// </summary>
  1035. /// <value>True if this hash map has been allocated (and not yet deallocated).</value>
  1036. public bool IsCreated => m_Buffer != null;
  1037. /// <summary>
  1038. /// Releases all resources (memory).
  1039. /// </summary>
  1040. public void Dispose()
  1041. {
  1042. UnsafeHashMapData.DeallocateHashMap(m_Buffer, m_AllocatorLabel);
  1043. m_Buffer = null;
  1044. }
  1045. /// <summary>
  1046. /// Creates and schedules a job that will dispose this hash map.
  1047. /// </summary>
  1048. /// <param name="inputDeps">A job handle. The newly scheduled job will depend upon this handle.</param>
  1049. /// <returns>The handle of a new job that will dispose this hash map.</returns>
  1050. [NotBurstCompatible /* This is not burst compatible because of IJob's use of a static IntPtr. Should switch to IJobBurstSchedulable in the future */]
  1051. public JobHandle Dispose(JobHandle inputDeps)
  1052. {
  1053. var jobHandle = new UnsafeHashMapDisposeJob { Data = m_Buffer, Allocator = m_AllocatorLabel }.Schedule(inputDeps);
  1054. m_Buffer = null;
  1055. return jobHandle;
  1056. }
  1057. /// <summary>
  1058. /// Returns an array with a copy of all this hash map's keys (in no particular order).
  1059. /// </summary>
  1060. /// <param name="allocator">The allocator to use.</param>
  1061. /// <returns>An array with a copy of all this hash map's keys (in no particular order).</returns>
  1062. public NativeArray<TKey> GetKeyArray(AllocatorManager.AllocatorHandle allocator)
  1063. {
  1064. var result = CollectionHelper.CreateNativeArray<TKey>(UnsafeHashMapData.GetCount(m_Buffer), allocator, NativeArrayOptions.UninitializedMemory);
  1065. UnsafeHashMapData.GetKeyArray(m_Buffer, result);
  1066. return result;
  1067. }
  1068. /// <summary>
  1069. /// Returns an array with a copy of all this hash map's values (in no particular order).
  1070. /// </summary>
  1071. /// <param name="allocator">The allocator to use.</param>
  1072. /// <returns>An array with a copy of all this hash map's values (in no particular order).</returns>
  1073. public NativeArray<TValue> GetValueArray(AllocatorManager.AllocatorHandle allocator)
  1074. {
  1075. var result = CollectionHelper.CreateNativeArray<TValue>(UnsafeHashMapData.GetCount(m_Buffer), allocator, NativeArrayOptions.UninitializedMemory);
  1076. UnsafeHashMapData.GetValueArray(m_Buffer, result);
  1077. return result;
  1078. }
  1079. /// <summary>
  1080. /// Returns a NativeKeyValueArrays with a copy of all this hash map's keys and values.
  1081. /// </summary>
  1082. /// <remarks>The key-value pairs are copied in no particular order. For all `i`, `Values[i]` will be the value associated with `Keys[i]`.</remarks>
  1083. /// <param name="allocator">The allocator to use.</param>
  1084. /// <returns>A NativeKeyValueArrays with a copy of all this hash map's keys and values.</returns>
  1085. public NativeKeyValueArrays<TKey, TValue> GetKeyValueArrays(AllocatorManager.AllocatorHandle allocator)
  1086. {
  1087. var result = new NativeKeyValueArrays<TKey, TValue>(UnsafeHashMapData.GetCount(m_Buffer), allocator, NativeArrayOptions.UninitializedMemory);
  1088. UnsafeHashMapData.GetKeyValueArrays(m_Buffer, result);
  1089. return result;
  1090. }
  1091. /// <summary>
  1092. /// Returns a parallel writer for this hash map.
  1093. /// </summary>
  1094. /// <returns>A parallel writer for this hash map.</returns>
  1095. public ParallelWriter AsParallelWriter()
  1096. {
  1097. ParallelWriter writer;
  1098. writer.m_ThreadIndex = 0;
  1099. writer.m_Buffer = m_Buffer;
  1100. return writer;
  1101. }
  1102. /// <summary>
  1103. /// A parallel writer for a NativeHashMap.
  1104. /// </summary>
  1105. /// <remarks>
  1106. /// Use <see cref="AsParallelWriter"/> to create a parallel writer for a NativeHashMap.
  1107. /// </remarks>
  1108. [NativeContainerIsAtomicWriteOnly]
  1109. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  1110. public unsafe struct ParallelWriter
  1111. {
  1112. [NativeDisableUnsafePtrRestriction]
  1113. internal UnsafeHashMapData* m_Buffer;
  1114. [NativeSetThreadIndex]
  1115. internal int m_ThreadIndex;
  1116. /// <summary>
  1117. /// The number of key-value pairs that fit in the current allocation.
  1118. /// </summary>
  1119. /// <value>The number of key-value pairs that fit in the current allocation.</value>
  1120. public int Capacity
  1121. {
  1122. get
  1123. {
  1124. UnsafeHashMapData* data = m_Buffer;
  1125. return data->keyCapacity;
  1126. }
  1127. }
  1128. /// <summary>
  1129. /// Adds a new key-value pair.
  1130. /// </summary>
  1131. /// <remarks>If the key is already present, this method returns false without modifying the hash map.</remarks>
  1132. /// <param name="key">The key to add.</param>
  1133. /// <param name="item">The value to add.</param>
  1134. /// <returns>True if the key-value pair was added.</returns>
  1135. public bool TryAdd(TKey key, TValue item)
  1136. {
  1137. Assert.IsTrue(m_ThreadIndex >= 0);
  1138. return UnsafeHashMapBase<TKey, TValue>.TryAddAtomic(m_Buffer, key, item, m_ThreadIndex);
  1139. }
  1140. }
  1141. /// <summary>
  1142. /// Returns an enumerator over the key-value pairs of this hash map.
  1143. /// </summary>
  1144. /// <returns>An enumerator over the key-value pairs of this hash map.</returns>
  1145. public Enumerator GetEnumerator()
  1146. {
  1147. return new Enumerator { m_Enumerator = new UnsafeHashMapDataEnumerator(m_Buffer) };
  1148. }
  1149. /// <summary>
  1150. /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
  1151. /// </summary>
  1152. /// <returns>Throws NotImplementedException.</returns>
  1153. /// <exception cref="NotImplementedException">Method is not implemented.</exception>
  1154. IEnumerator<KeyValue<TKey, TValue>> IEnumerable<KeyValue<TKey, TValue>>.GetEnumerator()
  1155. {
  1156. throw new NotImplementedException();
  1157. }
  1158. /// <summary>
  1159. /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
  1160. /// </summary>
  1161. /// <returns>Throws NotImplementedException.</returns>
  1162. /// <exception cref="NotImplementedException">Method is not implemented.</exception>
  1163. IEnumerator IEnumerable.GetEnumerator()
  1164. {
  1165. throw new NotImplementedException();
  1166. }
  1167. /// <summary>
  1168. /// An enumerator over the key-value pairs of a hash map.
  1169. /// </summary>
  1170. /// <remarks>
  1171. /// In an enumerator's initial state, <see cref="Current"/> is not valid to read.
  1172. /// From this state, the first <see cref="MoveNext"/> call advances the enumerator to the first key-value pair.
  1173. /// </remarks>
  1174. public struct Enumerator : IEnumerator<KeyValue<TKey, TValue>>
  1175. {
  1176. internal UnsafeHashMapDataEnumerator m_Enumerator;
  1177. /// <summary>
  1178. /// Does nothing.
  1179. /// </summary>
  1180. public void Dispose() { }
  1181. /// <summary>
  1182. /// Advances the enumerator to the next key-value pair.
  1183. /// </summary>
  1184. /// <returns>True if <see cref="Current"/> is valid to read after the call.</returns>
  1185. public bool MoveNext() => m_Enumerator.MoveNext();
  1186. /// <summary>
  1187. /// Resets the enumerator to its initial state.
  1188. /// </summary>
  1189. public void Reset() => m_Enumerator.Reset();
  1190. /// <summary>
  1191. /// The current key-value pair.
  1192. /// </summary>
  1193. /// <value>The current key-value pair.</value>
  1194. public KeyValue<TKey, TValue> Current => m_Enumerator.GetCurrent<TKey, TValue>();
  1195. object IEnumerator.Current => Current;
  1196. }
  1197. }
  1198. [BurstCompile]
  1199. internal unsafe struct UnsafeHashMapDisposeJob : IJob
  1200. {
  1201. [NativeDisableUnsafePtrRestriction]
  1202. public UnsafeHashMapData* Data;
  1203. public AllocatorManager.AllocatorHandle Allocator;
  1204. public void Execute()
  1205. {
  1206. UnsafeHashMapData.DeallocateHashMap(Data, Allocator);
  1207. }
  1208. }
  1209. sealed internal class UnsafeHashMapDebuggerTypeProxy<TKey, TValue>
  1210. where TKey : struct, IEquatable<TKey>
  1211. where TValue : struct
  1212. {
  1213. #if !NET_DOTS
  1214. UnsafeHashMap<TKey, TValue> m_Target;
  1215. public UnsafeHashMapDebuggerTypeProxy(UnsafeHashMap<TKey, TValue> target)
  1216. {
  1217. m_Target = target;
  1218. }
  1219. public List<Pair<TKey, TValue>> Items
  1220. {
  1221. get
  1222. {
  1223. var result = new List<Pair<TKey, TValue>>();
  1224. using (var kva = m_Target.GetKeyValueArrays(Allocator.Temp))
  1225. {
  1226. for (var i = 0; i < kva.Length; ++i)
  1227. {
  1228. result.Add(new Pair<TKey, TValue>(kva.Keys[i], kva.Values[i]));
  1229. }
  1230. }
  1231. return result;
  1232. }
  1233. }
  1234. #endif
  1235. }
  1236. /// <summary>
  1237. /// For internal use only.
  1238. /// </summary>
  1239. public unsafe struct UntypedUnsafeHashMap
  1240. {
  1241. #pragma warning disable 169
  1242. [NativeDisableUnsafePtrRestriction]
  1243. UnsafeHashMapData* m_Buffer;
  1244. AllocatorManager.AllocatorHandle m_AllocatorLabel;
  1245. #pragma warning restore 169
  1246. }
  1247. }