1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
|
// RJavaTools.java: rJava - low level R to java interface
//
// :tabSize=2:indentSize=2:noTabs=false:folding=explicit:collapseFolds=1:
//
// Copyright (C) 2009 - 2010 Simon Urbanek and Romain Francois
//
// This file is part of rJava.
//
// rJava is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// rJava is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with rJava. If not, see <http://www.gnu.org/licenses/>.
import java.lang.reflect.Array ;
import java.util.Map;
import java.util.HashMap;
import java.util.Vector ;
import java.util.Arrays ;
import java.util.Iterator;
import java.lang.reflect.Method ;
import java.lang.reflect.InvocationTargetException ;
public class RJavaArrayTools {
// TODO: maybe factor this out of this class
private static Map primitiveClasses = initPrimitiveClasses() ;
private static Map initPrimitiveClasses(){
Map primitives = new HashMap();
primitives.put( "I", Integer.TYPE );
primitives.put( "Z", Boolean.TYPE );
primitives.put( "B", Byte.TYPE );
primitives.put( "J", Long.TYPE );
primitives.put( "S", Short.TYPE );
primitives.put( "D", Double.TYPE );
primitives.put( "C", Character.TYPE );
primitives.put( "F", Float.TYPE );
return primitives;
}
// {{{ getObjectTypeName
/**
* Get the object type name of an multi dimensional array.
*
* @param o object
* @throws NotAnArrayException if the object is not an array
*/
public static String getObjectTypeName(Object o) throws NotAnArrayException {
Class o_clazz = o.getClass();
if( !o_clazz.isArray() ) throw new NotAnArrayException( o_clazz );
String cl = o_clazz.getName();
return cl.replaceFirst("\\[+L?", "").replace(";", "") ;
}
public static int getObjectTypeName(int x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : int ") ; }
public static int getObjectTypeName(boolean x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : boolean ") ; }
public static int getObjectTypeName(byte x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : byte ") ; }
public static int getObjectTypeName(long x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : long ") ; }
public static int getObjectTypeName(short x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : short ") ; }
public static int getObjectTypeName(double x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : double ") ; }
public static int getObjectTypeName(char x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : char ") ; }
public static int getObjectTypeName(float x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : float ") ; }
// }}}
// {{{ makeArraySignature
// TODO: test
public static String makeArraySignature( String typeName, int depth ){
StringBuffer buffer = new StringBuffer() ;
for( int i=0; i<depth; i++){
buffer.append( '[' ) ;
}
buffer.append( typeName );
if( ! isPrimitiveTypeName( typeName ) ){
buffer.append( ';') ;
}
return buffer.toString();
}
// }}}
// {{{ getClassForSignature
public static Class getClassForSignature(String signature, ClassLoader loader) throws ClassNotFoundException {
if( primitiveClasses.containsKey(signature) ){
return (Class)primitiveClasses.get( signature ) ;
}
return Class.forName(signature, true, loader) ;
}
// }}}
// {{{ isSingleDimensionArray
public static boolean isSingleDimensionArray( Object o) throws NotAnArrayException{
if( !isArray(o) ) throw new NotAnArrayException( o.getClass() ) ;
String cn = o.getClass().getName() ;
if( cn.lastIndexOf('[') != 0 ) return false;
return true ;
}
// }}}
// {{{ isPrimitiveTypeName
public static boolean isPrimitiveTypeName(String name){
if( name.length() > 1 ) return false;
if( name.equals("I") ) return true ;
if( name.equals("Z") ) return true ;
if( name.equals("B") ) return true ;
if( name.equals("J") ) return true ;
if( name.equals("S") ) return true ;
if( name.equals("D") ) return true ;
if( name.equals("C") ) return true ;
if( name.equals("F") ) return true ;
return false;
}
// }}}
// {{{ isRectangularArray
/**
* Indicates if o is a rectangular array
*
* @param o an array
* @deprecated use new ArrayWrapper(o).isRectangular() instead
*/
public static boolean isRectangularArray(Object o) {
if( !isArray(o) ) return false;
boolean res = false;
try{
if( getDimensionLength( o ) == 1 ) return true ;
res = ( new ArrayWrapper(o) ).isRectangular() ;
} catch( NotAnArrayException e){
res = false;
}
return res ;
}
// thoose below make java < 1.5 happy and me unhappy ;-)
public static boolean isRectangularArray(int x) { return false ; }
public static boolean isRectangularArray(boolean x) { return false ; }
public static boolean isRectangularArray(byte x) { return false ; }
public static boolean isRectangularArray(long x) { return false ; }
public static boolean isRectangularArray(short x) { return false ; }
public static boolean isRectangularArray(double x) { return false ; }
public static boolean isRectangularArray(char x) { return false ; }
public static boolean isRectangularArray(float x) { return false ; }
// }}}
// {{{ getDimensionLength
/**
* Returns the number of dimensions of an array
*
* @param o an array
* @throws NotAnArrayException if this is not an array
*/
public static int getDimensionLength( Object o) throws NotAnArrayException, NullPointerException {
if( o == null ) throw new NullPointerException( "array is null" ) ;
Class clazz = o.getClass();
if( !clazz.isArray() ) throw new NotAnArrayException(clazz) ;
int n = 0;
while( clazz.isArray() ){
n++ ;
clazz = clazz.getComponentType() ;
}
return n ;
}
// thoose below make java < 1.5 happy and me unhappy ;-)
public static int getDimensionLength(int x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : int ") ; }
public static int getDimensionLength(boolean x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : boolean ") ; }
public static int getDimensionLength(byte x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : byte ") ; }
public static int getDimensionLength(long x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : long ") ; }
public static int getDimensionLength(short x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : short ") ; }
public static int getDimensionLength(double x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : double ") ; }
public static int getDimensionLength(char x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : char ") ; }
public static int getDimensionLength(float x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : float ") ; }
// }}}
// {{{ getDimensions
/**
* Returns the dimensions of an array
*
* @param o an array
* @throws NotAnArrayException if this is not an array
* @return the dimensions of the array or null if the object is null
*/
public static int[] getDimensions( Object o) throws NotAnArrayException, NullPointerException {
if( o == null ) throw new NullPointerException( "array is null" ) ;
Class clazz = o.getClass();
if( !clazz.isArray() ) throw new NotAnArrayException(clazz) ;
Object a = o ;
int n = getDimensionLength( o ) ;
int[] dims = new int[n] ;
int i=0;
int current ;
while( clazz.isArray() ){
current = Array.getLength( a ) ;
dims[i] = current ;
i++;
if( current == 0 ){
break ; // the while loop
} else {
a = Array.get( a, 0 ) ;
clazz = clazz.getComponentType() ;
}
}
/* in case of premature stop, we fill the rest of the array with 0 */
// this might not be true:
// Object[][] = new Object[0][10] will return c(0,0)
while( i < dims.length){
dims[i] = 0 ;
i++ ;
}
return dims ;
}
// thoose below make java < 1.5 happy and me unhappy ;-)
public static int[] getDimensions(int x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : int ") ; }
public static int[] getDimensions(boolean x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : boolean ") ; }
public static int[] getDimensions(byte x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : byte ") ; }
public static int[] getDimensions(long x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : long ") ; }
public static int[] getDimensions(short x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : short ") ; }
public static int[] getDimensions(double x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : double ") ; }
public static int[] getDimensions(char x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : char ") ; }
public static int[] getDimensions(float x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : float ") ; }
// }}}
// {{{ getTrueLength
/**
* Returns the true length of an array (the product of its dimensions)
*
* @param o an array
* @throws NotAnArrayException if this is not an array
* @return the number of objects in the array (the product of its dimensions).
*/
public static int getTrueLength( Object o) throws NotAnArrayException, NullPointerException {
if( o == null ) throw new NullPointerException( "array is null" ) ;
Class clazz = o.getClass();
if( !clazz.isArray() ) throw new NotAnArrayException(clazz) ;
Object a = o ;
int len = 1 ;
int i = 0;
while( clazz.isArray() ){
len = len * Array.getLength( a ) ;
if( len == 0 ) return 0 ; /* no need to go further */
i++;
a = Array.get( a, 0 ) ;
clazz = clazz.getComponentType() ;
}
return len ;
}
// thoose below make java < 1.5 happy and me unhappy ;-)
public static int getTrueLength(int x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : int ") ; }
public static int getTrueLength(boolean x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : boolean ") ; }
public static int getTrueLength(byte x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : byte ") ; }
public static int getTrueLength(long x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : long ") ; }
public static int getTrueLength(short x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : short ") ; }
public static int getTrueLength(double x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : double ") ; }
public static int getTrueLength(char x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : char ") ; }
public static int getTrueLength(float x) throws NotAnArrayException { throw new NotAnArrayException("primitive type : float ") ; }
// }}}
// {{{ isArray
/**
* Indicates if a java object is an array
*
* @param o object
* @return true if the object is an array
* @deprecated use RJavaArrayTools#isArray
*/
public static boolean isArray(Object o){
if( o == null) return false ;
return o.getClass().isArray() ;
}
// thoose below make java < 1.5 happy and me unhappy ;-)
public static boolean isArray(int x){ return false ; }
public static boolean isArray(boolean x){ return false ; }
public static boolean isArray(byte x){ return false ; }
public static boolean isArray(long x){ return false ; }
public static boolean isArray(short x){ return false ; }
public static boolean isArray(double x){ return false ; }
public static boolean isArray(char x){ return false ; }
public static boolean isArray(float x){ return false ; }
// }}}
// {{{ ArrayDimensionMismatchException
public static class ArrayDimensionMismatchException extends Exception {
public ArrayDimensionMismatchException( int index_dim, int actual_dim ){
super( "dimension of indexer (" + index_dim + ") too large for array (depth ="+ actual_dim+ ")") ;
}
}
// }}}
// {{{ get
/**
* Gets a single object from a multi dimensional array
*
* @param array java array
* @param position
*/
public static Object get( Object array, int[] position ) throws NotAnArrayException, ArrayDimensionMismatchException {
return Array.get( getArray( array, position ), position[ position.length -1] );
}
public static int getInt( Object array, int[] position ) throws NotAnArrayException, ArrayDimensionMismatchException {
return Array.getInt( getArray( array, position ), position[ position.length -1] );
}
public static boolean getBoolean( Object array, int[] position ) throws NotAnArrayException, ArrayDimensionMismatchException {
return Array.getBoolean( getArray( array, position ), position[ position.length -1] );
}
public static byte getByte( Object array, int[] position ) throws NotAnArrayException, ArrayDimensionMismatchException {
return Array.getByte( getArray( array, position ), position[ position.length -1] );
}
public static long getLong( Object array, int[] position ) throws NotAnArrayException, ArrayDimensionMismatchException {
return Array.getLong( getArray( array, position ), position[ position.length -1] );
}
public static short getShort( Object array, int[] position ) throws NotAnArrayException, ArrayDimensionMismatchException {
return Array.getShort( getArray( array, position ), position[ position.length -1] );
}
public static double getDouble( Object array, int[] position ) throws NotAnArrayException, ArrayDimensionMismatchException {
return Array.getDouble( getArray( array, position ), position[ position.length -1] );
}
public static char getChar( Object array, int[] position ) throws NotAnArrayException, ArrayDimensionMismatchException {
return Array.getChar( getArray( array, position ), position[ position.length -1] );
}
public static float getFloat( Object array, int[] position ) throws NotAnArrayException, ArrayDimensionMismatchException {
return Array.getFloat( getArray( array, position ), position[ position.length -1] );
}
public static Object get( Object array, int position ) throws NotAnArrayException, ArrayDimensionMismatchException {
return get( array, new int[]{position} ) ;
}
public static int getInt( Object array, int position ) throws NotAnArrayException, ArrayDimensionMismatchException {
return getInt( array, new int[]{position} ) ;
}
public static boolean getBoolean( Object array, int position ) throws NotAnArrayException, ArrayDimensionMismatchException {
return getBoolean( array, new int[]{position} ) ;
}
public static byte getByte( Object array, int position ) throws NotAnArrayException, ArrayDimensionMismatchException {
return getByte( array, new int[]{position} ) ;
}
public static long getLong( Object array, int position ) throws NotAnArrayException, ArrayDimensionMismatchException {
return getLong( array, new int[]{position} ) ;
}
public static short getShort( Object array, int position ) throws NotAnArrayException, ArrayDimensionMismatchException {
return getShort( array, new int[]{position} ) ;
}
public static double getDouble( Object array, int position ) throws NotAnArrayException, ArrayDimensionMismatchException {
return getDouble( array, new int[]{position} ) ;
}
public static char getChar( Object array, int position ) throws NotAnArrayException, ArrayDimensionMismatchException {
return getChar( array, new int[]{position} ) ;
}
public static float getFloat( Object array, int position ) throws NotAnArrayException, ArrayDimensionMismatchException {
return getFloat( array, new int[]{position} ) ;
}
private static void checkDimensions(Object array, int[] position) throws NotAnArrayException, ArrayDimensionMismatchException {
int poslength = position.length ;
int actuallength = getDimensionLength(array);
if( poslength > actuallength ){
throw new ArrayDimensionMismatchException( poslength, actuallength ) ;
}
}
// }}}
// {{{ set
/**
* Replaces a single value of the array
*
* @param array array
* @param position index
* @param value the new value
*
* @throws NotAnArrayException if array is not an array
* @throws ArrayDimensionMismatchException if the length of position is too big
*/
public static void set( Object array, int[] position, Object value ) throws NotAnArrayException, ArrayDimensionMismatchException{
Array.set( getArray( array, position ), position[ position.length - 1], value ) ;
}
/* primitive versions */
public static void set( Object array, int[] position, int value ) throws NotAnArrayException, ArrayDimensionMismatchException{
Array.setInt( getArray( array, position ), position[ position.length - 1], value ) ;
}
public static void set( Object array, int[] position, boolean value ) throws NotAnArrayException, ArrayDimensionMismatchException{
Array.setBoolean( getArray( array, position ), position[ position.length - 1], value ) ;
}
public static void set( Object array, int[] position, byte value ) throws NotAnArrayException, ArrayDimensionMismatchException{
Array.setByte( getArray( array, position ), position[ position.length - 1], value ) ;
}
public static void set( Object array, int[] position, long value ) throws NotAnArrayException, ArrayDimensionMismatchException{
Array.setLong( getArray( array, position ), position[ position.length - 1], value ) ;
}
public static void set( Object array, int[] position, short value ) throws NotAnArrayException, ArrayDimensionMismatchException{
Array.setShort( getArray( array, position ), position[ position.length - 1], value ) ;
}
public static void set( Object array, int[] position, double value ) throws NotAnArrayException, ArrayDimensionMismatchException{
Array.setDouble( getArray( array, position ), position[ position.length - 1], value ) ;
}
public static void set( Object array, int[] position, char value ) throws NotAnArrayException, ArrayDimensionMismatchException{
Array.setChar( getArray( array, position ), position[ position.length - 1], value ) ;
}
public static void set( Object array, int[] position, float value ) throws NotAnArrayException, ArrayDimensionMismatchException{
Array.setFloat( getArray( array, position ), position[ position.length - 1], value ) ;
}
public static void set( Object array, int position, Object value ) throws NotAnArrayException, ArrayDimensionMismatchException{
set( array, new int[]{ position }, value );
}
public static void set( Object array, int position, int value ) throws NotAnArrayException, ArrayDimensionMismatchException{
set( array, new int[]{ position }, value );
}
public static void set( Object array, int position, boolean value ) throws NotAnArrayException, ArrayDimensionMismatchException{
set( array, new int[]{ position }, value );
}
public static void set( Object array, int position, byte value ) throws NotAnArrayException, ArrayDimensionMismatchException{
set( array, new int[]{ position }, value );
}
public static void set( Object array, int position, long value ) throws NotAnArrayException, ArrayDimensionMismatchException{
set( array, new int[]{ position }, value );
}
public static void set( Object array, int position, short value ) throws NotAnArrayException, ArrayDimensionMismatchException{
set( array, new int[]{ position }, value );
}
public static void set( Object array, int position, double value ) throws NotAnArrayException, ArrayDimensionMismatchException{
set( array, new int[]{ position }, value );
}
public static void set( Object array, int position, char value ) throws NotAnArrayException, ArrayDimensionMismatchException{
set( array, new int[]{ position }, value );
}
public static void set( Object array, int position, float value ) throws NotAnArrayException, ArrayDimensionMismatchException{
set( array, new int[]{ position }, value );
}
private static Object getArray( Object array, int[] position ) throws NotAnArrayException, ArrayDimensionMismatchException{
checkDimensions( array, position ) ;
int poslength = position.length ;
Object o = array ;
int i=0 ;
if( poslength > 1 ){
while( i< (poslength-1) ){
o = Array.get( o, position[i] ) ;
i++ ;
}
}
return o ;
}
// TODO: also have primitive types in value
// }}}
// {{{ unique
// TODO: cannot use LinkedHashSet because it first was introduced in 1.4
// and code in this area needs to work on 1.2 jvm
public static Object[] unique( Object[] array ){
int n = array.length ;
boolean[] unique = new boolean[ array.length ];
for( int i=0; i<array.length; i++){
unique[i] = true ;
}
Vector res = new Vector();
boolean added ;
for( int i=0; i<n; i++){
if( !unique[i] ) continue ;
Object current = array[i];
added = false;
for( int j=i+1; j<n; j++){
Object o_j = array[j] ;
if( unique[j] && current.equals( o_j ) ){
if( !added ){
unique[i] = false;
res.add( current );
added = true ;
}
unique[j] = false;
}
}
}
// build the array using newInstance so that it has the same
// component type as the original array and not just Object
Object[] res_array = (Object[])Array.newInstance( array.getClass().getComponentType(), res.size() ) ;
res.toArray( res_array );
return res_array ;
}
// }}}
// {{{ duplicated
public static boolean[] duplicated( Object[] array ){
int n = array.length ;
boolean[] duplicated = new boolean[ array.length ];
for( int i=0; i<array.length; i++){
duplicated[i] = false ;
}
for( int i=0; i<n; i++){
if( duplicated[i] ) continue ;
Object current = array[i];
for( int j=i+1; j<n; j++){
Object o_j = array[j] ;
if( !duplicated[j] && current.equals( o_j ) ){
duplicated[j] = true;
}
}
}
return duplicated ;
}
// }}}
// {{{ anyDuplicated
public static int anyDuplicated( Object[] array ){
int n = array.length ;
for( int i=0; i<n; i++){
Object current = array[i];
for( int j=i+1; j<n; j++){
Object o_j = array[j] ;
if( current.equals( o_j ) ){
return j ;
}
}
}
return -1 ;
}
// }}}
// {{{ sort
/**
* Returns a copy of the array where elements are sorted
*
* @param array array of Objects.
* @param decreasing if true the sort is in decreasing order
*
* @throws NotComparableException if the component type of the array does not
* implement the Comparable interface
*/
public static Object[] sort( Object[] array, boolean decreasing ) throws NotComparableException {
Class ct = array.getClass().getComponentType() ;
if( Comparable.class.isAssignableFrom( ct ) ){
throw new NotComparableException( ct ) ;
}
int n = array.length ;
Object[] res = copy( array ) ;
Arrays.sort( res ) ;
if( !decreasing ){
return res ;
} else{
Object current ;
int top = (res.length) / 2 ;
for( int i=0; i<top ; i++ ){
current = res[i] ;
res[ i ] = res[ n-i-1 ] ;
res[ n-i-1 ] = current ;
}
}
return res ;
}
// }}}
// {{{ rev
/**
* Returns a copy of the input array with elements in
* reverse order
*
* @param original input array
*/
public static Object[] rev( Object[] original ){
int n = original.length ;
Object[] copy = (Object[])Array.newInstance( original.getClass().getComponentType() , n );
for( int i=0; i<n ; i++ ){
copy[n-i-1] = original[i] ;
}
return copy ;
}
// }}}
// {{{ copy
public static Object[] copy( Object[] original ){
int n = original.length ;
Object[] copy = (Object[])Array.newInstance( original.getClass().getComponentType() , n );
for( int i=0; i<n ; i++ ){
copy[i] = original[i] ;
}
return copy ;
}
// }}}
// {{{ getIterableContent
public static Object[] getIterableContent( Iterable o){
Vector v = new Vector();
Iterator iterator = o.iterator();
while( iterator.hasNext() ){
v.add( iterator.next() );
}
return v.toArray();
}
// }}}
// {{{ rep
/**
* Creates a java array by cloning o several times
*
* @param o object to clone
* @param size number of times to replicate the object
*/
public static Object[] rep( Object o, int size ) throws Throwable {
Object[] res = (Object[])Array.newInstance( o.getClass(), size ) ;
if( ! ( o instanceof Cloneable )){
return res ;
}
Method m = getCloneMethod( o.getClass() ) ;
boolean access = m.isAccessible() ;
if (!access) {
try {
m.setAccessible( true ) ;
} catch (Throwable e) {
access = true; /* give up */
}
}
try{
for( int i=0; i<size; i++){
Object cloned = o.getClass().cast( m.invoke( o, (Object[])null ) );
res[i] = cloned ;
}
} catch( InvocationTargetException e){
throw e.getCause() ;
} finally {
if (!access)
m.setAccessible( access );
}
return res ;
}
private static Method getCloneMethod(Class cl){
Method[] methodz ;
Method m = null ;
while( cl != null ){
methodz = cl.getDeclaredMethods( ) ;
for( int i=0; i<methodz.length; i++){
m = methodz[i];
if( "clone".equals( m.getName() ) && m.getParameterTypes().length == 0 ){
return m ;
}
}
cl = cl.getSuperclass();
}
return null ; /* never happens */
}
// }}}
// {{{ cloneObject
public static Object cloneObject( Object o) throws Throwable {
Method m = getCloneMethod( o.getClass() ) ;
boolean access = m.isAccessible() ;
if (!access) {
try {
m.setAccessible( true ) ;
} catch (Throwable e) {
access = true; /* give up */
}
}
Object copy = null ;
try {
copy = o.getClass().cast( m.invoke( o, (Object[])null ) );
} catch( InvocationTargetException e){
throw e.getCause() ;
} finally {
if (!access)
m.setAccessible( access );
}
return copy ;
}
// }}}
/// boxing and unboxing for Double[] and Integer[]
public static final int NA_INTEGER = -2147483648;
public static final double NA_REAL = Double.longBitsToDouble(0x7ff00000000007a2L);
static final long NA_bits = Double.doubleToRawLongBits(Double.longBitsToDouble(0x7ff00000000007a2L));
public static double[] unboxDoubles(Double[] o) {
if (o == null) return null;
int i = 0, n = o.length;
double d[] = new double[n];
for (i = 0; i < n; i++) d[i] = (o[i] == null) ? NA_REAL : o[i].doubleValue();
return d;
}
public static int[] unboxIntegers(Integer[] o) {
if (o == null) return null;
int i = 0, n = o.length;
int d[] = new int[n];
for (i = 0; i < n; i++) d[i] = (o[i] == null) ? NA_INTEGER : o[i].intValue();
return d;
}
public static int[] unboxBooleans(Boolean[] o) {
if (o == null) return null;
int i = 0, n = o.length;
int d[] = new int[n];
for (i = 0; i < n; i++) d[i] = (o[i] == null) ? NA_INTEGER : (o[i].booleanValue() ? 1 : 0);
return d;
}
public static boolean isNA(double value) {
/* on OS X i386 the MSB of the fraction is set even though R doesn't set it.
Although this is technically a good idea (to make it a QNaN) it's not what R does and thus makes the comparison tricky */
return (Double.doubleToRawLongBits(value) & 0xfff7ffffffffffffL) == (NA_bits & 0xfff7ffffffffffffL);
}
public static Double[] boxDoubles(double[] d) {
if (d == null) return null;
int i = 0, n = d.length;
Double o[] = new Double[i];
for (i = 0; i < n; i++) if (!isNA(d[i])) o[i] = Double.valueOf(d[i]);
return o;
}
public static Integer[] boxIntegers(int[] d) {
if (d == null) return null;
int i = 0, n = d.length;
Integer o[] = new Integer[i];
for (i = 0; i < n; i++) if (d[i] != NA_INTEGER) o[i] = Integer.valueOf(d[i]);
return o;
}
public static Boolean[] boxBooleans(int[] d) {
if (d == null) return null;
int i = 0, n = d.length;
Boolean o[] = new Boolean[i];
for (i = 0; i < n; i++) if (d[i] != NA_INTEGER) o[i] = Boolean.valueOf((d[i] == 0) ? false : true);
return o;
}
}
|