using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // Open-addressing hash map with linear probing and backward-shift deletion. // // Layout: one flat native array of 16-byte slots {Key, Value}. Key 0 marks an // empty slot. A real entry with key 0 is stored in the table like any other // entry; its slot index is remembered in `zeroSlot` (-1 if absent), and a slot // with key 0 counts as empty only when its index differs from `zeroSlot`. public sealed unsafe class HashMap { [StructLayout(LayoutKind.Sequential)] private struct Entry { public long Key; public long Value; } private const ulong Mul = 0x9E3779B97F4A7C15UL; private const int InitialBits = 4; private Entry* table; private nuint mask; private int shift; // 64 - log2(capacity) private int count; private int maxLoad; // capacity * 7 / 8 private nint zeroSlot; // slot holding key 0, or -1 public HashMap() { Allocate(InitialBits); zeroSlot = -1; } ~HashMap() { if (table != null) { NativeMemory.Free(table); table = null; } } private void Allocate(int bits) { nuint cap = (nuint)1 << bits; table = (Entry*)NativeMemory.AllocZeroed(cap * (nuint)sizeof(Entry)); mask = cap - 1; shift = 64 - bits; maxLoad = (int)(cap / 8 * 7); } public int Count { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { return count; } } public int Capacity() { return (int)(mask + 1); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static nuint Home(long key, int shift) { unchecked { return (nuint)(((ulong)key * Mul) >> shift); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryGet(long key, out long value) { if (key != 0) { Entry* t = table; nuint m = mask; nuint i = Home(key, shift); while (true) { long k = t[i].Key; if (k == key) { value = t[i].Value; return true; } if (k == 0 && (nint)i != zeroSlot) { value = 0; return false; } i = (i + 1) & m; } } nint z = zeroSlot; if (z >= 0) { value = table[z].Value; return true; } value = 0; return false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Insert(long key, long value) { if (key == 0) return InsertZero(value); Entry* t = table; nuint m = mask; nuint i = Home(key, shift); while (true) { long k = t[i].Key; if (k == key) { t[i].Value = value; return false; } if (k == 0 && (nint)i != zeroSlot) break; i = (i + 1) & m; } if (count >= maxLoad) { InsertAfterGrow(key, value); return true; } t[i].Key = key; t[i].Value = value; count++; return true; } [MethodImpl(MethodImplOptions.NoInlining)] private void InsertAfterGrow(long key, long value) { Grow(); Entry* t = table; nuint m = mask; nuint i = Home(key, shift); // zeroSlot may hold the key-0 entry; treat it as occupied. while (t[i].Key != 0 || (nint)i == zeroSlot) i = (i + 1) & m; t[i].Key = key; t[i].Value = value; count++; } [MethodImpl(MethodImplOptions.NoInlining)] private bool InsertZero(long value) { if (zeroSlot >= 0) { table[zeroSlot].Value = value; return false; } if (count >= maxLoad) Grow(); Entry* t = table; nuint m = mask; nuint i = 0; // Home(0) == 0 while (t[i].Key != 0) i = (i + 1) & m; t[i].Value = value; zeroSlot = (nint)i; count++; return true; } [MethodImpl(MethodImplOptions.NoInlining)] private void Grow() { Entry* old = table; nuint oldCap = mask + 1; int bits = 64 - shift + 1; Allocate(bits); Entry* t = table; nuint m = mask; int sh = shift; for (nuint j = 0; j < oldCap; j++) { long k = old[j].Key; if (k == 0) continue; nuint i = Home(k, sh); while (t[i].Key != 0) i = (i + 1) & m; t[i] = old[j]; } if (zeroSlot >= 0) { long v = old[zeroSlot].Value; nuint i = 0; while (t[i].Key != 0) i = (i + 1) & m; t[i].Value = v; zeroSlot = (nint)i; } NativeMemory.Free(old); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Remove(long key) { if (key == 0) return RemoveZero(); Entry* t = table; nuint m = mask; nuint i = Home(key, shift); while (true) { long k = t[i].Key; if (k == key) break; if (k == 0 && (nint)i != zeroSlot) return false; i = (i + 1) & m; } ShiftBack(i); count--; return true; } [MethodImpl(MethodImplOptions.NoInlining)] private bool RemoveZero() { nint z = zeroSlot; if (z < 0) return false; zeroSlot = -1; ShiftBack((nuint)z); count--; return true; } // Backward-shift deletion: slot `hole` is being vacated. private void ShiftBack(nuint hole) { Entry* t = table; nuint m = mask; int sh = shift; nuint j = (hole + 1) & m; while (true) { long k = t[j].Key; if (k == 0 && (nint)j != zeroSlot) break; nuint h = Home(k, sh); if (((j - h) & m) >= ((j - hole) & m)) { t[hole] = t[j]; if ((nint)j == zeroSlot) zeroSlot = (nint)hole; hole = j; } j = (j + 1) & m; } t[hole].Key = 0; } public int Scan(long[] outKeys, long[] outVals) { int total = count; nint z = zeroSlot; int target = z >= 0 ? total - 1 : total; Entry* t = table; nuint cap = mask + 1; int c = 0; if (target > 0) { fixed (long* ok = outKeys, ov = outVals) { nuint j = 0; while (j < cap) { long k = t[j].Key; ok[c] = k; ov[c] = t[j].Value; c += k != 0 ? 1 : 0; j++; if (c >= target) break; } } } if (z >= 0) { outKeys[c] = 0; outVals[c] = table[z].Value; c++; } return c; } }