Skip to content

Commit

Permalink
remove
Browse files Browse the repository at this point in the history
  • Loading branch information
2A5F committed Dec 8, 2023
1 parent 350f87f commit 90e642d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
5 changes: 1 addition & 4 deletions BetterCollections/FlatHashMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ public bool ContainsKey(TKey key)
=> TryFind(key, new EqHashKey<TKey, TValue>(), out _);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Remove(TKey key)
{
throw new System.NotImplementedException();
}
public bool Remove(TKey key) => TryRemove(in key, new EqHashKey<TKey, TValue>(), out _);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetValue(TKey key, out TValue value)
Expand Down
57 changes: 57 additions & 0 deletions BetterCollections/Misc/ASwissTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,63 @@ private void ReInsert<V>(ReadOnlySpan<byte> old_ctrl, ReadOnlySpan<T> old_slots,

#endregion

#region Remove

protected bool TryRemove<K, KEH>(in K key, in KEH keh, out T value) where KEH : IEqHashKey<T, K, EH>
{
Unsafe.SkipInit(out value);
if (slots_size == 0 || count == 0) return false;

var hash = Hash((ulong)keh.CalcHash(in eh, in key));
var h1 = GetH1(hash);
var h2 = GetH2(hash);

#if NET7_0_OR_GREATER
#if NET8_0_OR_GREATER
if (Vector512.IsHardwareAccelerated)
return TryRemove<Vector512<byte>, K, KEH>(in key, in keh, h1, h2, out value);
#endif
if (Vector256.IsHardwareAccelerated)
return TryRemove<Vector256<byte>, K, KEH>(in key, in keh, h1, h2, out value);
if (Vector128.IsHardwareAccelerated)
return TryRemove<Vector128<byte>, K, KEH>(in key, in keh, h1, h2, out value);
if (Vector64.IsHardwareAccelerated)
return TryRemove<Vector64<byte>, K, KEH>(in key, in keh, h1, h2, out value);
#endif
return TryRemove<ulong, K, KEH>(in key, in keh, h1, h2, out value);
}

private bool TryRemove<V, K, KEH>(in K key, in KEH keh, ulong h1, byte h2, out T value) where KEH : IEqHashKey<T, K, EH>
{
Unsafe.SkipInit(out value);
var size = slots_size;
if (size == 0 || count == 0) return false;

var ctrl_bytes = Ctrl;
var slots = Slots;

for (var prop = H1StartProbe(h1);; prop.MoveNext(size))
{
LoadGroup<V>(in ctrl_bytes[(int)prop.pos], out var group);
foreach (var offset in MatchH2(ref group, h2))
{
var index = ModGetBucket(prop.pos + offset);
ref var slot = ref slots[(int)index];
if (keh.IsEq(eh, in key, in slot))
{
WriteCtrl(new(prop.pos, offset), SlotIsDeleted); // todo check empty
value = slot;
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
slot = default!;
return true;
}
}
if (MatchEmpty(ref group)) return false;
}
}

#endregion

#region Clear

public void Clear()
Expand Down

0 comments on commit 90e642d

Please sign in to comment.