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.
public class FilterInheritance
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.
public long AllocatedBytes { get; }Property Value
AnyTypes
Type indices of which at least one is required.
public ReadOnlySpan<int> AnyTypes { get; }Property Value
Count
Number of entities currently matching.
public int Count { get; }Property Value
DenseCapacity
Capacity of the dense entity array.
public int DenseCapacity { get; }Property Value
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.
public ReadOnlySpan<int> Entities { get; }Property Value
ExcludedTypes
Type indices excluded by this filter.
public ReadOnlySpan<int> ExcludedTypes { get; }Property Value
IncludedTypes
Type indices required by this filter.
public ReadOnlySpan<int> IncludedTypes { get; }Property Value
IsEmpty
True when no entity matches.
public bool IsEmpty { get; }Property Value
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().
public int ReservedCount { get; }Property Value
Methods
AddListener(IFilterListener)
Register a listener notified when entities enter or leave this filter.
public void AddListener(IFilterListener listener)Parameters
listener IFilterListener
Contains(int)
Check if entity is currently in this filter. O(1).
public bool Contains(int entityIndex)Parameters
entityIndex int
Returns
First()
Index of some matching entity. Throws when the filter is empty.
public int First()Returns
GetEnumerator()
Get enumerator for foreach. No heap allocation.
public Filter.Enumerator GetEnumerator()Returns
RemoveListener(IFilterListener)
Unregister a filter listener.
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.
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
When count is negative.
When the filter's world was destroyed.
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.
public int Single()Returns
ToString()
Describes the filter constraints by component name, e.g. Filter Inc<A, B>.
public override string ToString()Returns
TryGetFirst(out int)
Index of some matching entity, or false when the filter is empty.
public bool TryGetFirst(out int entityIndex)Parameters
entityIndex int
