<commit_before>/**
 *
 */
package org.jmist.util;

import java.util.Arrays;

/**
 * Static utility methods for working with arrays.
 * @author bkimmel
 */
public final class ArrayUtil {

	/**
	 * Resets all elements of the given array to zero.
	 * @param array The array of <code>double</code>s to zero the values of.
	 * 		This may be <code>null</code>, in which case nothing is done.
	 * @return A reference to <code>array</code>.
	 */
	public static double[] reset(double[] array) {
		if (array != null) {
			Arrays.fill(array, 0.0);
		}
		return array;
	}

	/**
	 * Returns an array of zeros of the specified length.
	 * @param array The array of <code>double</code>s to zero and return.  If
	 * 		<code>null</code>, a new array will be created.  Otherwise, all
	 * 		elements will be set to zero and this array will be returned.
	 * @param length The length of the array to return.
	 * @return An array of the specified length that is all zeros.
	 * @throws IllegalArgumentException if <code>array</code> is not
	 * 		<code>null</code> and <code>array.length != length</code>.
	 */
	public static double[] initialize(double[] array, int length) {

		if (array == null) {
			return new double[length];
		} else if (array.length != length) {
			throw new IllegalArgumentException(String.format(
					"Invalid array length: expected %d but got %d.", length,
					array.length));
		}

		return ArrayUtil.reset(array);

	}

	/**
	 * Returns an array of <code>double</code>s, all equal to the specified
	 * value, of the specified length.
	 * @param array The array of <code>double</code>s to zero and return.  If
	 * 		<code>null</code>, a new array will be created.  Otherwise, all
	 * 		elements will be set to zero and this array will be returned.
	 * @param length The length of the array to return.
	 * @param value The value to initialize each element of the array to.
	 * @return An array of the specified length that is all zeros.
	 * @throws IllegalArgumentException if <code>array</code> is not
	 * 		<code>null</code> and <code>array.length != length</code>.
	 */
	public static double[] initialize(double[] array, int length, double value) {

		if (array == null) {
			array = new double[length];
		} else if (array.length != length) {
			throw new IllegalArgumentException(String.format(
					"Invalid array length: expected %d but got %d.", length,
					array.length));
		}

		return ArrayUtil.setAll(array, value);

	}

	/**
	 * Populates a contiguous range within an array of <code>double</code>s
	 * with the values from another array of <code>double</code>s.
	 * @param array The array of <code>double</code>s to populate.
	 * @param start The index into <code>array</code> of the first element to
	 * 		populate.
	 * @param values An array of <code>double</code>s to populate part of
	 * 		<code>array</code> with.
	 * @return A reference to <code>array</code>.
	 */
	public static double[] setRange(double[] array, int start, double[] values) {
		assert(array.length <= start + values.length);
		for (int i = 0, j = start; i < values.length; i++, j++) {
			array[j] = values[i];
		}
		return array;
	}

	/**
	 * Copies a range of values from one array into another array.
	 * @param target The array of <code>double</code>s to receive the values.
	 * @param targetStart The index into <code>target</code> at which to start
	 * 		copying values into (this array will be modified).
	 * @param source The array of <code>double</code>s from which to copy.
	 * @param sourceStart The index into <code>source</code> at which to start
	 * 		copying values.
	 * @param length The number of values to copy from <code>source</code> to
	 * 		<code>target</code>.
	 * @return A reference to <code>target</code>.
	 */
	public static double[] setRange(double[] target, int targetStart,
			double[] source, int sourceStart, int length) {

		assert(target.length <= targetStart + length);
		assert(source.length <= sourceStart + length);

		for (int i = sourceStart, j = targetStart, n = 0; n < length; i++, j++, n++) {
			target[j] = source[i];
		}

		return target;

	}

	/**
	 * Sets all of the values in the specified array of <code>double</code>s to
	 * the same value.
	 * @param array The array of <code>double</code>s the elements of which are
	 * 		to be set (the elements of this array will be modified).
	 * @param value The value to set each element of <code>array</code> to.
	 * @return A reference to <code>array</code>.
	 */
	public static double[] setAll(double[] array, double value) {
		Arrays.fill(array, value);
		return array;
	}

	/**
	 * Default constructor.  This constructor is private because this class
	 * cannot be instantiated.
	 */
	private ArrayUtil() {
		/* do nothing */
	}

}<commit_msg>Added methods to fill an array uniformly with a range of values.<commit_after>/**
 *
 */
package org.jmist.util;

import java.util.Arrays;

/**
 * Static utility methods for working with arrays.
 * @author bkimmel
 */
public final class ArrayUtil {

	/**
	 * Resets all elements of the given array to zero.
	 * @param array The array of <code>double</code>s to zero the values of.
	 * 		This may be <code>null</code>, in which case nothing is done.
	 * @return A reference to <code>array</code>.
	 */
	public static double[] reset(double[] array) {
		if (array != null) {
			Arrays.fill(array, 0.0);
		}
		return array;
	}

	/**
	 * Returns an array of zeros of the specified length.
	 * @param array The array of <code>double</code>s to zero and return.  If
	 * 		<code>null</code>, a new array will be created.  Otherwise, all
	 * 		elements will be set to zero and this array will be returned.
	 * @param length The length of the array to return.
	 * @return An array of the specified length that is all zeros.
	 * @throws IllegalArgumentException if <code>array</code> is not
	 * 		<code>null</code> and <code>array.length != length</code>.
	 */
	public static double[] initialize(double[] array, int length) {

		if (array == null) {
			return new double[length];
		} else if (array.length != length) {
			throw new IllegalArgumentException(String.format(
					"Invalid array length: expected %d but got %d.", length,
					array.length));
		}

		return ArrayUtil.reset(array);

	}

	/**
	 * Returns an array of <code>double</code>s, all equal to the specified
	 * value, of the specified length.
	 * @param array The array of <code>double</code>s to zero and return.  If
	 * 		<code>null</code>, a new array will be created.  Otherwise, all
	 * 		elements will be set to zero and this array will be returned.
	 * @param length The length of the array to return.
	 * @param value The value to initialize each element of the array to.
	 * @return An array of the specified length that is all zeros.
	 * @throws IllegalArgumentException if <code>array</code> is not
	 * 		<code>null</code> and <code>array.length != length</code>.
	 */
	public static double[] initialize(double[] array, int length, double value) {

		if (array == null) {
			array = new double[length];
		} else if (array.length != length) {
			throw new IllegalArgumentException(String.format(
					"Invalid array length: expected %d but got %d.", length,
					array.length));
		}

		return ArrayUtil.setAll(array, value);

	}

	/**
	 * Populates a contiguous range within an array of <code>double</code>s
	 * with the values from another array of <code>double</code>s.
	 * @param array The array of <code>double</code>s to populate.
	 * @param start The index into <code>array</code> of the first element to
	 * 		populate.
	 * @param values An array of <code>double</code>s to populate part of
	 * 		<code>array</code> with.
	 * @return A reference to <code>array</code>.
	 */
	public static double[] setRange(double[] array, int start, double[] values) {
		assert(array.length <= start + values.length);
		for (int i = 0, j = start; i < values.length; i++, j++) {
			array[j] = values[i];
		}
		return array;
	}

	/**
	 * Copies a range of values from one array into another array.
	 * @param target The array of <code>double</code>s to receive the values.
	 * @param targetStart The index into <code>target</code> at which to start
	 * 		copying values into (this array will be modified).
	 * @param source The array of <code>double</code>s from which to copy.
	 * @param sourceStart The index into <code>source</code> at which to start
	 * 		copying values.
	 * @param length The number of values to copy from <code>source</code> to
	 * 		<code>target</code>.
	 * @return A reference to <code>target</code>.
	 */
	public static double[] setRange(double[] target, int targetStart,
			double[] source, int sourceStart, int length) {

		assert(target.length <= targetStart + length);
		assert(source.length <= sourceStart + length);

		for (int i = sourceStart, j = targetStart, n = 0; n < length; i++, j++, n++) {
			target[j] = source[i];
		}

		return target;

	}

	/**
	 * Sets all of the values in the specified array of <code>double</code>s to
	 * the same value.
	 * @param array The array of <code>double</code>s the elements of which are
	 * 		to be set (the elements of this array will be modified).
	 * @param value The value to set each element of <code>array</code> to.
	 * @return A reference to <code>array</code>.
	 */
	public static double[] setAll(double[] array, double value) {
		Arrays.fill(array, value);
		return array;
	}

	/**
	 * Fills an array with values uniformly within a specified range.
	 * @param array The array to populate.
	 * @param first The value to assign to the first element of
	 * 		<code>array</code>.
	 * @param last The value to assign to the last element of the
	 * 		<code>array</code>.
	 * @return A reference to <code>array</code>, populated with the values
	 * 		<code>array[i] == first + (i / (array.length - 1)) * last</code>,
	 * 		for <code>0 &lt;= i &lt; array.length</code>.
	 */
	public static double[] fillRange(double[] array, double first, double last) {

		if (array == null) {
			throw new IllegalArgumentException("array is null");
		}

		for (int i = 0; i < array.length; i++) {
			double t = i / (double) (array.length - 1);
			array[i] = first + t * (last - first);
		}

		return array;

	}

	/**
	 * Creates a new array populated with values spaced uniformly in the
	 * specified range.
	 * @param first The value to assign to the first element of the array.
	 * @param last The value to assign to the last element of the array.
	 * @param length The length of the array.
	 * @return An array of the given length, populated with the values
	 * 		<code>array[i] == first + (i / (length - 1)) * last</code>, for
	 * 		<code>0 &lt;= i &lt; length</code>.
	 */
	public static double[] range(double first, double last, int length) {
		return ArrayUtil.fillRange(new double[length], first, last);
	}

	/**
	 * Default constructor.  This constructor is private because this class
	 * cannot be instantiated.
	 */
	private ArrayUtil() {
		/* do nothing */
	}

}
{'fraction_non_alphanumeric': 0.12345452880300394, 'fraction_numerical': 0.0012821686967671032, 'mean_word_length': 3.336775218427323, 'pattern_counts': {'":': 0, '<': 156, '<?xml version=': 0, '>': 145, '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': '1191219', 'n_tokens_mistral': 3572, 'n_tokens_neox': 3209, 'n_words': 1327}