UnOrderMap.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.CompilerServices;
  5. using NativeCollection.UnsafeType;
  6. namespace NativeCollection
  7. {
  8. public unsafe class UnOrderMap<T, K> : IEnumerable<MapPair<T, K>>, INativeCollectionClass
  9. where T : unmanaged, IEquatable<T>, IComparable<T> where K : unmanaged, IEquatable<K>
  10. {
  11. private int _capacity;
  12. private UnsafeType.UnOrderMap<T, K>* _unOrderMap;
  13. public UnOrderMap(int initCapacity = 0)
  14. {
  15. _capacity = initCapacity;
  16. _unOrderMap = UnsafeType.UnOrderMap<T, K>.Create(_capacity);
  17. IsDisposed = false;
  18. }
  19. public K this[T key]
  20. {
  21. get => (*_unOrderMap)[key];
  22. set => (*_unOrderMap)[key] = value;
  23. }
  24. public int Count => _unOrderMap->Count;
  25. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  26. public void Add(in T key, K value)
  27. {
  28. _unOrderMap->Add(key, value);
  29. }
  30. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  31. public bool Remove(in T key)
  32. {
  33. return _unOrderMap->Remove(key);
  34. }
  35. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  36. public void Clear()
  37. {
  38. _unOrderMap->Clear();
  39. }
  40. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  41. public bool ContainsKey(in T key)
  42. {
  43. return _unOrderMap->ContainsKey(key);
  44. }
  45. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  46. public bool TryGetValue(in T key, out K value)
  47. {
  48. bool contains = _unOrderMap->TryGetValue(key, out var actualValue);
  49. if (contains)
  50. {
  51. value = actualValue;
  52. return true;
  53. }
  54. value = default;
  55. return false;
  56. }
  57. IEnumerator<MapPair<T, K>> IEnumerable<MapPair<T, K>>.GetEnumerator()
  58. {
  59. return GetEnumerator();
  60. }
  61. IEnumerator IEnumerable.GetEnumerator()
  62. {
  63. return GetEnumerator();
  64. }
  65. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  66. public UnsafeType.UnOrderMap<T,K>.Enumerator GetEnumerator()
  67. {
  68. return _unOrderMap->GetEnumerator();
  69. }
  70. public void Dispose()
  71. {
  72. if (IsDisposed) return;
  73. if (_unOrderMap != null)
  74. {
  75. _unOrderMap->Dispose();
  76. NativeMemoryHelper.Free(_unOrderMap);
  77. NativeMemoryHelper.RemoveNativeMemoryByte(Unsafe.SizeOf<UnsafeType.UnOrderMap<T, K>>());
  78. IsDisposed = true;
  79. }
  80. }
  81. public void ReInit()
  82. {
  83. if (IsDisposed)
  84. {
  85. _unOrderMap = UnsafeType.UnOrderMap<T, K>.Create(_capacity);
  86. IsDisposed = false;
  87. }
  88. }
  89. public bool IsDisposed { get; private set; }
  90. ~UnOrderMap()
  91. {
  92. Dispose();
  93. }
  94. }
  95. }