Skip to content

Commit

Permalink
Added more docs and refactored some code.
Browse files Browse the repository at this point in the history
  • Loading branch information
genaray committed Nov 15, 2024
1 parent 8cf706d commit fd6bc73
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
6 changes: 1 addition & 5 deletions src/Arch/Core/Archetype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/// <summary>
Expand Down
52 changes: 48 additions & 4 deletions src/Arch/Core/Chunk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,52 @@
namespace Arch.Core;


/// <summary>
/// The <see cref="Chunks"/> class
/// represents an array of <see cref="Chunk"/>s and manages them, including being able to create and add new space for them.
/// </summary>
public class Chunks
{
/// <summary>
/// Creates a new <see cref="Chunks"/> instance.
/// </summary>
/// <param name="capacity">The inital capacity.</param>
public Chunks(int capacity = 1)
{
Items = ArrayPool<Chunk>.Shared.Rent(capacity);
Count = 0;
Capacity = capacity;
}

public Array<Chunk> Items { get; set; }
/// <summary>
/// All used <see cref="Chunk"/>s in an <see cref="Array{T}"/>.
/// </summary>
private Array<Chunk> Items { get; set; }

/// <summary>
/// The number of allocated <see cref="Chunk"/>s in the <see cref="Items"/>.
/// </summary>
public int Count { get; set; }

/// <summary>
/// The total allocated capacity of <see cref="Chunk"/>s in <see cref="Items"/>.
/// </summary>
public int Capacity { get; private set; }

/// <summary>
/// Adds a new <see cref="Chunk"/> to this instance.
/// </summary>
/// <param name="chunk"></param>
public void Add(in Chunk chunk)
{
Debug.Assert(Count + 1 <= Capacity, "Capacity exceeded.");
Items[Count++] = chunk;
}

/// <summary>
/// Ensures capacity for this instance.
/// </summary>
/// <param name="newCapacity">The new capacity</param>
public void EnsureCapacity(int newCapacity)
{
if (newCapacity < Capacity)
Expand All @@ -48,6 +75,9 @@ public void EnsureCapacity(int newCapacity)
Capacity = newCapacity;
}

/// <summary>
/// Trims this instance and frees space not required anymore.
/// </summary>
public void TrimExcess()
{
// This always spares one single chunk.
Expand All @@ -71,20 +101,34 @@ public ref Chunk this[int index]
get => ref Items[index];
}

/// <summary>
/// Returns a <see cref="Span{T}"/> with which you can iterate over the <see cref="Items"/> content of this instance.
/// </summary>
/// <returns></returns>
public Span<Chunk> AsSpan()
{
return Items.AsSpan();
}

public void Clear()
/// <summary>
/// Clears this instance.
/// </summary>
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;
}

/// <summary>
/// Converts this instance to an <see cref="Chunk"/>s array.
/// </summary>
/// <param name="instance">The instance.</param>
/// <returns>The underlying <see cref="Chunk"/>s array.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Chunk[](Chunks instance)
{
Expand Down

0 comments on commit fd6bc73

Please sign in to comment.