[Rcpp-devel] Simple List Operations

Dirk Eddelbuettel edd at debian.org
Wed Mar 6 19:48:11 CET 2013


On 6 March 2013 at 12:07, Greg Minshall wrote:
| (since i've just been here...)
| 
| .../unitTests/runit.Vector.R has these tests:
| ----
| test.List.erase <- function(){
|     fun <- list_erase
|     d <- list( x = 1:10, y = letters[1:10] )
|     checkEquals(fun(d),
|                 list( y = letters[1:10] ),
|                 msg = "List.erase" )
| }
| 
| test.List.erase.range <- function(){
|     fun <- list_erase_range
|     d <- list( x = 1:10, y = letters[1:10], z = 1:10 )
|     checkEquals(fun(d),
|                 list( z = 1:10 ),
|                 msg = "List.erase (range version)" )
| }
| ----
| 
| the functions list_erase{,_range} are in .../unitTests/cpp/Vector.cpp:
| ----
| // [[Rcpp::export]]
| List list_erase( List list ){
|     list.erase( list.begin() ) ;
|     return list ;
| }
| 
| // [[Rcpp::export]]
| List list_erase_range( List list ){
|     list.erase( 0, 1 ) ;
|     return list ;
| }
| ----
| 
| i haven't tried these.

They are here "as a courtesy" as we otherwise support / mimic STL vector
ops.  But let us not forget that lists are vectors too, and we are in fact
bound by the vector semantics. So if you remove an element in the middle, you
are in fact copying the remainder to a new vector.  Not lightweight.

As for Rodney's question about "what is the foo[[1]] <- NULL equivalent":
that's what happens when you go multi-language. Sadly not all paradigms work
exactly the same way.

So the long asnwer is: IF you need list behaviour at the C++ level, consider
using use STL list object, not Rcpp::List object, and convert at the end.

Dirk


-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com  


More information about the Rcpp-devel mailing list