Skip to content

Class Filter ​

Namespace: KenseiECS

Cached query result — holds a dense list of entity indices that match the Include/Exclude/Any component constraints.

Updated reactively by World when components are added/removed. Iteration is zero-allocation via struct enumerator.

Internally a sparse set (dense + sparse) without data — only entity indices.

csharp
public class Filter

Inheritance ​

object ← Filter

Inherited Members ​

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Properties ​

AllocatedBytes ​

Bytes held by the sparse and dense arrays.

csharp
public long AllocatedBytes { get; }

Property Value ​

long

AnyTypes ​

Type indices of which at least one is required.

csharp
public ReadOnlySpan<int> AnyTypes { get; }

Property Value ​

ReadOnlySpan<int>

Count ​

Number of entities currently matching.

csharp
public int Count { get; }

Property Value ​

int

DenseCapacity ​

Capacity of the dense entity array.

csharp
public int DenseCapacity { get; }

Property Value ​

int

Entities ​

Matching entity indices as a span; order is unspecified. Valid until the next structural change that adds an entity to the filter or removes one from it, and until a reserve that grows the filter (Reserve(), Reserve() of one of its types, a ReserveEntities() that raises the reserve it takes from its types, the obsolete Reserve()): growth moves the entities to a new array and fills the old one with -1, so a span taken before it reads -1 from then on. Take a new span afterwards.

csharp
public ReadOnlySpan<int> Entities { get; }

Property Value ​

ReadOnlySpan<int>

ExcludedTypes ​

Type indices excluded by this filter.

csharp
public ReadOnlySpan<int> ExcludedTypes { get; }

Property Value ​

ReadOnlySpan<int>

IncludedTypes ​

Type indices required by this filter.

csharp
public ReadOnlySpan<int> IncludedTypes { get; }

Property Value ​

ReadOnlySpan<int>

IsEmpty ​

True when no entity matches.

csharp
public bool IsEmpty { get; }

Property Value ​

bool

ReservedCount ​

Entities this filter is reserved for, 0 while it is not reserved: the larger of the highest count passed to Reserve() and the reserve it takes from its component types. A filter whose Inc types are all reserved (Reserve()) takes the smallest of their reserves, since a matching entity has every one of them; when its Any types are all reserved too, at most the sum of theirs. A filter without Inc types takes the sum of its Any types' reserves when they are all reserved. Once the world reserved entity slots, the reserve taken from the types is at most ReservedEntities: no filter holds more entities than are alive. This holds for a filter created after its types were reserved as well. The reserve taken from the types follows their reserves as they are now, whatever the order of the calls, so a later ReserveEntities() can also bring it down; the arrays keep the larger size. A reserved filter has room for that many entities and covers ReservedEntities with its sparse pages, extended by a later ReserveEntities().

csharp
public int ReservedCount { get; }

Property Value ​

int

Methods ​

AddListener(IFilterListener) ​

Register a listener notified when entities enter or leave this filter.

csharp
public void AddListener(IFilterListener listener)

Parameters ​

listener IFilterListener

Contains(int) ​

Check if entity is currently in this filter. O(1).

csharp
public bool Contains(int entityIndex)

Parameters ​

entityIndex int

Returns ​

bool

First() ​

Index of some matching entity. Throws when the filter is empty.

csharp
public int First()

Returns ​

int

GetEnumerator() ​

Get enumerator for foreach. No heap allocation.

csharp
public Filter.Enumerator GetEnumerator()

Returns ​

Filter.Enumerator

RemoveListener(IFilterListener) ​

Unregister a filter listener.

csharp
public void RemoveListener(IFilterListener listener)

Parameters ​

listener IFilterListener

Reserve(int) ​

Reserve this filter for count matching entities: its dense entity array gets room for exactly count (it does not round up), and its sparse pages cover ReservedEntities (or the indices below count, when that is larger), also after a later ReserveEntities(). Raises ReservedCount; never shrinks. Allocates; call at load time.

csharp
public void Reserve(int count)

Parameters ​

count int

Number of matching entities to have room for. 0 does nothing.

Remarks ​

A filter whose Inc types are all reserved is reserved by them already (see ReservedCount): call this for a filter that needs more room than that, or one over a type that is not reserved. Safe during iteration: a running enumerator switches to the new array as after any other growth. A span from Entities taken before a Reserve that grows the array is invalid afterwards (it reads -1), as after any growth.

Exceptions ​

ArgumentOutOfRangeException

When count is negative.

ObjectDisposedException

When the filter's world was destroyed.

OutOfMemoryException

When an array cannot be allocated, which includes int.MaxValue (the dense array needs one slot more than count). The filter keeps its old arrays and its reserve.

Single() ​

Index of the only matching entity. Throws unless exactly one entity matches.

csharp
public int Single()

Returns ​

int

ToString() ​

Describes the filter constraints by component name, e.g. Filter Inc<A, B>.

csharp
public override string ToString()

Returns ​

string

TryGetFirst(out int) ​

Index of some matching entity, or false when the filter is empty.

csharp
public bool TryGetFirst(out int entityIndex)

Parameters ​

entityIndex int

Returns ​

bool