1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-27 20:18:01 +02:00

Added more Alt methods

This commit is contained in:
Brian Fiete 2024-10-24 14:56:28 -04:00
parent df96ca1dfe
commit 9501ee2c22

View file

@ -321,6 +321,54 @@ namespace System.Collections
return false; return false;
} }
bool RemoveAlt<TAltKey>(TAltKey item, T* outValue) where TAltKey : IHashable where bool : operator T == TAltKey
{
if (mBuckets != null)
{
int32 hashCode = InternalGetHashCodeAlt(item);
int32 bucket = hashCode % (int32)mBuckets.Count;
int32 last = -1;
for (int32 i = mBuckets[bucket] - 1; i >= 0; last = i,i = mSlots[i].mNext)
{
if (mSlots[i].mHashCode == hashCode && /*m_comparer.Equals*/(mSlots[i].mValue == item))
{
if (last < 0)
{
// first iteration; update buckets
mBuckets[bucket] = mSlots[i].mNext + 1;
}
else
{
// subsequent iterations; update 'next' pointers
mSlots[last].mNext = mSlots[i].mNext;
}
if (outValue != null)
*outValue = mSlots[i].mValue;
mSlots[i].mHashCode = -1;
mSlots[i].mValue = default(T);
mSlots[i].mNext = mFreeList;
mCount--;
#if VERSION_HASHSET
mVersion++;
#endif
if (mCount == 0)
{
mLastIndex = 0;
mFreeList = -1;
}
else
{
mFreeList = i;
}
return true;
}
}
}
// either m_buckets is null or wasn't found
return false;
}
/// Remove item from this container /// Remove item from this container
/// @param item item to remove /// @param item item to remove
/// @return true if removed; false if not (i.e. if the item wasn't in the HashSet) /// @return true if removed; false if not (i.e. if the item wasn't in the HashSet)
@ -340,6 +388,17 @@ namespace System.Collections
return .Ok(value); return .Ok(value);
} }
/// Remove item from this container
/// @param item item to remove
/// @return .Ok(value) if removed, with 'value' being the stored value; .Err if not (i.e. if the item wasn't in the HashSet)
public Result<T> GetAndRemoveAlt<TAltKey>(TAltKey item) where TAltKey : IHashable where bool : operator T == TAltKey
{
T value = ?;
if (!RemoveAlt(item, &value))
return .Err;
return .Ok(value);
}
/// Number of elements in this hashset /// Number of elements in this hashset
public int Count public int Count
{ {
@ -1198,7 +1257,7 @@ namespace System.Collections
return GetHashKey(item.GetHashCode()); return GetHashKey(item.GetHashCode());
} }
private int InternalGetHashCodeAlt<TAltKey>(TAltKey item) where TAltKey : IHashable private int32 InternalGetHashCodeAlt<TAltKey>(TAltKey item) where TAltKey : IHashable
{ {
if (item == null) if (item == null)
return 0; return 0;