1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-19 08:30:25 +02:00

Reworked ref enumerators to support non-pointer refs

This commit is contained in:
Brian Fiete 2020-05-01 16:29:12 -07:00
parent 70d32885b1
commit d5073e810c
10 changed files with 82 additions and 24 deletions

View file

@ -13,9 +13,13 @@ namespace System.Collections
using System.Diagnostics;
using System.Diagnostics.Contracts;
public class Dictionary<TKey, TValue> : ICollection<(TKey key, TValue value)>, IEnumerable<(TKey key, TValue value)> where TKey : IHashable
public class Dictionary<TKey, TValue> :
ICollection<(TKey key, TValue value)>,
IEnumerable<(TKey key, TValue value)>,
IRefEnumerable<(TKey key, TValue* valueRef)> where TKey : IHashable
{
typealias KeyValuePair=(TKey key, TValue value);
typealias KeyRefValuePair=(TKey key, TValue* valueRef);
private struct Entry
{
@ -606,7 +610,7 @@ namespace System.Collections
return (key is TKey);
}
public struct Enumerator : IEnumerator<KeyValuePair>//, IDictionaryEnumerator
public struct Enumerator : IEnumerator<KeyValuePair>, IRefEnumerator<KeyRefValuePair>
{
private Dictionary<TKey, TValue> mDictionary;
#if VERSION_DICTIONARY
@ -687,6 +691,11 @@ namespace System.Collections
get { return (mDictionary.mEntries[mCurrentIndex].mKey, mDictionary.mEntries[mCurrentIndex].mValue); }
}
public KeyRefValuePair CurrentRef
{
get { return (mDictionary.mEntries[mCurrentIndex].mKey, &mDictionary.mEntries[mCurrentIndex].mValue); }
}
public void Dispose()
{
@ -753,9 +762,16 @@ namespace System.Collections
return .Err;
return Current;
}
public Result<KeyRefValuePair> GetNextRef() mut
{
if (!MoveNext())
return .Err;
return CurrentRef;
}
}
public struct ValueEnumerator : IRefEnumerator<TValue>, IResettable
public struct ValueEnumerator : IRefEnumerator<TValue*>, IEnumerator<TValue>, IResettable
{
private Dictionary<TKey, TValue> mDictionary;
#if VERSION_DICTIONARY

View file

@ -12,13 +12,18 @@ namespace System.Collections
void Reset() mut;
}
interface IRefEnumerator<T> : IEnumerator<T>
interface IRefEnumerator<T>
{
Result<T*> GetNextRef() mut;
Result<T> GetNextRef() mut;
}
concrete interface IEnumerable<T>
{
concrete IEnumerator<T> GetEnumerator();
}
concrete interface IRefEnumerable<T>
{
concrete IRefEnumerator<T> GetEnumerator();
}
}

View file

@ -623,7 +623,7 @@ namespace System.Collections
}
}
public struct Enumerator : IRefEnumerator<T>, IResettable
public struct Enumerator : IRefEnumerator<T*>, IEnumerator<T>, IResettable
{
private List<T> mList;
private int mIndex;

View file

@ -351,7 +351,7 @@ namespace System.Collections
/// Implements an enumerator for a Queue. The enumerator uses the
/// internal version number of the list to ensure that no modifications are
/// made to the list while an enumeration is in progress.
public struct Enumerator : IRefEnumerator<T>
public struct Enumerator : IRefEnumerator<T*>, IEnumerator<T>
{
private Queue<T> mQueue;
private int32 mIndex; // -1 = not started, -2 = ended/disposed

View file

@ -184,7 +184,7 @@ namespace System
return Enumerator(this);
}
public struct Enumerator : IEnumerator<T>, IRefEnumerator<T>
public struct Enumerator : IEnumerator<T>, IRefEnumerator<T*>
{
private Span<T> mList;
private int mIndex;

View file

@ -2360,7 +2360,7 @@ namespace System
}
}
public struct RawEnumerator : IRefEnumerator<char8>
public struct RawEnumerator : IRefEnumerator<char8*>, IEnumerator<char8>
{
char8* mPtr;
int_strsize mIdx;