using System; using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; namespace NativeCollection { public unsafe class SortedSet : ICollection, INativeCollectionClass where T : unmanaged, IEquatable,IComparable { private UnsafeType.SortedSet* _sortedSet; private const int _defaultNodePoolBlockSize = 64; private int _poolBlockSize; public SortedSet(int nodePoolSize = _defaultNodePoolBlockSize) { _poolBlockSize = nodePoolSize; _sortedSet = UnsafeType.SortedSet.Create(_poolBlockSize); IsDisposed = false; } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public UnsafeType.SortedSet.Enumerator GetEnumerator() { return new UnsafeType.SortedSet.Enumerator(_sortedSet); } void ICollection.Add(T item) { _sortedSet->Add(item); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Add(T item) { return _sortedSet->Add(item); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Clear() { _sortedSet->Clear(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Contains(T item) { return _sortedSet->Contains(item); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void CopyTo(T[] array, int arrayIndex) { _sortedSet->CopyTo(array,arrayIndex); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Remove(T item) { return _sortedSet->Remove(item); } public T? Min => _sortedSet->Min; public T? Max => _sortedSet->Max; public int Count => _sortedSet->Count; public bool IsReadOnly => false; public void Dispose() { if (IsDisposed) { return; } if (_sortedSet!=null) { _sortedSet->Dispose(); NativeMemoryHelper.Free(_sortedSet); NativeMemoryHelper.RemoveNativeMemoryByte(Unsafe.SizeOf>()); IsDisposed = true; } } public void ReInit() { if (IsDisposed) { _sortedSet = UnsafeType.SortedSet.Create(_poolBlockSize); IsDisposed = false; } } public bool IsDisposed { get; private set; } } }