using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; public sealed unsafe class HashMap { private const ulong MUL = 0x9E3779B97F4A7C15UL; private const nuint INITIAL_CAPACITY = 16; private const int INITIAL_SHIFT = 60; // 64 - 4 private const int INITIAL_THRESHOLD = 14; // 16 * 7 / 8 [StructLayout(LayoutKind.Sequential)] private struct Entry { public long Key; public long Value; } private Entry* _slots; private Entry* _slotsEnd; private Entry* _zeroPtr; private nuint _mask; private int _shift; private int _count; private int _threshold; public HashMap() { Entry* slots = (Entry*)NativeMemory.AllocZeroed(INITIAL_CAPACITY, (nuint)sizeof(Entry)); _slots = slots; _slotsEnd = slots + INITIAL_CAPACITY; _zeroPtr = null; _mask = INITIAL_CAPACITY - 1; _shift = INITIAL_SHIFT; _count = 0; _threshold = INITIAL_THRESHOLD; } ~HashMap() { Entry* slots = _slots; if (slots != null) { _slots = null; NativeMemory.Free(slots); } } public int Count => _count; public int Capacity() => (int)(_mask + 1); [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryGet(long key, out long value) { if (key == 0) { return TryGetZero(out value); } Entry* slots = _slots; Entry* e = slots + (nuint)(unchecked((ulong)key * MUL) >> _shift); Entry* end = _slotsEnd; while (true) { long sk = e->Key; if (sk == key) { value = e->Value; return true; } if (sk == 0) { if (e != _zeroPtr) { Unsafe.SkipInit(out value); return false; } } e++; if (e == end) { e = slots; } } } [MethodImpl(MethodImplOptions.NoInlining)] private bool TryGetZero(out long value) { Entry* zp = _zeroPtr; if (zp != null) { value = zp->Value; return true; } Unsafe.SkipInit(out value); return false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Insert(long key, long value) { if (key == 0) { return InsertZero(value); } Entry* slots = _slots; Entry* e = slots + (nuint)(unchecked((ulong)key * MUL) >> _shift); Entry* end = _slotsEnd; while (true) { long sk = e->Key; if (sk == key) { e->Value = value; return false; } if (sk == 0) { if (e != _zeroPtr) { if (_count == _threshold) { GrowAndInsert(key, value); return true; } e->Key = key; e->Value = value; _count++; return true; } } e++; if (e == end) { e = slots; } } } [MethodImpl(MethodImplOptions.NoInlining)] private bool InsertZero(long value) { Entry* zp = _zeroPtr; if (zp != null) { zp->Value = value; return false; } if (_count == _threshold) { Grow(); } Entry* dst = _slots; while (dst->Key != 0) { dst++; } dst->Key = 0; dst->Value = value; _zeroPtr = dst; _count++; return true; } [MethodImpl(MethodImplOptions.NoInlining)] private void GrowAndInsert(long key, long value) { Grow(); Entry* slots = _slots; Entry* end = _slotsEnd; Entry* zeroPtr = _zeroPtr; Entry* e = slots + (nuint)(unchecked((ulong)key * MUL) >> _shift); while (e->Key != 0 || e == zeroPtr) { e++; if (e == end) { e = slots; } } e->Key = key; e->Value = value; _count++; } private void Grow() { Entry* oldSlots = _slots; Entry* oldEnd = _slotsEnd; Entry* oldZero = _zeroPtr; nuint oldCap = _mask + 1; nuint newCap = oldCap << 1; int newShift = _shift - 1; Entry* newSlots = (Entry*)NativeMemory.AllocZeroed(newCap, (nuint)sizeof(Entry)); Entry* newEnd = newSlots + newCap; for (Entry* src = oldSlots; src < oldEnd; src++) { long k = src->Key; if (k == 0) { continue; } Entry* dst = newSlots + (nuint)(unchecked((ulong)k * MUL) >> newShift); while (dst->Key != 0) { dst++; if (dst == newEnd) { dst = newSlots; } } dst->Key = k; dst->Value = src->Value; } Entry* newZero = null; if (oldZero != null) { Entry* dst = newSlots; while (dst->Key != 0) { dst++; } dst->Key = 0; dst->Value = oldZero->Value; newZero = dst; } _slots = newSlots; _slotsEnd = newEnd; _mask = newCap - 1; _shift = newShift; _threshold = (int)((newCap >> 3) * 7); _zeroPtr = newZero; NativeMemory.Free(oldSlots); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Remove(long key) { if (key == 0) { return RemoveZero(); } Entry* slots = _slots; Entry* e = slots + (nuint)(unchecked((ulong)key * MUL) >> _shift); Entry* end = _slotsEnd; while (true) { long sk = e->Key; if (sk == key) { _count--; Entry* next = e + 1; if (next == end) { next = slots; } if (next->Key == 0 && next != _zeroPtr) { e->Key = 0; return true; } BackwardShift(slots, (nuint)(e - slots), (nuint)(next - slots)); return true; } if (sk == 0) { if (e != _zeroPtr) { return false; } } e++; if (e == end) { e = slots; } } } [MethodImpl(MethodImplOptions.NoInlining)] private bool RemoveZero() { Entry* zp = _zeroPtr; if (zp == null) { return false; } _zeroPtr = null; _count--; Entry* slots = _slots; nuint hole = (nuint)(zp - slots); nuint curr = (hole + 1) & _mask; if (slots[curr].Key == 0) { return true; } BackwardShift(slots, hole, curr); return true; } private void BackwardShift(Entry* slots, nuint hole, nuint curr) { nuint mask = _mask; int shift = _shift; Entry* zeroPtr = _zeroPtr; while (true) { Entry* ce = slots + curr; long ck = ce->Key; if (ck == 0) { if (ce != zeroPtr) { slots[hole].Key = 0; return; } if (curr >= ((curr - hole) & mask)) { slots[hole] = *ce; Entry* newZp = slots + hole; _zeroPtr = newZp; zeroPtr = newZp; hole = curr; } curr = (curr + 1) & mask; continue; } nuint h = (nuint)(unchecked((ulong)ck * MUL) >> shift); if (((curr - h) & mask) >= ((curr - hole) & mask)) { slots[hole].Key = ck; slots[hole].Value = ce->Value; hole = curr; } curr = (curr + 1) & mask; } } public int Scan(long[] outKeys, long[] outVals) { int count = _count; if (count == 0) { return 0; } fixed (long* pKeys = outKeys) fixed (long* pVals = outVals) { nuint outIdx = 0; nuint uCount = (nuint)count; Entry* zp = _zeroPtr; if (zp != null) { pKeys[0] = 0; pVals[0] = zp->Value; outIdx = 1; if (outIdx == uCount) { return count; } } Entry* p = _slots; if (uCount >= 4) { nuint safeEnd = uCount - 3; while (outIdx < safeEnd) { long k0 = p[0].Key; long v0 = p[0].Value; long k1 = p[1].Key; long v1 = p[1].Value; long k2 = p[2].Key; long v2 = p[2].Value; long k3 = p[3].Key; long v3 = p[3].Value; nuint m0 = Unsafe.BitCast(k0 != 0); nuint m1 = Unsafe.BitCast(k1 != 0); nuint m2 = Unsafe.BitCast(k2 != 0); nuint m3 = Unsafe.BitCast(k3 != 0); nuint m01 = m0 + m1; nuint i1 = outIdx + m0; nuint i2 = outIdx + m01; nuint i3 = i2 + m2; pKeys[outIdx] = k0; pVals[outIdx] = v0; pKeys[i1] = k1; pVals[i1] = v1; pKeys[i2] = k2; pVals[i2] = v2; pKeys[i3] = k3; pVals[i3] = v3; outIdx = i2 + (m2 + m3); p += 4; } } while (outIdx < uCount) { long k = p->Key; long v = p->Value; pKeys[outIdx] = k; pVals[outIdx] = v; outIdx += Unsafe.BitCast(k != 0); p++; } } return count; } }