From ca232ac33b64fd38c51feeb61b6ddd930bf0ca18 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sat, 31 Aug 2024 14:51:13 +0800 Subject: [PATCH] [*] update ToArray add ToCollection for collections --- src/Collections/EcsGroup.cs | 25 ++++++++++++++++- src/Collections/EcsSpan.cs | 56 ++++++++++++++++++++++++++++++++++--- 2 files changed, 76 insertions(+), 5 deletions(-) diff --git a/src/Collections/EcsGroup.cs b/src/Collections/EcsGroup.cs index 0bd8715..6c15275 100644 --- a/src/Collections/EcsGroup.cs +++ b/src/Collections/EcsGroup.cs @@ -90,6 +90,10 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] public int[] ToArray() { return _source.ToArray(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] + public int ToArray(ref int[] dynamicBuffer) { return _source.ToArray(ref dynamicBuffer); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void ToCollection(ICollection collection) { _source.ToCollection(collection); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public EcsGroup.Enumerator GetEnumerator() { return _source.GetEnumerator(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -387,13 +391,32 @@ namespace DCFApixels.DragonECS { return new EcsSpan(WorldID, _dense, 1, _count); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] public int[] ToArray() { int[] result = new int[_count]; Array.Copy(_dense, 1, result, 0, _count); return result; } + public int ToArray(ref int[] dynamicBuffer) + { + if (dynamicBuffer.Length < _count) + { + Array.Resize(ref dynamicBuffer, ArrayUtility.NormalizeSizeToPowerOfTwo(_count)); + } + int i = 0; + foreach (var e in this) + { + dynamicBuffer[i++] = e; + } + return i; + } + public void ToCollection(ICollection collection) + { + foreach (var e in this) + { + collection.Add(e); + } + } #endregion #region Set operations diff --git a/src/Collections/EcsSpan.cs b/src/Collections/EcsSpan.cs index db5926f..f70910d 100644 --- a/src/Collections/EcsSpan.cs +++ b/src/Collections/EcsSpan.cs @@ -1,5 +1,6 @@ using DCFApixels.DragonECS.Internal; using System; +using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Runtime.CompilerServices; @@ -78,13 +79,32 @@ namespace DCFApixels.DragonECS } #endregion - #region Slice/ToArry + #region Slice/ToArray [MethodImpl(MethodImplOptions.AggressiveInlining)] public EcsSpan Slice(int start) { return new EcsSpan(_worldID, _values.Slice(start)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public EcsSpan Slice(int start, int length) { return new EcsSpan(_worldID, _values.Slice(start, length)); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] public int[] ToArray() { return _values.ToArray(); } + public int ToArray(ref int[] dynamicBuffer) + { + if (dynamicBuffer.Length < _values.Length) + { + Array.Resize(ref dynamicBuffer, ArrayUtility.NormalizeSizeToPowerOfTwo(_values.Length)); + } + int i = 0; + foreach (var e in this) + { + dynamicBuffer[i++] = e; + } + return i; + } + public void ToCollection(ICollection collection) + { + foreach (var e in this) + { + collection.Add(e); + } + } #endregion #region operators @@ -199,8 +219,36 @@ namespace DCFApixels.DragonECS public EcsLongsSpan Slice(int start, int length) { return new EcsLongsSpan(_source.Slice(start, length)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public EcsSpan ToSpan() { return _source; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int[] ToArray() { return _source.ToArray(); } + public entlong[] ToArray() + { + entlong[] result = new entlong[_source.Count]; + int i = 0; + foreach (var e in this) + { + result[i++] = e; + } + return result; + } + public int ToArray(ref entlong[] dynamicBuffer) + { + if (dynamicBuffer.Length < _source.Count) + { + Array.Resize(ref dynamicBuffer, ArrayUtility.NormalizeSizeToPowerOfTwo(_source.Count)); + } + int i = 0; + foreach (var e in this) + { + dynamicBuffer[i++] = e; + } + return i; + } + public void ToCollection(ICollection collection) + { + foreach (var e in this) + { + collection.Add(e); + } + } #endregion #region operators