<commit_before>/**\file
 *
 * \copyright
 * Copyright (c) 2012-2014, ef.gy Project Members
 * \copyright
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * \copyright
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * \copyright
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * \see Project Documentation: http://ef.gy/documentation/libefgy
 * \see Project Source Code: http://git.becquerel.org/jyujin/libefgy.git
 */

#if !defined(EF_GY_STATISTICS_H)
#define EF_GY_STATISTICS_H

#include <array>
#include <ef.gy/maybe.h>
#include <numeric>

using std::accumulate;

namespace efgy
{
    namespace statistics
    {
        template<typename Q>
        static maybe<Q> average (const std::vector<Q> &input)
        {
            if (input.size() == 0)
            {
                return maybe<Q>();
            }

            Q s = 0;
            for (const Q &i : input)
            {
                s += i;
            }
            return maybe<Q>(s / Q(input.size()));
        }

        /**
         * Calculates the variance of a range.
         *
         * \tparam T The type of the actual values.
         * \tparam _InputIterator The type of iterators for begin and end.
         *
         * \param _InputIterator begin The start of the range.
         * \param _InputIterator end The end of the range.
         *
         * \code
         * vector<double> list = {1.0, 2.0, 1.0};
         * double var = variance<double>(list.begin(), list.end());
         * \endcode
         *
         * \return T Result of the calculat ion.
         */
        template<typename T, typename _InputIterator>
        static maybe<T> variance(_InputIterator begin, _InputIterator end)
        {
            if (begin == end)
            {
                return maybe<T>();
            }

            struct square_functor {
                T operator()(T first, T element) { return first + element * element; };
            } square;

            T sum = accumulate(begin, end, 0.0);
            // TODO Require C14 to be able to use a lambda expression here.
            T sum_square = accumulate(begin, end, 0.0, square);
            unsigned int count_elements = end - begin;

            T var = (sum_square - sum * sum / count_elements) / count_elements;

            return maybe<T>(var);
        }

        /**
         * Calculates the variance for a vector.
         */
        template<typename T>
        static maybe<T> variance(const std::vector<T> &input)
        {
            return variance<T>(input.begin(), input.end());
        }
    };
};

#endif<commit_msg>don't use functor but a lambda<commit_after>/**\file
 *
 * \copyright
 * Copyright (c) 2012-2014, ef.gy Project Members
 * \copyright
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * \copyright
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * \copyright
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * \see Project Documentation: http://ef.gy/documentation/libefgy
 * \see Project Source Code: http://git.becquerel.org/jyujin/libefgy.git
 */

#if !defined(EF_GY_STATISTICS_H)
#define EF_GY_STATISTICS_H

#include <array>
#include <ef.gy/maybe.h>
#include <numeric>

using std::accumulate;

namespace efgy
{
    namespace statistics
    {
        template<typename Q>
        static maybe<Q> average (const std::vector<Q> &input)
        {
            if (input.size() == 0)
            {
                return maybe<Q>();
            }

            Q s = 0;
            for (const Q &i : input)
            {
                s += i;
            }
            return maybe<Q>(s / Q(input.size()));
        }

        /**
         * Calculates the variance of a range.
         *
         * \tparam T The type of the actual values.
         * \tparam _InputIterator The type of iterators for begin and end.
         *
         * \param _InputIterator begin The start of the range.
         * \param _InputIterator end The end of the range.
         *
         * \code
         * vector<double> list = {1.0, 2.0, 1.0};
         * double var = variance<double>(list.begin(), list.end());
         * \endcode
         *
         * \return T Result of the calculat ion.
         */
        template<typename T, typename _InputIterator>
        static maybe<T> variance(_InputIterator begin, _InputIterator end)
        {
            if (begin == end)
            {
                return maybe<T>();
            }

            T sum = accumulate(begin, end, 0.0);
            T sum_square = accumulate(begin, end, 0.0, [](T first, T element) { return first + element * element; });
            unsigned int count_elements = end - begin;

            T var = (sum_square - sum * sum / count_elements) / count_elements;

            return maybe<T>(var);
        }

        /**
         * Calculates the variance for a vector.
         */
        template<typename T>
        static maybe<T> variance(const std::vector<T> &input)
        {
            return variance<T>(input.begin(), input.end());
        }
    };
};

#endif
{'fraction_non_alphanumeric': 0.09422403733955659, 'fraction_numerical': 0.006126021003500583, 'mean_word_length': 2.128193430656934, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2102647', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1810, 'n_words': 713}