<commit_before>#include "Python.h"
#include "structmember.h"
#include "numpy/noprefix.h"

static PyObject *ErrorObject;
#define Py_Try(BOOLEAN) {if (!(BOOLEAN)) goto fail;}
#define Py_Assert(BOOLEAN,MESS) {if (!(BOOLEAN)) {      \
            PyErr_SetString(ErrorObject, (MESS));       \
            goto fail;}                                 \
    }

#define PYSETERROR(message) \
{ PyErr_SetString(ErrorObject, message); goto fail; }

static intp
incr_slot_ (double x, double *bins, intp lbins)
{
    intp i ;
    for ( i = 0 ; i < lbins ; i ++ )
        if ( x < bins [i] )
            return i ;
    return lbins ;
}

static intp
decr_slot_ (double x, double * bins, intp lbins)
{
    intp i ;
    for ( i = lbins - 1 ; i >= 0; i -- )
        if (x < bins [i])
            return i + 1 ;
    return 0 ;
}

static int
monotonic_ (double * a, int lena)
{
    int i;
    if (a [0] <= a [1]) /* possibly monotonic increasing */
        {
            for (i = 1 ; i < lena - 1; i ++)
                if (a [i] > a [i + 1]) return 0 ;
            return 1 ;
        }
    else              /* possibly monotonic decreasing */
        {
            for (i = 1 ; i < lena - 1; i ++)
                if (a [i] < a [i + 1]) return 0 ;
            return -1 ;
        }
}



static intp
mxx (intp *i , intp len)
{
    /* find the index of the maximum element of an integer array */
    intp mx = 0, max = i[0] ;
    intp j ;
    for ( j = 1 ; j < len; j ++ )
        if ( i [j] > max )
            {max = i [j] ;
                mx = j ;}
    return mx;
}

static intp
mnx (intp *i , intp len)
{
    /* find the index of the minimum element of an integer array */
    intp mn = 0, min = i [0] ;
    intp j ;
    for ( j = 1 ; j < len; j ++ )
        if ( i [j] < min )
            {min = i [j] ;
                mn = j ;}
    return mn;
}


static PyObject *
arr_bincount(PyObject *self, PyObject *args, PyObject *kwds)
{
    /* histogram accepts one or two arguments. The first is an array
     * of non-negative integers and the second, if present, is an
     * array of weights, which must be promotable to double.
     * Call these arguments list and weight. Both must be one-
     * dimensional. len (weight) == len(list)
     * If weight is not present:
     *   histogram (list) [i] is the number of occurrences of i in list.
     * If weight is present:
     *   histogram (list, weight) [i] is the sum of all weight [j]
     * where list [j] == i.                                              */
    /* self is not used */
    PyArray_Descr *type;
    PyObject *list = NULL, *weight=Py_None ;
    PyObject *lst=NULL, *ans=NULL, *wts=NULL;
    intp *numbers, *ians, len , mxi, mni, ans_size;
    int i;
    double *weights , *dans;
    static char *kwlist[] = {"list", "weights", NULL};


    Py_Try(PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist,
                                       &list, &weight));
    Py_Try(lst = PyArray_ContiguousFromAny(list, PyArray_INTP, 1, 1));
    len = PyArray_SIZE(lst);
    numbers = (intp *) PyArray_DATA(lst);
    mxi = mxx (numbers, len) ;
    mni = mnx (numbers, len) ;
    Py_Assert(numbers[mni] >= 0,
              "irst argument of bincount must be non-negative");
    ans_size = numbers [mxi] + 1 ;
    type = PyArray_DescrFromType(PyArray_INTP);
    if (weight == Py_None) {
        Py_Try(ans = PyArray_Zeros(1, &ans_size, type, 0));
        ians = (intp *)(PyArray_DATA(ans));
        for (i = 0 ; i < len ; i++)
            ians [numbers [i]] += 1 ;
        Py_DECREF(lst);
    }
    else {
        Py_Try(wts = PyArray_ContiguousFromAny(weight,
                                               PyArray_DOUBLE, 1, 1));
        weights = (double *)PyArray_DATA (wts);
        Py_Assert(PyArray_SIZE(wts) == len, "bincount: length of weights " \
                  "does not match that of list");
        type = PyArray_DescrFromType(PyArray_DOUBLE);
        Py_Try(ans = PyArray_Zeros(1, &ans_size, type, 0));
        dans = (double *)PyArray_DATA (ans);
        for (i = 0 ; i < len ; i++) {
            dans[numbers[i]] += weights[i];
        }
        Py_DECREF(lst);
        Py_DECREF(wts);
    }
    return ans;

 fail:
    Py_XDECREF(lst);
    Py_XDECREF(wts);
    Py_XDECREF(ans);
    return NULL;
}


static PyObject *
arr_digitize(PyObject *self, PyObject *args, PyObject *kwds)
{
    /* digitize (x, bins) returns an array of python integers the same
       length of x. The values i returned are such that
       bins [i - 1] <= x < bins [i] if bins is monotonically increasing,
       or bins [i - 1] > x >= bins [i] if bins is monotonically decreasing.
       Beyond the bounds of bins, returns either i = 0 or i = len (bins)
       as appropriate.                                                      */
    /* self is not used */
    PyObject *ox, *obins ;
    PyObject *ax=NULL, *abins=NULL, *aret=NULL;
    double *dx, *dbins ;
    intp lbins, lx ;             /* lengths */
    intp *iret;
    int m, i ;
    static char *kwlist[] = {"x", "bins", NULL};
    PyArray_Descr *type;

    Py_Try(PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist,
                                       &ox, &obins));

    type = PyArray_DescrFromType(PyArray_DOUBLE);
    Py_Try(ax=PyArray_FromAny(ox, type, 1, 1, CARRAY, NULL));
    Py_INCREF(type);
    Py_Try(abins = PyArray_FromAny(obins, type, 1, 1, CARRAY, NULL));

    lx = PyArray_SIZE(ax);
    dx = (double *)PyArray_DATA(ax);
    lbins = PyArray_SIZE(abins);
    dbins = (double *)PyArray_DATA(abins);
    Py_Try(aret = PyArray_SimpleNew(1, &lx, PyArray_INTP));
    iret = (intp *)PyArray_DATA(aret);

    Py_Assert(lx > 0 && lbins > 0,
              "x and bins both must have non-zero length");

    if (lbins == 1)  {
        for (i=0 ; i<lx ; i++)
            if (dx [i] >= dbins[0])<commit_msg>Fix histogram references in arr_bincount.<commit_after>#include "Python.h"
#include "structmember.h"
#include "numpy/noprefix.h"

static PyObject *ErrorObject;
#define Py_Try(BOOLEAN) {if (!(BOOLEAN)) goto fail;}
#define Py_Assert(BOOLEAN,MESS) {if (!(BOOLEAN)) {      \
            PyErr_SetString(ErrorObject, (MESS));       \
            goto fail;}                                 \
    }

#define PYSETERROR(message) \
{ PyErr_SetString(ErrorObject, message); goto fail; }

static intp
incr_slot_ (double x, double *bins, intp lbins)
{
    intp i ;
    for ( i = 0 ; i < lbins ; i ++ )
        if ( x < bins [i] )
            return i ;
    return lbins ;
}

static intp
decr_slot_ (double x, double * bins, intp lbins)
{
    intp i ;
    for ( i = lbins - 1 ; i >= 0; i -- )
        if (x < bins [i])
            return i + 1 ;
    return 0 ;
}

static int
monotonic_ (double * a, int lena)
{
    int i;
    if (a [0] <= a [1]) /* possibly monotonic increasing */
        {
            for (i = 1 ; i < lena - 1; i ++)
                if (a [i] > a [i + 1]) return 0 ;
            return 1 ;
        }
    else              /* possibly monotonic decreasing */
        {
            for (i = 1 ; i < lena - 1; i ++)
                if (a [i] < a [i + 1]) return 0 ;
            return -1 ;
        }
}



static intp
mxx (intp *i , intp len)
{
    /* find the index of the maximum element of an integer array */
    intp mx = 0, max = i[0] ;
    intp j ;
    for ( j = 1 ; j < len; j ++ )
        if ( i [j] > max )
            {max = i [j] ;
                mx = j ;}
    return mx;
}

static intp
mnx (intp *i , intp len)
{
    /* find the index of the minimum element of an integer array */
    intp mn = 0, min = i [0] ;
    intp j ;
    for ( j = 1 ; j < len; j ++ )
        if ( i [j] < min )
            {min = i [j] ;
                mn = j ;}
    return mn;
}


static PyObject *
arr_bincount(PyObject *self, PyObject *args, PyObject *kwds)
{
    /* arr_bincount is registered as bincount.
     * arr_bincount accepts one or two arguments. The first is an array of
     * non-negative integers and the second, if present, is an array of
     * weights, which must be promotable to double.  Call these arguments list
     * and weight. Both must be one- dimensional. len (weight) == len(list)
     * If weight is not present:
     *   arr_bincount(self,list)[i] is the number of occurrences of i in list.
     * If weight is present:
     *   arr_bincount(self,list, weight)[i] is the sum of all weight[j]
     * where list [j] == i.
     * Self is not used.
     */
    PyArray_Descr *type;
    PyObject *list = NULL, *weight=Py_None ;
    PyObject *lst=NULL, *ans=NULL, *wts=NULL;
    intp *numbers, *ians, len , mxi, mni, ans_size;
    int i;
    double *weights , *dans;
    static char *kwlist[] = {"list", "weights", NULL};


    Py_Try(PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist,
                                       &list, &weight));
    Py_Try(lst = PyArray_ContiguousFromAny(list, PyArray_INTP, 1, 1));
    len = PyArray_SIZE(lst);
    numbers = (intp *) PyArray_DATA(lst);
    mxi = mxx (numbers, len) ;
    mni = mnx (numbers, len) ;
    Py_Assert(numbers[mni] >= 0,
              "irst argument of bincount must be non-negative");
    ans_size = numbers [mxi] + 1 ;
    type = PyArray_DescrFromType(PyArray_INTP);
    if (weight == Py_None) {
        Py_Try(ans = PyArray_Zeros(1, &ans_size, type, 0));
        ians = (intp *)(PyArray_DATA(ans));
        for (i = 0 ; i < len ; i++)
            ians [numbers [i]] += 1 ;
        Py_DECREF(lst);
    }
    else {
        Py_Try(wts = PyArray_ContiguousFromAny(weight,
                                               PyArray_DOUBLE, 1, 1));
        weights = (double *)PyArray_DATA (wts);
        Py_Assert(PyArray_SIZE(wts) == len, "bincount: length of weights " \
                  "does not match that of list");
        type = PyArray_DescrFromType(PyArray_DOUBLE);
        Py_Try(ans = PyArray_Zeros(1, &ans_size, type, 0));
        dans = (double *)PyArray_DATA (ans);
        for (i = 0 ; i < len ; i++) {
            dans[numbers[i]] += weights[i];
        }
        Py_DECREF(lst);
        Py_DECREF(wts);
    }
    return ans;

 fail:
    Py_XDECREF(lst);
    Py_XDECREF(wts);
    Py_XDECREF(ans);
    return NULL;
}


static PyObject *
arr_digitize(PyObject *self, PyObject *args, PyObject *kwds)
{
    /* digitize (x, bins) returns an array of python integers the same
       length of x. The values i returned are such that bins [i - 1] <= x <
       bins [i] if bins is monotonically increasing, or bins [i - 1] > x >=
       bins [i] if bins is monotonically decreasing.  Beyond the bounds of
       bins, returns either i = 0 or i = len (bins) as appropriate.
    */
    /* self is not used */
    PyObject *ox, *obins ;
    PyObject *ax=NULL, *abins=NULL, *aret=NULL;
    double *dx, *dbins ;
    intp lbins, lx ;             /* lengths */
    intp *iret;
    int m, i ;
    static char *kwlist[] = {"x", "bins", NULL};
    PyArray_Descr *type;

    Py_Try(PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist,
                                       &ox, &obins));

    type = PyArray_DescrFromType(PyArray_DOUBLE);
    Py_Try(ax=PyArray_FromAny(ox, type, 1, 1, CARRAY, NULL));
    Py_INCREF(type);
    Py_Try(abins = PyArray_FromAny(obins, type, 1, 1, CARRAY, NULL));

    lx = PyArray_SIZE(ax);
    dx = (double *)PyArray_DATA(ax);
    lbins = PyArray_SIZE(abins);
    dbins = (double *)PyArray_DATA(abins);
    Py_Try(aret = PyArray_SimpleNew(1, &lx, PyArray_INTP));
    iret = (intp *)PyArray_DATA(aret);

    Py_Assert(lx > 0 && lbins > 0,
              "x and bins both must have non-zero length");

    if (lbins == 1)  {
        for (i=0 ; i<lx ; i++)
            if (dx [i] >= dbins[0])
{'fraction_non_alphanumeric': 0.13074935400516796, 'fraction_numerical': 0.008440999138673558, 'mean_word_length': 1.9665304036791007, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1660497', 'n_tokens_mistral': 4153, 'n_tokens_neox': 4010, 'n_words': 1229}