Skip to content

Instantly share code, notes, and snippets.

@romainfrancois
Created October 23, 2013 12:15
Show Gist options
  • Save romainfrancois/7117540 to your computer and use it in GitHub Desktop.
Save romainfrancois/7117540 to your computer and use it in GitHub Desktop.
create lists with more than 20 elements
#include <Rcpp.h>
using namespace Rcpp ;
template <typename T>
inline void set_item_impl( List& target, int i, const T& obj, CharacterVector& names, traits::true_type ){
target[i] = obj.object ;
names[i] = obj.name ;
}
template <typename T>
inline void set_item_impl( List& target, int i, const T& obj, CharacterVector&, traits::false_type ){
target[i] = obj ;
}
template <typename T>
inline void set_item( List& target, int i, const T& obj, CharacterVector& names){
set_item_impl( target, i, obj, names, typename traits::is_named<T>::type() ) ;
}
// [[Rcpp::export]]
List biglist(){
List out( 30 ) ;
CharacterVector names(30) ;
set_item( out, 0 , _["x0"] = 42, names) ;
set_item( out, 1 , _["x1"] = 42, names) ;
set_item( out, 2 , _["x2"] = 42, names) ;
set_item( out, 3 , _["x3"] = 42, names) ;
set_item( out, 4 , _["x4"] = 42, names) ;
set_item( out, 5 , _["x5"] = 42, names) ;
set_item( out, 6 , _["x6"] = 42, names) ;
set_item( out, 7 , _["x7"] = 42, names) ;
set_item( out, 8 , _["x8"] = 42, names) ;
set_item( out, 9 , _["x9"] = 42, names) ;
set_item( out, 10, _["x10"] = 42, names) ;
set_item( out, 11, _["x11"] = 42, names) ;
set_item( out, 12, _["x12"] = 42, names) ;
set_item( out, 13, _["x13"] = 42, names) ;
set_item( out, 14, _["x14"] = 42, names) ;
set_item( out, 15, _["x15"] = 42, names) ;
set_item( out, 16, _["x16"] = 42, names) ;
set_item( out, 17, _["x17"] = 42, names) ;
set_item( out, 18, _["x18"] = 42, names) ;
set_item( out, 19, _["x19"] = 42, names) ;
set_item( out, 20, _["x20"] = 42, names) ;
set_item( out, 21, _["x21"] = 42, names) ;
set_item( out, 22, _["x22"] = 42, names) ;
set_item( out, 23, _["x23"] = 42, names) ;
set_item( out, 24, _["x24"] = 42, names) ;
set_item( out, 25, _["x25"] = 42, names) ;
set_item( out, 26, _["x26"] = 42, names) ;
set_item( out, 27, _["x27"] = 42, names) ;
set_item( out, 28, _["x28"] = 42, names) ;
set_item( out, 29, _["x29"] = 42, names) ;
out.names() = names ;
return out ;
}
/*** R
biglist()
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment