<commit_before>/*
 * Copyright 2000-2005 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.util;

import com.intellij.util.text.CharArrayCharSequence;
import gnu.trove.Equality;

import java.lang.reflect.Array;
import java.util.Collection;

/**
 * Author: msk
 */
public class ArrayUtil {
  public static final short[] EMPTY_SHORT_ARRAY = new short[0];
  public static final char[] EMPTY_CHAR_ARRAY = new char[0];
  public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
  public static final int [] EMPTY_INT_ARRAY = new int[0];
  public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0];
  public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
  public static final String[] EMPTY_STRING_ARRAY = new String[0];
  public static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
  public static final long[] EMPTY_LONG_ARRAY = new long[0];
  public static final CharSequence EMPTY_CHAR_SEQUENCE = new CharArrayCharSequence(EMPTY_CHAR_ARRAY);

  public static byte[] realloc (final byte [] array, final int newSize) {
    if (newSize == 0) {
      return EMPTY_BYTE_ARRAY;
    }

    final int oldSize = array.length;
    if (oldSize == newSize) {
      return array;
    }

    final byte [] result = new byte [newSize];
    System.arraycopy(array, 0, result, 0, Math.min (oldSize, newSize));
    return result;
  }
  public static char[] realloc (final char[] array, final int newSize) {
    if (newSize == 0) {
      return EMPTY_CHAR_ARRAY;
    }

    final int oldSize = array.length;
    if (oldSize == newSize) {
      return array;
    }

    final char[] result = new char[newSize];
    System.arraycopy(array, 0, result, 0, Math.min (oldSize, newSize));
    return result;
  }

  public static<T> T[] toObjectArray(Collection<T> collection, Class<T> aClass) {
    T[] array = (T[]) Array.newInstance(aClass, collection.size());
    return collection.toArray(array);
  }

  public static Object[] toObjectArray(Collection collection) {
    return toObjectArray(collection, Object.class);
  }

  public static String[] toStringArray(Collection<String> collection) {
    return toObjectArray(collection, String.class);
  }

  public static <T> T[] mergeArrays(T[] a1, T[] a2, Class<T> aClass) {
    if (a1.length == 0) {
      return a2;
    }
    if (a2.length == 0) {
      return a1;
    }
    T[] highlights =(T[])Array.newInstance(aClass, a1.length + a2.length);
    System.arraycopy(a1, 0, highlights, 0, a1.length);
    System.arraycopy(a2, 0, highlights, a1.length, a2.length);
    return highlights;
  }

  public static <T> T[] emptyArray() {
    return (T[])EMPTY_OBJECT_ARRAY;
  }

  /**
   * Appends <code>element</code> to the <code>src</code> array. As you can
   * imagine the appended element will be the last one in the returned result.
   * @param src array to which the <code>element</code> should be appended.
   * @param element object to be appended to the end of <code>src</code> array.
   */
  public static <T> T[] append(final T[] src,final T element){
    if (src == null){
      //noinspection HardCodedStringLiteral
      throw new IllegalArgumentException("src cannot be null");
    }
    int length=src.length;
    T[] result=(T[])Array.newInstance(src.getClass().getComponentType(), length+ 1);
    System.arraycopy(src,0,result,0,length);
    result[length] = element;<commit_msg>IDEADEV-4816<commit_after>/*
 * Copyright 2000-2005 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.util;

import com.intellij.util.text.CharArrayCharSequence;
import gnu.trove.Equality;

import java.lang.reflect.Array;
import java.util.Collection;

/**
 * Author: msk
 */
public class ArrayUtil {
  public static final short[] EMPTY_SHORT_ARRAY = new short[0];
  public static final char[] EMPTY_CHAR_ARRAY = new char[0];
  public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
  public static final int [] EMPTY_INT_ARRAY = new int[0];
  public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0];
  public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
  public static final String[] EMPTY_STRING_ARRAY = new String[0];
  public static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
  public static final long[] EMPTY_LONG_ARRAY = new long[0];
  public static final CharSequence EMPTY_CHAR_SEQUENCE = new CharArrayCharSequence(EMPTY_CHAR_ARRAY);

  public static byte[] realloc (final byte [] array, final int newSize) {
    if (newSize == 0) {
      return EMPTY_BYTE_ARRAY;
    }

    final int oldSize = array.length;
    if (oldSize == newSize) {
      return array;
    }

    final byte [] result = new byte [newSize];
    System.arraycopy(array, 0, result, 0, Math.min (oldSize, newSize));
    return result;
  }
  public static char[] realloc (final char[] array, final int newSize) {
    if (newSize == 0) {
      return EMPTY_CHAR_ARRAY;
    }

    final int oldSize = array.length;
    if (oldSize == newSize) {
      return array;
    }

    final char[] result = new char[newSize];
    System.arraycopy(array, 0, result, 0, Math.min (oldSize, newSize));
    return result;
  }

  public static<T> T[] toObjectArray(Collection<T> collection, Class<T> aClass) {
    T[] array = (T[]) Array.newInstance(aClass, collection.size());
    return collection.toArray(array);
  }

  public static Object[] toObjectArray(Collection collection) {
    return toObjectArray(collection, Object.class);
  }

  public static String[] toStringArray(Collection<String> collection) {
    return toObjectArray(collection, String.class);
  }

  public static <T> T[] mergeArrays(T[] a1, T[] a2, Class<T> aClass) {
    if (a1.length == 0) {
      return a2;
    }
    if (a2.length == 0) {
      return a1;
    }
    T[] highlights =(T[])Array.newInstance(aClass, a1.length + a2.length);
    System.arraycopy(a1, 0, highlights, 0, a1.length);
    System.arraycopy(a2, 0, highlights, a1.length, a2.length);
    return highlights;
  }

  /**
   * Appends <code>element</code> to the <code>src</code> array. As you can
   * imagine the appended element will be the last one in the returned result.
   * @param src array to which the <code>element</code> should be appended.
   * @param element object to be appended to the end of <code>src</code> array.
   */
  public static <T> T[] append(final T[] src,final T element){
    if (src == null){
      //noinspection HardCodedStringLiteral
      throw new IllegalArgumentException("src cannot be null");
    }
    int length=src.length;
    T[] result=(T[])Array.newInstance(src.getClass().getComponentType(), length+ 1);
    System.arraycopy(src,0,result,0,length);
    result[length] = element;
{'fraction_non_alphanumeric': 0.11536962452903729, 'fraction_numerical': 0.012992074834351046, 'mean_word_length': 3.7111383108935128, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6707885', 'n_tokens_mistral': 2433, 'n_tokens_neox': 2369, 'n_words': 778}