From fd6bc73ee61eb437cc852fe30b8f2c0d5e71f91b Mon Sep 17 00:00:00 2001 From: Lars Date: Fri, 15 Nov 2024 17:22:58 +0100 Subject: [PATCH] Added more docs and refactored some code. --- src/Arch/Core/Archetype.cs | 6 +---- src/Arch/Core/Chunk.cs | 52 +++++++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/Arch/Core/Archetype.cs b/src/Arch/Core/Archetype.cs index 9ec7234..5489e3d 100644 --- a/src/Arch/Core/Archetype.cs +++ b/src/Arch/Core/Archetype.cs @@ -641,11 +641,7 @@ public void Clear() { Count = 0; EntityCount = 0; - for (var index = 0; index < Chunks.Count; index++) - { - ref var chunk = ref Chunks[index]; - chunk.Clear(); - } + Chunks.Clear(); } /// diff --git a/src/Arch/Core/Chunk.cs b/src/Arch/Core/Chunk.cs index d473def..472edd9 100644 --- a/src/Arch/Core/Chunk.cs +++ b/src/Arch/Core/Chunk.cs @@ -13,8 +13,16 @@ namespace Arch.Core; +/// +/// The class +/// represents an array of s and manages them, including being able to create and add new space for them. +/// public class Chunks { + /// + /// Creates a new instance. + /// + /// The inital capacity. public Chunks(int capacity = 1) { Items = ArrayPool.Shared.Rent(capacity); @@ -22,16 +30,35 @@ public Chunks(int capacity = 1) Capacity = capacity; } - public Array Items { get; set; } + /// + /// All used s in an . + /// + private Array Items { get; set; } + + /// + /// The number of allocated s in the . + /// public int Count { get; set; } + + /// + /// The total allocated capacity of s in . + /// public int Capacity { get; private set; } + /// + /// Adds a new to this instance. + /// + /// public void Add(in Chunk chunk) { Debug.Assert(Count + 1 <= Capacity, "Capacity exceeded."); Items[Count++] = chunk; } + /// + /// Ensures capacity for this instance. + /// + /// The new capacity public void EnsureCapacity(int newCapacity) { if (newCapacity < Capacity) @@ -48,6 +75,9 @@ public void EnsureCapacity(int newCapacity) Capacity = newCapacity; } + /// + /// Trims this instance and frees space not required anymore. + /// public void TrimExcess() { // This always spares one single chunk. @@ -71,20 +101,34 @@ public ref Chunk this[int index] get => ref Items[index]; } + /// + /// Returns a with which you can iterate over the content of this instance. + /// + /// public Span AsSpan() { return Items.AsSpan(); } - public void Clear() + /// + /// Clears this instance. + /// + public void Clear(bool clearCount = false) { - Count = 0; - foreach (ref var chunk in Items) + for (var index = 0; index < Count; index++) { + ref var chunk = ref this[index]; chunk.Clear(); } + + Count = clearCount ? 0 : Count; } + /// + /// Converts this instance to an s array. + /// + /// The instance. + /// The underlying s array. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator Chunk[](Chunks instance) {