Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/TimSort/ArrayBuilder.java
38811 views
/*1* Copyright 2009 Google Inc. All Rights Reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.Random;24import java.math.BigInteger;2526public enum ArrayBuilder {2728// These seven are from Tim's paper (listsort.txt)2930RANDOM_INT {31public Object[] build(int len) {32Integer[] result = new Integer[len];33for (int i = 0; i < len; i++)34result[i] = rnd.nextInt();35return result;36}37},3839DESCENDING_INT {40public Object[] build(int len) {41Integer[] result = new Integer[len];42for (int i = 0; i < len; i++)43result[i] = len - i;44return result;45}46},4748ASCENDING_INT {49public Object[] build(int len) {50Integer[] result = new Integer[len];51for (int i = 0; i < len; i++)52result[i] = i;53return result;54}55},5657ASCENDING_3_RND_EXCH_INT {58public Object[] build(int len) {59Integer[] result = new Integer[len];60for (int i = 0; i < len; i++)61result[i] = i;62for (int i = 0; i < 3; i++)63swap(result, rnd.nextInt(result.length),64rnd.nextInt(result.length));65return result;66}67},6869ASCENDING_10_RND_AT_END_INT {70public Object[] build(int len) {71Integer[] result = new Integer[len];72int endStart = len - 10;73for (int i = 0; i < endStart; i++)74result[i] = i;75for (int i = endStart; i < len; i++)76result[i] = rnd.nextInt(endStart + 10);77return result;78}79},8081ALL_EQUAL_INT {82public Object[] build(int len) {83Integer[] result = new Integer[len];84for (int i = 0; i < len; i++)85result[i] = 666;86return result;87}88},8990DUPS_GALORE_INT {91public Object[] build(int len) {92Integer[] result = new Integer[len];93for (int i = 0; i < len; i++)94result[i] = rnd.nextInt(4);95return result;96}97},9899RANDOM_WITH_DUPS_INT {100public Object[] build(int len) {101Integer[] result = new Integer[len];102for (int i = 0; i < len; i++)103result[i] = rnd.nextInt(len);104return result;105}106},107108PSEUDO_ASCENDING_STRING {109public String[] build(int len) {110String[] result = new String[len];111for (int i = 0; i < len; i++)112result[i] = Integer.toString(i);113return result;114}115},116117RANDOM_BIGINT {118public BigInteger[] build(int len) {119BigInteger[] result = new BigInteger[len];120for (int i = 0; i < len; i++)121result[i] = HUGE.add(BigInteger.valueOf(rnd.nextInt(len)));122return result;123}124};125126public abstract Object[] build(int len);127128public void resetRandom() {129rnd = new Random(666);130}131132private static Random rnd = new Random(666);133134private static void swap(Object[] a, int i, int j) {135Object t = a[i];136a[i] = a[j];137a[j] = t;138}139140private static BigInteger HUGE = BigInteger.ONE.shiftLeft(100);141}142143144