T median()

in Source/Shared/arcana/algorithm.h [214:238]


    T median(const IteratorT& begin, const IteratorT& end, ModifierT&& modifier)
    {
        size_t length = std::distance(begin, end);

        if (length == 0)
        {
            assert(false && "no median for an empty data set");
            return {};
        }

        if (length % 2 == 1)
        {
            auto idx = length / 2;
            auto nth = begin + idx;
            std::nth_element(begin, nth, end);
            return modifier(*nth);
        }
        else
        {
            auto nth = begin + length / 2;
            std::nth_element(begin, nth, end);
            auto nth_minus_one = std::max_element(begin, nth);
            return (modifier(*nth_minus_one) + modifier(*nth)) / 2;
        }
    }