From c64dc37d614acf6e774d2d2a6c6a3b0e6731f4a0 Mon Sep 17 00:00:00 2001 From: disarray2077 <86157825+disarray2077@users.noreply.github.com> Date: Fri, 17 May 2024 15:17:51 -0300 Subject: [PATCH] Implement IRefEnumerator in HashSet Enumerator --- BeefLibs/corlib/src/Collections/HashSet.bf | 29 ++++++++++++++++------ 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/BeefLibs/corlib/src/Collections/HashSet.bf b/BeefLibs/corlib/src/Collections/HashSet.bf index bc26bfa1..08553cba 100644 --- a/BeefLibs/corlib/src/Collections/HashSet.bf +++ b/BeefLibs/corlib/src/Collections/HashSet.bf @@ -1217,14 +1217,14 @@ namespace System.Collections public int32 mNext; // Index of next entry, -1 if last } - public struct Enumerator : IEnumerator + public struct Enumerator : IRefEnumerator, IEnumerator { private HashSet mSet; private int_cosize mIndex; #if VERSION_HASHSET private int32 mVersion; #endif - private T mCurrent; + private T* mCurrent; public this(HashSet set) { @@ -1233,7 +1233,7 @@ namespace System.Collections #if VERSION_HASHSET mVersion = set.mVersion; #endif - mCurrent = default; + mCurrent = null; } #if VERSION_HASHSET @@ -1257,14 +1257,14 @@ namespace System.Collections { if (mSet.mSlots[mIndex].mHashCode >= 0) { - mCurrent = mSet.mSlots[mIndex].mValue; + mCurrent = &mSet.mSlots[mIndex].mValue; mIndex++; return true; } mIndex++; } mIndex = mSet.mLastIndex + 1; - mCurrent = default(T); + mCurrent = null; return false; } @@ -1272,7 +1272,15 @@ namespace System.Collections { get { - return mCurrent; + return *mCurrent; + } + } + + public ref T CurrentRef + { + get + { + return ref *mCurrent; } } @@ -1292,7 +1300,7 @@ namespace System.Collections CheckVersion(); #endif mIndex = 0; - mCurrent = default(T); + mCurrent = null; } public Result GetNext() mut @@ -1301,6 +1309,13 @@ namespace System.Collections return .Err; return Current; } + + public Result GetNextRef() mut + { + if (!MoveNext()) + return .Err; + return &CurrentRef; + } } }