<commit_before>import cupy
from cupy.core._reduction import ReductionKernel

# TODO(okuta): Implement delete


# TODO(okuta): Implement insert


# TODO(okuta): Implement append


# TODO(okuta): Implement resize


_first_non_zero_krnl = ReductionKernel(
    'raw S data, T indices, T len, int64 mode',
    'T y',
    '''mode? data[indices] ? indices : len
           : data[len - 1 - indices] ? len - 1 - indices : T(-1)''',
    'mode? min(a,b): max(a, b) ',
    'y = a',
    'mode ? len : -1',
    'first_non_zero'
)
_first_non_zero_cplx_krnl = ReductionKernel(
    'raw S real, raw S imag, T indices, T len, int64 mode',
    'T y',
    '''mode? (real[indices] || imag[indices] ? indices : len)
           : (real[len - 1 - indices] || imag[len - 1 - indices] ?
              len - 1 - indices : T(-1))''',
    'mode? min(a,b): max(a, b) ',
    'y = a',
    'mode ? len : -1',
    'first_non_zero_cplx'
)


def trim_zeros(filt, trim='fb'):
    """Trim the leading and/or trailing zeros from a 1-D array or sequence.

    Returns the trimmed array

    Args:
        filt(1D array or sequence): Input array
        trim(str, optional):
            'fb' default option trims the array from both sides.
            'f' option trim zeros from front.
            'b' option trim zeros from front

    Returns:
        cupy.ndarray: trimmed input

    .. seealso:: :func:`numpy.trim_zeros`
    """
    filt = cupy.asarray(filt)
    start, end = cupy.asarray([0, filt.size - 1], dtype=int)
    indices = cupy.arange(filt.size)
    trim = trim.upper()
    if 'F' in trim:
        if filt.dtype.kind == 'c':
            _first_non_zero_cplx_krnl(
                filt.real, filt.imag, indices, filt.size, 1, start)
        else:
            _first_non_zero_krnl(filt, indices, filt.size, 1, start)
    if 'B' in trim:
        if filt.dtype.kind == 'c':
            _first_non_zero_cplx_krnl(
                filt.real, filt.imag, indices, filt.size, 0, end)
        else:
            _first_non_zero_krnl(filt, indices, filt.size, 0, end)
    return filt[start:end + 1]

<commit_msg>fixing import, single mode, kernel index, ndarray input<commit_after>import cupy

# TODO(okuta): Implement delete


# TODO(okuta): Implement insert


# TODO(okuta): Implement append


# TODO(okuta): Implement resize


_first_nonzero_krnl = cupy.ReductionKernel(
    'raw S data, T indices, T len',
    'T y',
    'data[_j] ? _j : len',
    'min(a, b)',
    'y = a',
    'len',
    'first_nonzero'
)
_first_nonzero_cplx_krnl = cupy.ReductionKernel(
    'raw S real, raw S imag, T indices, T len',
    'T y',
    'real[_j] || imag[_j] ? _j : len',
    'min(a,b)',
    'y = a',
    'len',
    'first_nonzero_cplx'
)


def trim_zeros(filt, trim='fb'):
    """Trim the leading and/or trailing zeros from a 1-D array or sequence.

    Returns the trimmed array

    Args:
        filt(1D array or sequence): Input array
        trim(str, optional):
            'fb' default option trims the array from both sides.
            'f' option trim zeros from front.
            'b' option trim zeros from front

    Returns:
        cupy.ndarray: trimmed input

    .. seealso:: :func:`numpy.trim_zeros`
    """
    start, end = cupy.asarray([0, filt.size - 1], dtype=int)
    indices = cupy.arange(filt.size)
    trim = trim.upper()
    if 'F' in trim:
        if filt.dtype.kind == 'c':
            start = _first_nonzero_cplx_krnl(
                filt.real, filt.imag, indices, filt.size)
        else:
            start = _first_nonzero_krnl(filt, indices, filt.size)
    if 'B' in trim:
        if filt.dtype.kind == 'c':
            end = filt.size - _first_nonzero_cplx_krnl(
                filt[::-1].real, filt[::-1].imag, indices, filt.size) - 1
        else:
            end = filt.size - _first_nonzero_krnl(
                filt[::-1], indices, filt.size) - 1
    return filt[start:end + 1]


{'fraction_non_alphanumeric': 0.13440165417420522, 'fraction_numerical': 0.008270871026104937, 'mean_word_length': 2.282442748091603, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4645555', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1376, 'n_words': 376}