using System; using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; using NativeCollection.UnsafeType; namespace NativeCollection { public unsafe class UnOrderMap : IEnumerable>, INativeCollectionClass where T : unmanaged, IEquatable, IComparable where K : unmanaged, IEquatable { private int _capacity; private UnsafeType.UnOrderMap* _unOrderMap; public UnOrderMap(int initCapacity = 0) { _capacity = initCapacity; _unOrderMap = UnsafeType.UnOrderMap.Create(_capacity); IsDisposed = false; } public K this[T key] { get => (*_unOrderMap)[key]; set => (*_unOrderMap)[key] = value; } public int Count => _unOrderMap->Count; [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Add(in T key, K value) { _unOrderMap->Add(key, value); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Remove(in T key) { return _unOrderMap->Remove(key); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Clear() { _unOrderMap->Clear(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool ContainsKey(in T key) { return _unOrderMap->ContainsKey(key); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryGetValue(in T key, out K value) { bool contains = _unOrderMap->TryGetValue(key, out var actualValue); if (contains) { value = actualValue; return true; } value = default; return false; } IEnumerator> IEnumerable>.GetEnumerator() { return GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public UnsafeType.UnOrderMap.Enumerator GetEnumerator() { return _unOrderMap->GetEnumerator(); } public void Dispose() { if (IsDisposed) return; if (_unOrderMap != null) { _unOrderMap->Dispose(); NativeMemoryHelper.Free(_unOrderMap); NativeMemoryHelper.RemoveNativeMemoryByte(Unsafe.SizeOf>()); IsDisposed = true; } } public void ReInit() { if (IsDisposed) { _unOrderMap = UnsafeType.UnOrderMap.Create(_capacity); IsDisposed = false; } } public bool IsDisposed { get; private set; } ~UnOrderMap() { Dispose(); } } }