View Javadoc
1 /* 2 * Copyright (C) 2002-2003, Mark Diggory 3 * 4 * This file is part of the Repast Taglibrary Package for use with Jelly. 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. License 19 * information is also available at http://www.gnu.org. 20 * 21 * @author Mark Diggory <mdiggory@latte.harvard.edu> 22 */ 23 package org.mrd.repast.util; 24 25 public abstract class MoreArrays{ 26 27 private MoreArrays(){ 28 } 29 30 public static void copy(Object array1, int i1, 31 Object array2, int i2, 32 int regionLength) { 33 System.arraycopy(array1, i1, array2, i2, regionLength); 34 } 35 36 public static boolean contains(boolean[] array, boolean target) { 37 for (int i = 0; i < array.length; i++) { 38 if (array[i] == target) { return true; } 39 } 40 return false; 41 } 42 43 public static boolean contains(char[] array, char target) { 44 for (int i = 0; i < array.length; i++) { 45 if (array[i] == target) { return true; } 46 } 47 return false; 48 } 49 50 public static boolean contains(double[] array, double target) { 51 for (int i = 0; i < array.length; i++) { 52 if (array[i] == target) { return true; } 53 } 54 return false; 55 } 56 57 public static boolean contains(int[] array, int target) { 58 for (int i = 0; i < array.length; i++) { 59 if (array[i] == target) { return true; } 60 } 61 return false; 62 } 63 64 public static boolean contains(Object[] array, Object target) { 65 for (int i = 0; i < array.length; i++) { 66 if (array[i].equals(target)) { return true; } 67 } 68 return false; 69 } 70 71 public static void insertionSort(double[] array) { 72 for (int numSorted = 1; numSorted < array.length; numSorted++) { 73 double x = array[numSorted]; 74 int i = numSorted; 75 while ((i >= 1) && (array[i - 1] > x)) { 76 array[i] = array[i - 1]; 77 i--; 78 } 79 array[i] = x; 80 } 81 } 82 83 public static boolean interpolationSearch(double[] array, double target) { 84 int lo = 0; 85 int n = array.length; 86 while (n > 1) { 87 double min = array[lo]; 88 if (target < min) { return false; } 89 double max = array[lo + n - 1]; 90 if (target >= max) { return target == max; } 91 if (min == max) { return target == min; } 92 int midpoint = 93 lo + (int)(Math.floor((n * (target - min) / (max - min)))); 94 if (target < array[midpoint]) { 95 n = midpoint - lo; 96 } else if (target == array[midpoint]) { 97 return true; 98 } else { 99 n = n + lo - midpoint - 1; 100 lo = midpoint + 1; 101 } 102 } 103 return array[lo] == target; 104 } 105 106 public static boolean isEmpty(boolean[] array) { return array.length == 0; } 107 108 public static boolean isEmpty(char[] array) { return array.length == 0; } 109 110 public static boolean isEmpty(double[] array) { return array.length == 0; } 111 112 public static boolean isEmpty(int[] array) { return array.length == 0; } 113 114 public static boolean isEmpty(Object[] array) { return array.length == 0; } 115 116 public static boolean linearSearch(double[] array, double target) { 117 for (int i = 0; i < array.length; i++) { 118 if (array[i] == target) { return true; } 119 if (array[i] > target) { return false; } 120 } 121 return false; 122 } 123 124 protected static double[] merge(double[] array1, double[] array2) { 125 double[] result = new double[array1.length + array2.length]; 126 int i = 0, j1 = 0, j2 = 0; 127 while (i < result.length) { 128 if ((j2 == array2.length) || 129 ((j1 < array1.length) && (array1[j1] < array2[j2]))) { 130 result[i] = array1[j1]; 131 j1++; 132 } else { 133 result[i] = array2[j2]; 134 j2++; 135 } 136 i++; 137 } 138 return result; 139 } 140 141 142 public static double[] mergeSort(double[] array) { 143 return mergeSort(array, 0, array.length - 1); 144 } 145 146 protected static double[] mergeSort(double[] array, int lo, int hi) { 147 if (lo == hi) { 148 return new double[] {array[lo]}; 149 } else { 150 int middle = (lo + hi) / 2; 151 return merge(mergeSort(array, lo, middle), 152 mergeSort(array, middle + 1, hi)); 153 } 154 } 155 156 protected static int partition(double[] array, int lo, int hi) { 157 double pivot = array[hi]; 158 int firstLarge = lo; 159 for (int i = lo; i < hi; i++) { 160 if (array[i] < pivot) { 161 swap(array, i, firstLarge); 162 firstLarge++; 163 } 164 } 165 swap(array, hi, firstLarge); 166 return firstLarge; 167 } 168 169 public static void quickSort(double[] array) { 170 quickSort(array, 0, array.length - 1); 171 } 172 173 protected static void quickSort(double[] array, int lo, int hi) { 174 if (lo < hi) { 175 int middle = partition(array, lo, hi); 176 quickSort(array, lo, middle - 1); 177 quickSort(array, middle + 1, hi); 178 } 179 } 180 181 public static void swap(boolean[] array, int i, int j) { 182 boolean temp = array[i]; 183 array[i] = array[j]; 184 array[j] = temp; 185 } 186 187 public static void swap(char[] array, int i, int j) { 188 char temp = array[i]; 189 array[i] = array[j]; 190 array[j] = temp; 191 } 192 193 public static void swap(double[] array, int i, int j) { 194 double temp = array[i]; 195 array[i] = array[j]; 196 array[j] = temp; 197 } 198 199 public static void swap(int[] array, int i, int j) { 200 int temp = array[i]; 201 array[i] = array[j]; 202 array[j] = temp; 203 } 204 205 public static void swap(Object[] array, int i, int j) { 206 Object temp = array[i]; 207 array[i] = array[j]; 208 array[j] = temp; 209 } 210 211 public static String toString(boolean[] array) { 212 if (isEmpty(array)) return ""; 213 String result = "" + array[0]; 214 for (int i = 1; i < array.length; i++) { 215 result += ", " + array[i]; 216 } 217 return result; 218 } 219 220 public static String toString(char[] array) { 221 if (isEmpty(array)) return ""; 222 String result = "" + array[0]; 223 for (int i = 1; i < array.length; i++) { 224 result += ", " + array[i]; 225 } 226 return result; 227 } 228 229 public static String toString(double[] array) { 230 if (isEmpty(array)) return ""; 231 String result = "" + array[0]; 232 for (int i = 1; i < array.length; i++) { 233 result += ", " + array[i]; 234 } 235 return result; 236 } 237 238 public static String toString(int[] array) { 239 if (isEmpty(array)) return ""; 240 String result = "" + array[0]; 241 for (int i = 1; i < array.length; i++) { 242 result += ", " + array[i]; 243 } 244 return result; 245 } 246 247 public static String toString(Object[] array) { 248 if (isEmpty(array)) return ""; 249 String result = "" + array[0]; 250 for (int i = 1; i < array.length; i++) { 251 result += ", " + array[i]; 252 } 253 return result; 254 } 255 256 }

This page was automatically generated by Maven