|
HSQLDB example source code file (ArrayUtil.java)
The HSQLDB ArrayUtil.java source code/* Copyright (c) 2001-2008, The HSQL Development Group * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of the HSQL Development Group nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG, * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.hsqldb.lib; import java.lang.reflect.Array; /** * Collection of static methods for operations on arrays * * @author Fred Toussi (fredt@users dot sourceforge.net) * @version 1.9.0 * @since 1.7.2 */ public class ArrayUtil { public static final int CLASS_CODE_BYTE = 'B'; public static final int CLASS_CODE_CHAR = 'C'; public static final int CLASS_CODE_DOUBLE = 'D'; public static final int CLASS_CODE_FLOAT = 'F'; public static final int CLASS_CODE_INT = 'I'; public static final int CLASS_CODE_LONG = 'J'; public static final int CLASS_CODE_OBJECT = 'L'; public static final int CLASS_CODE_SHORT = 'S'; public static final int CLASS_CODE_BOOLEAN = 'Z'; private static IntValueHashMap classCodeMap = new IntValueHashMap(); static { classCodeMap.put(byte.class, ArrayUtil.CLASS_CODE_BYTE); classCodeMap.put(char.class, ArrayUtil.CLASS_CODE_SHORT); classCodeMap.put(short.class, ArrayUtil.CLASS_CODE_SHORT); classCodeMap.put(int.class, ArrayUtil.CLASS_CODE_INT); classCodeMap.put(long.class, ArrayUtil.CLASS_CODE_LONG); classCodeMap.put(float.class, ArrayUtil.CLASS_CODE_FLOAT); classCodeMap.put(double.class, ArrayUtil.CLASS_CODE_DOUBLE); classCodeMap.put(boolean.class, ArrayUtil.CLASS_CODE_BOOLEAN); classCodeMap.put(Object.class, ArrayUtil.CLASS_CODE_OBJECT); } /** * Returns a distinct int code for each primitive type and for all Object types. */ static int getClassCode(Class cla) { if (!cla.isPrimitive()) { return ArrayUtil.CLASS_CODE_OBJECT; } return classCodeMap.get(cla, -1); } /** * Clears an area of the given array of the given type. */ public static void clearArray(int type, Object data, int from, int to) { switch (type) { case ArrayUtil.CLASS_CODE_BYTE : { byte[] array = (byte[]) data; while (--to >= from) { array[to] = 0; } return; } case ArrayUtil.CLASS_CODE_CHAR : { byte[] array = (byte[]) data; while (--to >= from) { array[to] = 0; } return; } case ArrayUtil.CLASS_CODE_SHORT : { short[] array = (short[]) data; while (--to >= from) { array[to] = 0; } return; } case ArrayUtil.CLASS_CODE_INT : { int[] array = (int[]) data; while (--to >= from) { array[to] = 0; } return; } case ArrayUtil.CLASS_CODE_LONG : { long[] array = (long[]) data; while (--to >= from) { array[to] = 0; } return; } case ArrayUtil.CLASS_CODE_FLOAT : { float[] array = (float[]) data; while (--to >= from) { array[to] = 0; } return; } case ArrayUtil.CLASS_CODE_DOUBLE : { double[] array = (double[]) data; while (--to >= from) { array[to] = 0; } return; } case ArrayUtil.CLASS_CODE_BOOLEAN : { boolean[] array = (boolean[]) data; while (--to >= from) { array[to] = false; } return; } default : { Object[] array = (Object[]) data; while (--to >= from) { array[to] = null; } return; } } } /** * Moves the contents of an array to allow both addition and removal of * elements. Used arguments must be in range. * * @param type class type of the array * @param array the array * @param usedElements count of elements of array in use * @param index point at which to add or remove elements * @param count number of elements to add or remove */ public static void adjustArray(int type, Object array, int usedElements, int index, int count) { if (index >= usedElements) { return; } int newCount = usedElements + count; int source; int target; int size; if (count >= 0) { source = index; target = index + count; size = usedElements - index; } else { source = index - count; target = index; size = usedElements - index + count; } if (size > 0) { System.arraycopy(array, source, array, target, size); } if (count < 0) { clearArray(type, array, newCount, usedElements); } } /** * Basic sort for small arrays of int. */ public static void sortArray(int[] array) { boolean swapped; do { swapped = false; for (int i = 0; i < array.length - 1; i++) { if (array[i] > array[i + 1]) { int temp = array[i + 1]; array[i + 1] = array[i]; array[i] = temp; swapped = true; } } } while (swapped); } /** * Basic find for small arrays of Object. */ public static int find(Object[] array, Object object) { for (int i = 0; i < array.length; i++) { if (array[i] == object) { // hadles both nulls return i; } if (object != null && object.equals(array[i])) { return i; } } return -1; } /** * Basic find for small arrays of int. */ public static int find(int[] array, int value) { for (int i = 0; i < array.length; i++) { if (array[i] == value) { return i; } } return -1; } /** * Finds the first element of the array that is not equal to the given value. */ public static int findNot(int[] array, int value) { for (int i = 0; i < array.length; i++) { if (array[i] != value) { return i; } } return -1; } /** * Returns true if arra and arrb contain the same set of integers, not * necessarily in the same order. This implies the arrays are of the same * length. */ public static boolean areEqualSets(int[] arra, int[] arrb) { return arra.length == arrb.length && ArrayUtil.haveEqualSets(arra, arrb, arra.length); } /** * For full == true returns true if arra and arrb are identical (have the * same length and contain the same integers in the same sequence). * * For full == false returns the result * of haveEqualArrays(arra,arrb,count) * * For full == true, the array lengths must be the same as count * */ public static boolean areEqual(int[] arra, int[] arrb, int count, boolean full) { if (ArrayUtil.haveEqualArrays(arra, arrb, count)) { if (full) { return arra.length == arrb.length && count == arra.length; } return true; } return false; } /** * Returns true if the first count elements of arra and arrb are identical * sets of integers (not necessarily in the same order). * */ public static boolean haveEqualSets(int[] arra, int[] arrb, int count) { if (ArrayUtil.haveEqualArrays(arra, arrb, count)) { return true; } if (count > arra.length || count > arrb.length) { return false; } if (count == 1) { return arra[0] == arrb[0]; } int[] tempa = (int[]) resizeArray(arra, count); int[] tempb = (int[]) resizeArray(arrb, count); sortArray(tempa); sortArray(tempb); for (int j = 0; j < count; j++) { if (tempa[j] != tempb[j]) { return false; } } return true; } /** * Returns true if the first count elements of arra and arrb are identical * subarrays of integers * */ public static boolean haveEqualArrays(int[] arra, int[] arrb, int count) { if (count > arra.length || count > arrb.length) { return false; } for (int j = 0; j < count; j++) { if (arra[j] != arrb[j]) { return false; } } return true; } /** * Returns true if the first count elements of arra and arrb are identical * subarrays of Objects * */ public static boolean haveEqualArrays(Object[] arra, Object[] arrb, int count) { if (count > arra.length || count > arrb.length) { return false; } for (int j = 0; j < count; j++) { if (arra[j] != arrb[j]) { if (arra[j] == null || !arra[j].equals(arrb[j])) { return false; } } } return true; } /** * Returns true if arra and the first bcount elements of arrb share any * element. <p> * * Used for checks for any overlap between two arrays of column indexes. */ public static boolean haveCommonElement(int[] arra, int[] arrb, int bcount) { for (int i = 0; i < arra.length; i++) { int c = arra[i]; for (int j = 0; j < bcount; j++) { if (c == arrb[j]) { return true; } } } return false; } /** * Returns an int[] containing elements shared between the two arrays * arra and arrb. The arrays contain sets (no value is repeated). * * Used to find the overlap between two arrays of column indexes. * Ordering of the result arrays will be the same as in array * a. The method assumes that each index is only listed * once in the two input arrays. * <p> * e.g. * </p> * <code> * <table width="90%" bgcolor="lightblue"> * <tr> | int []arra | = | {2,11,5,8} | * <tr>int []arrb | = | {20,8,10,11,28,12} | * <tr>will result in: | * <tr>int []arrc | = | {11,8} | * </table> * * @param arra int[]; first column indexes * @param arrb int[]; second column indexes * @return int[] common indexes or <code>null if there is no overlap. */ public static int[] commonElements(int[] arra, int[] arrb) { int[] c = null; int n = countCommonElements(arra, arrb); if (n > 0) { c = new int[n]; int k = 0; for (int i = 0; i < arra.length; i++) { for (int j = 0; j < arrb.length; j++) { if (arra[i] == arrb[j]) { c[k++] = arra[i]; } } } } return c; } /** * Returns the number of elements shared between the two arrays containing * sets.<p> * * Return the number of elements shared by two column index arrays. * This method assumes that each of these arrays contains a set (each * element index is listed only once in each index array). Otherwise the * returned number will NOT represent the number of unique column indexes * shared by both index array. * * @param arra int[]; first array of column indexes. * * @param arrb int[]; second array of column indexes * * @return int; number of elements shared by <code>a and
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.