<commit_before>/*
 * Java Genetic Algorithm Library (@!identifier!@).
 * Copyright (c) @!year!@ <NAME>
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author:
 * 	 <NAME> (<EMAIL>)
 * 	 
 */
package org.jenetics.util;

import static org.jenetics.util.object.nonNull;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Random;


/**
 * Static helper methods concerning arrays.
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
public final class arrays {
	
	private arrays() {
		throw new AssertionError("Don't create an 'arrays' instance.");
	}
	
	/**
	 * Swap two elements of an given array.
	 * 
	 * @param array the array
	 * @param i index of the first array element.
	 * @param j index of the second array element.
	 * @throws ArrayIndexOutOfBoundsException if <tt>i &lt; 0</tt> or
	 *			<tt>j &lt; 0</tt> or <tt>i &gt; a.length</tt> or
	 *			<tt>j &gt; a.length</tt>
	 * @throws NullPointerException if the give array is {@code null}.
	 */
	public static void swap(final int[] array, final int i, final int j) {
		nonNull(array, "Array");
		final int old = array[i];
		array[i] = array[j];
		array[j] = old;
	}
	
	/**
	 * Swap two elements of an given array.
	 * 
	 * @param <T> the array type.
	 * @param array the array
	 * @param i index of the first array element.
	 * @param j index of the second array element.
	 * @throws ArrayIndexOutOfBoundsException if <tt>i &lt; 0</tt> or
	 *			<tt>j &lt; 0</tt> or <tt>i &gt; a.length</tt> or
	 *			<tt>j &gt; a.length</tt>
	 * @throws NullPointerException if the give array is {@code null}.
	 */
	public static <T> void swap(final T[] array, final int i, final int j) {
		nonNull(array, "Array");
		
		final T old = array[i];
		array[i] = array[j];
		array[j] = old;
	}
	
	/**
	 * Swap two elements of an given list.
	 * 
	 * @param <T> the list type.
	 * @param list the array
	 * @param i index of the first list element.
	 * @param j index of the second list element.
	 * @throws ArrayIndexOutOfBoundsException if <tt>i &lt; 0</tt> or
	 *			<tt>j &lt; 0</tt> or <tt>i &gt; a.length</tt> or
	 *			<tt>j &gt; a.length</tt>
	 * @throws NullPointerException if the give list is {@code null}.
	 */
	public static <T> void swap(final List<T> list, final int i, final int j) {
		nonNull(list, "Array");
		
		final T old = list.get(i);
		list.set(i, list.get(j));
		list.set(j, old);
	}
	
	/**
	 * Swap two elements of an given array.
	 * 
	 * @param <T> the array type.
	 * @param array the array
	 * @param i index of the first array element.
	 * @param j index of the second array element.
	 * @throws ArrayIndexOutOfBoundsException if <tt>i &lt; 0</tt> or
	 *			<tt>j &lt; 0</tt> or <tt>i &gt; a.length</tt> or
	 *			<tt>j &gt; a.length</tt>
	 * @throws NullPointerException if the give array is {@code null}.
	 */
	public static <T> void swap(final MSeq<T> array, final int i, final int j) {
		nonNull(array, "Array");

		final T old = array.get(i);
		array.set(i, array.get(j));
		array.set(j, old);
	}
	
	/**
	 * Calls the sort method on the {@link Arrays} class.
	 * 
	 * @see Arrays#sort(Object[], int, int, Comparator)
	 * @throws IllegalArgumentException if <tt>from &gt; to</tt>
	 * @throws ArrayIndexOutOfBoundsException if <tt>from &lt; 0</tt> or
	 *			<tt>to &gt; a.length</tt>
	 * @throws NullPointerException if the give array or comparator is 
	 *			{@code null}.
	 */
	public static <T> void sort(
		final Array<T> array, final int from, final int to,
		final Comparator<? super T> comparator
	) {
		nonNull(array, "Array");
		nonNull(comparator, "Comparator");
		array._array.cloneIfSealed();
		array.checkIndex(from, to);
		
		@SuppressWarnings("unchecked")
		final Comparator<Object> c = (Comparator<Object>)comparator;
		array._array.cloneIfSealed();
		Arrays.sort(array._array.data, from + array._start, to + array._start, c);
	}
	
	/**
	 * Calls the sort method on the {@link Arrays} class.
	 * @see Arrays#sort(Object[], Comparator)
	 * 
	 * @throws NullPointerException if the give array or comparator is 
	 *			{@code null}.
	 */
	public static <T> void sort(
		final Array<T> array, final Comparator<? super T> comparator
	) {
		nonNull(array, "Array");
		nonNull(comparator, "Comparator");
		
		sort(array, 0, array.length(), comparator);
	}
	
	/**
	 * Calls the sort method on the {@link Arrays} class.
	 * @see Arrays#sort(Object[], int, int)
	 * @throws IllegalArgumentException if <tt>from &gt; to</tt>
	 * @throws ArrayIndexOutOfBoundsException if <tt>from &lt; 0</tt> or
	 *			<tt>to &gt; a.length</tt>
	 * @throws NullPointerException if the give array is {@code null}.
	 */
	public static <T extends Object & Comparable<? super T>> void 
	sort(final Array<T> array, final int from, final int to) 
	{
		nonNull(array, "Array");
		array._array.cloneIfSealed();
		array.checkIndex(from, to);
		
		array._array.cloneIfSealed();
		Arrays.sort(array._array.data, from + array._start, to + array._start);
	}
	
	/**
	 * Calls the sort method on the {@link Arrays} class.
	 * 
	 * @see Arrays#sort(Object[])
	 * @throws NullPointerException if the give array is {@code null}.
	 * @throws UnsupportedOperationException if the array is sealed 
	 * 		  ({@code array.isSealed() == true}).
	 */
	public static <T extends Object & Comparable<? super T>> void <commit_msg>Fix javadoc.<commit_after>/*
 * Java Genetic Algorithm Library (@!identifier!@).
 * Copyright (c) @!year!@ <NAME>
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author:
 * 	 <NAME> (<EMAIL>)
 * 	 
 */
package org.jenetics.util;

import static org.jenetics.util.object.nonNull;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Random;


/**
 * Static helper methods concerning arrays.
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
public final class arrays {
	
	private arrays() {
		throw new AssertionError("Don't create an 'arrays' instance.");
	}
	
	/**
	 * Swap two elements of an given array.
	 * 
	 * @param array the array
	 * @param i index of the first array element.
	 * @param j index of the second array element.
	 * @throws ArrayIndexOutOfBoundsException if <tt>i &lt; 0</tt> or
	 *			<tt>j &lt; 0</tt> or <tt>i &gt; a.length</tt> or
	 *			<tt>j &gt; a.length</tt>
	 * @throws NullPointerException if the give array is {@code null}.
	 */
	public static void swap(final int[] array, final int i, final int j) {
		nonNull(array, "Array");
		final int old = array[i];
		array[i] = array[j];
		array[j] = old;
	}
	
	/**
	 * Swap two elements of an given array.
	 * 
	 * @param <T> the array type.
	 * @param array the array
	 * @param i index of the first array element.
	 * @param j index of the second array element.
	 * @throws ArrayIndexOutOfBoundsException if <tt>i &lt; 0</tt> or
	 *			<tt>j &lt; 0</tt> or <tt>i &gt; a.length</tt> or
	 *			<tt>j &gt; a.length</tt>
	 * @throws NullPointerException if the give array is {@code null}.
	 */
	public static <T> void swap(final T[] array, final int i, final int j) {
		nonNull(array, "Array");
		
		final T old = array[i];
		array[i] = array[j];
		array[j] = old;
	}
	
	/**
	 * Swap two elements of an given list.
	 * 
	 * @param <T> the list type.
	 * @param list the array
	 * @param i index of the first list element.
	 * @param j index of the second list element.
	 * @throws ArrayIndexOutOfBoundsException if <tt>i &lt; 0</tt> or
	 *			<tt>j &lt; 0</tt> or <tt>i &gt; a.length</tt> or
	 *			<tt>j &gt; a.length</tt>
	 * @throws NullPointerException if the give list is {@code null}.
	 */
	public static <T> void swap(final List<T> list, final int i, final int j) {
		nonNull(list, "Array");
		
		final T old = list.get(i);
		list.set(i, list.get(j));
		list.set(j, old);
	}
	
	/**
	 * Swap two elements of an given array.
	 * 
	 * @param <T> the array type.
	 * @param array the array
	 * @param i index of the first array element.
	 * @param j index of the second array element.
	 * @throws ArrayIndexOutOfBoundsException if <tt>i &lt; 0</tt> or
	 *			<tt>j &lt; 0</tt> or <tt>i &gt; a.length</tt> or
	 *			<tt>j &gt; a.length</tt>
	 * @throws NullPointerException if the give array is {@code null}.
	 */
	public static <T> void swap(final MSeq<T> array, final int i, final int j) {
		nonNull(array, "Array");

		final T old = array.get(i);
		array.set(i, array.get(j));
		array.set(j, old);
	}
	
	/**
	 * Calls the sort method on the {@link Arrays} class.
	 * 
	 * @see arrays#sort(Array, int, int, Comparator)
	 * @throws IllegalArgumentException if <tt>from &gt; to</tt>
	 * @throws ArrayIndexOutOfBoundsException if <tt>from &lt; 0</tt> or
	 *			<tt>to &gt; a.length</tt>
	 * @throws NullPointerException if the give array or comparator is 
	 *			{@code null}.
	 */
	public static <T> void sort(
		final Array<T> array, final int from, final int to,
		final Comparator<? super T> comparator
	) {
		nonNull(array, "Array");
		nonNull(comparator, "Comparator");
		array._array.cloneIfSealed();
		array.checkIndex(from, to);
		
		@SuppressWarnings("unchecked")
		final Comparator<Object> c = (Comparator<Object>)comparator;
		array._array.cloneIfSealed();
		Arrays.sort(array._array.data, from + array._start, to + array._start, c);
	}
	
	/**
	 * Calls the sort method on the {@link Arrays} class.
	 * @see arrays#sort(Array, Comparator)
	 * 
	 * @throws NullPointerException if the give array or comparator is 
	 *			{@code null}.
	 */
	public static <T> void sort(
		final Array<T> array, final Comparator<? super T> comparator
	) {
		nonNull(array, "Array");
		nonNull(comparator, "Comparator");
		
		sort(array, 0, array.length(), comparator);
	}
	
	/**
	 * Calls the sort method on the {@link Arrays} class.
	 * @see arrays#sort(Array, int, int)
	 * @throws IllegalArgumentException if <tt>from &gt; to</tt>
	 * @throws ArrayIndexOutOfBoundsException if <tt>from &lt; 0</tt> or
	 *			<tt>to &gt; a.length</tt>
	 * @throws NullPointerException if the give array is {@code null}.
	 */
	public static <T extends Object & Comparable<? super T>> void 
	sort(final Array<T> array, final int from, final int to) 
	{
		nonNull(array, "Array");
		array._array.cloneIfSealed();
		array.checkIndex(from, to);
		
		array._array.cloneIfSealed();
		Arrays.sort(array._array.data, from + array._start, to + array._start);
	}
	
	/**
	 * Calls the sort method on the {@link Arrays} class.
	 * 
	 * @see arrays#sort(MSeq)
	 * @throws NullPointerException if the give array is {@code null}.
	 * @throws UnsupportedOperationException if the array is sealed 
	 * 		  ({@code array.isSealed() == true}).
	 */
	public static <T extends Object & Comparable<? super T>> void 
{'fraction_non_alphanumeric': 0.13572607260726072, 'fraction_numerical': 0.0039603960396039604, 'mean_word_length': 3.444811147781445, 'pattern_counts': {'":': 0, '<': 147, '<?xml version=': 0, '>': 147, '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': '923156', 'n_tokens_mistral': 4307, 'n_tokens_neox': 3921, 'n_words': 1441}