Skip to content

Commit

Permalink
immutable array
Browse files Browse the repository at this point in the history
  • Loading branch information
andreakarasho committed Feb 25, 2024
1 parent f024825 commit 8aba07e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
7 changes: 6 additions & 1 deletion src/DotnetAddons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static Span<T> AsSpan<T>(List<T>? list)
// }
}

public static class SortExtensions
public static class DotnetExtensions
{
public static void Sort<T>(this Span<T> span) where T : IComparable<T>
{
Expand Down Expand Up @@ -169,5 +169,10 @@ public static void Sort<T>(this Span<T> span, IComparer<T> comparer)
}
}
}

public static System.Collections.Immutable.ImmutableArray<TSource> ToImmutableArray<TSource>(this IEnumerable<TSource> items)

Check failure on line 173 in src/DotnetAddons.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Immutable' does not exist in the namespace 'System.Collections' (are you missing an assembly reference?)

Check failure on line 173 in src/DotnetAddons.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Immutable' does not exist in the namespace 'System.Collections' (are you missing an assembly reference?)
{
return System.Collections.Immutable.ImmutableArray.CreateRange(items);
}
}
#endif
28 changes: 13 additions & 15 deletions src/World.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Immutable;

Check failure on line 1 in src/World.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Immutable' does not exist in the namespace 'System.Collections' (are you missing an assembly reference?)

Check failure on line 1 in src/World.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Immutable' does not exist in the namespace 'System.Collections' (are you missing an assembly reference?)
using Microsoft.Collections.Extensions;

namespace TinyEcs;
Expand Down Expand Up @@ -409,7 +410,7 @@ internal FilterQuery(ReadOnlySpan<Archetype> archetypes)
public void Query(QueryFilterDelegateWithEntity fn)
{
var terms = Lookup.Query<TFilter>.Terms;
var query = new QueryInternal(_archetypes, terms);
var query = new QueryInternal(_archetypes, terms.AsSpan());
foreach (var arch in query)
{
foreach (ref readonly var chunk in arch)
Expand All @@ -427,7 +428,7 @@ public void Query(QueryFilterDelegateWithEntity fn)

public QueryIterator GetEnumerator()
{
return new QueryIterator(_archetypes, Lookup.Query<TFilter>.Terms);
return new QueryIterator(_archetypes, Lookup.Query<TFilter>.Terms.AsSpan());
}
}

Expand Down Expand Up @@ -550,7 +551,7 @@ private static int GetSize()
}
}

static void ParseTuple(ITuple tuple, HashSet<Term> terms)
static void ParseTuple(ITuple tuple, SortedSet<Term> terms)
{
for (var i = 0; i < tuple.Length; ++i)
{
Expand All @@ -571,7 +572,7 @@ static void ParseTuple(ITuple tuple, HashSet<Term> terms)
}
}

static void ParseType<T>(HashSet<Term> terms) where T : struct
static void ParseType<T>(SortedSet<Term> terms) where T : struct
{
var type = typeof(T);
if (_typesConvertion.TryGetValue(type, out var id))
Expand All @@ -597,32 +598,29 @@ static void ParseType<T>(HashSet<Term> terms) where T : struct

internal static class Query<TQuery, TFilter> where TQuery : struct where TFilter : struct
{
public static readonly Term[] Terms;
public static readonly Term[] Columns;
public static readonly ImmutableArray<Term> Terms;

Check failure on line 601 in src/World.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'ImmutableArray<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 601 in src/World.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'ImmutableArray<>' could not be found (are you missing a using directive or an assembly reference?)
public static readonly ImmutableArray<Term> Columns;

Check failure on line 602 in src/World.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'ImmutableArray<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 602 in src/World.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'ImmutableArray<>' could not be found (are you missing a using directive or an assembly reference?)

static Query()
{
var list = new HashSet<Term>();
var list = new SortedSet<Term>();
ParseType<TQuery>(list);
Columns = list.ToArray();
Array.Sort(Columns);
Columns = list.ToImmutableArray();

ParseType<TFilter>(list);
Terms = list.ToArray();
Array.Sort(Terms);
Terms = list.ToImmutableArray();
}
}

internal static class Query<T> where T : struct
{
public static readonly Term[] Terms;
public static readonly ImmutableArray<Term> Terms;

Check failure on line 617 in src/World.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'ImmutableArray<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 617 in src/World.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'ImmutableArray<>' could not be found (are you missing a using directive or an assembly reference?)

static Query()
{
var list = new HashSet<Term>();
var list = new SortedSet<Term>();
ParseType<T>(list);
Terms = list.ToArray();
Array.Sort(Terms);
Terms = list.ToImmutableArray();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tools/TinyEcs.Generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public partial {className}{filter}
sb.AppendLine($@"
public void Query<{typeParams}>({delegateName}<{typeParams}> fn) {whereParams}
{{
var terms = Lookup.Query<{(i > 0 ? "(" : "")}{typeParams}{(i > 0 ? ")" : "")}{filterMethod}>.Terms;
var terms = Lookup.Query<{(i > 0 ? "(" : "")}{typeParams}{(i > 0 ? ")" : "")}{filterMethod}>.Terms.AsSpan();
var query = new QueryInternal({(withFilter ? "_archetypes" : "Archetypes")}, terms);
foreach (var arch in query)
{{
Expand Down

0 comments on commit 8aba07e

Please sign in to comment.