DragonECS/src/Collections/EcsSpan.cs

156 lines
5.5 KiB
C#
Raw Normal View History

2024-02-07 22:16:41 +08:00
using DCFApixels.DragonECS.Internal;
2024-01-05 23:49:29 +08:00
using System;
2023-12-31 17:33:48 +08:00
using System.Collections.Generic;
2023-12-24 15:40:19 +08:00
using System.ComponentModel;
2024-01-05 22:16:48 +08:00
using System.Diagnostics;
2023-12-24 15:40:19 +08:00
using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS
{
2024-01-05 22:16:48 +08:00
[DebuggerTypeProxy(typeof(DebuggerProxy))]
2023-12-24 15:40:19 +08:00
public readonly ref struct EcsSpan
{
private readonly int _worldID;
private readonly ReadOnlySpan<int> _values;
#region Properties
2024-03-02 04:20:34 +08:00
public bool IsNull
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _worldID == 0; }
}
public int WorldID
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-02 04:20:34 +08:00
get { return _worldID; }
}
public EcsWorld World
{
2024-03-02 04:20:34 +08:00
get { return EcsWorld.GetWorld(_worldID); }
}
2024-03-02 04:20:34 +08:00
public int Length //TODO rename to Count
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-02 04:20:34 +08:00
get { return _values.Length; }
}
2024-01-08 13:39:38 +08:00
public int this[int index]
2023-12-24 15:40:19 +08:00
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-02 04:20:34 +08:00
get { return _values[index]; }
2024-01-07 19:32:16 +08:00
}
2023-12-24 15:40:19 +08:00
#endregion
#region Constructors
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal EcsSpan(int worldID, ReadOnlySpan<int> span)
{
_worldID = worldID;
_values = span;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal EcsSpan(int worldID, int[] array)
{
_worldID = worldID;
2024-03-02 04:20:34 +08:00
_values = new ReadOnlySpan<int>(array);
2023-12-24 15:40:19 +08:00
}
internal EcsSpan(int worldID, int[] array, int length)
{
_worldID = worldID;
_values = new ReadOnlySpan<int>(array, 0, length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal EcsSpan(int worldID, int[] array, int start, int length)
{
_worldID = worldID;
_values = new ReadOnlySpan<int>(array, start, length);
}
#endregion
2023-12-31 17:33:48 +08:00
#region Methdos
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int[] Bake()
{
2024-03-02 04:20:34 +08:00
return _values.ToArray();
2023-12-31 17:33:48 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Bake(ref int[] entities)
{
if (entities.Length < _values.Length)
2024-03-02 04:20:34 +08:00
{
2023-12-31 17:33:48 +08:00
Array.Resize(ref entities, _values.Length);
2024-03-02 04:20:34 +08:00
}
2023-12-31 17:33:48 +08:00
int[] result = new int[_values.Length];
_values.CopyTo(result);
return _values.Length;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Bake(List<int> entities)
{
entities.Clear();
foreach (var e in _values)
{
entities.Add(e);
}
}
#endregion
2023-12-24 15:40:19 +08:00
#region operators
2024-03-02 04:20:34 +08:00
public static bool operator ==(EcsSpan left, EcsSpan right) { return left._values == right._values; }
public static bool operator !=(EcsSpan left, EcsSpan right) { return left._values != right._values; }
2023-12-24 15:40:19 +08:00
#endregion
#region Enumerator
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-02 04:20:34 +08:00
public ReadOnlySpan<int>.Enumerator GetEnumerator() { return _values.GetEnumerator(); }
2023-12-24 15:40:19 +08:00
#endregion
#region Other
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-02 04:20:34 +08:00
public EcsSpan Slice(int start) { return new EcsSpan(_worldID, _values.Slice(start)); }
2023-12-24 15:40:19 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-02 04:20:34 +08:00
public EcsSpan Slice(int start, int length) { return new EcsSpan(_worldID, _values.Slice(start, length)); }
2023-12-24 15:40:19 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-02 04:20:34 +08:00
public int[] ToArray() { return _values.ToArray(); }
public override string ToString()
{
return CollectionUtility.EntitiesToString(_values.ToArray(), "span");
}
2024-01-05 22:16:48 +08:00
#pragma warning disable CS0809 // Устаревший член переопределяет неустаревший член
[Obsolete("Equals() on EcsSpan will always throw an exception. Use the equality operator instead.")]
[EditorBrowsable(EditorBrowsableState.Never)]
2024-03-02 04:20:34 +08:00
public override bool Equals(object obj) { throw new NotSupportedException(); }
2024-01-05 22:16:48 +08:00
[Obsolete("GetHashCode() on EcsSpan will always throw an exception.")]
[EditorBrowsable(EditorBrowsableState.Never)]
2024-03-02 04:20:34 +08:00
public override int GetHashCode() { throw new NotSupportedException(); }
2024-01-05 22:16:48 +08:00
#pragma warning restore CS0809 // Устаревший член переопределяет неустаревший член
internal class DebuggerProxy
{
private int[] _values;
private int _worldID;
2024-03-02 04:20:34 +08:00
public EcsWorld World { get { return EcsWorld.GetWorld(_worldID); } }
2024-02-29 22:42:33 +08:00
public EntitySlotInfo[] Entities
2024-01-05 22:16:48 +08:00
{
get
{
2024-02-29 22:42:33 +08:00
EntitySlotInfo[] result = new EntitySlotInfo[_values.Length];
2024-01-05 22:16:48 +08:00
int i = 0;
foreach (var e in _values)
2024-02-29 22:42:33 +08:00
{
result[i++] = World.GetEntitySlotInfoDebug(e);
}
2024-01-05 22:16:48 +08:00
return result;
}
}
2024-03-02 04:20:34 +08:00
public int Count { get { return _values.Length; } }
2024-01-05 22:16:48 +08:00
public DebuggerProxy(EcsSpan span)
{
_values = new int[span.Length];
span._values.CopyTo(_values);
_worldID = span._worldID;
}
}
2023-12-24 15:40:19 +08:00
#endregion
}
}