Source code: org/hsqldb/lib/ArrayUtil.java
1 /* Copyright (c) 2001-2002, The HSQL Development Group
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of the HSQL Development Group nor the names of its
15 * contributors may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31
32 package org.hsqldb.lib;
33
34 /**
35 * Collection of static methods for operations on arrays
36 *
37 * @author fredt@users
38 * @version 1.7.0
39 */
40 public class ArrayUtil {
41
42 /**
43 * Basic sort for small arrays.
44 */
45 public static void sortArray(int intarr[]) {
46
47 boolean swapped;
48
49 do {
50 swapped = false;
51
52 for (int i = 0; i < intarr.length - 1; i++) {
53 if (intarr[i] > intarr[i + 1]) {
54 int temp = intarr[i + 1];
55
56 intarr[i + 1] = intarr[i];
57 intarr[i] = temp;
58 swapped = true;
59 }
60 }
61 } while (swapped);
62 }
63
64 /**
65 * For sets == true returns true if a and b are the same length and
66 * contain the same set of integers. For sets == false returns the result
67 * of haveEqualArrays(a,b,count)
68 *
69 */
70 public static boolean haveEquality(int[] a, int[] b, int count,
71 boolean sets) {
72
73 if (sets) {
74 if (a.length == b.length && count == a.length
75 && ArrayUtil.haveEqualSets(a, b, count)) {
76 return true;
77 }
78 } else {
79 if (ArrayUtil.haveEqualArrays(a, b, count)) {
80 return true;
81 }
82 }
83
84 return false;
85 }
86
87 /**
88 * Returns true if the first count elements of a and b are identical sets
89 * of integers
90 *
91 */
92 public static boolean haveEqualSets(int[] a, int[] b, int count) {
93
94 if (count > a.length || count > b.length) {
95 return false;
96 }
97
98 if (count == 1) {
99 return a[0] == b[0];
100 }
101
102 int[] tempa = new int[count];
103 int[] tempb = new int[count];
104
105 copyArray(a, tempa, count);
106 copyArray(b, tempb, count);
107 sortArray(tempa);
108 sortArray(tempb);
109
110 for (int j = 0; j < count; j++) {
111 if (tempa[j] != tempb[j]) {
112 return false;
113 }
114 }
115
116 return true;
117 }
118
119 /**
120 * Returns true if the first count elements of a and b are identical
121 * subarrays of integers
122 *
123 */
124 public static boolean haveEqualArrays(int[] a, int[] b, int count) {
125
126 if (count > a.length || count > b.length) {
127 return false;
128 }
129
130 if (count == 1) {
131 return a[0] == b[0];
132 }
133
134 for (int j = 0; j < count; j++) {
135 if (a[j] != b[j]) {
136 return false;
137 }
138 }
139
140 return true;
141 }
142
143 /**
144 * Checks for any overlap between two arrays of column indexes.
145 * Limit check to lenb elements of b
146 */
147 public static boolean haveCommonElement(int[] a, int[] b, int lenb) {
148
149 for (int i = 0; i < a.length; i++) {
150 int c = a[i];
151
152 for (int j = 0; j < lenb; j++) {
153 if (c == b[j]) {
154 return true;
155 }
156 }
157 }
158
159 return false;
160 }
161
162 public static void copyArray(int[] source, int[] dest, int count) {
163 System.arraycopy(source, 0, dest, 0, count);
164 }
165
166 /**
167 * Copies elements of source to dest. If adjust is -1 the element at
168 * colindex is not copied. If adjust is +1 that element is filled with
169 * the Object addition. All the rest of the elements in source are
170 * shifted left or right accordingly when they are copied.
171 *
172 * No checks are perfomed on array sizes and an exception is thrown
173 * if they are not consistent with the other arguments.
174 *
175 * @param source
176 * @param dest
177 * @param addition
178 * @param colindex
179 * @param adjust +1 or 0 or -1
180 * return new, adjusted array or null if an element is removed
181 */
182 public static void copyAdjustArray(Object[] source, Object[] dest,
183 Object addition, int colindex,
184 int adjust) {
185
186 int i;
187
188 for (i = 0; i < colindex; i++) {
189 dest[i] = source[i];
190 }
191
192 if (i == dest.length) {
193 return;
194 }
195
196 if (adjust < 0) {
197 i++;
198 } else {
199 dest[i] = addition;
200 }
201
202 for (; i < source.length; i++) {
203 dest[i + adjust] = source[i];
204 }
205 }
206
207 /**
208 * Returns new array with the elements in collar ajusted to reflect
209 * changes at colindex.
210 *
211 * Each element in collarr represents an index into another array
212 * otherarr.
213 * colindex is the index at which an element is added or removed form
214 * otherarr. Each element in the result array represents the new,
215 * adjusted index to otherarr.
216 * For each element of collarr that represents an index equal to
217 * colindex and adjust is -1, the result will not contain that element
218 * and will be shorter than collar by one element.
219 *
220 *
221 * @param colarr
222 * @param colindex
223 * @param adjust +1 or 0 or -1
224 * return new, adjusted array
225 */
226 public static int[] toAdjustedColumnArray(int[] colarr, int colindex,
227 int adjust) {
228
229 if (colarr == null) {
230 return null;
231 }
232
233 int[] intarr = new int[colarr.length];
234 int j = 0;
235
236 for (int i = 0; i < colarr.length; i++) {
237 if (colarr[i] > colindex) {
238 intarr[j] = colarr[i] + adjust;
239
240 j++;
241 } else if (colarr[i] == colindex) {
242 if (adjust < 0) {
243
244 // skip an element from colarr
245 } else {
246 intarr[j] = colarr[i] + adjust;
247
248 j++;
249 }
250 } else {
251 intarr[j] = colarr[i];
252
253 j++;
254 }
255 }
256
257 if (colarr.length != j) {
258 int[] newarr = new int[j];
259
260 copyArray(intarr, newarr, j);
261
262 return newarr;
263 }
264
265 return intarr;
266 }
267
268 /**
269 * Convenience wrapper for toAdjustedColumnArray() that creates the new
270 * array.
271 */
272 public static int[] getAdjustedColumnArray(int[] colarr, int size,
273 int colindex, int adjust) {
274
275 int[] newarr = new int[size];
276
277 copyArray(colarr, newarr, size);
278
279 return toAdjustedColumnArray(newarr, colindex, adjust);
280 }
281
282 /**
283 * Copies some elements of row into colobject by using colindex as the
284 * list of indexes into row. colindex and colobject are of equal length
285 * and normally shorter than row;
286 *
287 */
288 public static void copyColumnValues(Object row[], int colindex[],
289 Object colobject[]) {
290
291 for (int i = 0; i < colindex.length; i++) {
292 colobject[i] = row[colindex[i]];
293 }
294 }
295 /*
296 public static void main(String[] args) {
297
298 int[] a = new int[] {
299 23, 11, 37, 7, 1, 5
300 };
301 int[] b = new int[] {
302 1, 3, 7, 11, 13, 17, 19, 3, 1
303 };
304 int[] c = toAdjustedColumnArray(a, 7, -1);
305 int[] d = toAdjustedColumnArray(b, 11, 1);
306 int[] e = new int[a.length];
307
308 copyArray(a, e, a.length);
309 sortArray(e);
310
311 int[] f = new int[b.length];
312
313 copyArray(b, f, b.length);
314 sortArray(f);
315
316 boolean x = haveEqualSets(a, e, a.length);
317 boolean y = haveEqualSets(b, f, b.length);
318
319 System.out.print("test passed: ");
320 System.out.print(x == true && y == true && c.length == a.length - 1
321 && d.length == b.length);
322 }
323 */
324 }