KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > lib > ArrayUtil


1 /* Copyright (c) 2001-2005, 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 import java.lang.reflect.Array JavaDoc;
35
36 /**
37  * Collection of static methods for operations on arrays
38  *
39  * @author fredt@users
40  * @version 1.7.2
41  * @since 1.7.2
42  */

43 public class ArrayUtil {
44
45     public static final int CLASS_CODE_BYTE = 'B';
46     public static final int CLASS_CODE_CHAR = 'C';
47     public static final int CLASS_CODE_DOUBLE = 'D';
48     public static final int CLASS_CODE_FLOAT = 'F';
49     public static final int CLASS_CODE_INT = 'I';
50     public static final int CLASS_CODE_LONG = 'J';
51     public static final int CLASS_CODE_OBJECT = 'L';
52     public static final int CLASS_CODE_SHORT = 'S';
53     public static final int CLASS_CODE_BOOLEAN = 'Z';
54     private static IntValueHashMap classCodeMap = new IntValueHashMap();
55
56     static {
57         classCodeMap.put(byte.class, ArrayUtil.CLASS_CODE_BYTE);
58         classCodeMap.put(char.class, ArrayUtil.CLASS_CODE_SHORT);
59         classCodeMap.put(short.class, ArrayUtil.CLASS_CODE_SHORT);
60         classCodeMap.put(int.class, ArrayUtil.CLASS_CODE_INT);
61         classCodeMap.put(long.class, ArrayUtil.CLASS_CODE_LONG);
62         classCodeMap.put(float.class, ArrayUtil.CLASS_CODE_FLOAT);
63         classCodeMap.put(double.class, ArrayUtil.CLASS_CODE_DOUBLE);
64         classCodeMap.put(boolean.class, ArrayUtil.CLASS_CODE_BOOLEAN);
65         classCodeMap.put(Object JavaDoc.class, ArrayUtil.CLASS_CODE_OBJECT);
66     }
67
68     /**
69      * Returns a distinct int code for each primitive type and for all Object types.
70      */

71     static int getClassCode(Class JavaDoc cla) {
72
73         if (!cla.isPrimitive()) {
74             return ArrayUtil.CLASS_CODE_OBJECT;
75         }
76
77         return classCodeMap.get(cla, -1);
78     }
79
80     /**
81      * Clears an area of the given array of the given type.
82      */

83     public static void clearArray(int type, Object JavaDoc data, int from, int to) {
84
85         switch (type) {
86
87             case ArrayUtil.CLASS_CODE_BYTE : {
88                 byte[] array = (byte[]) data;
89
90                 while (--to >= from) {
91                     array[to] = 0;
92                 }
93
94                 return;
95             }
96             case ArrayUtil.CLASS_CODE_CHAR : {
97                 byte[] array = (byte[]) data;
98
99                 while (--to >= from) {
100                     array[to] = 0;
101                 }
102
103                 return;
104             }
105             case ArrayUtil.CLASS_CODE_SHORT : {
106                 short[] array = (short[]) data;
107
108                 while (--to >= from) {
109                     array[to] = 0;
110                 }
111
112                 return;
113             }
114             case ArrayUtil.CLASS_CODE_INT : {
115                 int[] array = (int[]) data;
116
117                 while (--to >= from) {
118                     array[to] = 0;
119                 }
120
121                 return;
122             }
123             case ArrayUtil.CLASS_CODE_LONG : {
124                 long[] array = (long[]) data;
125
126                 while (--to >= from) {
127                     array[to] = 0;
128                 }
129
130                 return;
131             }
132             case ArrayUtil.CLASS_CODE_FLOAT : {
133                 float[] array = (float[]) data;
134
135                 while (--to >= from) {
136                     array[to] = 0;
137                 }
138
139                 return;
140             }
141             case ArrayUtil.CLASS_CODE_DOUBLE : {
142                 double[] array = (double[]) data;
143
144                 while (--to >= from) {
145                     array[to] = 0;
146                 }
147
148                 return;
149             }
150             case ArrayUtil.CLASS_CODE_BOOLEAN : {
151                 boolean[] array = (boolean[]) data;
152
153                 while (--to >= from) {
154                     array[to] = false;
155                 }
156
157                 return;
158             }
159             default : {
160                 Object JavaDoc[] array = (Object JavaDoc[]) data;
161
162                 while (--to >= from) {
163                     array[to] = null;
164                 }
165
166                 return;
167             }
168         }
169     }
170
171     /**
172      * Moves the contents of an array to allow both addition and removal of
173      * elements. Used arguments must be in range.
174      *
175      * @param type class type of the array
176      * @param array the array
177      * @param usedElements count of elements of array in use
178      * @param index point at which to add or remove elements
179      * @count number of elements to add or remove
180      */

181     public static void adjustArray(int type, Object JavaDoc array, int usedElements,
182                                    int index, int count) {
183
184         if (index >= usedElements) {
185             return;
186         }
187
188         int newCount = usedElements + count;
189         int source;
190         int target;
191         int size;
192
193         if (count >= 0) {
194             source = index;
195             target = index + count;
196             size = usedElements - index;
197         } else {
198             source = index - count;
199             target = index;
200             size = usedElements - index + count;
201         }
202
203         if (size > 0) {
204             System.arraycopy(array, source, array, target, size);
205         }
206
207         if (count < 0) {
208             clearArray(type, array, newCount, usedElements);
209         }
210     }
211
212     /**
213      * Basic sort for small arrays of int.
214      */

215     public static void sortArray(int[] array) {
216
217         boolean swapped;
218
219         do {
220             swapped = false;
221
222             for (int i = 0; i < array.length - 1; i++) {
223                 if (array[i] > array[i + 1]) {
224                     int temp = array[i + 1];
225
226                     array[i + 1] = array[i];
227                     array[i] = temp;
228                     swapped = true;
229                 }
230             }
231         } while (swapped);
232     }
233
234     /**
235      * Basic find for small arrays of Object.
236      */

237     public static int find(Object JavaDoc[] array, Object JavaDoc object) {
238
239         for (int i = 0; i < array.length; i++) {
240             if (array[i] == object) {
241
242                 // hadles both nulls
243
return i;
244             }
245
246             if (object != null && object.equals(array[i])) {
247                 return i;
248             }
249         }
250
251         return -1;
252     }
253
254     /**
255      * Basic find for small arrays of int.
256      */

257     public static int find(int[] array, int value) {
258
259         for (int i = 0; i < array.length; i++) {
260             if (array[i] == value) {
261                 return i;
262             }
263         }
264
265         return -1;
266     }
267
268     /**
269      * Finds the first element of the array that is not equal to the given value.
270      */

271     public static int findNot(int[] array, int value) {
272
273         for (int i = 0; i < array.length; i++) {
274             if (array[i] != value) {
275                 return i;
276             }
277         }
278
279         return -1;
280     }
281
282     /**
283      * Returns true if arra and arrb contain the same set of integers, not
284      * necessarily in the same order. This implies the arrays are of the same
285      * length.
286      */

287     public static boolean areEqualSets(int[] arra, int[] arrb) {
288         return arra.length == arrb.length
289                && ArrayUtil.haveEqualSets(arra, arrb, arra.length);
290     }
291
292     /**
293      * For full == true returns true if arra and arrb are identical (have the
294      * same length and contain the same integers in the same sequence).
295      *
296      * For full == false returns the result
297      * of haveEqualArrays(arra,arrb,count)
298      *
299      * For full == true, the array lengths must be the same as count
300      *
301      */

302     public static boolean areEqual(int[] arra, int[] arrb, int count,
303                                    boolean full) {
304
305         if (ArrayUtil.haveEqualArrays(arra, arrb, count)) {
306             if (full) {
307                 return arra.length == arrb.length && count == arra.length;
308             }
309
310             return true;
311         }
312
313         return false;
314     }
315
316     /**
317      * Returns true if the first count elements of arra and arrb are identical
318      * sets of integers (not necessarily in the same order).
319      *
320      */

321     public static boolean haveEqualSets(int[] arra, int[] arrb, int count) {
322
323         if (count > arra.length || count > arrb.length) {
324             return false;
325         }
326
327         if (count == 1) {
328             return arra[0] == arrb[0];
329         }
330
331         int[] tempa = (int[]) resizeArray(arra, count);
332         int[] tempb = (int[]) resizeArray(arrb, count);
333
334         sortArray(tempa);
335         sortArray(tempb);
336
337         for (int j = 0; j < count; j++) {
338             if (tempa[j] != tempb[j]) {
339                 return false;
340             }
341         }
342
343         return true;
344     }
345
346     /**
347      * Returns true if the first count elements of arra and arrb are identical
348      * subarrays of integers
349      *
350      */

351     public static boolean haveEqualArrays(int[] arra, int[] arrb, int count) {
352
353         if (count > arra.length || count > arrb.length) {
354             return false;
355         }
356
357         for (int j = 0; j < count; j++) {
358             if (arra[j] != arrb[j]) {
359                 return false;
360             }
361         }
362
363         return true;
364     }
365
366     /**
367      * Returns true if the first count elements of arra and arrb are identical
368      * subarrays of Objects
369      *
370      */

371     public static boolean haveEqualArrays(Object JavaDoc[] arra, Object JavaDoc[] arrb,
372                                           int count) {
373
374         if (count > arra.length || count > arrb.length) {
375             return false;
376         }
377
378         for (int j = 0; j < count; j++) {
379             if (arra[j] != arrb[j]) {
380                 if (arra[j] == null ||!arra[j].equals(arrb[j])) {
381                     return false;
382                 }
383             }
384         }
385
386         return true;
387     }
388
389     /**
390      * Returns true if arra and the first bcount elements of arrb share any
391      * element. <p>
392      *
393      * Used for checks for any overlap between two arrays of column indexes.
394      */

395     public static boolean haveCommonElement(int[] arra, int[] arrb,
396             int bcount) {
397
398         for (int i = 0; i < arra.length; i++) {
399             int c = arra[i];
400
401             for (int j = 0; j < bcount; j++) {
402                 if (c == arrb[j]) {
403                     return true;
404                 }
405             }
406         }
407
408         return false;
409     }
410
411     /**
412      * Returns an int[] containing elements shared between the two arrays
413      * arra and arrb. The arrays contain sets (no value is repeated).
414      *
415      * Used to find the overlap between two arrays of column indexes.
416      * Ordering of the result arrays will be the same as in array
417      * a. The method assumes that each index is only listed
418      * once in the two input arrays.
419      * <p>
420      * e.g.
421      * </p>
422      * <code>
423      * <table width="90%" bgcolor="lightblue">
424      * <tr><td colspane="3">The arrays</td></tr>
425      * <tr><td>int []arra</td><td>=</td><td>{2,11,5,8}</td></tr>
426      * <tr><td>int []arrb</td><td>=</td><td>{20,8,10,11,28,12}</td></tr>
427      * <tr><td colspane="3">will result in:</td></tr>
428      * <tr><td>int []arrc</td><td>=</td><td>{11,8}</td></tr>
429      * </table>
430      *
431      * @param arra int[]; first column indexes
432      *
433      * @param arrb int[]; second column indexes
434      *
435      * @return int[] common indexes or <code>null</code> if there is no overlap.
436      *
437      * @short Return the overlap between two arrays of column indexes.
438      */

439     public static int[] commonElements(int[] arra, int[] arrb) {
440
441         int[] c = null;
442         int n = countCommonElements(arra, arrb);
443
444         if (n > 0) {
445             c = new int[n];
446
447             int k = 0;
448
449             for (int i = 0; i < arra.length; i++) {
450                 for (int j = 0; j < arrb.length; j++) {
451                     if (arra[i] == arrb[j]) {
452                         c[k++] = arra[i];
453                     }
454                 }
455             }
456         }
457
458         return c;
459     }
460
461     /**
462      * Returns the number of elements shared between the two arrays containing
463      * sets.<p>
464      *
465      * Return the number of elements shared by two column index arrays.
466      * This method assumes that each of these arrays contains a set (each
467      * element index is listed only once in each index array). Otherwise the
468      * returned number will NOT represent the number of unique column indexes
469      * shared by both index array.
470      *
471      * @param arra int[]; first array of column indexes.
472      *
473      * @param arrb int[]; second array of column indexes
474      *
475      * @return int; number of elements shared by <code>a</code> and <code>b</code>
476      */

477     public static int countCommonElements(int[] arra, int[] arrb) {
478
479         int k = 0;
480
481         for (int i = 0; i < arra.length; i++) {
482             for (int j = 0; j < arrb.length; j++) {
483                 if (arra[i] == arrb[j]) {
484                     k++;
485                 }
486             }
487         }
488
489         return k;
490     }
491
492     /**
493      * Returns the count of elements in arra from position start that are
494      * sequentially equal to the elements of arrb.
495      */

496     public static int countSameElements(byte[] arra, int start, byte[] arrb) {
497
498         int k = 0;
499         int limit = arra.length - start;
500
501         if (limit > arrb.length) {
502             limit = arrb.length;
503         }
504
505         for (int i = 0; i < limit; i++) {
506             if (arra[i + start] == arrb[i]) {
507                 k++;
508             } else {
509                 break;
510             }
511         }
512
513         return k;
514     }
515
516     /**
517      * Returns the index of the first occurence of arrb in arra. Or -1 if not found.
518      */

519     public static int find(byte[] arra, int start, int limit, byte[] arrb) {
520
521         int k = 0;
522
523         limit = limit - arrb.length + 1;
524
525         int value = arrb[0];
526
527         for (; k < limit; k++) {
528             if (arra[k] == value) {
529                 if (arrb.length == 1) {
530                     return k;
531                 }
532
533                 if (containsAt(arra, k, arrb)) {
534                     return k;
535                 }
536             }
537         }
538
539         return -1;
540     }
541
542     /**
543      * Returns an index into arra (or -1) where the character is not in the
544      * charset byte array.
545      */

546     public static int findNotIn(byte[] arra, int start, int limit,
547                                 byte[] charset) {
548
549         int k = 0;
550
551         for (; k < limit; k++) {
552             for (int i = 0; i < charset.length; i++) {
553                 if (arra[k] == charset[i]) {
554                     continue;
555                 }
556             }
557
558             return k;
559         }
560
561         return -1;
562     }
563
564     /**
565      * Returns an index into arra (or -1) where the character is in the
566      * charset byte array.
567      */

568     public static int findIn(byte[] arra, int start, int limit,
569                              byte[] charset) {
570
571         int k = 0;
572
573         for (; k < limit; k++) {
574             for (int i = 0; i < charset.length; i++) {
575                 if (arra[k] == charset[i]) {
576                     return k;
577                 }
578             }
579         }
580
581         return -1;
582     }
583
584     /**
585      * Returns the index of b or c in arra. Or -1 if not found.
586      */

587     public static int find(byte[] arra, int start, int limit, int b, int c) {
588
589         int k = 0;
590
591         for (; k < limit; k++) {
592             if (arra[k] == b || arra[k] == c) {
593                 return k;
594             }
595         }
596
597         return -1;
598     }
599
600     /**
601      * Set elements of arrb true if their indexes appear in arrb.
602      */

603     public static void intIndexesToBooleanArray(int[] arra, boolean[] arrb) {
604
605         int k = 0;
606
607         for (int i = 0; i < arra.length; i++) {
608             if (arra[i] < arrb.length) {
609                 arrb[arra[i]] = true;
610             }
611         }
612     }
613
614     /**
615      * Return true if for each true element in arrb, the corresponding
616      * element in arra is true
617      */

618     public static boolean containsAllTrueElements(boolean[] arra,
619             boolean[] arrb) {
620
621         for (int i = 0; i < arra.length; i++) {
622             if (arrb[i] &&!arra[i]) {
623                 return false;
624             }
625         }
626
627         return true;
628     }
629
630     /**
631      * Returns true if arra from position start contains all elements of arrb
632      * in sequential order.
633      */

634     public static boolean containsAt(byte[] arra, int start, byte[] arrb) {
635         return countSameElements(arra, start, arrb) == arrb.length;
636     }
637
638     /**
639      * Returns the count of elements in arra from position start that are
640      * among the elements of arrb. Stops at any element not in arrb.
641      */

642     public static int countStartElementsAt(byte[] arra, int start,
643                                            byte[] arrb) {
644
645         int k = 0;
646
647         mainloop:
648         for (int i = start; i < arra.length; i++) {
649             for (int j = 0; j < arrb.length; j++) {
650                 if (arra[i] == arrb[j]) {
651                     k++;
652
653                     continue mainloop;
654                 }
655             }
656
657             break;
658         }
659
660         return k;
661     }
662
663     /**
664      * Returns the count of elements in arra from position start that are not
665      * among the elements of arrb.
666      *
667      */

668     public static int countNonStartElementsAt(byte[] arra, int start,
669             byte[] arrb) {
670
671         int k = 0;
672
673         mainloop:
674         for (int i = start; i < arra.length; i++) {
675             for (int j = 0; j < arrb.length; j++) {
676                 if (arra[i] == arrb[j]) {
677                     break mainloop;
678                 }
679             }
680
681             k++;
682         }
683
684         return k;
685     }
686
687     /**
688      * Convenience wrapper for System.arraycopy().
689      */

690     public static void copyArray(Object JavaDoc source, Object JavaDoc dest, int count) {
691         System.arraycopy(source, 0, dest, 0, count);
692     }
693
694     /**
695      * Returns a range of elements of source from start to end of the array.
696      */

697     public static int[] arraySlice(int[] source, int start, int count) {
698
699         int[] slice = new int[count];
700
701         System.arraycopy(source, start, slice, 0, count);
702
703         return slice;
704     }
705
706     /**
707      * Fills the array with a value.
708      */

709     public static void fillArray(Object JavaDoc[] array, Object JavaDoc value) {
710
711         int to = array.length;
712
713         while (--to >= 0) {
714             array[to] = value;
715         }
716     }
717
718     /**
719      * Fills the int array with a value
720      */

721     public static void fillArray(int[] array, int value) {
722
723         int to = array.length;
724
725         while (--to >= 0) {
726             array[to] = value;
727         }
728     }
729
730     /**
731      * Returns a duplicates of an array.
732      */

733     public static Object JavaDoc duplicateArray(Object JavaDoc source) {
734
735         int size = Array.getLength(source);
736         Object JavaDoc newarray =
737             Array.newInstance(source.getClass().getComponentType(), size);
738
739         System.arraycopy(source, 0, newarray, 0, size);
740
741         return newarray;
742     }
743
744     /**
745      * Returns the given array if newsize is the same as existing.
746      * Returns a new array of given size, containing as many elements of
747      * the original array as it can hold.
748      */

749     public static Object JavaDoc resizeArrayIfDifferent(Object JavaDoc source, int newsize) {
750
751         int oldsize = Array.getLength(source);
752
753         if (oldsize == newsize) {
754             return source;
755         }
756
757         Object JavaDoc newarray =
758             Array.newInstance(source.getClass().getComponentType(), newsize);
759
760         if (oldsize < newsize) {
761             newsize = oldsize;
762         }
763
764         System.arraycopy(source, 0, newarray, 0, newsize);
765
766         return newarray;
767     }
768
769     /**
770      * Returns a new array of given size, containing as many elements of
771      * the original array as it can hold. N.B. Always returns a new array
772      * even if newsize parameter is the same as the old size.
773      */

774     public static Object JavaDoc resizeArray(Object JavaDoc source, int newsize) {
775
776         Object JavaDoc newarray =
777             Array.newInstance(source.getClass().getComponentType(), newsize);
778         int oldsize = Array.getLength(source);
779
780         if (oldsize < newsize) {
781             newsize = oldsize;
782         }
783
784         System.arraycopy(source, 0, newarray, 0, newsize);
785
786         return newarray;
787     }
788
789     /**
790      * Returns an array containing the elements of parameter source, with one
791      * element removed or added. Parameter adjust {-1, +1} indicates the
792      * operation. Parameter colindex indicates the position at which an element
793      * is removed or added. Parameter addition is an Object to add when
794      * adjust is +1.
795      */

796     public static Object JavaDoc toAdjustedArray(Object JavaDoc source, Object JavaDoc addition,
797                                          int colindex, int adjust) {
798
799         int newsize = Array.getLength(source) + adjust;
800         Object JavaDoc newarray =
801             Array.newInstance(source.getClass().getComponentType(), newsize);
802
803         copyAdjustArray(source, newarray, addition, colindex, adjust);
804
805         return newarray;
806     }
807
808     /**
809      * Copies elements of source to dest. If adjust is -1 the element at
810      * colindex is not copied. If adjust is +1 that element is filled with
811      * the Object addition. All the rest of the elements in source are
812      * shifted left or right accordingly when they are copied. If adjust is 0
813      * the addition is copied over the element at colindex.
814      *
815      * No checks are perfomed on array sizes and an exception is thrown
816      * if they are not consistent with the other arguments.
817      */

818     public static void copyAdjustArray(Object JavaDoc source, Object JavaDoc dest,
819                                        Object JavaDoc addition, int colindex,
820                                        int adjust) {
821
822         int length = Array.getLength(source);
823
824         if (colindex < 0) {
825             System.arraycopy(source, 0, dest, 0, length);
826
827             return;
828         }
829
830         System.arraycopy(source, 0, dest, 0, colindex);
831
832         if (adjust == 0) {
833             int endcount = length - colindex - 1;
834
835             Array.set(dest, colindex, addition);
836
837             if (endcount > 0) {
838                 System.arraycopy(source, colindex + 1, dest, colindex + 1,
839                                  endcount);
840             }
841         } else if (adjust < 0) {
842             int endcount = length - colindex - 1;
843
844             if (endcount > 0) {
845                 System.arraycopy(source, colindex + 1, dest, colindex,
846                                  endcount);
847             }
848         } else {
849             int endcount = length - colindex;
850
851             Array.set(dest, colindex, addition);
852
853             if (endcount > 0) {
854                 System.arraycopy(source, colindex, dest, colindex + 1,
855                                  endcount);
856             }
857         }
858     }
859
860     /**
861      * Returns a new array with the elements in collar adjusted to reflect
862      * changes at colindex. <p>
863      *
864      * Each element in collarr represents an index into another array
865      * otherarr. <p>
866      *
867      * colindex is the index at which an element is added or removed.
868      * Each element in the result array represents the new,
869      * adjusted index. <p>
870      *
871      * For each element of collarr that represents an index equal to
872      * colindex and adjust is -1, the result will not contain that element
873      * and will be shorter than collar by one element.
874      *
875      * @param colarr the source array
876      * @param colindex index at which to perform adjustement
877      * @param adjust +1, 0 or -1
878      * @return new, adjusted array
879      */

880     public static int[] toAdjustedColumnArray(int[] colarr, int colindex,
881             int adjust) {
882
883         if (colarr == null) {
884             return null;
885         }
886
887         int[] intarr = new int[colarr.length];
888         int j = 0;
889
890         for (int i = 0; i < colarr.length; i++) {
891             if (colarr[i] > colindex) {
892                 intarr[j] = colarr[i] + adjust;
893
894                 j++;
895             } else if (colarr[i] == colindex) {
896                 if (adjust < 0) {
897
898                     // skip an element from colarr
899
} else {
900                     intarr[j] = colarr[i] + adjust;
901
902                     j++;
903                 }
904             } else {
905                 intarr[j] = colarr[i];
906
907                 j++;
908             }
909         }
910
911         if (colarr.length != j) {
912             int[] newarr = new int[j];
913
914             copyArray(intarr, newarr, j);
915
916             return newarr;
917         }
918
919         return intarr;
920     }
921
922     /**
923      * Copies some elements of row into colobject by using colindex as
924      * the list of indexes into row. <p>
925      *
926      * colindex and colobject are of equal length and are normally
927      * shorter than row. <p>
928      *
929      * @param row the source array
930      * @param colindex the list of indexes into row
931      * @param colobject the destination array
932      */

933     public static void copyColumnValues(Object JavaDoc[] row, int[] colindex,
934                                         Object JavaDoc[] colobject) {
935
936         for (int i = 0; i < colindex.length; i++) {
937             colobject[i] = row[colindex[i]];
938         }
939     }
940
941     public static void copyColumnValues(int[] row, int[] colindex,
942                                         int[] colobject) {
943
944         for (int i = 0; i < colindex.length; i++) {
945             colobject[i] = row[colindex[i]];
946         }
947     }
948
949     public static void fillSequence(int[] colindex) {
950
951         for (int i = 0; i < colindex.length; i++) {
952             colindex[i] = i;
953         }
954     }
955 /*
956     public static void main(String[] args) {
957
958         int[] a = new int[] {
959             23, 11, 37, 7, 1, 5
960         };
961         int[] b = new int[] {
962             1, 3, 7, 11, 13, 17, 19, 3, 1
963         };
964         int[] c = toAdjustedColumnArray(a, 7, -1);
965         int[] d = toAdjustedColumnArray(b, 11, 1);
966         int[] e = new int[a.length];
967
968         copyArray(a, e, a.length);
969         sortArray(e);
970
971         int[] f = new int[b.length];
972
973         copyArray(b, f, b.length);
974         sortArray(f);
975
976         boolean x = haveEqualSets(a, e, a.length);
977         boolean y = haveEqualSets(b, f, b.length);
978
979         System.out.print("test passed: ");
980         System.out.print(x == true && y == true && c.length == a.length - 1
981                          && d.length == b.length);
982     }
983 */

984 }
985
Popular Tags