Skip to content

MapReduceQuery

Martin Evans edited this page Jul 29, 2024 · 1 revision

A Map/Reduce query executes two stages:

  • A "mapper" which accepts all of the component references and returns a value
  • A "reducer" accepts all the values and combines them into one final result value

For example, to get the sum of all components of a certain type:

public record struct ComponentInt(int Value) : IComponent;

private struct Mapper
    : IQueryMap1<int, ComponentInt32>
{
    public readonly int Execute(Entity e, ref ComponentInt32 t0) => t0.Value;
}

private struct Reducer
     : IQueryReduce1<int>
{
    public int Reduce(int a, int b) => a + b;
}

world.ExecuteMapReduce<Mapper, Reducer, int, ComponentInt>(new Mapper(), new Reducer(), 0);
Clone this wiki locally