Source Code Cross Referenced for ArrayUtil.java in  » Database-DBMS » hsql » org » hsqldb » lib » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database DBMS » hsql » org.hsqldb.lib 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* Copyright (c) 2001-2005, The HSQL Development Group
002:         * All rights reserved.
003:         *
004:         * Redistribution and use in source and binary forms, with or without
005:         * modification, are permitted provided that the following conditions are met:
006:         *
007:         * Redistributions of source code must retain the above copyright notice, this
008:         * list of conditions and the following disclaimer.
009:         *
010:         * Redistributions in binary form must reproduce the above copyright notice,
011:         * this list of conditions and the following disclaimer in the documentation
012:         * and/or other materials provided with the distribution.
013:         *
014:         * Neither the name of the HSQL Development Group nor the names of its
015:         * contributors may be used to endorse or promote products derived from this
016:         * software without specific prior written permission.
017:         *
018:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019:         * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020:         * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021:         * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022:         * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023:         * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024:         * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025:         * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026:         * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027:         * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028:         * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029:         */
030:
031:        package org.hsqldb.lib;
032:
033:        import java.lang.reflect.Array;
034:
035:        /**
036:         * Collection of static methods for operations on arrays
037:         *
038:         * @author fredt@users
039:         * @version 1.7.2
040:         * @since 1.7.2
041:         */
042:        public class ArrayUtil {
043:
044:            public static final int CLASS_CODE_BYTE = 'B';
045:            public static final int CLASS_CODE_CHAR = 'C';
046:            public static final int CLASS_CODE_DOUBLE = 'D';
047:            public static final int CLASS_CODE_FLOAT = 'F';
048:            public static final int CLASS_CODE_INT = 'I';
049:            public static final int CLASS_CODE_LONG = 'J';
050:            public static final int CLASS_CODE_OBJECT = 'L';
051:            public static final int CLASS_CODE_SHORT = 'S';
052:            public static final int CLASS_CODE_BOOLEAN = 'Z';
053:            private static IntValueHashMap classCodeMap = new IntValueHashMap();
054:
055:            static {
056:                classCodeMap.put(byte.class, ArrayUtil.CLASS_CODE_BYTE);
057:                classCodeMap.put(char.class, ArrayUtil.CLASS_CODE_SHORT);
058:                classCodeMap.put(short.class, ArrayUtil.CLASS_CODE_SHORT);
059:                classCodeMap.put(int.class, ArrayUtil.CLASS_CODE_INT);
060:                classCodeMap.put(long.class, ArrayUtil.CLASS_CODE_LONG);
061:                classCodeMap.put(float.class, ArrayUtil.CLASS_CODE_FLOAT);
062:                classCodeMap.put(double.class, ArrayUtil.CLASS_CODE_DOUBLE);
063:                classCodeMap.put(boolean.class, ArrayUtil.CLASS_CODE_BOOLEAN);
064:                classCodeMap.put(Object.class, ArrayUtil.CLASS_CODE_OBJECT);
065:            }
066:
067:            /**
068:             * Returns a distinct int code for each primitive type and for all Object types.
069:             */
070:            static int getClassCode(Class cla) {
071:
072:                if (!cla.isPrimitive()) {
073:                    return ArrayUtil.CLASS_CODE_OBJECT;
074:                }
075:
076:                return classCodeMap.get(cla, -1);
077:            }
078:
079:            /**
080:             * Clears an area of the given array of the given type.
081:             */
082:            public static void clearArray(int type, Object data, int from,
083:                    int to) {
084:
085:                switch (type) {
086:
087:                case ArrayUtil.CLASS_CODE_BYTE: {
088:                    byte[] array = (byte[]) data;
089:
090:                    while (--to >= from) {
091:                        array[to] = 0;
092:                    }
093:
094:                    return;
095:                }
096:                case ArrayUtil.CLASS_CODE_CHAR: {
097:                    byte[] array = (byte[]) data;
098:
099:                    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[] array = (Object[]) 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 array,
182:                    int usedElements, 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[] array, Object 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
308:                                && count == arra.length;
309:                    }
310:
311:                    return true;
312:                }
313:
314:                return false;
315:            }
316:
317:            /**
318:             * Returns true if the first count elements of arra and arrb are identical
319:             * sets of integers (not necessarily in the same order).
320:             *
321:             */
322:            public static boolean haveEqualSets(int[] arra, int[] arrb,
323:                    int count) {
324:
325:                if (count > arra.length || count > arrb.length) {
326:                    return false;
327:                }
328:
329:                if (count == 1) {
330:                    return arra[0] == arrb[0];
331:                }
332:
333:                int[] tempa = (int[]) resizeArray(arra, count);
334:                int[] tempb = (int[]) resizeArray(arrb, count);
335:
336:                sortArray(tempa);
337:                sortArray(tempb);
338:
339:                for (int j = 0; j < count; j++) {
340:                    if (tempa[j] != tempb[j]) {
341:                        return false;
342:                    }
343:                }
344:
345:                return true;
346:            }
347:
348:            /**
349:             * Returns true if the first count elements of arra and arrb are identical
350:             * subarrays of integers
351:             *
352:             */
353:            public static boolean haveEqualArrays(int[] arra, int[] arrb,
354:                    int count) {
355:
356:                if (count > arra.length || count > arrb.length) {
357:                    return false;
358:                }
359:
360:                for (int j = 0; j < count; j++) {
361:                    if (arra[j] != arrb[j]) {
362:                        return false;
363:                    }
364:                }
365:
366:                return true;
367:            }
368:
369:            /**
370:             * Returns true if the first count elements of arra and arrb are identical
371:             * subarrays of Objects
372:             *
373:             */
374:            public static boolean haveEqualArrays(Object[] arra, Object[] arrb,
375:                    int count) {
376:
377:                if (count > arra.length || count > arrb.length) {
378:                    return false;
379:                }
380:
381:                for (int j = 0; j < count; j++) {
382:                    if (arra[j] != arrb[j]) {
383:                        if (arra[j] == null || !arra[j].equals(arrb[j])) {
384:                            return false;
385:                        }
386:                    }
387:                }
388:
389:                return true;
390:            }
391:
392:            /**
393:             * Returns true if arra and the first bcount elements of arrb share any
394:             * element. <p>
395:             *
396:             * Used for checks for any overlap between two arrays of column indexes.
397:             */
398:            public static boolean haveCommonElement(int[] arra, int[] arrb,
399:                    int bcount) {
400:
401:                for (int i = 0; i < arra.length; i++) {
402:                    int c = arra[i];
403:
404:                    for (int j = 0; j < bcount; j++) {
405:                        if (c == arrb[j]) {
406:                            return true;
407:                        }
408:                    }
409:                }
410:
411:                return false;
412:            }
413:
414:            /**
415:             * Returns an int[] containing elements shared between the two arrays
416:             * arra and arrb. The arrays contain sets (no value is repeated).
417:             *
418:             * Used to find the overlap between two arrays of column indexes.
419:             * Ordering of the result arrays will be the same as in array
420:             * a. The method assumes that each index is only listed
421:             * once in the two input arrays.
422:             * <p>
423:             * e.g.
424:             * </p>
425:             * <code>
426:             * <table width="90%" bgcolor="lightblue">
427:             * <tr><td colspane="3">The arrays</td></tr>
428:             * <tr><td>int []arra</td><td>=</td><td>{2,11,5,8}</td></tr>
429:             * <tr><td>int []arrb</td><td>=</td><td>{20,8,10,11,28,12}</td></tr>
430:             * <tr><td colspane="3">will result in:</td></tr>
431:             * <tr><td>int []arrc</td><td>=</td><td>{11,8}</td></tr>
432:             * </table>
433:             *
434:             * @param arra int[]; first column indexes
435:             *
436:             * @param arrb int[]; second column indexes
437:             *
438:             * @return int[] common indexes or <code>null</code> if there is no overlap.
439:             *
440:             * @short Return the overlap between two arrays of column indexes.
441:             */
442:            public static int[] commonElements(int[] arra, int[] arrb) {
443:
444:                int[] c = null;
445:                int n = countCommonElements(arra, arrb);
446:
447:                if (n > 0) {
448:                    c = new int[n];
449:
450:                    int k = 0;
451:
452:                    for (int i = 0; i < arra.length; i++) {
453:                        for (int j = 0; j < arrb.length; j++) {
454:                            if (arra[i] == arrb[j]) {
455:                                c[k++] = arra[i];
456:                            }
457:                        }
458:                    }
459:                }
460:
461:                return c;
462:            }
463:
464:            /**
465:             * Returns the number of elements shared between the two arrays containing
466:             * sets.<p>
467:             *
468:             * Return the number of elements shared by two column index arrays.
469:             * This method assumes that each of these arrays contains a set (each
470:             * element index is listed only once in each index array). Otherwise the
471:             * returned number will NOT represent the number of unique column indexes
472:             * shared by both index array.
473:             *
474:             * @param arra int[]; first array of column indexes.
475:             *
476:             * @param arrb int[]; second array of column indexes
477:             *
478:             * @return int; number of elements shared by <code>a</code> and <code>b</code>
479:             */
480:            public static int countCommonElements(int[] arra, int[] arrb) {
481:
482:                int k = 0;
483:
484:                for (int i = 0; i < arra.length; i++) {
485:                    for (int j = 0; j < arrb.length; j++) {
486:                        if (arra[i] == arrb[j]) {
487:                            k++;
488:                        }
489:                    }
490:                }
491:
492:                return k;
493:            }
494:
495:            /**
496:             * Returns the count of elements in arra from position start that are
497:             * sequentially equal to the elements of arrb.
498:             */
499:            public static int countSameElements(byte[] arra, int start,
500:                    byte[] arrb) {
501:
502:                int k = 0;
503:                int limit = arra.length - start;
504:
505:                if (limit > arrb.length) {
506:                    limit = arrb.length;
507:                }
508:
509:                for (int i = 0; i < limit; i++) {
510:                    if (arra[i + start] == arrb[i]) {
511:                        k++;
512:                    } else {
513:                        break;
514:                    }
515:                }
516:
517:                return k;
518:            }
519:
520:            /**
521:             * Returns the index of the first occurence of arrb in arra. Or -1 if not found.
522:             */
523:            public static int find(byte[] arra, int start, int limit,
524:                    byte[] arrb) {
525:
526:                int k = 0;
527:
528:                limit = limit - arrb.length + 1;
529:
530:                int value = arrb[0];
531:
532:                for (; k < limit; k++) {
533:                    if (arra[k] == value) {
534:                        if (arrb.length == 1) {
535:                            return k;
536:                        }
537:
538:                        if (containsAt(arra, k, arrb)) {
539:                            return k;
540:                        }
541:                    }
542:                }
543:
544:                return -1;
545:            }
546:
547:            /**
548:             * Returns an index into arra (or -1) where the character is not in the
549:             * charset byte array.
550:             */
551:            public static int findNotIn(byte[] arra, int start, int limit,
552:                    byte[] charset) {
553:
554:                int k = 0;
555:
556:                for (; k < limit; k++) {
557:                    for (int i = 0; i < charset.length; i++) {
558:                        if (arra[k] == charset[i]) {
559:                            continue;
560:                        }
561:                    }
562:
563:                    return k;
564:                }
565:
566:                return -1;
567:            }
568:
569:            /**
570:             * Returns an index into arra (or -1) where the character is in the
571:             * charset byte array.
572:             */
573:            public static int findIn(byte[] arra, int start, int limit,
574:                    byte[] charset) {
575:
576:                int k = 0;
577:
578:                for (; k < limit; k++) {
579:                    for (int i = 0; i < charset.length; i++) {
580:                        if (arra[k] == charset[i]) {
581:                            return k;
582:                        }
583:                    }
584:                }
585:
586:                return -1;
587:            }
588:
589:            /**
590:             * Returns the index of b or c in arra. Or -1 if not found.
591:             */
592:            public static int find(byte[] arra, int start, int limit, int b,
593:                    int c) {
594:
595:                int k = 0;
596:
597:                for (; k < limit; k++) {
598:                    if (arra[k] == b || arra[k] == c) {
599:                        return k;
600:                    }
601:                }
602:
603:                return -1;
604:            }
605:
606:            /**
607:             * Set elements of arrb true if their indexes appear in arrb.
608:             */
609:            public static void intIndexesToBooleanArray(int[] arra,
610:                    boolean[] arrb) {
611:
612:                int k = 0;
613:
614:                for (int i = 0; i < arra.length; i++) {
615:                    if (arra[i] < arrb.length) {
616:                        arrb[arra[i]] = true;
617:                    }
618:                }
619:            }
620:
621:            /**
622:             * Return true if for each true element in arrb, the corresponding
623:             * element in arra is true
624:             */
625:            public static boolean containsAllTrueElements(boolean[] arra,
626:                    boolean[] arrb) {
627:
628:                for (int i = 0; i < arra.length; i++) {
629:                    if (arrb[i] && !arra[i]) {
630:                        return false;
631:                    }
632:                }
633:
634:                return true;
635:            }
636:
637:            /**
638:             * Returns true if arra from position start contains all elements of arrb
639:             * in sequential order.
640:             */
641:            public static boolean containsAt(byte[] arra, int start, byte[] arrb) {
642:                return countSameElements(arra, start, arrb) == arrb.length;
643:            }
644:
645:            /**
646:             * Returns the count of elements in arra from position start that are
647:             * among the elements of arrb. Stops at any element not in arrb.
648:             */
649:            public static int countStartElementsAt(byte[] arra, int start,
650:                    byte[] arrb) {
651:
652:                int k = 0;
653:
654:                mainloop: for (int i = start; i < arra.length; i++) {
655:                    for (int j = 0; j < arrb.length; j++) {
656:                        if (arra[i] == arrb[j]) {
657:                            k++;
658:
659:                            continue mainloop;
660:                        }
661:                    }
662:
663:                    break;
664:                }
665:
666:                return k;
667:            }
668:
669:            /**
670:             * Returns the count of elements in arra from position start that are not
671:             * among the elements of arrb.
672:             *
673:             */
674:            public static int countNonStartElementsAt(byte[] arra, int start,
675:                    byte[] arrb) {
676:
677:                int k = 0;
678:
679:                mainloop: for (int i = start; i < arra.length; i++) {
680:                    for (int j = 0; j < arrb.length; j++) {
681:                        if (arra[i] == arrb[j]) {
682:                            break mainloop;
683:                        }
684:                    }
685:
686:                    k++;
687:                }
688:
689:                return k;
690:            }
691:
692:            /**
693:             * Convenience wrapper for System.arraycopy().
694:             */
695:            public static void copyArray(Object source, Object dest, int count) {
696:                System.arraycopy(source, 0, dest, 0, count);
697:            }
698:
699:            /**
700:             * Returns a range of elements of source from start to end of the array.
701:             */
702:            public static int[] arraySlice(int[] source, int start, int count) {
703:
704:                int[] slice = new int[count];
705:
706:                System.arraycopy(source, start, slice, 0, count);
707:
708:                return slice;
709:            }
710:
711:            /**
712:             * Fills the array with a value.
713:             */
714:            public static void fillArray(Object[] array, Object value) {
715:
716:                int to = array.length;
717:
718:                while (--to >= 0) {
719:                    array[to] = value;
720:                }
721:            }
722:
723:            /**
724:             * Fills the int array with a value
725:             */
726:            public static void fillArray(int[] array, int value) {
727:
728:                int to = array.length;
729:
730:                while (--to >= 0) {
731:                    array[to] = value;
732:                }
733:            }
734:
735:            /**
736:             * Returns a duplicates of an array.
737:             */
738:            public static Object duplicateArray(Object source) {
739:
740:                int size = Array.getLength(source);
741:                Object newarray = Array.newInstance(source.getClass()
742:                        .getComponentType(), size);
743:
744:                System.arraycopy(source, 0, newarray, 0, size);
745:
746:                return newarray;
747:            }
748:
749:            /**
750:             * Returns the given array if newsize is the same as existing.
751:             * Returns a new array of given size, containing as many elements of
752:             * the original array as it can hold.
753:             */
754:            public static Object resizeArrayIfDifferent(Object source,
755:                    int newsize) {
756:
757:                int oldsize = Array.getLength(source);
758:
759:                if (oldsize == newsize) {
760:                    return source;
761:                }
762:
763:                Object newarray = Array.newInstance(source.getClass()
764:                        .getComponentType(), newsize);
765:
766:                if (oldsize < newsize) {
767:                    newsize = oldsize;
768:                }
769:
770:                System.arraycopy(source, 0, newarray, 0, newsize);
771:
772:                return newarray;
773:            }
774:
775:            /**
776:             * Returns a new array of given size, containing as many elements of
777:             * the original array as it can hold. N.B. Always returns a new array
778:             * even if newsize parameter is the same as the old size.
779:             */
780:            public static Object resizeArray(Object source, int newsize) {
781:
782:                Object newarray = Array.newInstance(source.getClass()
783:                        .getComponentType(), newsize);
784:                int oldsize = Array.getLength(source);
785:
786:                if (oldsize < newsize) {
787:                    newsize = oldsize;
788:                }
789:
790:                System.arraycopy(source, 0, newarray, 0, newsize);
791:
792:                return newarray;
793:            }
794:
795:            /**
796:             * Returns an array containing the elements of parameter source, with one
797:             * element removed or added. Parameter adjust {-1, +1} indicates the
798:             * operation. Parameter colindex indicates the position at which an element
799:             * is removed or added. Parameter addition is an Object to add when
800:             * adjust is +1.
801:             */
802:            public static Object toAdjustedArray(Object source,
803:                    Object addition, int colindex, int adjust) {
804:
805:                int newsize = Array.getLength(source) + adjust;
806:                Object newarray = Array.newInstance(source.getClass()
807:                        .getComponentType(), newsize);
808:
809:                copyAdjustArray(source, newarray, addition, colindex, adjust);
810:
811:                return newarray;
812:            }
813:
814:            /**
815:             *  Copies elements of source to dest. If adjust is -1 the element at
816:             *  colindex is not copied. If adjust is +1 that element is filled with
817:             *  the Object addition. All the rest of the elements in source are
818:             *  shifted left or right accordingly when they are copied. If adjust is 0
819:             *  the addition is copied over the element at colindex.
820:             *
821:             *  No checks are perfomed on array sizes and an exception is thrown
822:             *  if they are not consistent with the other arguments.
823:             */
824:            public static void copyAdjustArray(Object source, Object dest,
825:                    Object addition, int colindex, int adjust) {
826:
827:                int length = Array.getLength(source);
828:
829:                if (colindex < 0) {
830:                    System.arraycopy(source, 0, dest, 0, length);
831:
832:                    return;
833:                }
834:
835:                System.arraycopy(source, 0, dest, 0, colindex);
836:
837:                if (adjust == 0) {
838:                    int endcount = length - colindex - 1;
839:
840:                    Array.set(dest, colindex, addition);
841:
842:                    if (endcount > 0) {
843:                        System.arraycopy(source, colindex + 1, dest,
844:                                colindex + 1, endcount);
845:                    }
846:                } else if (adjust < 0) {
847:                    int endcount = length - colindex - 1;
848:
849:                    if (endcount > 0) {
850:                        System.arraycopy(source, colindex + 1, dest, colindex,
851:                                endcount);
852:                    }
853:                } else {
854:                    int endcount = length - colindex;
855:
856:                    Array.set(dest, colindex, addition);
857:
858:                    if (endcount > 0) {
859:                        System.arraycopy(source, colindex, dest, colindex + 1,
860:                                endcount);
861:                    }
862:                }
863:            }
864:
865:            /**
866:             * Returns a new array with the elements in collar adjusted to reflect
867:             * changes at colindex. <p>
868:             *
869:             * Each element in collarr represents an index into another array
870:             * otherarr. <p>
871:             *
872:             * colindex is the index at which an element is added or removed.
873:             * Each element in the result array represents the new,
874:             * adjusted index. <p>
875:             *
876:             * For each element of collarr that represents an index equal to
877:             * colindex and adjust is -1, the result will not contain that element
878:             * and will be shorter than collar by one element.
879:             *
880:             * @param  colarr the source array
881:             * @param  colindex index at which to perform adjustement
882:             * @param  adjust +1, 0 or -1
883:             * @return new, adjusted array
884:             */
885:            public static int[] toAdjustedColumnArray(int[] colarr,
886:                    int colindex, int adjust) {
887:
888:                if (colarr == null) {
889:                    return null;
890:                }
891:
892:                int[] intarr = new int[colarr.length];
893:                int j = 0;
894:
895:                for (int i = 0; i < colarr.length; i++) {
896:                    if (colarr[i] > colindex) {
897:                        intarr[j] = colarr[i] + adjust;
898:
899:                        j++;
900:                    } else if (colarr[i] == colindex) {
901:                        if (adjust < 0) {
902:
903:                            // skip an element from colarr
904:                        } else {
905:                            intarr[j] = colarr[i] + adjust;
906:
907:                            j++;
908:                        }
909:                    } else {
910:                        intarr[j] = colarr[i];
911:
912:                        j++;
913:                    }
914:                }
915:
916:                if (colarr.length != j) {
917:                    int[] newarr = new int[j];
918:
919:                    copyArray(intarr, newarr, j);
920:
921:                    return newarr;
922:                }
923:
924:                return intarr;
925:            }
926:
927:            /**
928:             *  Copies some elements of row into colobject by using colindex as
929:             *  the list of indexes into row. <p>
930:             *
931:             *  colindex and colobject are of equal length and are normally
932:             *  shorter than row. <p>
933:             *
934:             *  @param row the source array
935:             *  @param colindex the list of indexes into row
936:             *  @param colobject the destination array
937:             */
938:            public static void copyColumnValues(Object[] row, int[] colindex,
939:                    Object[] colobject) {
940:
941:                for (int i = 0; i < colindex.length; i++) {
942:                    colobject[i] = row[colindex[i]];
943:                }
944:            }
945:
946:            public static void copyColumnValues(int[] row, int[] colindex,
947:                    int[] colobject) {
948:
949:                for (int i = 0; i < colindex.length; i++) {
950:                    colobject[i] = row[colindex[i]];
951:                }
952:            }
953:
954:            public static void fillSequence(int[] colindex) {
955:
956:                for (int i = 0; i < colindex.length; i++) {
957:                    colindex[i] = i;
958:                }
959:            }
960:            /*
961:             public static void main(String[] args) {
962:
963:             int[] a = new int[] {
964:             23, 11, 37, 7, 1, 5
965:             };
966:             int[] b = new int[] {
967:             1, 3, 7, 11, 13, 17, 19, 3, 1
968:             };
969:             int[] c = toAdjustedColumnArray(a, 7, -1);
970:             int[] d = toAdjustedColumnArray(b, 11, 1);
971:             int[] e = new int[a.length];
972:
973:             copyArray(a, e, a.length);
974:             sortArray(e);
975:
976:             int[] f = new int[b.length];
977:
978:             copyArray(b, f, b.length);
979:             sortArray(f);
980:
981:             boolean x = haveEqualSets(a, e, a.length);
982:             boolean y = haveEqualSets(b, f, b.length);
983:
984:             System.out.print("test passed: ");
985:             System.out.print(x == true && y == true && c.length == a.length - 1
986:             && d.length == b.length);
987:             }
988:             */
989:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.