<commit_before>package toxi.util.datatypes;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;

/**
 * A collection of array utilities.
 * 
 * @author toxi
 */
public class ArrayUtil {

    /**
     * Adds all array elements to the given collection of the same type.
     * 
     * @param <T>
     * @param array
     *            array
     * @param collection
     *            existing collection or null (to create a new {@link ArrayList}
     *            automatically)
     */
    public static <T> void addArrayToCollection(T[] array,
            Collection<T> collection) {
        if (collection == null) {
            collection = new ArrayList<T>();
        }
        for (T o : array) {
            collection.add(o);
        }
    }

    /**
     * Converts the generic array into an {@link ArrayList} of the same type.
     * 
     * @param array
     * @return array list version
     */
    public static <T> ArrayList<T> arrayToList(T[] array) {
        ArrayList<T> list = new ArrayList<T>(array.length);
        for (T element : array) {
            list.add(element);
        }
        return list;
    }

    /**
     * Creates a normalized version of the values of the given int[] array.
     * Supports packed integers (e.g. ARGB data) by allowing to specify a
     * bitshift amount & bitmask, e.g. do this to get the normalized
     * representation of the red channel of an ARGB array:
     * 
     * <pre>
     * 
     * // use 16 bits as shift offset for accessing red channel 
     * float[] red = ArrayUtil.getAsNormalizedFloatArray(argbPixels, 16, 255, 255);
     * </pre>
     * 
     * @param array
     *            source data
     * @param bits
     *            number of bits to right shift each value
     * @param mask
     *            bitmask to apply after bitshifting
     * @param peak
     *            peak value (in the source domain) to normalize against
     * @return
     */
    public static float[] getAsNormalizedFloatArray(int[] array, int bits,
            int mask, int peak) {
        float invPeak = 1f / peak;
        float[] normalized = new float[array.length];
        for (int i = 0; i < array.length; i++) {
            int val = array[i];
            if (bits > 0) {
                val >>= bits;
            }
            val &= mask;
            normalized[i] = val * invPeak;
        }
        return normalized;
    }

    /**
     * Returns the index of the element where the given value is found in the
     * array.
     * 
     * @param needle
     *            number to find
     * @param stack
     *            array to search
     * @param maxLen
     *            number of elements to search
     * @return array index or -1 if value couldn't be found in array
     */
    public static int indexInArray(float needle, float[] stack, int maxLen) {
        for (int i = 0; i < maxLen; i++) {
            if (stack[i] == needle) {
                return i;<commit_msg>adding target peak value to getAsNormalizedFloatArray()<commit_after>package toxi.util.datatypes;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;

/**
 * A collection of array utilities.
 * 
 * @author toxi
 */
public class ArrayUtil {

    /**
     * Adds all array elements to the given collection of the same type.
     * 
     * @param <T>
     * @param array
     *            array
     * @param collection
     *            existing collection or null (to create a new {@link ArrayList}
     *            automatically)
     */
    public static <T> void addArrayToCollection(T[] array,
            Collection<T> collection) {
        if (collection == null) {
            collection = new ArrayList<T>();
        }
        for (T o : array) {
            collection.add(o);
        }
    }

    /**
     * Converts the generic array into an {@link ArrayList} of the same type.
     * 
     * @param array
     * @return array list version
     */
    public static <T> ArrayList<T> arrayToList(T[] array) {
        ArrayList<T> list = new ArrayList<T>(array.length);
        for (T element : array) {
            list.add(element);
        }
        return list;
    }

    /**
     * Creates a normalized version of the values of the given int[] array.
     * Supports packed integers (e.g. ARGB data) by allowing to specify a
     * bitshift amount & bitmask, e.g. do this to get the normalized
     * representation of the red channel of an ARGB array:
     * 
     * <pre>
     * 
     * 
     * 
     * // use 16 bits as shift offset for accessing red channel 
     * float[] red = ArrayUtil.getAsNormalizedFloatArray(argbPixels, 16, 255, 255);
     * </pre>
     * 
     * @param source
     *            source data
     * @param bits
     *            number of bits to right shift each value
     * @param mask
     *            bitmask to apply after bitshifting
     * @param peak
     *            peak value (in the source domain) to normalize against
     * @param target
     *            peak of the normalized values
     * @return
     */
    public static float[] getAsNormalizedFloatArray(int[] source, int bits,
            int mask, int peak, float target) {
        float invPeak = target / peak;
        float[] normalized = new float[source.length];
        for (int i = 0; i < source.length; i++) {
            int val = source[i];
            if (bits > 0) {
                val >>= bits;
            }
            val &= mask;
            normalized[i] = val * invPeak;
        }
        return normalized;
    }

    /**
     * Returns the index of the element where the given value is found in the
     * array.
     * 
     * @param needle
     *            number to find
     * @param stack
     *            array to search
     * @param maxLen
     *            number of elements to search
     * @return array index or -1 if value couldn't be found in array
     */
    public static int indexInArray(float needle, float[] stack, int maxLen) {
        for (int i = 0; i < maxLen; i++) {
            if (stack[i] == needle) {
                return i;
{'fraction_non_alphanumeric': 0.09525368248772505, 'fraction_numerical': 0.004746317512274959, 'mean_word_length': 1.6826163301141353, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, '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': '3737941', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1667, 'n_words': 615}