<commit_before>/*******************************************************
 * Copyright (c) 2014, ArrayFire
 * All rights reserved.
 *
 * This file is distributed under 3-clause BSD license.
 * The complete license agreement can be obtained at:
 * http://arrayfire.com/licenses/BSD-3-Clause
 ********************************************************/

#include <af/dim4.hpp>
#include <af/defines.h>
#include <af/statistics.h>
#include <af/index.h>
#include <af/arith.h>
#include <af/data.h>
#include <handle.hpp>
#include <err_common.hpp>
#include <backend.hpp>
#include <sort.hpp>
#include <math.hpp>
#include <cast.hpp>

using namespace detail;
using af::dim4;

template<typename T>
static double median(const af_array& in)
{
    dim_t nElems = getInfo(in).elements();
    double mid      = (nElems + 1) / 2;
    af_seq mdSpan[1]= {af_make_seq(mid-1, mid, 1)};
    dim4 dims(nElems, 1, 1, 1);

    af_array temp = 0;
    AF_CHECK(af_moddims(&temp, in, 1, dims.get()));
    const Array<T> input  = getArray<T>(temp);

    Array<T> sortedArr = sort<T, true>(input, 0);

    double result;
    T resPtr[2];
    af_array res = 0;
    AF_CHECK(af_index(&res, getHandle<T>(sortedArr), 1, mdSpan));
    AF_CHECK(af_get_data_ptr((void*)&resPtr, res));

    if (nElems % 2 == 1) {
        result = resPtr[0];
    } else {
        if (input.isFloating()) {
            result = division(resPtr[0] + resPtr[1], 2);
        } else {
            result = division((float)resPtr[0] + (float)resPtr[1], 2);
        }
    }

    AF_CHECK(af_release_array(res));
    AF_CHECK(af_release_array(temp));

    return result;
}

template<typename T>
static af_array median(const af_array& in, const dim_t dim)
{
    const Array<T> input = getArray<T>(in);
    Array<T> sortedIn   = sort<T, true>(input, dim);

    int nElems    = input.dims()[0];
    double mid    = (nElems + 1) / 2;
    af_array left = 0;

    af_seq slices[4] = {af_span, af_span, af_span, af_span};
    slices[dim] = af_make_seq(mid-1.0, mid-1.0, 1.0);

    af_array sortedIn_handle = getHandle<T>(sortedIn);
    AF_CHECK(af_index(&left, sortedIn_handle, input.ndims(), slices));

    if (nElems % 2 == 1) {
        // mid-1 is our guy
        if (input.isFloating()) return left;

        // Return as floats for consistency<commit_msg>Memory leak fix in af_median_all<commit_after>/*******************************************************
 * Copyright (c) 2014, ArrayFire
 * All rights reserved.
 *
 * This file is distributed under 3-clause BSD license.
 * The complete license agreement can be obtained at:
 * http://arrayfire.com/licenses/BSD-3-Clause
 ********************************************************/

#include <af/dim4.hpp>
#include <af/defines.h>
#include <af/statistics.h>
#include <af/index.h>
#include <af/arith.h>
#include <af/data.h>
#include <handle.hpp>
#include <err_common.hpp>
#include <backend.hpp>
#include <sort.hpp>
#include <math.hpp>
#include <cast.hpp>

using namespace detail;
using af::dim4;

template<typename T>
static double median(const af_array& in)
{
    dim_t nElems = getInfo(in).elements();
    double mid      = (nElems + 1) / 2;
    af_seq mdSpan[1]= {af_make_seq(mid-1, mid, 1)};
    dim4 dims(nElems, 1, 1, 1);

    af_array temp = 0;
    AF_CHECK(af_moddims(&temp, in, 1, dims.get()));
    const Array<T> input  = getArray<T>(temp);

    Array<T> sortedArr = sort<T, true>(input, 0);

    af_array sarrHandle = getHandle<T>(sortedArr);

    double result;
    T resPtr[2];
    af_array res = 0;
    AF_CHECK(af_index(&res, sarrHandle, 1, mdSpan));
    AF_CHECK(af_get_data_ptr((void*)&resPtr, res));

    AF_CHECK(af_release_array(res));
    AF_CHECK(af_release_array(sarrHandle));
    AF_CHECK(af_release_array(temp));

    if (nElems % 2 == 1) {
        result = resPtr[0];
    } else {
        if (input.isFloating()) {
            result = division(resPtr[0] + resPtr[1], 2);
        } else {
            result = division((float)resPtr[0] + (float)resPtr[1], 2);
        }
    }

    return result;
}

template<typename T>
static af_array median(const af_array& in, const dim_t dim)
{
    const Array<T> input = getArray<T>(in);
    Array<T> sortedIn   = sort<T, true>(input, dim);

    int nElems    = input.dims()[0];
    double mid    = (nElems + 1) / 2;
    af_array left = 0;

    af_seq slices[4] = {af_span, af_span, af_span, af_span};
    slices[dim] = af_make_seq(mid-1.0, mid-1.0, 1.0);

    af_array sortedIn_handle = getHandle<T>(sortedIn);
    AF_CHECK(af_index(&left, sortedIn_handle, input.ndims(), slices));

    if (nElems % 2 == 1) {
        // mid-1 is our guy
        if (input.isFloating()) return left;

        // Return as floats for consistency
{'fraction_non_alphanumeric': 0.1929336188436831, 'fraction_numerical': 0.019700214132762312, 'mean_word_length': 3.394167450611477, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, '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': '2808335', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1671, 'n_words': 395}