<commit_before>/*
 * Copyright(c) Sophist Solutions, Inc. 1990-2017.  All rights reserved
 */
#ifndef _Stroika_Foundation_Math_Statistics_inl_
#define _Stroika_Foundation_Math_Statistics_inl_ 1

/*
 ********************************************************************************
 ***************************** Implementation Details ***************************
 ********************************************************************************
 */
#include <algorithm>

#include "../Debug/Assertions.h"
#include "../Memory/SmallStackBuffer.h"
#include "Common.h"

namespace Stroika {
    namespace Foundation {
        namespace Math {

            /*
             ********************************************************************************
             ************************************ Mean **************************************
             ********************************************************************************
             */
            template <typename ITERATOR_OF_T, typename RESULT_TYPE>
            RESULT_TYPE Mean (ITERATOR_OF_T start, ITERATOR_OF_T end)
            {
                Require (start != end);
                unsigned int cnt{};
                RESULT_TYPE  result{};
                for (ITERATOR_OF_T i = start; i != end; ++i) {
                    result += *i;
                    cnt++;
                }
                return result / cnt;
            }
            template <typename CONTAINER_OF_T, typename RESULT_TYPE>
            inline RESULT_TYPE Mean (const CONTAINER_OF_T& container)
            {
                Require (not container.empty ());
                using ITERATOR_TYPE = decltype (begin (container));
                return Mean<ITERATOR_TYPE, RESULT_TYPE> (begin (container), end (container));
            }

            /*
             ********************************************************************************
             ********************************** Median **************************************
             ********************************************************************************
             */
            template <typename ITERATOR_OF_T, typename RESULT_TYPE>
            RESULT_TYPE Median (ITERATOR_OF_T start, ITERATOR_OF_T end)
            {
                Require (start != end);
                Memory::SmallStackBuffer<RESULT_TYPE> tmp{start, end}; // copy cuz data modified
                size_t                                size = distance (start, end);
                nth_element (tmp.begin (), tmp.begin () + size / 2, tmp.end ());
                RESULT_TYPE result{tmp[size / 2]};
                if ((size % 2) == 0) {
                    Assert (size >= 2); // cuz require at start >=1 and since even
                    // NB: Could use sort instead of nth_element, and some on the web suggest faster, but sort is nlogn, and nth_elemnet is O(n) (even
                    // when you do it twice.
                    nth_element (tmp.begin (), tmp.begin () + size / 2 - 1, tmp.end ());
                    result += tmp[size / 2 - 1];
                    result /= static_cast<RESULT_TYPE> (2);
                }
                return result;
            }
            template <typename CONTAINER_OF_T, typename RESULT_TYPE>
            inline RESULT_TYPE Median (const CONTAINER_OF_T& container)
            {
                Require (not container.empty ());
                using ITERATOR_TYPE = decltype (begin (container));
                return Median<ITERATOR_TYPE, RESULT_TYPE> (begin (container), end (container));
            }

            /*
             ********************************************************************************
             **************************** StandardDeviation *********************************
             ********************************************************************************
             */
            template <typename ITERATOR_OF_T, typename RESULT_TYPE>
            RESULT_TYPE StandardDeviation (ITERATOR_OF_T start, ITERATOR_OF_T end)
            {
                RESULT_TYPE mean  = Mean (star, t end);
                RESULT_TYPE accum = 0.0;
                size_t      n{};
                for (auto i = start; i !end; i++) {
                    n++;
                    accum += (d - m) * (d - m);
                }
                return sqrt (accum / n);
            }
            template <typename CONTAINER_OF_T, typename RESULT_TYPE>
            RESULT_TYPE StandardDeviation (const CONTAINER_OF_T& container)
            {
                Require (not container.empty ());
                using ITERATOR_TYPE = decltype (begin (container));
                return StandardDeviation<ITERATOR_TYPE, RESULT_TYPE> (begin (container), end (container));
            }
        }
    }
}
#endif /*_Stroika_Foundation_Math_Statistics_inl_*/
<commit_msg>fixed typo<commit_after>/*
 * Copyright(c) Sophist Solutions, Inc. 1990-2017.  All rights reserved
 */
#ifndef _Stroika_Foundation_Math_Statistics_inl_
#define _Stroika_Foundation_Math_Statistics_inl_ 1

/*
 ********************************************************************************
 ***************************** Implementation Details ***************************
 ********************************************************************************
 */
#include <algorithm>

#include "../Debug/Assertions.h"
#include "../Memory/SmallStackBuffer.h"
#include "Common.h"

namespace Stroika {
    namespace Foundation {
        namespace Math {

            /*
             ********************************************************************************
             ************************************ Mean **************************************
             ********************************************************************************
             */
            template <typename ITERATOR_OF_T, typename RESULT_TYPE>
            RESULT_TYPE Mean (ITERATOR_OF_T start, ITERATOR_OF_T end)
            {
                Require (start != end);
                unsigned int cnt{};
                RESULT_TYPE  result{};
                for (ITERATOR_OF_T i = start; i != end; ++i) {
                    result += *i;
                    cnt++;
                }
                return result / cnt;
            }
            template <typename CONTAINER_OF_T, typename RESULT_TYPE>
            inline RESULT_TYPE Mean (const CONTAINER_OF_T& container)
            {
                Require (not container.empty ());
                using ITERATOR_TYPE = decltype (begin (container));
                return Mean<ITERATOR_TYPE, RESULT_TYPE> (begin (container), end (container));
            }

            /*
             ********************************************************************************
             ********************************** Median **************************************
             ********************************************************************************
             */
            template <typename ITERATOR_OF_T, typename RESULT_TYPE>
            RESULT_TYPE Median (ITERATOR_OF_T start, ITERATOR_OF_T end)
            {
                Require (start != end);
                Memory::SmallStackBuffer<RESULT_TYPE> tmp{start, end}; // copy cuz data modified
                size_t                                size = distance (start, end);
                nth_element (tmp.begin (), tmp.begin () + size / 2, tmp.end ());
                RESULT_TYPE result{tmp[size / 2]};
                if ((size % 2) == 0) {
                    Assert (size >= 2); // cuz require at start >=1 and since even
                    // NB: Could use sort instead of nth_element, and some on the web suggest faster, but sort is nlogn, and nth_elemnet is O(n) (even
                    // when you do it twice.
                    nth_element (tmp.begin (), tmp.begin () + size / 2 - 1, tmp.end ());
                    result += tmp[size / 2 - 1];
                    result /= static_cast<RESULT_TYPE> (2);
                }
                return result;
            }
            template <typename CONTAINER_OF_T, typename RESULT_TYPE>
            inline RESULT_TYPE Median (const CONTAINER_OF_T& container)
            {
                Require (not container.empty ());
                using ITERATOR_TYPE = decltype (begin (container));
                return Median<ITERATOR_TYPE, RESULT_TYPE> (begin (container), end (container));
            }

            /*
             ********************************************************************************
             **************************** StandardDeviation *********************************
             ********************************************************************************
             */
            template <typename ITERATOR_OF_T, typename RESULT_TYPE>
            RESULT_TYPE StandardDeviation (ITERATOR_OF_T start, ITERATOR_OF_T end)
            {
                RESULT_TYPE mean  = Mean (start, end);
                RESULT_TYPE accum = 0.0;
                size_t      n{};
                for (auto i = start; i !end; i++) {
                    n++;
                    accum += (d - m) * (d - m);
                }
                return sqrt (accum / n);
            }
            template <typename CONTAINER_OF_T, typename RESULT_TYPE>
            RESULT_TYPE StandardDeviation (const CONTAINER_OF_T& container)
            {
                Require (not container.empty ());
                using ITERATOR_TYPE = decltype (begin (container));
                return StandardDeviation<ITERATOR_TYPE, RESULT_TYPE> (begin (container), end (container));
            }
        }
    }
}
#endif /*_Stroika_Foundation_Math_Statistics_inl_*/

{'fraction_non_alphanumeric': 0.25921350627959644, 'fraction_numerical': 0.004529544986617253, 'mean_word_length': 1.9501973883996355, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 31, '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': '2009436', 'n_tokens_mistral': 2368, 'n_tokens_neox': 2109, 'n_words': 554}