1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-19 16:40:26 +02:00
Beef/BeefLibs/corlib/src/SizedArray.bf

115 lines
1.8 KiB
Beef
Raw Normal View History

2021-01-20 15:00:13 -08:00
using System.Collections;
2019-08-23 11:56:54 -07:00
namespace System
{
[AlwaysInclude]
2021-01-20 15:00:13 -08:00
struct SizedArray<T, CSize> : IEnumerable<T> where CSize : const int
2019-08-23 11:56:54 -07:00
{
2020-08-23 05:42:42 -07:00
protected T[CSize] mVal;
2019-08-23 11:56:54 -07:00
public int Count
{
[Inline]
get
{
return CSize;
}
}
public explicit static operator T[CSize] (Self val)
{
return val.mVal;
}
2021-01-27 09:01:47 -08:00
public implicit static operator Span<T> (in Self val)
{
#unwarn
return .(&val.mVal, CSize);
}
2019-08-23 11:56:54 -07:00
public override void ToString(String strBuffer) mut
{
if (typeof(T) == typeof(char8))
{
int len = 0;
for (; len < CSize; len++)
{
if (mVal[len] == default)
break;
}
strBuffer.Append((char8*)&mVal, len);
2019-08-23 11:56:54 -07:00
return;
}
strBuffer.Append('(');
for (int i < CSize)
{
if (i != 0)
strBuffer.Append(", ");
mVal[i].ToString(strBuffer);
}
strBuffer.Append(')');
}
2021-01-20 15:00:13 -08:00
public Enumerator GetEnumerator()
{
return .((T[CSize])this);
}
public struct Enumerator : IEnumerator<T>
{
private T[CSize] mList;
private int mIndex;
private T* mCurrent;
public this(T[CSize] list)
{
mList = list;
mIndex = 0;
mCurrent = null;
}
public bool MoveNext() mut
{
if ((uint(mIndex) < uint(CSize)))
{
mCurrent = &mList[mIndex];
mIndex++;
return true;
}
return MoveNextRare();
}
private bool MoveNextRare() mut
{
mIndex = CSize + 1;
mCurrent = null;
return false;
}
public T Current
{
get
{
return *mCurrent;
}
}
public int Index
{
get
{
return mIndex - 1;
}
2021-01-20 15:00:13 -08:00
}
public Result<T> GetNext() mut
{
if (!MoveNext())
return .Err;
return Current;
}
}
2019-08-23 11:56:54 -07:00
}
}