Collection of static methods for operations on arrays
Method from org.hsqldb.lib.ArrayUtil Detail: |
public static void copyAdjustArray(Object[] source,
Object[] dest,
Object addition,
int colindex,
int adjust) {
int i;
for (i = 0; i < colindex; i++) {
dest[i] = source[i];
}
if (i == dest.length) {
return;
}
if (adjust < 0) {
i++;
} else {
dest[i] = addition;
}
for (; i < source.length; i++) {
dest[i + adjust] = source[i];
}
}
Copies elements of source to dest. If adjust is -1 the element at
colindex is not copied. If adjust is +1 that element is filled with
the Object addition. All the rest of the elements in source are
shifted left or right accordingly when they are copied.
No checks are perfomed on array sizes and an exception is thrown
if they are not consistent with the other arguments. |
public static void copyArray(int[] source,
int[] dest,
int count) {
System.arraycopy(source, 0, dest, 0, count);
}
|
public static void copyColumnValues(Object[] row,
int[] colindex,
Object[] colobject) {
for (int i = 0; i < colindex.length; i++) {
colobject[i] = row[colindex[i]];
}
}
Copies some elements of row into colobject by using colindex as the
list of indexes into row. colindex and colobject are of equal length
and normally shorter than row; |
public static int[] getAdjustedColumnArray(int[] colarr,
int size,
int colindex,
int adjust) {
int[] newarr = new int[size];
copyArray(colarr, newarr, size);
return toAdjustedColumnArray(newarr, colindex, adjust);
}
Convenience wrapper for toAdjustedColumnArray() that creates the new
array. |
public static boolean haveCommonElement(int[] a,
int[] b,
int lenb) {
for (int i = 0; i < a.length; i++) {
int c = a[i];
for (int j = 0; j < lenb; j++) {
if (c == b[j]) {
return true;
}
}
}
return false;
}
Checks for any overlap between two arrays of column indexes.
Limit check to lenb elements of b |
public static boolean haveEqualArrays(int[] a,
int[] b,
int count) {
if (count > a.length || count > b.length) {
return false;
}
if (count == 1) {
return a[0] == b[0];
}
for (int j = 0; j < count; j++) {
if (a[j] != b[j]) {
return false;
}
}
return true;
}
Returns true if the first count elements of a and b are identical
subarrays of integers |
public static boolean haveEqualSets(int[] a,
int[] b,
int count) {
if (count > a.length || count > b.length) {
return false;
}
if (count == 1) {
return a[0] == b[0];
}
int[] tempa = new int[count];
int[] tempb = new int[count];
copyArray(a, tempa, count);
copyArray(b, tempb, 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 a and b are identical sets
of integers |
public static boolean haveEquality(int[] a,
int[] b,
int count,
boolean sets) {
if (sets) {
if (a.length == b.length && count == a.length
&& ArrayUtil.haveEqualSets(a, b, count)) {
return true;
}
} else {
if (ArrayUtil.haveEqualArrays(a, b, count)) {
return true;
}
}
return false;
}
For sets == true returns true if a and b are the same length and
contain the same set of integers. For sets == false returns the result
of haveEqualArrays(a,b,count) |
public static void sortArray(int[] intarr) {
boolean swapped;
do {
swapped = false;
for (int i = 0; i < intarr.length - 1; i++) {
if (intarr[i] > intarr[i + 1]) {
int temp = intarr[i + 1];
intarr[i + 1] = intarr[i];
intarr[i] = temp;
swapped = true;
}
}
} while (swapped);
}
Basic sort for small arrays. |
public static int[] toAdjustedColumnArray(int[] colarr,
int colindex,
int adjust) {
if (colarr == null) {
return null;
}
int[] intarr = new int[colarr.length];
int j = 0;
for (int i = 0; i < colarr.length; i++) {
if (colarr[i] > colindex) {
intarr[j] = colarr[i] + adjust;
j++;
} else if (colarr[i] == colindex) {
if (adjust < 0) {
// skip an element from colarr
} else {
intarr[j] = colarr[i] + adjust;
j++;
}
} else {
intarr[j] = colarr[i];
j++;
}
}
if (colarr.length != j) {
int[] newarr = new int[j];
copyArray(intarr, newarr, j);
return newarr;
}
return intarr;
}
Returns new array with the elements in collar ajusted to reflect
changes at colindex.
Each element in collarr represents an index into another array
otherarr.
colindex is the index at which an element is added or removed form
otherarr. Each element in the result array represents the new,
adjusted index to otherarr.
For each element of collarr that represents an index equal to
colindex and adjust is -1, the result will not contain that element
and will be shorter than collar by one element. |