<commit_before>/*
 * Functions.h
 *
 *  Created on: 02.01.2013
 *      Author: <NAME> (<EMAIL>)
 */

#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_

#include <algorithm>

// INFO: see http://stackoverflow.com/questions/14199798/very-generic-argmax-function-in-c-wanted for discussion on argmax function

/**
 * Given
 * 		@param[in]	cont	a container
 * 		@param[in]	func	a function mapping an element from the container to a value
 * 	, return the element from the container for which the function maps to the maximum value.
 */
template<typename Container, typename F>
auto argmax(Container const & container, F && func) -> decltype(*std::begin(container))
{
    typedef decltype(*std::begin(container)) V;
    auto cmp = [&](V a, V b){ return func(a) < func(b); };
    return *std::max_element(std::begin(container), std::end(container), cmp);
}

/*
template<typename Iter, typename Function>
Iter argmax2(Iter begin, Iter end, Function f)
{
    typedef typename Iter::value_type T;
    return std::min_element(begin, end, [&value](const T& a, const T& b){
        return f(a) < f(b);
    });
}
*/


<commit_msg>replace call-by-value with call-by-ref in lambda<commit_after>/*
 * Functions.h
 *
 *  Created on: 02.01.2013
 *      Author: <NAME> (<EMAIL>)
 */

#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_

#include <algorithm>

// INFO: see http://stackoverflow.com/questions/14199798/very-generic-argmax-function-in-c-wanted for discussion on argmax function

/**
 * Given
 * 		@param[in]	cont	a container
 * 		@param[in]	func	a function mapping an element from the container to a value
 * 	, return the element from the container for which the function maps to the maximum value.
 */
template<typename Container, typename F>
auto argmax(Container const & container, F && func) -> decltype(*std::begin(container))
{
    typedef decltype(*std::begin(container)) V;
    auto cmp = [&](const V& a, const V& b){ return func(a) < func(b); };
    return *std::max_element(std::begin(container), std::end(container), cmp);
}

/*
template<typename Iter, typename Function>
Iter argmax2(Iter begin, Iter end, Function f)
{
    typedef typename Iter::value_type T;
    return std::min_element(begin, end, [&value](const T& a, const T& b){
        return f(a) < f(b);
    });
}
*/



{'fraction_non_alphanumeric': 0.14292015880017644, 'fraction_numerical': 0.014997794441993825, 'mean_word_length': 3.995594713656388, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '6514075', 'n_tokens_mistral': 784, 'n_tokens_neox': 736, 'n_words': 225}