﻿namespace RoaringDB.Circuits.Aggregators
{
    //https://www.cs.duke.edu/courses/spring03/cps216/papers/oneil-quass-1997.pdf (Algorithm 3.2)
    public class OneilQuassUnsignedSumAggregator : IAggregator<ulong>
    {
        public static OneilQuassUnsignedSumAggregator Instance { get; } = new OneilQuassUnsignedSumAggregator();

        public ulong Aggregate(IBitmap foundset, params IBitmap[] bsi)
        {
            if (foundset.Cardinality == 0)
                return 0;

            ulong result = 0;
            for (int i = 0; i < bsi.Length; i++)
                result += (1UL << i) * bsi[i].And(foundset).Cardinality;
            return result;
        }
    }
}
