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
|
# :tabSize=4:indentSize=4:noTabs=false:folding=explicit:collapseFolds=1:
# {{{ utilities to deal with arrays
#' Indicates if a object refers to a java array
#'
#' @param o object
#' @return TRUE if the object is a java array, FALSE if not
#' (including when the object is not even a java reference)
isJavaArray <- function( o ){
if( ( is( o, "jobjRef" ) || is( o, "jarrayRef") || is( o, "jrectRef") ) && !is.jnull(o) ){
.jcall( "RJavaArrayTools", "Z", "isArray", .jcast(o) )
} else FALSE
}
._must_be_java_array <- function( o, message = "object is not a java array" ){
if( !isJavaArray(o ) ){
stop( message )
}
}
isJavaArraySignature <- function( sig ){
identical( substr( sig, 1, 1 ), '[' )
}
#' get the component type of a java array
getComponentType <- function( o, check = TRUE ){
if( check ) ._must_be_java_array( o )
.jcall( .jcall( o, "Ljava/lang/Class;", "getClass" ), "Ljava/lang/Class;", "getComponentType" )
}
._jarray_simplify <- function( x ){
._must_be_java_array( x )
clname <- .jclass(x, true = TRUE )
Array <- "java/lang/reflect/Array"
obj <- switch( clname,
# deal with array of primitive first
"[I" = .Call(RgetIntArrayCont , x@jobj),
"[J" = .Call(RgetLongArrayCont , x@jobj),
"[Z" = .Call(RgetBoolArrayCont , x@jobj) ,
"[B" = .Call(RgetByteArrayCont , x@jobj) ,
"[D" = .Call(RgetDoubleArrayCont, x@jobj) ,
"[S" = .Call(RgetShortArrayCont , x@jobj) ,
"[C" = .Call(RgetCharArrayCont , x@jobj) ,
"[F" = .Call(RgetFloatArrayCont , x@jobj) ,
"[Ljava.lang.String;" = .Call(RgetStringArrayCont, x@jobj),
# otherwise, just get the object
x )
obj
}
# }}}
# {{{ length
#' get the length of the array
._length_java_array <- function(x){
if( isJavaArray( x ) ){
.jcall( "java/lang/reflect/Array", "I", "getLength", .jcast( x, check = FALSE, convert.array = FALSE) )
} else{
stop( "the supplied object is not a java array" )
}
}
setMethod( "length", "jarrayRef", ._length_java_array )
setMethod( "length", "jrectRef", ._length_java_array )
setGeneric( "str" )
setMethod("str", "jarrayRef", function(object, ...){
txt <- sprintf( "Formal class 'jarrayRef' [package \"rJava\"] with 2 slots
..@ jobj :<externalptr>
..@ jclass: chr \"%s\"
..@ jsig : chr \"%s\"
", object@jclass, object@jsig )
cat( txt )
} )
setMethod("str", "jrectRef", function(object, ...){
dim <- object@dimension
dim.txt <- if( length( dim ) == 1L ){
sprintf( "int %d", dim )
} else {
sprintf( "int[1:%d] %s", length(dim), paste( if( length(dim) > 6 ) c( dim[1:6], "...") else dim, collapse = " ") )
}
txt <- sprintf( "Formal class 'jrectRef' [package \"rJava\"] with 2 slots
..@ jobj :<externalptr>
..@ jclass : chr \"%s\"
..@ jsig : chr \"%s\"
..@ dimension: %s
", object@jclass, object@jsig, dim.txt )
cat( txt )
} )
# }}}
# {{{ single bracket indexing : [
# indexing of .jarrayRef
# is is not quite clear what the proper result should be, because technically
# [ should always return a jarrayRef, but it's not the most useful thing to do.
# the code below (ab)uses drop to try to deal with that, but it's not optimal ...
# ._jctype <- function(x) if (is.jnull(x)) NA else if(is(x, "jarrayRef")) x@jsig else paste("L", x@jclass, ";", sep='')
# #' index a java array
# #'
# #' @param x a reference to a java array
# #' @param i indexer (only 1D indexing supported so far)
# #' @param drop if the result if of length 1, just return the java object instead of an array of length one
# #' @param simplify further simplify the result
# ._java_array_single_indexer <- function( x, i, j, drop, simplify = FALSE, silent = FALSE, ... ){
# # arrays only
#
# if( !silent ){
# if( ! missing( j ) ){
# warning( "only one dimensional indexing is currently supported in i, ignoring j argument" )
# }
# dots <- list( ... )
# if( length(dots) ){
# unnamed.dots <- dots[ names(dots) == "" ]
# if( length( unnamed.dots ) ){
# warning( "only one dimensional indexing is currently supported in [, ignoring ... arguments" )
# }
# }
# }
#
# # the component type of the array - maybe used to make
# # arrays with the same component type, but of length 0
# component.type <- getComponentType( x, check = FALSE )
#
# # 'eval' the array
# ja <- .jevalArray( x )
#
# # native type - use R subsetting and maybe remap to java
# if (!is.list(ja)) {
# # perform the subset
# o <- ja[i]
#
# # return native type if simplify
# if( simplify ){
# return(o)
# }
#
# if( length(o) == 0L) {
# # return an array of the same component type as the original array
# # but of length 0
# return( .jcall( "java/lang/reflect/Array", "Ljava/lang/Object;", "newInstance", component.type, 0L ) )
# } else {
# # drop makes no sense here
# return( .jarray( o ) )
# }
# }
#
# # the result an array of java objects
# sl <- ja[i]
#
# if( length( sl ) == 0L ){
# # TODO: make simplify influencial here
# # for example if x is int[] then we want to get integer(0)
# return( .jcall( "java/lang/reflect/Array", "Ljava/lang/Object;", "newInstance", component.type, 0L ) )
# } else{
# # just return the array
# return( .jarray( sl ) )
# }
# }
# ## this is all weird - we need to distinguish between x[i] and x[i,] yet S4 fails to do so ...
setMethod( "[", signature( x = "jarrayRef" ),
function(x, i, j, ..., drop = FALSE){
# the code above is not good enough
.NotYetImplemented()
} )
# }}}
# {{{ double bracket indexing : [[
._collectIndex <- function( i, j, ...){
dots <- list( ... )
unnamed.dots <- if( length( dots ) ){
dots[ names(dots) == "" ]
}
firstInteger <- function(.) as.integer(.)[1]
firstIntegerOfEach <- function(.) sapply( ., firstInteger )
index <- c(
if( !missing(i) ) firstInteger(i),
if( !missing(j) ) firstInteger(j),
if( !is.null(unnamed.dots) && length(unnamed.dots) ) firstIntegerOfEach( unnamed.dots )
)
}
# R version of RJavaArrayTools#getDimensionLength
# it only works on the signature so should be used with caution
getDimensionLength <- function( x, true.class = TRUE ){
nchar( sub( "[^[]+", "", .jclass(x, true = true.class) ) )
}
# R version of RJavaArrayTools#getObjectTypeName
getObjectTypeName <- function( x, true.class=TRUE){
sub( "^[[]*(.*);?$", "\\1", .jclass(x, true = true.class) )
}
._java_array_double_indexer <- function( x, i, j, ..., evalArray = FALSE, evalString = FALSE ){
# initial checks
._must_be_java_array( x )
index <- ._collectIndex( i, j, ... )
if( !length(index) || is.null(index) ){
# return the full object
x
} else{
# shift one left (java style indexing starts from 0 )
index <- index - 1L
depth <- getDimensionLength( x )
typename <- getObjectTypeName( x )
if( length( index) == depth ){
# we need to dispatch primitive
if( isPrimitiveTypeName( typename ) ){
res <- switch( typename,
# deal with array of primitive first
"I" = .jcall( "RJavaArrayTools", "I", "getInt" , .jcast(x), index ) ,
"J" = .jcall( "RJavaArrayTools", "J", "getLong" , .jcast(x), index ) ,
"Z" = .jcall( "RJavaArrayTools", "Z", "getBoolean", .jcast(x), index ) ,
"B" = .jcall( "RJavaArrayTools", "B", "getByte" , .jcast(x), index ) ,
"D" = .jcall( "RJavaArrayTools", "D", "getDouble" , .jcast(x), index ) ,
"S" = .jcall( "RJavaArrayTools", "S", "getShort" , .jcast(x), index ) ,
"C" = .jcall( "RJavaArrayTools", "C", "getChar" , .jcast(x), index ) ,
"F" = .jcall( "RJavaArrayTools", "F", "getFloat" , .jcast(x), index ),
stop( "wrong primitive" ) # should never happen
)
return( res )
}
}
# otherwise use the Object version
.jcall( "RJavaArrayTools", "Ljava/lang/Object;", "get", .jcast(x), index,
evalArray = evalArray, evalString = evalString )
}
}
# this is the only case that makes sense: i is an integer or a numeric of length one
# we cannot use logical indexing or indexing by name because there is no such thing in java
setMethod( "[[", signature( x = "jarrayRef" ),
function(x, i, j, ...){
._java_array_double_indexer( x, i, j, ... )
} )
._java_array_double_replacer <- function( x, i, j, ..., value ){
# initial checks
._must_be_java_array( x )
index <- ._collectIndex( i, j, ... )
if( !length(index) || is.null(index) ){
# allow for x[[]] <- value
newArray( value , simplify = FALSE )
} else{
jvalue <- ._java_valid_object( value )
if( ._isPrimitiveReference( value ) ){
# then use a primitive version
.jcall( "RJavaArrayTools", "V", "set", .jcast(x),
index - 1L, value )
} else{
# use the Object version
.jcall( "RJavaArrayTools", "V", "set", .jcast(x),
index - 1L, .jcast( jvalue ) )
if( isJavaArray( jvalue ) ){
# rectangularity might have changed
# we have no choice but to reset the array
x <- newArray( jobj = x@jobj, signature = x@jsig )
}
}
x
}
}
setReplaceMethod( "[[", signature( x = "jarrayRef" ),
function(x, i, j, ..., value ){
._java_array_double_replacer( x, i, j, ..., value = value)
} )
# }}}
# {{{ head and tail
setGeneric( "head" )
setMethod("head", signature( x = "jarrayRef" ), function(x, n = 6L, ... ){
if( !isJavaArray( x ) ){
stop( "not a java array" )
}
# FIXME : this only makes sense for 1d arays
n_objs <- length(x)
if( abs( n ) >= n_objs ){
return( x )
}
len <- if( n > 0L ) n else n_objs + n
x[seq_len(n), ... ]
} )
setGeneric( "tail" )
setMethod("tail", signature( x = "jarrayRef" ), function(x, n = 6L, ... ){
if( !isJavaArray( x ) ){
stop( "not a java array" )
}
# FIXME : this only makes sense for 1d arays
n_objs <- length(x)
if( abs( n ) >= n_objs ) return(x)
if( n < 0L){
n <- n_objs + n
}
return( x[ seq.int( n_objs-n+1, n_objs ) , ... ] )
} )
# }}}
# {{{ newArray - dispatch to jarrayRef or jrectRef
#' creates a new jarrayRef or jrectRef depending on the rectangularity
#' of the array
#'
#' @param o a jobjRef object
#' @param simplify if TRUE and the result is a rectangular array
#' of primitives, simplify it to an R object
newArray <- function( o, simplify = TRUE, jobj, signature ){
if( !missing(jobj) ){
o <- new("jobjRef", jobj = jobj, jclass = signature)
}
if( !isJavaArray( o ) ){
stop( "o does not refer to a java array" )
}
if( inherits( o, "jrectRef" ) ){
# no need to go further
return(o)
}
clazz <- tojni( .jclass( o, true = TRUE ) )
wrapper <- .jnew("ArrayWrapper", .jcast(o) )
isRect <- .jcall( wrapper, "Z", "isRectangular" )
if( isRect ){
dims <- .jcall( wrapper, "[I", "getDimensions" )
if( !simplify ){
# no need to go further down, return a reference
return( new( "jrectRef", jobj = o@jobj, jsig = clazz, jclass = clazz,
dimension = dims ) )
}
isprim <- .jcall( wrapper, "Z", "isPrimitive" )
typename <- .jcall( wrapper, "Ljava/lang/String;", "getObjectTypeName" )
isstrings <- identical( typename, "java.lang.String" )
if( !isprim && !isstrings ){
# cannot simplify, return a reference
return( new( "jrectRef", jobj = o@jobj, jsig = clazz, jclass = clazz,
dimension = dims ) )
}
if( isprim || isstrings ){
# array of java primitives, we can translate this to R array
out <- structure( switch( typename ,
"I" = .jcall( wrapper, "[I" , "flat_int" ),
"Z" = .jcall( wrapper, "[Z" , "flat_boolean" ),
"B" = .jcall( wrapper, "[B" , "flat_byte" ),
"J" = .jlong( .jcall( wrapper, "[J" , "flat_long" ) ),
"S" = .jshort( .jcall( wrapper, "[T" , "flat_short" ) ), # [T is remapped to [S in .jcall
"D" = .jcall( wrapper, "[D" , "flat_double" ),
"C" = .jchar( .jcall( wrapper, "[C" , "flat_char" ) ),
"F" = .jfloat( .jcall( wrapper, "[F" , "flat_float" ) ),
"java.lang.String" = .jcall( wrapper, "[Ljava/lang/String;", "flat_String" ),
stop( sprintf("cannot simplify type : ", typename) ) # this should not happen
), dim = dims )
return( out )
}
} else {
# not a rectangular array -> jarrayRef
new( "jarrayRef", jobj = o@jobj, jsig = clazz, jclass = clazz )
}
}
# }}}
# {{{ [ indexing of rectangular arrays
setMethod( "[", signature( x = "jrectRef" ),
function(x, i, j, ..., simplify = FALSE, drop = TRUE ){
# first we extract th data as a flat (one dimensional) R array
# called 'flat'
dim <- x@dimension
wrapper <- .jnew( "ArrayWrapper", .jcast(x) )
typename <- .jcall( wrapper, "Ljava/lang/String;", "getObjectTypeName" )
isprim <- .jcall( wrapper, "Z", "isPrimitive" )
flat <- switch( typename,
"I" = .jcall( wrapper, "[I" , "flat_int" , evalArray = TRUE ),
"Z" = .jcall( wrapper, "[Z" , "flat_boolean" , evalArray = TRUE ),
"B" = .jcall( wrapper, "[B" , "flat_byte" , evalArray = TRUE ),
"J" = .jcall( wrapper, "[J" , "flat_long" , evalArray = TRUE ),
"S" = .jcall( wrapper, "[T" , "flat_short" , evalArray = TRUE ), # [T is remapped to [S in .jcall
"D" = .jcall( wrapper, "[D" , "flat_double" , evalArray = TRUE ),
"C" = .jcall( wrapper, "[C" , "flat_char" , evalArray = TRUE ) ,
"F" = .jcall( wrapper, "[F" , "flat_float" , evalArray = TRUE ),
"java.lang.String" = .jcall( wrapper, "[Ljava/lang/String;" , "flat_String" , evalArray = TRUE ),
.jcall( wrapper, "[Ljava/lang/Object;" , "flat_Object" , evalArray = TRUE ) )
# then we give to flat the correct dimensions
if( length(dim) != 1L ){
dim( flat ) <- dim
}
# now we construct the call to '[' on flat.
# this call uses all the regular R indexing
call <- match.call( call = sys.call(sys.parent()) )
n.args <- nargs( )
e <- as.list( call )[ -(1:2) ]
names.e <- names(e)
if( any( have.name <- (names.e != "") ) ){
# we need to extract drop and simplify
nam <- names.e[ have.name ]
if( !all( nam %in% c("simplify", "drop", "i", "j" ) ) ){
stop( "only 'drop' and 'simplify' are allowed as named arguments, they need to be written exactly" )
}
}
if( missing(i) && missing(j) && all( names.e != "" ) ){
# special case with no indexing at all
actual.call <- sprintf( "flat[ , drop = %s ]", as.character(drop) )
} else if( !missing(i) && missing(j) && all( names.e != "" ) ){
# special case where there is only one index
actual.call <- sprintf( "flat[ %s , drop = %s ]", deparse(i), as.character(drop) )
} else{
# we need to be careful about the missing's
# we cannot just do things like list(...) because with missings
# it just does not work
actual.call <- "flat["
itoken <- if( missing(i ) ) " " else deparse(i)
jtoken <- if( missing(j ) ) " " else deparse(j)
actual.call <- sprintf( "flat[ %s , %s", itoken, jtoken )
iii <- 1L
for( a in e ){
if( missing(a) ){
actual.call <- sprintf( "%s , ", actual.call )
} else if( have.name[iii] ) {
# we put both at the end
} else {
# not missing, not named
actual.call <- sprintf( "%s, %s", actual.call, deparse(a) )
}
iii <- iii + 1L
}
actual.call <- sprintf( "%s, drop = %s ]", actual.call, as.character(drop) )
}
# now we eval the call
subs <- eval( parse( text = actual.call ) )
# now if we need and can simplify it, we return the subsetted array as is
# otherwise, we rewrap it to java
if( simplify && (typename == "java.lang.String" || isprim ) ) subs else .jarray( subs, dispatch = TRUE )
} )
# }}}
# {{{ dim.jrectRef
setMethod( "dim", signature( x = "jrectRef" ), function(x) x@dimension )
setReplaceMethod( "dim", signature( x = "jrectRef" ), function(x, value){
expected_prod <- prod( x@dimension )
if( is.null( value ) ){
value <- expected_prod
} else{
received_prod <- prod(value)
if( received_prod != expected_prod ){
stop( sprintf("dims [product %d] do not match the length of object [%d]", received_prod, expected_prod ) )
}
}
dim <- x@dimension
wrapper <- .jnew( "ArrayWrapper", .jcast(x) )
typename <- .jcall( wrapper, "Ljava/lang/String;", "getObjectTypeName" )
flat <- structure(
switch( typename,
"I" = .jcall( wrapper, "[I" , "flat_int" , evalArray = TRUE ),
"Z" = .jcall( wrapper, "[Z" , "flat_boolean" , evalArray = TRUE ),
"B" = .jcall( wrapper, "[B" , "flat_byte" , evalArray = TRUE ),
"J" = .jcall( wrapper, "[J" , "flat_long" , evalArray = TRUE ),
"S" = .jcall( wrapper, "[T" , "flat_short" , evalArray = TRUE ), # [T is remapped to [S in .jcall
"D" = .jcall( wrapper, "[D" , "flat_double" , evalArray = TRUE ),
"C" = .jcall( wrapper, "[C" , "flat_char" , evalArray = TRUE ) ,
"F" = .jcall( wrapper, "[F" , "flat_float" , evalArray = TRUE ),
"java.lang.String" = .jcall( wrapper, "[Ljava/lang/String;" , "flat_String" , evalArray = TRUE ),
.jcall( wrapper, "[Ljava/lang/Object;" , "flat_Object" , evalArray = TRUE ) ) ,
dim = value )
.jarray(flat, dispatch = TRUE)
} )
# }}}
PRIMITIVE_TYPES <- c( "I", "Z", "B", "J", "S", "D", "C", "F" )
isPrimitiveTypeName <- function( type, include.strings = TRUE ){
type %in% PRIMITIVE_TYPES || ( include.strings && identical( type, "java.lang.String" ) )
}
PRIMITIVE_TYPES_RX <- sprintf( "^[[]+[%s]$" , paste( PRIMITIVE_TYPES, collapse = "" ) )
isPrimitiveArraySignature <- function( x, ... ){
regexpr( PRIMITIVE_TYPES_RX, x, ... ) > 0
}
isArraySignature <- function( x ){
substr( x, 1, 1 ) == "["
}
# {{{ unique.jarrayRef
setGeneric( "unique" )
._unique_jrectRef <- function( x, incomparables = FALSE, ...){
dim <- x@dimension
if( length( dim ) > 1L ){
stop( "'unique' only implemented for 1d array so far" )
}
typename <- .jcall( "RJavaArrayTools", "Ljava/lang/String;",
"getObjectTypeName", .jcast(x) )
if( isPrimitiveTypeName( typename, include.strings = TRUE ) ){
.jarray( unique( .jevalArray( x ) ), dispatch = TRUE )
} else{
.jcall( "RJavaArrayTools", "[Ljava/lang/Object;", "unique",
.jcast( x, "[Ljava/lang/Object;" ), evalArray = TRUE, simplify = TRUE )
}
}
setMethod( "unique", "jarrayRef", function(x, incomparables = FALSE, ...){
.NotYetImplemented()
} )
setMethod( "unique", "jrectRef", ._unique_jrectRef )
# }}}
# {{{ duplicated
setGeneric( "duplicated" )
._duplicated_jrectRef <- function( x, incomparables = FALSE, ...){
dim <- x@dimension
if( length( dim ) > 1L ){
stop( "'duplicated' only implemented for 1d array so far" )
}
typename <- .jcall( "RJavaArrayTools", "Ljava/lang/String;",
"getObjectTypeName", .jcast(x) )
if( isPrimitiveTypeName( typename, include.strings = TRUE ) ){
duplicated( .jevalArray( x ) )
} else{
.jcall( "RJavaArrayTools", "[Z", "duplicated",
.jcast( x, "[Ljava/lang/Object;" ), evalArray = TRUE )
}
}
setMethod( "duplicated", "jrectRef", ._duplicated_jrectRef )
setMethod( "duplicated", "jarrayRef", function( x, incomparables = FALSE, ...){
.NotYetImplemented()
})
# }}}
# {{{ anyDuplicated
.base.has.anyDuplicated <- exists("anyDuplicated", asNamespace("base"))
if (!.base.has.anyDuplicated) {
anyDuplicated <- function(x, incomparables = FALSE, ...) UseMethod("anyDuplicated")
}
setGeneric( "anyDuplicated" )
._anyduplicated_jrectRef <- function( x, incomparables = FALSE, ...){
dim <- x@dimension
if( length( dim ) > 1L ){
stop( "'anyDuplicated' only implemented for 1d array so far" )
}
typename <- .jcall( "RJavaArrayTools", "Ljava/lang/String;",
"getObjectTypeName", .jcast(x) )
if( isPrimitiveTypeName( typename, include.strings = TRUE ) ){
anyDuplicated( .jevalArray( x ) )
} else{
.jcall( "RJavaArrayTools", "I", "anyDuplicated",
.jcast( x, "[Ljava/lang/Object;" ), evalArray = TRUE ) + 1L
}
}
setMethod( "anyDuplicated", "jrectRef", ._anyduplicated_jrectRef )
setMethod( "anyDuplicated", "jarrayRef", function( x, incomparables = FALSE, ...){
.NotYetImplemented()
})
# }}}
# {{{ flat
#' utility to flatten an array
flat <- function(x, simplify = FALSE){
stop( "undefined" )
}
setGeneric( "flat")
._flat_jrectRef <- function( x, simplify = FALSE ){
dim <- dim(x)
if( length(dim) == 1L ) {
if( !simplify) x else x[ simplify = TRUE ]
} else {
x[ seq_len(prod(dim)), drop = TRUE, simplify = simplify ]
}
}
setMethod( "flat", "jrectRef", ._flat_jrectRef )
setMethod( "flat", "jarrayRef", function(x, simplify=FALSE){
.NotYetImplemented()
} )
# }}}
# {{{ sort
setGeneric( "sort" )
._sort_jrectRef <- function( x, decreasing = FALSE, ...){
x <- flat( x )
dim <- x@dimension
typename <- .jcall( "RJavaArrayTools", "Ljava/lang/String;",
"getObjectTypeName", .jcast(x) )
if( isPrimitiveTypeName( typename, include.strings = TRUE ) ){
.jarray( sort( .jevalArray( x ), decreasing = decreasing ), dispatch = TRUE )
} else{
.jcall( "RJavaArrayTools", "[Ljava/lang/Object;", "sort",
.jcast( x, "[Ljava/lang/Object;" ), decreasing, evalArray = TRUE, simplify = TRUE )
}
}
setMethod( "sort", "jrectRef", ._sort_jrectRef )
setMethod( "sort", "jarrayRef", function(x, decreasing=FALSE, ...){
.NotYetImplemented()
})
# }}}
# {{{ rev
setGeneric( "rev" )
setMethod( "rev", "jrectRef", function(x){
x <- flat( x )
dim <- x@dimension
typename <- .jcall( "RJavaArrayTools", "Ljava/lang/String;",
"getObjectTypeName", .jcast(x) )
if( isPrimitiveTypeName( typename, include.strings = TRUE ) ){
.jarray( rev( .jevalArray( x ) ), dispatch = TRUE )
} else{
.jcall( "RJavaArrayTools", "[Ljava/lang/Object;", "rev",
.jcast( x, "[Ljava/lang/Object;" ), evalArray = TRUE, simplify = TRUE )
}
} )
setMethod( "rev", "jarrayRef", function(x){
.NotYetImplemented()
})
# }}}
# {{{ as.list
# S4 dispatch does not work
as.list.jarrayRef <- function(x, ... ){
.jevalArray( x )
}
as.list.jrectRef <- function( x, ...){
.jevalArray( x )
}
as.list.jobjRef <- function( x, ... ){
if( ! .jinstanceof( x, "java.lang.Iterable" ) ){
stop( "only objects that implements java.lang.Iterable can be converted to lists" )
}
.jcall( "RJavaArrayTools", "[Ljava/lang/Object;",
"getIterableContent", .jcast(x, "java/lang/Iterable") , evalArray = TRUE, ... )
}
# }}}
# {{{ min, max, range
setMethod("min", "jrectRef", function(x, ...,na.rm=TRUE){
dim <- x@dimension
typename <- .jcall( "RJavaArrayTools", "Ljava/lang/String;",
"getObjectTypeName", .jcast(x) )
if( isPrimitiveTypeName( typename, include.strings = TRUE ) ){
min( x[simplify=TRUE], na.rm = na.rm )
} else{
summarizer <- .jnew( "RectangularArraySummary", .jcast(x), dim )
.jcall( summarizer, "Ljava/lang/Object;", "min", na.rm )
}
} )
setMethod("min", "jarrayRef", function(x, ...,na.rm=TRUE){
.NotYetImplemented()
})
setMethod("max", "jrectRef", function(x, ..., na.rm=TRUE){
dim <- x@dimension
typename <- .jcall( "RJavaArrayTools", "Ljava/lang/String;",
"getObjectTypeName", .jcast(x) )
if( isPrimitiveTypeName( typename, include.strings = TRUE ) ){
max( x[simplify=TRUE], na.rm = na.rm )
} else{
summarizer <- .jnew( "RectangularArraySummary", .jcast(x), dim )
.jcall( summarizer, "Ljava/lang/Object;", "max", na.rm )
}
} )
setMethod("max", "jarrayRef", function(x, ..., na.rm=TRUE){
.NotYetImplemented()
} )
setMethod("range", "jrectRef", function(x, ..., na.rm=TRUE){
dim <- x@dimension
typename <- .jcall( "RJavaArrayTools", "Ljava/lang/String;",
"getObjectTypeName", .jcast(x) )
if( isPrimitiveTypeName( typename, include.strings = TRUE ) ){
range( x[simplify=TRUE], na.rm = na.rm )
} else{
summarizer <- .jnew( "RectangularArraySummary", .jcast(x), dim )
.jcall( summarizer, "[Ljava/lang/Object;", "range", na.rm, evalArray = TRUE, simplify = TRUE )
}
} )
setMethod("range", "jarrayRef", function(x, ..., na.rm=TRUE){
.NotYetImplemented()
})
# }}}
|