/* * @(#) CharVector.java - Class for 'char' array wrappers. * (c) 2000 Ivan Maidanski http://ivmai.chat.ru * Freeware class library sources. All rights reserved. ** * Language: Java [pure] * Tested with: JDK v1.1.6 * Last modified: 2000-12-31 13:05:00 GMT+03:00 */ /* * This software is the proprietary information of the author. ** * Permission to use, copy, and distribute this software and its * documentation for non-commercial purposes and without fee is * hereby granted provided that this copyright notice appears in all * copies. ** * This software should not be modified in any way; any found bug * should be reported to the author. ** * The author disclaims all warranties with regard to this software, * including all implied warranties of merchantability and fitness. * In no event shall the author be liable for any special, indirect * or consequential damages or any damages whatsoever resulting from * loss of use, data or profits, whether in an action of contract, * negligence or other tortuous action, arising out of or in * connection with the use or performance of this software. */ package ivmai.util; import java.io.InvalidObjectException; import java.io.IOException; import java.io.ObjectInputStream; import java.io.Serializable; /** * Class for 'char' array wrappers. ** * This class wraps a primitive char-type array, and * has the possibility to resize (when required) the wrapped array. * This class supports cloning, serialization and comparison of its * instances. In addition, the class contains static * methods for char arrays resizing, filling in, * reversing, non-zero elements counting, sorting, linear/binary * searching in for a value or sequence, case-sensitive or * case-insensitive 'less-equal-greater' comparison, mismatches * counting and hashing. ** * @see ByteVector * @see DoubleVector * @see FloatVector * @see IntVector * @see LongVector * @see ShortVector * @see BooleanVector * @see ObjectVector * @see StrComparator ** * @version 2.0 * @author Ivan Maidanski */ public final class CharVector implements ReallyCloneable, Serializable, Indexable, Sortable, Verifiable { /** * The class version unique identifier for serialization * interoperability. ** * @since 1.8 */ private static final long serialVersionUID = 704428284230945389L; /** * A constant initialized with an instance of empty * char array. ** * @see #array */ protected static final char[] EMPTY = {}; /** * The wrapped (encapsulated) custom char array. ** * array must be non-null. ** * @serial ** * @see #EMPTY * @see CharVector#CharVector() * @see CharVector#CharVector(int) * @see CharVector#CharVector(char[]) * @see #setArray(char[]) * @see #array() * @see #length() * @see #resize(int) * @see #ensureSize(int) * @see #setAt(int, char) * @see #getCharAt(int) * @see #copyAt(int, int, int) * @see #clone() * @see #integrityCheck() */ protected char[] array; /** * Constructs an empty char vector. ** * This constructor is used for the creation of a resizable vector. * The length of such a vector is changed only by * resize(int) and ensureSize(int) * methods. ** * @see CharVector#CharVector(int) * @see CharVector#CharVector(char[]) * @see #array() * @see #length() * @see #resize(int) * @see #ensureSize(int) * @see #setAt(int, char) * @see #getCharAt(int) * @see #copyAt(int, int, int) * @see #clone() * @see #toString() */ public CharVector() { this.array = EMPTY; } /** * Constructs a new char vector of the specified * length. ** * This constructor is typically used for the creation of a vector * with a fixed size. All elements of the created vector are set to * zero. ** * @param size * the initial length (unsigned) of the vector to be created. * @exception OutOfMemoryError * if there is not enough memory. ** * @see CharVector#CharVector() * @see CharVector#CharVector(char[]) * @see #array() * @see #length() * @see #setAt(int, char) * @see #getCharAt(int) * @see #copyAt(int, int, int) * @see #fill(char[], int, int, char) * @see #clone() * @see #toString() */ public CharVector(int size) { if (size < 0) size = -1 >>> 1; this.array = new char[size]; } /** * Constructs a new char array wrapper. ** * This constructor is used for the creation of a vector which wraps * the specified array (without copying it). The wrapped array may * be further replaced with another one only by * setArray(char[]) and by resize(int), * ensureSize(int) methods. ** * @param array * the char array (must be non-null) to be * wrapped. * @exception NullPointerException * if array is null. ** * @see CharVector#CharVector() * @see CharVector#CharVector(int) * @see #setArray(char[]) * @see #array() * @see #resize(int) * @see #ensureSize(int) * @see #setAt(int, char) * @see #getCharAt(int) * @see #copyAt(int, int, int) * @see #clone() * @see #toString() ** * @since 2.0 */ public CharVector(char[] array) throws NullPointerException { int len; len = array.length; this.array = array; } /** * Sets another array to be wrapped by this vector. ** * Important notes: resize(int) and * ensureSize(int) methods may change the array to be * wrapped too (but only with its copy of a different length); this * method does not copy array. If an exception is thrown * then this vector remains unchanged. ** * @param array * the char array (must be non-null) to be * wrapped. * @exception NullPointerException * if array is null. ** * @see CharVector#CharVector() * @see CharVector#CharVector(char[]) * @see #array() * @see #resize(int) * @see #ensureSize(int) * @see #setAt(int, char) * @see #getCharAt(int) * @see #copyAt(int, int, int) * @see #clone() ** * @since 2.0 */ public void setArray(char[] array) throws NullPointerException { int len; len = array.length; this.array = array; } /** * Returns array wrapped by this vector. ** * Important notes: this method does not copy array. ** * @return * the char array (not null), which is * wrapped. ** * @see CharVector#CharVector(char[]) * @see #setArray(char[]) * @see #length() * @see #resize(int) * @see #ensureSize(int) * @see #copyAt(int, int, int) * @see #clone() ** * @since 1.8 */ public final char[] array() { return this.array; } /** * Returns the number of elements in this vector. ** * The result is the same as length of * array(). ** * @return * the length (non-negative value) of this vector. ** * @see #setArray(char[]) * @see #array() * @see #setAt(int, char) * @see #resize(int) * @see #ensureSize(int) * @see #getCharAt(int) * @see #getAt(int) ** * @since 1.8 */ public int length() { return this.array.length; } /** * Returns the wrapped value of the element at the specified index. ** * The result is the same as of * new Character(array()[index]). ** * @param index * the index (must be in the range) at which to return an element. * @return * an element (instance of Character) at * index. * @exception ArrayIndexOutOfBoundsException * if index is negative or is not less than * length(). * @exception OutOfMemoryError * if there is not enough memory. ** * @see #getCharAt(int) * @see #array() * @see #length() */ public Object getAt(int index) throws ArrayIndexOutOfBoundsException { return new Character(this.array[index]); } /** * Returns value of the element at the specified index. ** * The result is the same as of array()[index]. ** * @param index * the index (must be in the range) at which to return an element. * @return * a char element at index. * @exception ArrayIndexOutOfBoundsException * if index is negative or is not less than * length(). ** * @see #array() * @see #length() * @see #setAt(int, char) * @see #resize(int) * @see #ensureSize(int) */ public final char getCharAt(int index) throws ArrayIndexOutOfBoundsException { return this.array[index]; } /** * Assigns a new value to the element at the specified index. ** * If an exception is thrown then this vector remains * unchanged. ** * @param index * the index (must be in the range) at which to assign a new value. * @param value * the value to be assigned. * @exception ArrayIndexOutOfBoundsException * if index is negative or is not less than * length(). ** * @see #setArray(char[]) * @see #array() * @see #length() * @see #getCharAt(int) * @see #resize(int) * @see #ensureSize(int) * @see #copyAt(int, int, int) * @see #fill(char[], int, int, char) */ public void setAt(int index, char value) throws ArrayIndexOutOfBoundsException { this.array[index] = value; } /** * Copies a region of values at one offset to another offset in * this vector. ** * Copying is performed here through * arraycopy(Object, int, Object, int, int) method of * System class. Negative len is treated as * zero. If an exception is thrown then this vector * remains unchanged. ** * @param srcOffset * the source first index (must be in the range) of the region to be * copied. * @param destOffset * the first index (must be in the range) of the region copy * destination. * @param len * the length of the region to be copied. * @exception ArrayIndexOutOfBoundsException * if len is positive and (srcOffset is * negative or is greater than length() minus * len, or destOffset is negative or is * greater than length() minus len). ** * @see #array() * @see #length() * @see #setAt(int, char) * @see #getCharAt(int) * @see #resize(int) * @see #ensureSize(int) */ public void copyAt(int srcOffset, int destOffset, int len) throws ArrayIndexOutOfBoundsException { if (len > 0) { char[] array = this.array; System.arraycopy(array, srcOffset, array, destOffset, len); } } /** * Resizes this vector. ** * The result is the same as of * setArray(resize(array(), size)). This method changes * the length of this vector to the specified one. * Important notes: if size (length) of the vector grows then its * new elements are set to zero. If an exception is thrown then * this vector remains unchanged. ** * @param size * the (unsigned) length of this vector to set. * @exception OutOfMemoryError * if there is not enough memory. ** * @see CharVector#CharVector(int) * @see #setArray(char[]) * @see #array() * @see #length() * @see #ensureSize(int) * @see #resize(char[], int) */ public void resize(int size) { int len; char[] array = this.array; if ((len = array.length) != size) { char[] newArray = EMPTY; if (size != 0) { if (len > size) if (size < 0) size = -1 >>> 1; else len = size; System.arraycopy(array, 0, newArray = new char[size], 0, len); } this.array = newArray; } } /** * Ensures the size (capacity) of this vector. ** * The result is the same as of * setArray(ensureSize(array(), size)). This method * changes (only if size is greater than * length()) the length of this vector to * a value not less than size. Important notes: if size * (length) of the vector grows then its new elements are set to * zero. If an exception is thrown then this vector * remains unchanged. ** * @param size * the (unsigned) length of this vector to be ensured. * @exception OutOfMemoryError * if there is not enough memory. ** * @see #array() * @see #length() * @see #setAt(int, char) * @see #resize(int) * @see #ensureSize(char[], int) */ public void ensureSize(int size) { int len; char[] array = this.array, newArray; if ((((len = array.length) - size) | size) < 0) { if (size < 0) size = -1 >>> 1; if ((len += len >> 1) >= size) size = len; System.arraycopy(array, 0, newArray = new char[size], 0, array.length); this.array = newArray; } } /** * Resizes a given array. ** * This method 'changes' (creates a new array and copies the content * to it) the length of the specified array to the specified one. * Important notes: array elements are not changed; if * length of array is the same as * size then array is returned else * array content is copied into the result (all new * elements are set to zero). ** * @param array * the array (must be non-null) to be resized. * @param size * the (unsigned) length of the array to set. * @return * the resized array (not null, with * length equal to size). * @exception NullPointerException * if array is null. * @exception OutOfMemoryError * if there is not enough memory. ** * @see #resize(int) * @see #ensureSize(char[], int) * @see #fill(char[], int, int, char) */ public static final char[] resize(char[] array, int size) throws NullPointerException { int len; if ((len = array.length) != size) { char[] newArray = EMPTY; if (size != 0) { if (len > size) if (size < 0) size = -1 >>> 1; else len = size; System.arraycopy(array, 0, newArray = new char[size], 0, len); } array = newArray; } return array; } /** * Ensures the length (capacity) of a given array. ** * This method 'grows' (only if size is greater than * length of array) the length of * array. Important notes: array elements are * not changed; if length of array is * greater or the same as size then array is * returned else array content is copied into the result * (all new elements are set to zero). ** * @param array * the array (must be non-null) to be length-ensured. * @param size * the (unsigned) length of the array to ensure. * @return * the length-ensured array (not null, with * length not less than size). * @exception NullPointerException * if array is null. * @exception OutOfMemoryError * if there is not enough memory. ** * @see #ensureSize(int) * @see #resize(char[], int) * @see #fill(char[], int, int, char) */ public static final char[] ensureSize(char[] array, int size) throws NullPointerException { int len; if ((((len = array.length) - size) | size) < 0) { if (size < 0) size = -1 >>> 1; if ((len += len >> 1) >= size) size = len; char[] newArray; System.arraycopy(array, 0, newArray = new char[size], 0, array.length); array = newArray; } return array; } /** * Fills in the region of a given array with the specified value. ** * All the elements in the specified region of array are * set to value. Negative len is treated as * zero. If an exception is thrown then array remains * unchanged. Else array content is altered. Important * notes: region filling is performed using * arraycopy(Object, int, Object, int, int) method of * System class. ** * @param array * the array (must be non-null) to be filled in. * @param offset * the first index (must be in the range) of the region to fill in. * @param len * the length of the region to be filled. * @param value * the value to fill with. * @exception NullPointerException * if array is null. * @exception ArrayIndexOutOfBoundsException * if len is positive and (offset is negative * or is greater than length of array minus * len). ** * @see #array() * @see #copyAt(int, int, int) * @see #indexOf(char[], int, int, int, char[], boolean) * @see #lastIndexOf(char[], int, int, int, char[], boolean) * @see #quickSort(char[], int, int) * @see #binarySearch(char[], int, int, char) ** * @since 2.0 */ public static final void fill(char[] array, int offset, int len, char value) throws NullPointerException, ArrayIndexOutOfBoundsException { int next = array.length, block; if (len > 0) { next = array[(block = offset) + (--len)]; if ((next = len) > 2) next = 3; do { array[block++] = value; } while (next-- > 0); len--; next = 2; while ((len -= next) > 0) { if ((block = next <<= 1) >= len) next = len; System.arraycopy(array, offset, array, offset + block, next); } } } /** * Reverses the elements order in a given array. ** * The first element is exchanged with the least one, the second one * is exchanged with the element just before the last one, etc. * array content is altered. ** * @param array * the array (must be non-null) to be reversed. * @exception NullPointerException * if array is null. ** * @see #array() * @see #countNonZero(char[]) * @see #indexOf(char, int, char[], boolean) * @see #lastIndexOf(char, int, char[], boolean) * @see #hashCode(char[], boolean) * @see #equals(char[], char[]) * @see #compare(char[], int, int, char[], int, int, boolean) * @see #mismatches(char[], int, char[], int, int, boolean) */ public static final void reverse(char[] array) throws NullPointerException { int offset = 0, len = array.length; while (--len > offset) { char value = array[offset]; array[offset++] = array[len]; array[len] = value; } } /** * Count non-zero elements in a given array. ** * This method returns the count of elements of array * which are not equal to zero. ** * @param array * the array (must be non-null) to count non-zero * elements in. * @return * the count (non-negative and not greater than length * of array) of non-zero elements. * @exception NullPointerException * if array is null. ** * @see #array() * @see #fill(char[], int, int, char) * @see #equals(char[], char[]) * @see #compare(char[], int, int, char[], int, int, boolean) * @see #mismatches(char[], int, char[], int, int, boolean) ** * @since 2.0 */ public static final int countNonZero(char[] array) throws NullPointerException { int offset = array.length, count = 0; while (offset-- > 0) if (array[offset] != 0) count++; return count; } /** * Searches forward for value in a given array. ** * Negative index is treated as zero, too big * index is treated as length of * array. Characters case is ignored only if * ignoreCase. If value is not found then the * result is -1. ** * @param value * the character to sequentially search for. * @param index * the first index, from which to begin forward searching. * @param array * the array (must be non-null) to be searched in. * @param ignoreCase * true if and only if characters case is ignored. * @return * the index (non-negative) of the found value or -1 * (if not found). * @exception NullPointerException * if array is null. ** * @see #array() * @see #lastIndexOf(char, int, char[], boolean) * @see #indexOf(char[], int, int, int, char[], boolean) * @see #binarySearch(char[], int, int, char) * @see #equals(char[], char[]) * @see #compare(char[], int, int, char[], int, int, boolean) */ public static final int indexOf(char value, int index, char[] array, boolean ignoreCase) throws NullPointerException { if (index <= 0) index = 0; index--; int len = array.length; if (ignoreCase) { char temp, upper; char lower = Character.toLowerCase(upper = Character.toUpperCase(value)); while (++index < len && (temp = array[index]) != value && (temp = Character.toUpperCase(temp)) != upper && Character.toLowerCase(temp) != lower); } else while (++index < len && array[index] != value); if (index >= len) index = -1; return index; } /** * Searches backward for value in a given array. ** * Negative index is treated as -1, too big * index is treated as length of * array minus one. Characters case is ignored only if * ignoreCase. If value is not found then the * result is -1. ** * @param value * the character to sequentially search for. * @param index * the first index, from which to begin backward searching. * @param array * the array (must be non-null) to be searched in. * @param ignoreCase * true if and only if characters case is ignored. * @return * the index (non-negative) of the found value or -1 * (if not found). * @exception NullPointerException * if array is null. ** * @see #array() * @see #indexOf(char, int, char[], boolean) * @see #lastIndexOf(char[], int, int, int, char[], boolean) * @see #binarySearch(char[], int, int, char) * @see #reverse(char[]) * @see #equals(char[], char[]) * @see #compare(char[], int, int, char[], int, int, boolean) */ public static final int lastIndexOf(char value, int index, char[] array, boolean ignoreCase) throws NullPointerException { if (index < 0) index = -1; int len; if ((len = array.length) <= index) index = len - 1; index++; if (ignoreCase) { char temp, upper; char lower = Character.toLowerCase(upper = Character.toUpperCase(value)); while (index-- > 0 && (temp = array[index]) != value && (temp = Character.toUpperCase(temp)) != upper && Character.toLowerCase(temp) != lower); } else while (index-- > 0 && array[index] != value); return index; } /** * Searches forward for the specified sequence in a given array. ** * The searched sequence of values is specified by * subArray, offset and len. * Negative len is treated as zero. Negative * index is treated as zero, too big index is * treated as length of array. Characters * case is ignored only if ignoreCase. If the sequence is * not found then the result is -1. ** * @param subArray * the array (must be non-null) specifying the sequence * of characters to search for. * @param offset * the offset (must be in the range) of the sequence in * subArray. * @param len * the length of the sequence. * @param index * the first index, from which to begin forward searching. * @param array * the array (must be non-null) to be searched in. * @param ignoreCase * true if and only if characters case is ignored. * @return * the index (non-negative) of the found sequence or -1 * (if not found). * @exception NullPointerException * if subArray is null or array * is null. * @exception ArrayIndexOutOfBoundsException * if len is positive and (offset is negative * or is greater than length of subArray * minus len). ** * @see #array() * @see #indexOf(char, int, char[], boolean) * @see #lastIndexOf(char[], int, int, int, char[], boolean) * @see #equals(char[], char[]) * @see #compare(char[], int, int, char[], int, int, boolean) */ public static final int indexOf(char[] subArray, int offset, int len, int index, char[] array, boolean ignoreCase) throws NullPointerException, ArrayIndexOutOfBoundsException { int curOffset = subArray.length, arrayLen = array.length; if (index <= 0) index = 0; if (len > 0) { arrayLen -= len; char value = subArray[offset]; curOffset = subArray[len += offset - 1]; index--; if (ignoreCase) { char temp, upper; char lower = Character.toLowerCase(upper = Character.toUpperCase(value)); while (++index <= arrayLen) if ((temp = array[index]) == value || (temp = Character.toUpperCase(temp)) == upper || Character.toLowerCase(temp) == lower) { curOffset = offset; int curIndex = index; while (++curOffset <= len) { char curValue = subArray[curOffset]; if ((temp = array[++curIndex]) != curValue) { temp = Character.toUpperCase(temp); if ((curValue = Character.toUpperCase(curValue)) != temp && Character.toLowerCase(curValue) != Character.toLowerCase(temp)) break; } } if (curOffset > len) break; } } else while (++index <= arrayLen) if (array[index] == value) { curOffset = offset; int curIndex = index; while (++curOffset <= len && array[++curIndex] == subArray[curOffset]); if (curOffset > len) break; } } if (index > arrayLen) index = -1; return index; } /** * Searches backward for the specified sequence in a given array. ** * The searched sequence of values is specified by * subArray, offset and len. * Negative len is treated as zero. Negative * index is treated as -1, too big * index is treated as length of * array minus one. Characters case is ignored only if * ignoreCase. If the sequence is not found then the * result is -1. ** * @param subArray * the array (must be non-null) specifying the sequence * of characters to search for. * @param offset * the offset (must be in the range) of the sequence in * subArray. * @param len * the length of the sequence. * @param index * the first index, from which to begin backward searching. * @param array * the array (must be non-null) to be searched in. * @param ignoreCase * true if and only if characters case is ignored. * @return * the index (non-negative) of the found sequence or -1 * (if not found). * @exception NullPointerException * if subArray is null or array * is null. * @exception ArrayIndexOutOfBoundsException * if len is positive and (offset is negative * or is greater than length of subArray * minus len). ** * @see #array() * @see #lastIndexOf(char, int, char[], boolean) * @see #indexOf(char[], int, int, int, char[], boolean) * @see #equals(char[], char[]) * @see #compare(char[], int, int, char[], int, int, boolean) */ public static final int lastIndexOf(char[] subArray, int offset, int len, int index, char[] array, boolean ignoreCase) throws NullPointerException, ArrayIndexOutOfBoundsException { int curOffset = subArray.length, arrayLen; if (len <= 0) len = 0; if ((arrayLen = array.length - len) <= index) index = arrayLen; if (index < 0) index = -1; if (len > 0) { char value = subArray[offset]; curOffset = subArray[len += offset - 1]; index++; if (ignoreCase) { char temp, upper; char lower = Character.toLowerCase(upper = Character.toUpperCase(value)); while (index-- > 0) if ((temp = array[index]) == value || (temp = Character.toUpperCase(temp)) == upper || Character.toLowerCase(temp) == lower) { curOffset = offset; arrayLen = index; while (++curOffset <= len) { char curValue = subArray[curOffset]; if ((temp = array[++arrayLen]) != curValue) { temp = Character.toUpperCase(temp); if ((curValue = Character.toUpperCase(curValue)) != temp && Character.toLowerCase(curValue) != Character.toLowerCase(temp)) break; } } if (curOffset > len) break; } } else while (index-- > 0) if (array[index] == value) { curOffset = offset; arrayLen = index; while (++curOffset <= len && array[++arrayLen] == subArray[curOffset]); if (curOffset > len) break; } } return index; } /** * Produces a hash code value for a given array. ** * This method mixes all the characters (or their lower-case * equivalents if ignoreCase) of array to * produce a single hash code value. ** * @param array * the array (must be non-null) to evaluate hash of. * @param ignoreCase * true if and only if characters case is ignored. * @return * the hash code value for array. * @exception NullPointerException * if array is null. ** * @see #array() * @see #hashCode() * @see #fill(char[], int, int, char) * @see #reverse(char[]) * @see #countNonZero(char[]) * @see #indexOf(char, int, char[], boolean) * @see #lastIndexOf(char, int, char[], boolean) * @see #equals(char[], char[]) * @see #compare(char[], int, int, char[], int, int, boolean) * @see #mismatches(char[], int, char[], int, int, boolean) */ public static final int hashCode(char[] array, boolean ignoreCase) throws NullPointerException { int code = 0, offset = 0, len = array.length; if (ignoreCase) while (offset < len) { code ^= Character.toLowerCase(array[offset++]); code = (code << 5) - code; } while (offset < len) { code ^= array[offset++]; code = (code << 5) - code; } return code ^ offset; } /** * Tests whether or not the specified two arrays are equal. ** * This method returns true if and only if both of the * arrays are of the same length and all the characters of the first * array are equal to the corresponding characters of the second * array (matching their case). ** * @param arrayA * the first array (must be non-null) to be compared. * @param arrayB * the second array (must be non-null) to compare with. * @return * true if and only if arrayA content is the * same as arrayB content. * @exception NullPointerException * if arrayA is null or arrayB is * null. ** * @see #array() * @see #equals(java.lang.Object) * @see #fill(char[], int, int, char) * @see #reverse(char[]) * @see #countNonZero(char[]) * @see #indexOf(char, int, char[], boolean) * @see #lastIndexOf(char, int, char[], boolean) * @see #indexOf(char[], int, int, int, char[], boolean) * @see #lastIndexOf(char[], int, int, int, char[], boolean) * @see #hashCode(char[], boolean) * @see #compare(char[], int, int, char[], int, int, boolean) * @see #mismatches(char[], int, char[], int, int, boolean) ** * @since 2.0 */ public static final boolean equals(char[] arrayA, char[] arrayB) throws NullPointerException { int offset = arrayA.length; if (arrayA != arrayB) if (arrayB.length != offset) return false; else while (offset-- > 0) if (arrayA[offset] != arrayB[offset]) return false; return true; } /** * Count the mismatches of two given array regions. ** * This method returns the count of characters of the first array * region which are not equal to the corresponding characters of the * second array region (matching case or not). Negative * len is treated as zero. ** * @param arrayA * the first array (must be non-null) to be compared. * @param offsetA * the first index (must be in the range) of the first region. * @param arrayB * the second array (must be non-null) to compare with. * @param offsetB * the first index (must be in the range) of the second region. * @param len * the length of the regions. * @param ignoreCase * true if and only if characters case is ignored. * @return * the count (non-negative) of found mismatches of the regions. * @exception NullPointerException * if arrayA is null or arrayB is * null. * @exception ArrayIndexOutOfBoundsException * if len is positive and (offsetA is negative * or is greater than length of arrayA minus * len, or offsetB is negative or is greater * than length of arrayB minus * len). ** * @see #array() * @see #fill(char[], int, int, char) * @see #reverse(char[]) * @see #countNonZero(char[]) * @see #hashCode(char[], boolean) * @see #equals(char[], char[]) * @see #compare(char[], int, int, char[], int, int, boolean) ** * @since 2.0 */ public static final int mismatches(char[] arrayA, int offsetA, char[] arrayB, int offsetB, int len, boolean ignoreCase) throws NullPointerException, ArrayIndexOutOfBoundsException { int count = arrayA.length - arrayB.length; count = 0; if (len > 0) { char value = arrayA[offsetA]; value = arrayA[offsetA + len - 1]; value = arrayB[offsetB]; value = arrayB[offsetB + len - 1]; if (offsetA != offsetB || arrayA != arrayB) if (ignoreCase) do { char temp = arrayB[offsetB++]; if ((value = arrayA[offsetA++]) != temp) { temp = Character.toUpperCase(temp); if ((value = Character.toUpperCase(value)) != temp && Character.toLowerCase(value) != Character.toLowerCase(temp)) count++; } } while (--len > 0); else do { if (arrayA[offsetA++] != arrayB[offsetB++]) count++; } while (--len > 0); } return count; } /** * Compares two given array regions. ** * This method returns a signed integer indicating case-sensitive or * case-insensitive 'less-equal-greater' relation between the * specified array regions of characters (the absolute value of the * result, in fact, is the distance between the first found mismatch * and the end of the bigger-length region). Negative * lenA is treated as zero. Negative lenB is * treated as zero. Important notes: the content of array regions is * compared before comparing their length. ** * @param arrayA * the first array (must be non-null) to be compared. * @param offsetA * the first index (must be in the range) of the first region. * @param lenA * the length of the first region. * @param arrayB * the second array (must be non-null) to compare with. * @param offsetB * the first index (must be in the range) of the second region. * @param lenB * the length of the second region. * @param ignoreCase * true if and only if characters case is ignored. * @return * a negative integer, zero, or a positive integer as * arrayA region is less than, equal to, or greater than * arrayB one. * @exception NullPointerException * if arrayA is null or arrayB is * null. * @exception ArrayIndexOutOfBoundsException * if lenA is positive and (offsetA is * negative or is greater than length of * arrayA minus lenA), or if lenB * is positive and (offsetB is negative or is greater * than length of arrayB minus * lenB). ** * @see #array() * @see #greaterThan(java.lang.Object) * @see #fill(char[], int, int, char) * @see #reverse(char[]) * @see #indexOf(char, int, char[], boolean) * @see #lastIndexOf(char, int, char[], boolean) * @see #hashCode(char[], boolean) * @see #equals(char[], char[]) * @see #mismatches(char[], int, char[], int, int, boolean) */ public static final int compare(char[] arrayA, int offsetA, int lenA, char[] arrayB, int offsetB, int lenB, boolean ignoreCase) throws NullPointerException, ArrayIndexOutOfBoundsException { char value = (char)(arrayA.length - arrayB.length); if (lenA > 0) { value = arrayA[offsetA]; value = arrayA[offsetA + lenA - 1]; } else lenA = 0; if (lenB > 0) { value = arrayB[offsetB]; value = arrayB[offsetB + lenB - 1]; } else lenB = 0; if ((lenB = lenA - lenB) >= 0) lenA -= lenB; if (offsetA != offsetB || arrayA != arrayB) { char temp = value; while (lenA > 0) { temp = arrayB[offsetB++]; if ((value = arrayA[offsetA++]) != temp) { if (!ignoreCase) break; temp = Character.toUpperCase(temp); if ((value = Character.toUpperCase(value)) != temp) { temp = Character.toLowerCase(temp); if ((value = Character.toLowerCase(value)) != temp) break; } } lenA--; } if (lenA > 0) { if (lenB <= 0) lenB = -lenB; lenB += lenA; if (value < temp) lenB = -lenB; } } return lenB; } /** * Sorts the elements in the region of a given array using 'Quick' * algorithm. ** * Elements in the region are sorted into ascending natural * (unsigned) order. But equal elements may be reordered (since the * algorithm is not 'stable'). A small working stack is allocated * (since the algorithm is 'in-place' and recursive). The algorithm * cost is O(log(len) * len) typically, but may be of * O(len * len) in the worst case (which is rare, in * fact). Negative len is treated as zero. If an * exception is thrown then array remains unchanged. Else * the region content is altered. ** * @param array * the array (must be non-null) to be sorted. * @param offset * the first index (must be in the range) of the region to sort. * @param len * the length of the region to sort. * @exception NullPointerException * if array is null. * @exception ArrayIndexOutOfBoundsException * if len is positive and (offset is negative * or is greater than length of array minus * len). * @exception OutOfMemoryError * if there is not enough memory. ** * @see #array() * @see #counterSort(char[], int, int) * @see #binarySearch(char[], int, int, char) * @see #fill(char[], int, int, char) */ public static final void quickSort(char[] array, int offset, int len) throws NullPointerException, ArrayIndexOutOfBoundsException { if (len > 0) { char value = array[offset], temp; if (len > 1) { value = array[len += offset - 1]; int[] bounds = new int[(JavaConsts.INT_SIZE - 3) << 1]; int level = 2, index, last; do { do { index = offset; if ((last = len) - offset < 8) { len = offset; do { value = array[offset = ++index]; do { if (!((temp = array[offset - 1]) > value)) break; array[offset--] = temp; } while (offset > len); array[offset] = value; } while (index < last); break; } value = array[len = (offset + len) >>> 1]; array[len] = array[offset]; array[offset] = value; len = last; do { while (++offset < len && value > array[offset]); len++; while (--len >= offset && array[len] > value); if (offset >= len) break; temp = array[len]; array[len--] = array[offset]; array[offset] = temp; } while (true); array[offset = index] = array[len]; array[len] = value; if (len - offset > last - len) { offset = len + 1; len = last; last = offset - 2; } else index = (len--) + 1; bounds[level++] = index; bounds[level++] = last; } while (offset < len); len = bounds[--level]; offset = bounds[--level]; } while (level > 0); } } len = array.length; } /** * Sorts the elements in the region of a given array by counting the * amount of each possible value in it. ** * Elements in the region are sorted into ascending natural * (unsigned) order. A working (counter) buffer of * (CHAR_MASK + 1) (or (BYTE_MASK + 1) if * all characters in the region are bytes) integer values is * allocated. The algorithm cost is linear but only for large * regions. Negative len is treated as zero. If an * exception is thrown then array remains unchanged. Else * the region content is altered. ** * @param array * the array (must be non-null) to be sorted. * @param offset * the first index (must be in the range) of the region to sort. * @param len * the length of the region to sort. * @exception NullPointerException * if array is null. * @exception ArrayIndexOutOfBoundsException * if len is positive and (offset is negative * or is greater than length of array minus * len). * @exception OutOfMemoryError * if there is not enough memory. ** * @see #array() * @see #quickSort(char[], int, int) * @see #binarySearch(char[], int, int, char) * @see #fill(char[], int, int, char) ** * @since 2.0 */ public static final void counterSort(char[] array, int offset, int len) throws NullPointerException, ArrayIndexOutOfBoundsException { if (len > 0) { char value = array[offset]; if (len > 1) { value = array[len += offset - 1]; int[] counter = null; int count = offset; if (JavaConsts.BYTE_MASK < JavaConsts.CHAR_MASK && JavaConsts.BYTE_MASK + 1 > 0) { counter = new int[JavaConsts.BYTE_MASK + 1]; do { if ((value = array[offset]) > JavaConsts.BYTE_MASK) break; counter[value]++; } while (++offset <= len); } if (offset <= len) { int[] newCounter = new int[JavaConsts.CHAR_MASK + 1 > 0 ? JavaConsts.CHAR_MASK + 1 : -1 >>> 1]; if (offset > count) System.arraycopy(counter, 0, newCounter, 0, JavaConsts.BYTE_MASK + 1); counter = newCounter; do { counter[array[offset]]++; } while (++offset <= len); } value = 0; offset = count; do { for (count = counter[value]; count > 0; count--) array[offset++] = value; value++; } while (offset <= len); } } len = array.length; } /** * Searches (fast) for value in a given sorted array. ** * array (or its specified range) must be sorted * ascending, or the result is undefined. The algorithm cost is of * O(log(len)). Negative len is treated as * zero. If value is not found then * (-result - 1) is the offset of the insertion point * for value. ** * @param array * the sorted array (must be non-null) to be searched * in. * @param offset * the first index (must be in the range) of the region to search * in. * @param len * the length of the region to search in. * @param value * the case-sensitive value to search for. * @return * the index (non-negative) of the found value or * (-insertionOffset - 1) (a negative integer) if not * found. * @exception NullPointerException * if array is null. * @exception ArrayIndexOutOfBoundsException * if len is positive and (offset is negative * or is greater than length of array minus * len). ** * @see #array() * @see #indexOf(char, int, char[], boolean) * @see #lastIndexOf(char, int, char[], boolean) * @see #quickSort(char[], int, int) * @see #counterSort(char[], int, int) * @see #fill(char[], int, int, char) */ public static final int binarySearch(char[] array, int offset, int len, char value) throws NullPointerException, ArrayIndexOutOfBoundsException { if (len > 0) { int middle; char temp = array[offset]; temp = array[len += offset - 1]; do { if ((temp = array[middle = (offset + len) >>> 1]) > value) len = middle - 1; else if (temp != value) offset = middle + 1; else break; } while (offset <= len); if (offset <= len) offset = ~middle; } len = array.length; return ~offset; } /** * Creates and returns a copy of this object. ** * This method creates a new instance of the class of this object * and initializes its array with a copy of * array of this vector. ** * @return * a copy (not null and != this) of * this instance. * @exception OutOfMemoryError * if there is not enough memory. ** * @see CharVector#CharVector() * @see #array() * @see #getCharAt(int) * @see #equals(java.lang.Object) */ public Object clone() { Object obj; try { if ((obj = super.clone()) instanceof CharVector && obj != this) { CharVector vector = (CharVector)obj; vector.array = (char[])vector.array.clone(); return obj; } } catch (CloneNotSupportedException e) {} throw new InternalError("CloneNotSupportedException"); } /** * Computes and returns a hash code value for the object. ** * This method mixes lower-case equivalents of all the characters of * this vector to produce a single hash code value. ** * @return * a hash code value for this object. ** * @see #hashCode(char[], boolean) * @see #array() * @see #length() * @see #getCharAt(int) * @see #equals(java.lang.Object) */ public int hashCode() { return hashCode(this.array, true); } /** * Indicates whether this object is equal to the * specified one. ** * This method returns true if and only if * obj is instance of this vector class and all elements * of this vector are equal to the corresponding * elements of obj vector. ** * @param obj * the object (may be null) with which to compare. * @return * true if and only if this value is the * same as obj value. ** * @see CharVector#CharVector() * @see #equals(char[], char[]) * @see #array() * @see #length() * @see #getCharAt(int) * @see #hashCode() * @see #greaterThan(java.lang.Object) */ public boolean equals(Object obj) { return obj == this || obj instanceof CharVector && equals(this.array, ((CharVector)obj).array); } /** * Tests for being semantically greater than the argument. ** * The result is true if and only if obj is * instance of this class and this object * is greater than the specified object. Vectors are compared in the * element-by-element case-sensitive manner, starting at index * 0. ** * @param obj * the second compared object (may be null). * @return * true if obj is comparable with * this and this object is greater than * obj, else false. ** * @see #compare(char[], int, int, char[], int, int, boolean) * @see #array() * @see #length() * @see #getCharAt(int) * @see #equals(java.lang.Object) ** * @since 2.0 */ public boolean greaterThan(Object obj) { if (obj != this && obj instanceof CharVector) { char[] array = this.array, otherArray = ((CharVector)obj).array; if (compare(array, 0, array.length, otherArray, 0, otherArray.length, false) > 0) return true; } return false; } /** * Converts this character vector to a string. ** * All the characters of the wrapped array are concatenated together * (without spaces) to produce a single string (using * String(char[]) constructor). ** * @return * the string representation (not null) of * this object. * @exception OutOfMemoryError * if there is not enough memory. ** * @see #array() * @see #length() */ public String toString() { return new String(this.array); } /** * Verifies this object for its integrity. ** * For debug purpose only. ** * @exception InternalError * if integrity violation is detected. ** * @see CharVector#CharVector(char[]) * @see #setArray(char[]) * @see #array() ** * @since 2.0 */ public void integrityCheck() { if (this.array == null) throw new InternalError("array: null"); } /** * Deserializes an object of this class from a given stream. ** * This method is responsible for reading from in stream, * restoring the classes fields, and verifying that the serialized * object is not corrupted. First of all, it calls * defaultReadObject() for in to invoke the * default deserialization mechanism. Then, it restores the state of * transient fields and performs additional * verification of the deserialized object. This method is used only * internally by ObjectInputStream class. ** * @param in * the stream (must be non-null) to read data from in * order to restore the object. * @exception NullPointerException * if in is null. * @exception IOException * if any I/O error occurs or the serialized object is corrupted. * @exception ClassNotFoundException * if the class for an object being restored cannot be found. * @exception OutOfMemoryError * if there is not enough memory. ** * @see CharVector#CharVector(char[]) * @see #integrityCheck() */ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); if (this.array == null) throw new InvalidObjectException("array: null"); } }