1 /******************************************************************************* 2 * Copyright (c) 2019 Sebastian Zarnekow and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * Sebastian Zarnekow - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.jdt.internal.compiler.util; 15 16 import java.util.Comparator; 17 18 /** 19 * @since 3.18 20 */ 21 public class SortedCharArrays { 22 23 // there may be better thresholds available for different scenarios 24 public static final int BINARY_SEARCH_THRESHOLD = 16; 25 26 /** 27 * @param target same as source array or new array with higher capacity 28 * @param idx position for new element 29 * @param currentCount the current number of elements in the source array 30 * @return given target array 31 */ insertIntoArray(T[] src, T[] target, T entry, int idx, int currentCount)32 public static <T> T[] insertIntoArray(T[] src, T[] target, T entry, int idx, int currentCount) { 33 if (src != target) { 34 // src and target point to different instances 35 // -> we need to copy the elements into the new result 36 System.arraycopy(src, 0, target, 0, idx); 37 System.arraycopy(src, idx, target, idx+1, currentCount - idx); 38 } else if (idx != currentCount) { 39 // src and target point to the same instance 40 // -> we need to shift the elements one slot to the right 41 System.arraycopy(src, idx, target, idx+1, currentCount - idx); 42 } 43 target[idx] = entry; 44 return target; 45 } 46 47 /** 48 * Compares the two char arrays. 49 * Longer arrays are considered to be smaller than shorter arrays. 50 * Arrays with the same length are compared char by char lexicographically. 51 * 52 * @see Character#compare(char, char) 53 */ compareCharArray(char[] left, char[] right)54 public static int compareCharArray(char[] left, char[] right){ 55 if (left == right) { 56 return 0; 57 } 58 int l = left.length; 59 int diff = right.length - l; 60 if (diff == 0) { 61 for(int i = 0; i < l && (diff = left[i] - right[i]) == 0; i++) { 62 // all logic is in the loop header 63 } 64 } 65 return diff; 66 } 67 public static final Comparator<char[]> CHAR_ARR_COMPARATOR = SortedCharArrays::compareCharArray; 68 69 /** 70 * Compares the two char-char arrays. 71 * Longer arrays are considered to be smaller than shorter arrays. 72 * Arrays with the same length are compared according to the logic in {@link #compareCharArray(char[], char[])}. 73 */ compareCharCharArray(char[][] left, char[][]right)74 public static int compareCharCharArray(char[][] left, char[][]right) { 75 if (left == right) { 76 return 0; 77 } 78 int l = left.length; 79 int diff = right.length - l; 80 if (diff == 0) { 81 for(int i = 0; i < l && (diff = compareCharArray(left[i], right[i])) == 0; i++) { 82 // all logic is in the loop header 83 } 84 } 85 return diff; 86 } 87 public static final Comparator<char[][]> CHAR_CHAR_ARR_COMPARATOR = SortedCharArrays::compareCharCharArray; 88 } 89