Skip to content

Commit cdb0b59

Browse files
testing unique
git-svn-id: svn://svn.rforge.net/rJava/trunk@471 634b503b-a89c-4e0f-88f7-82c2bf7838a2
1 parent 9cba527 commit cdb0b59

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

man/jrectRef-class.Rd

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,19 @@ dim( xj ) <- NULL
172172
stopifnot( xj@jsig == "[Ljava/lang/String;" )
173173
stopifnot( dim( xj ) == 30L )
174174

175+
# test unique
176+
x <- .jarray( rep( 1:2, each = 5 ) )
177+
xu <- unique( x )
178+
stopifnot( dim(xu) == 2L )
179+
180+
p1 <- .jnew( "java/awt/Point" )
181+
p2 <- .jnew( "java/awt/Point" )
182+
x <- .jarray( list( p1, p2 ) )
183+
xu <- unique( x )
184+
stopifnot( dim( xu ) == 1L )
185+
186+
187+
175188
}
176189
}
177190
\keyword{classes}

src/java/RJavaArrayTools.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ public static Object get( Object array, int position ) throws NotAnArrayExceptio
304304
// }}}
305305

306306
// {{{ unique
307+
// TODO: cannot use LinkedHashSet because it first was introduced in 1.4
308+
// and code in this area needs to work on 1.2 jvm
307309
public static Object[] unique( Object[] array ){
308310
int n = array.length ;
309311
boolean[] unique = new boolean[ array.length ];
@@ -330,9 +332,11 @@ public static Object[] unique( Object[] array ){
330332
}
331333
}
332334
}
333-
334-
return res.toArray();
335-
335+
// build the array using newInstance so that it has the same
336+
// component type as the original array and not just Object
337+
Object[] res_array = (Object[])Array.newInstance( array.getClass().getComponentType(), res.size() ) ;
338+
res.toArray( res_array );
339+
return res_array ;
336340
}
337341
// }}}
338342

0 commit comments

Comments
 (0)