Rcpp Version 0.9.10
Vector.h
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
00002 //
00003 // Vector.h: Rcpp R/C++ interface class library -- vectors
00004 //
00005 // Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
00006 //
00007 // This file is part of Rcpp.
00008 //
00009 // Rcpp is free software: you can redistribute it and/or modify it
00010 // under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 2 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // Rcpp is distributed in the hope that it will be useful, but
00015 // WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
00021 
00022 #ifndef Rcpp__vector__Vector_h
00023 #define Rcpp__vector__Vector_h
00024 
00025 template <int RTYPE>
00026 class Vector :
00027     public RObject,       
00028     public VectorBase< RTYPE, true, Vector<RTYPE> >, 
00029     public internal::eval_methods<RTYPE> 
00030 {
00031 public:
00032     typedef typename traits::r_vector_proxy<RTYPE>::type Proxy ;
00033     typedef typename traits::r_vector_name_proxy<RTYPE>::type NameProxy ;
00034     typedef typename traits::r_vector_proxy<RTYPE>::type value_type ;
00035     typedef typename traits::r_vector_iterator<RTYPE>::type iterator ;
00036     typedef typename traits::init_type<RTYPE>::type init_type ;
00037     typedef typename traits::r_vector_element_converter<RTYPE>::type converter_type ;
00038     typedef typename traits::storage_type<RTYPE>::type stored_type ;
00039         
00040     Vector() : RObject() {
00041         RCPP_DEBUG( "Vector()" ) ;
00042         RObject::setSEXP( Rf_allocVector( RTYPE, 0 ) ) ;
00043         init() ;
00044     } ;
00045     ~Vector(){
00046         RCPP_DEBUG( "~Vector()" ) ;
00047     };
00048     
00049     Vector( const Vector& other) : RObject() {
00050         RObject::setSEXP(other.asSexp()) ;
00051     }
00052         
00053     Vector& operator=( const Vector& other ){
00054         set_sexp( other.asSexp() ) ;
00055         return *this ;
00056     }
00057         
00058     Vector( const RObject::SlotProxy& proxy ) {
00059         RObject::setSEXP( r_cast<RTYPE>( (SEXP)proxy ) ) ;
00060     }
00061         
00062     Vector( const RObject::AttributeProxy& proxy ) {
00063         RObject::setSEXP( r_cast<RTYPE>( (SEXP)proxy ) ) ;
00064     }
00065                 
00066     template <typename T>
00067     Vector& operator=( const T& x){
00068         assign_object( x, typename traits::is_sugar_expression<T>::type() ) ;
00069         return *this ;
00070     }
00071         
00072 private:
00073     
00074     // sugar
00075     template <typename T>
00076     inline void assign_object( const T& x, traits::true_type ){
00077         int n = size() ;
00078         if( n == x.size() ){
00079            
00080             // just copy the data 
00081             iterator start = begin() ;
00082             
00083             int __trip_count = n >> 2 ;
00084             int i = 0 ;
00085             for ( ; __trip_count > 0 ; --__trip_count) { 
00086                 start[i] = x[i] ; i++ ;            
00087                 start[i] = x[i] ; i++ ;            
00088                 start[i] = x[i] ; i++ ;            
00089                 start[i] = x[i] ; i++ ;            
00090             }                                            
00091             switch (n - i){                          
00092             case 3:                                    
00093                 start[i] = x[i] ; i++ ;             
00094             case 2:                                    
00095                 start[i] = x[i] ; i++ ;             
00096             case 1:                                    
00097                 start[i] = x[i] ; i++ ;             
00098             case 0:                                    
00099             default:                                   
00100                 {}                         
00101             }
00102         } else{
00103             // different size, so we change the memory
00104             set_sexp( r_cast<RTYPE>( wrap(x) ) ); 
00105         }
00106     }
00107     
00108     // anything else
00109     template <typename T>
00110     inline void assign_object( const T& x, traits::false_type ){
00111         // TODO: maybe we already have the memory to host the results
00112         set_sexp( r_cast<RTYPE>( wrap(x) ) ) ;
00113     }
00114     
00115 public:
00116         
00117     internal::ListInitialization<iterator,init_type> operator=( init_type x){
00118         iterator start = begin() ; *start = x; 
00119         return internal::ListInitialization<iterator,init_type>( start + 1 ) ; ;
00120     }
00121         
00122     Vector( SEXP x ) : RObject() {
00123         RCPP_DEBUG_2( "Vector<%d>( SEXP = <%p> )", RTYPE, x) ;
00124         RObject::setSEXP( r_cast<RTYPE>( x ) ) ;
00125         RCPP_DEBUG( "===========" ) ;
00126     }
00127     
00128     Vector( const int& size ) : RObject()  {
00129         RCPP_DEBUG_2( "Vector<%d>( int = %d )", RTYPE, size ) ;
00130         RObject::setSEXP( Rf_allocVector( RTYPE, size) ) ;
00131         init() ;
00132     }
00133     
00134     template <bool NA, typename VEC>
00135     Vector( const VectorBase<RTYPE,NA,VEC>& other ) : RObject() {
00136         int n = other.size() ;
00137         RObject::setSEXP( Rf_allocVector( RTYPE, n ) ) ;
00138         import_expression<NA,VEC>( other, n ) ;
00139     }
00140     
00141 private:
00142           
00143     // TODO: do some dispatch when VEC == Vector so that we use std::copy
00144     template <bool NA, typename VEC>
00145     inline void import_expression( const VectorBase<RTYPE,NA,VEC>& other, int n ){
00146         iterator start = begin() ; 
00147         const VEC& ref = other.get_ref() ;
00148         
00149         int __trip_count = n >> 2 ;
00150         int i = 0 ;
00151         for ( ; __trip_count > 0 ; --__trip_count) { 
00152             start[i] = ref[i] ; i++ ;            
00153             start[i] = ref[i] ; i++ ;            
00154             start[i] = ref[i] ; i++ ;            
00155             start[i] = ref[i] ; i++ ;            
00156         }                                            
00157         switch (n - i){                          
00158         case 3:                                    
00159             start[i] = ref[i] ; i++ ;             
00160         case 2:                                    
00161             start[i] = ref[i] ; i++ ;             
00162         case 1:                                    
00163             start[i] = ref[i] ; i++ ;             
00164         case 0:                                    
00165         default:                                   
00166             {}                         
00167         }
00168     }
00169     
00170     // template <>
00171     // inline void import_expression<true,Vector>( const VectorBase<RTYPE,NA,VEC>& other, int n ){
00172     //     const VEC& ref = other.get_ref() ;
00173     //     std::copy( ref.begin(), ref.end(), begin() ) ;
00174     // }
00175     
00176     template <typename T>
00177     inline void fill_or_generate( const T& t){
00178         fill_or_generate__impl( t, typename traits::is_generator<T>::type() ) ;
00179     }
00180     
00181     template <typename T>
00182     inline void fill_or_generate__impl( const T& gen, traits::true_type){
00183         iterator first = begin() ;
00184         iterator last = end() ;
00185         while( first != last ) *first++ = gen() ;
00186     }
00187     
00188     template <typename T>
00189     inline void fill_or_generate__impl( const T& t, traits::false_type){
00190         fill(t) ;
00191     }
00192     
00193         
00194 public:
00195     
00196     template <typename U>
00197     Vector( const int& size, const U& u){
00198         RObject::setSEXP( Rf_allocVector( RTYPE, size) ) ;
00199         fill_or_generate( u ) ; 
00200     }
00201     
00202     Vector( const int& size, const stored_type& u ){
00203         RObject::setSEXP( Rf_allocVector( RTYPE, size) ) ;
00204         fill( u ) ;
00205     }
00206 
00207     Vector( const int& siz, stored_type (*gen)(void) ){
00208         RObject::setSEXP( Rf_allocVector( RTYPE, siz) ) ;
00209         iterator first = begin(), last = end() ;
00210         while( first != last ) *first++ = gen() ;
00211     }
00212     
00213     template <typename U1>
00214     Vector( const int& siz, stored_type (*gen)(U1), const U1& u1){
00215         RObject::setSEXP( Rf_allocVector( RTYPE, siz) ) ;
00216         iterator first = begin(), last = end() ;
00217         while( first != last ) *first++ = gen(u1) ;
00218     }
00219     
00220     template <typename U1, typename U2>
00221     Vector( const int& siz, stored_type (*gen)(U1,U2), const U1& u1, const U2& u2){
00222         RObject::setSEXP( Rf_allocVector( RTYPE, siz) ) ;
00223         iterator first = begin(), last = end() ;
00224         while( first != last ) *first++ = gen(u1,u2) ;
00225     }
00226 
00227     template <typename U1, typename U2, typename U3>
00228     Vector( const int& siz, stored_type (*gen)(U1,U2,U3), const U1& u1, const U2& u2, const U3& u3){
00229         RObject::setSEXP( Rf_allocVector( RTYPE, siz) ) ;
00230         iterator first = begin(), last = end() ;
00231         while( first != last ) *first++ = gen(u1,u2,u3) ;
00232     }
00233 
00234     Vector( const Dimension& dims) : RObject() {
00235         RObject::setSEXP( Rf_allocVector( RTYPE, dims.prod() ) ) ;
00236         init() ;
00237         if( dims.size() > 1 ){
00238             RObject::attr( "dim" ) = dims;
00239         }
00240     }
00241     
00242     template <typename U>
00243     Vector( const Dimension& dims, const U& u) : RObject() {
00244         RObject::setSEXP( Rf_allocVector( RTYPE, dims.prod() ) ) ;
00245         fill(u) ;
00246         if( dims.size() > 1 ){
00247             RObject::attr( "dim" ) = dims;
00248         }
00249     }
00250     
00251    
00252     template <typename InputIterator>
00253     Vector( InputIterator first, InputIterator last) : RObject(){
00254         assign( first, last ) ;
00255     }
00256 
00257     Vector( const std::string& st ) : RObject(){
00258         RObject::setSEXP( internal::vector_from_string<RTYPE>(st) );
00259     }
00260         
00261 #ifdef HAS_INIT_LISTS
00262     Vector( std::initializer_list<init_type> list ) : RObject(){
00263         assign( list.begin() , list.end() ) ;
00264     }
00265 #endif
00266         
00270     inline R_len_t length() const { return ::Rf_length( RObject::m_sexp ) ; }
00271     
00275     inline R_len_t size() const { return ::Rf_length( RObject::m_sexp ) ; }
00276     
00280     size_t offset(const size_t& i, const size_t& j) const {
00281         if( !::Rf_isMatrix(RObject::m_sexp) ) throw not_a_matrix() ;
00282         /* we need to extract the dimensions */
00283         int *dim = dims() ;
00284         size_t nrow = static_cast<size_t>(dim[0]) ;
00285         size_t ncol = static_cast<size_t>(dim[1]) ;
00286         if( i >= nrow || j >= ncol ) throw index_out_of_bounds() ;
00287         return i + nrow*j ;
00288     }
00289     
00294     size_t offset(const size_t& i) const {
00295         if( static_cast<R_len_t>(i) >= ::Rf_length(RObject::m_sexp) ) throw index_out_of_bounds() ;
00296         return i ;
00297     }
00298     
00299     R_len_t offset(const std::string& name) const {
00300         SEXP names = RCPP_GET_NAMES( RObject::m_sexp ) ;
00301         if( names == R_NilValue ) throw index_out_of_bounds(); 
00302         R_len_t n=size() ;
00303         for( R_len_t i=0; i<n; ++i){
00304             if( ! name.compare( CHAR(STRING_ELT(names, i)) ) ){
00305                 return i ;
00306             }
00307         }
00308         throw index_out_of_bounds() ;
00309         return -1 ; /* -Wall */
00310     }
00311     
00312 private:
00313         
00314     template <typename U>
00315     void fill_dispatch( traits::false_type, const U& u){
00316         // when this is not trivial, this is SEXP
00317         SEXP elem = PROTECT( converter_type::get( u ) ); 
00318         iterator it(begin());
00319         for( int i=0; i<size() ; i++, ++it){
00320             *it = ::Rf_duplicate( elem ) ;
00321         }
00322         UNPROTECT(1) ; /* elem */
00323     }
00324         
00325     template <typename U>
00326     void fill__dispatch( traits::true_type, const U& u){
00327         std::fill( begin(), end(), converter_type::get( u ) ) ;
00328     }
00329         
00330 public: 
00331         
00332     template <typename U>
00333     void fill( const U& u){
00334         fill__dispatch( typename traits::is_trivial<RTYPE>::type(), u ) ;
00335     }
00336 
00337                                                       
00338     /* TODO: 3 dimensions, ... n dimensions through variadic templates */
00339     
00340     class NamesProxy {
00341     public:
00342         NamesProxy( const Vector& v) : parent(v){} ;
00343         
00344         /* lvalue uses */              
00345         NamesProxy& operator=(const NamesProxy& rhs) {
00346             set( rhs.get() ) ;
00347             return *this ;
00348         }
00349         
00350         template <typename T>
00351         NamesProxy& operator=(const T& rhs){
00352             set( wrap(rhs) ) ;
00353             return *this ;
00354         }
00355         
00356         template <typename T> operator T() const {
00357             return Rcpp::as<T>(get()) ;
00358         }
00359                 
00360     private:
00361         const Vector& parent; 
00362                 
00363         SEXP get() const {
00364             return RCPP_GET_NAMES(parent) ;
00365         }
00366                 
00367         void set(SEXP x) const {
00368                         
00369             /* check if we can use a fast version */
00370             if( TYPEOF(x) == STRSXP && parent.size() == Rf_length(x) ){
00371                 SEXP y = const_cast<Vector&>(parent).asSexp() ; 
00372                 Rf_setAttrib( y, R_NamesSymbol, x ) ;
00373             } else {
00374                 /* use the slower and more flexible version (callback to R) */
00375                 SEXP namesSym = Rf_install( "names<-" );
00376                 SEXP new_vec = PROTECT( internal::try_catch(Rf_lang3( namesSym, parent, x ))) ;
00377                 /* names<- makes a new vector, so we have to change 
00378                    the SEXP of the parent of this proxy */
00379                 const_cast<Vector&>(parent).set_sexp( new_vec ) ;
00380                 UNPROTECT(1) ; /* new_vec */
00381             }
00382                 
00383         }
00384                 
00385     } ;
00386         
00387     NamesProxy names() const {
00388         return NamesProxy(*this) ;
00389     }
00390     
00391     inline iterator begin() const{ return cache.get() ; }
00392     inline iterator end() const{ return cache.get(size()) ; }
00393         
00394     inline Proxy operator[]( int i ){ return cache.ref(i) ; }
00395     inline Proxy operator[]( int i ) const { return cache.ref(i) ; }
00396     inline Proxy operator()( const size_t& i) {
00397         return cache.ref( offset(i) ) ;
00398     }
00399     // TODO: should it be const_Proxy
00400     inline Proxy operator()( const size_t& i) const {
00401         return cache.ref( offset(i) ) ;
00402     }
00403     inline Proxy operator()( const size_t& i, const size_t& j) {
00404         return cache.ref( offset(i,j) ) ;
00405     }
00406     // TODO: should it be const_Proxy
00407     inline Proxy operator()( const size_t& i, const size_t& j) const {
00408         return cache.ref( offset(i,j) ) ;
00409     }
00410         
00411     inline NameProxy operator[]( const std::string& name ){
00412         return NameProxy( *this, name ) ;
00413     }
00414     inline NameProxy operator()( const std::string& name ){
00415         return NameProxy( *this, name ) ;
00416     }
00417         
00418     template <typename InputIterator>
00419     void assign( InputIterator first, InputIterator last){
00420         /* FIXME: we can do better than this r_cast to avoid 
00421            allocating an unnecessary temporary object
00422         */
00423         SEXP x = PROTECT( r_cast<RTYPE>( wrap( first, last ) ) );
00424         RObject::setSEXP( x) ;
00425         UNPROTECT(1) ;
00426     }
00427 
00428     template <typename InputIterator>
00429     static Vector import( InputIterator first, InputIterator last){
00430         Vector v ;
00431         v.assign( first , last ) ;
00432         return v ;
00433     }
00434 
00435     template <typename InputIterator, typename F>
00436     static Vector import_transform( InputIterator first, InputIterator last, F f){
00437         int n = std::distance( first, last ) ;
00438         Vector v( n ) ;
00439         std::transform( first, last, v.begin(), f) ;
00440         return v ;
00441     }
00442         
00443     template <typename T>
00444     void push_back( const T& object){
00445         push_back__impl( converter_type::get(object), 
00446                          typename traits::same_type<stored_type,SEXP>()
00447                          ) ;
00448     }
00449         
00450     template <typename T>
00451     void push_back( const T& object, const std::string& name ){
00452         push_back_name__impl( converter_type::get(object), name, 
00453                               typename traits::same_type<stored_type,SEXP>()
00454                               ) ;
00455     }
00456         
00457     template <typename T>
00458     void push_front( const T& object){
00459         push_front__impl( converter_type::get(object), 
00460                           typename traits::same_type<stored_type,SEXP>() ) ;
00461     }
00462         
00463     template <typename T>
00464     void push_front( const T& object, const std::string& name){
00465         push_front_name__impl( converter_type::get(object), name, 
00466                                typename traits::same_type<stored_type,SEXP>() ) ;
00467     }
00468         
00469         
00470     template <typename T>
00471     iterator insert( iterator position, const T& object){
00472         return insert__impl( position, converter_type::get(object), 
00473                              typename traits::same_type<stored_type,SEXP>()
00474                              ) ;
00475     }
00476         
00477     template <typename T>
00478     iterator insert( int position, const T& object){
00479         return insert__impl( cache.get(position), converter_type::get(object), 
00480                              typename traits::same_type<stored_type,SEXP>()
00481                              ); 
00482     }
00483         
00484     iterator erase( int position){
00485         return erase_single__impl( cache.get(position) ) ;
00486     }
00487         
00488     iterator erase( iterator position){
00489         return erase_single__impl( position ) ;
00490     }
00491         
00492     iterator erase( int first, int last){
00493         return erase_range__impl( cache.get(first), cache.get(last) ) ;
00494     }
00495         
00496     iterator erase( iterator first, iterator last){
00497         return erase_range__impl( first, last ) ;
00498     }
00499         
00500     void update_vector(){
00501         RCPP_DEBUG_2(  " update_vector( VECTOR = %s, SEXP = < %p > )", DEMANGLE(Vector), reinterpret_cast<void*>( RObject::asSexp() ) ) ;
00502         cache.update(*this) ;
00503     }
00504                 
00505     static Vector create(){
00506         return Vector( 0 ) ;
00507     }
00508 
00509 #include <Rcpp/generated/Vector__create.h>
00510 
00511     template <typename U>
00512     static void replace_element( iterator it, SEXP names, int index, const U& u){
00513         replace_element__dispatch( typename traits::is_named<U>::type(), 
00514                                    it, names, index, u ) ;
00515     }
00516         
00517     template <typename U>
00518     static void replace_element__dispatch( traits::false_type, iterator it, SEXP names, int index, const U& u){
00519         *it = converter_type::get(u);
00520     }
00521         
00522     template <typename U>
00523     static void replace_element__dispatch( traits::true_type, iterator it, SEXP names, int index, const U& u){
00524         replace_element__dispatch__isArgument( typename traits::same_type<U,Argument>(), it, names, index, u ) ;
00525     }
00526 
00527     template <typename U>
00528     static void replace_element__dispatch__isArgument( traits::false_type, iterator it, SEXP names, int index, const U& u){
00529         RCPP_DEBUG_2( "  Vector::replace_element__dispatch<%s>(true, index= %d) ", DEMANGLE(U), index ) ; 
00530                 
00531         *it = converter_type::get(u.object ) ;
00532         SET_STRING_ELT( names, index, ::Rf_mkChar( u.name.c_str() ) ) ;
00533     }
00534         
00535     template <typename U>
00536     static void replace_element__dispatch__isArgument( traits::true_type, iterator it, SEXP names, int index, const U& u){
00537         RCPP_DEBUG_2( "  Vector::replace_element__dispatch<%s>(true, index= %d) ", DEMANGLE(U), index ) ; 
00538                 
00539         *it = R_MissingArg ;
00540         SET_STRING_ELT( names, index, ::Rf_mkChar( u.name.c_str() ) ) ;
00541     }
00542         
00543 public:
00544     void set_sexp(SEXP x){
00545         RObject::setSEXP( x) ;
00546         update_vector() ;
00547     }
00548 private:
00549         
00550     void push_back__impl(const stored_type& object, traits::true_type ) ; 
00551     void push_back__impl(const stored_type& object, traits::false_type ) ; 
00552         
00553     void push_back_name__impl(const stored_type& object, const std::string& name, traits::true_type ) ;
00554     void push_back_name__impl(const stored_type& object, const std::string& name, traits::false_type ) ;
00555         
00556     void push_front__impl(const stored_type& object, traits::true_type ) ;
00557     void push_front__impl(const stored_type& object, traits::false_type ) ;
00558         
00559     void push_front_name__impl(const stored_type& object, const std::string& name, traits::true_type ) ; 
00560     void push_front_name__impl(const stored_type& object, const std::string& name, traits::false_type ) ; 
00561         
00562     iterator insert__impl( iterator position, const stored_type& object, traits::true_type ) ;
00563     iterator insert__impl( iterator position, const stored_type& object, traits::false_type ) ;
00564                 
00565     iterator erase_single__impl( iterator position ){
00566         if( position < begin() || position >= end() ) throw index_out_of_bounds( ) ;
00567         int n = size() ;
00568         Vector target( n - 1 ) ;
00569         iterator target_it(target.begin()) ;
00570         iterator it(begin()) ;
00571         iterator this_end(end()) ;
00572         SEXP names = RCPP_GET_NAMES(RObject::m_sexp) ;
00573         if( names == R_NilValue ){
00574             for( ; it < position; ++it, ++target_it){
00575                 *target_it = *it;
00576             }
00577             iterator result(target_it) ;
00578             ++it ;
00579             for( ; it < this_end ; ++it, ++target_it){
00580                 *target_it = *it;
00581             }
00582             set_sexp( target.asSexp() ) ;
00583             return result ;
00584         } else {
00585             SEXP newnames = PROTECT(::Rf_allocVector( STRSXP, n-1 ));
00586             int i= 0 ;
00587             for( ; it < position; ++it, ++target_it,i++){
00588                 *target_it = *it;
00589                 SET_STRING_ELT( newnames, i , STRING_ELT(names,i) ) ;
00590             }
00591             iterator result(target_it) ;
00592             ++it ;
00593             i++ ;
00594             for( ; it < this_end ; ++it, ++target_it, i++){
00595                 *target_it = *it;
00596                 SET_STRING_ELT( newnames, i-1, STRING_ELT(names,i) ) ;
00597             }
00598             target.attr( "names" ) = newnames ;
00599             UNPROTECT(1) ; /* newnames */
00600             set_sexp( target.asSexp() ) ;
00601             return result ;
00602         }
00603     }
00604         
00605     iterator erase_range__impl( iterator first, iterator last ){
00606         if( first > last ) throw std::range_error("invalid range") ;
00607         if( last >= end() || first < begin() ) throw index_out_of_bounds() ;
00608                 
00609         iterator it = begin() ;
00610         iterator this_end = end() ;
00611         int nremoved = std::distance(first,last)+1 ;
00612         int target_size = size() - nremoved  ;
00613         Vector target( target_size ) ;
00614         iterator target_it = target.begin() ;
00615                 
00616         SEXP names = RCPP_GET_NAMES(RObject::m_sexp) ;
00617         iterator result ;
00618         if( names == R_NilValue ){
00619             for( ; it < first; ++it, ++target_it ){
00620                 *target_it = *it ;
00621             }
00622             result = it ;
00623             for( it = last +1 ; it < this_end; ++it, ++target_it ){
00624                 *target_it = *it ;
00625             }
00626         } else{
00627             SEXP newnames = PROTECT( ::Rf_allocVector(STRSXP, target_size) ) ;
00628             int i= 0 ;
00629             for( ; it < first; ++it, ++target_it, i++ ){
00630                 *target_it = *it ;
00631                 SET_STRING_ELT( newnames, i, STRING_ELT(names, i ) );
00632             }
00633             result = it ;
00634             for( it = last +1 ; it < this_end; ++it, ++target_it, i++ ){
00635                 *target_it = *it ;
00636                 SET_STRING_ELT( newnames, i, STRING_ELT(names, i + nremoved ) );
00637             }
00638             target.attr("names" ) = newnames ;
00639             UNPROTECT(1) ; /* newnames */
00640         }
00641         set_sexp(target.asSexp() );
00642         return result ;
00643     }
00644 protected:      
00645     void init(){
00646         internal::r_init_vector<RTYPE>(RObject::m_sexp) ;
00647     }
00648 private:
00649     virtual void update(){
00650         RCPP_DEBUG_2( "Vector<%d>::update( SEXP = <%p> )", RTYPE, RObject::asSexp() ) ;
00651         update_vector() ;
00652     }
00653         
00654     traits::r_vector_cache<RTYPE> cache ;
00655 
00656 public:
00657         
00658     typedef internal::RangeIndexer<RTYPE,Vector> Indexer ;
00659         
00660     inline Indexer operator[]( const Range& range ){
00661         return Indexer( const_cast<Vector&>(*this), range );
00662     }
00663 
00664 protected:
00665     inline int* dims() const {
00666         if( !::Rf_isMatrix(RObject::m_sexp) ) throw not_a_matrix() ;
00667         return INTEGER( ::Rf_getAttrib( RObject::m_sexp, R_DimSymbol ) ) ;
00668     }
00669 
00670         
00671 public:
00672     
00673     template <bool EXPR_NA, typename EXPR_VEC>
00674     Vector& operator+=( const VectorBase<RTYPE,EXPR_NA,EXPR_VEC>& rhs ){
00675         const EXPR_VEC& ref = rhs.get_ref() ;
00676         iterator start = begin() ;
00677         int n = size() ;
00678         // TODO: maybe unroll this
00679         stored_type tmp ;
00680         for( int i=0; i<n; i++){
00681             Proxy left = start[i] ;
00682             if( ! traits::is_na<RTYPE>( left ) ){
00683                 tmp = ref[i] ;
00684                 left = traits::is_na<RTYPE>( tmp ) ? tmp : ( left + tmp ) ;
00685             }
00686         }
00687         return *this ;
00688     }
00689     
00690     template <typename EXPR_VEC>
00691     Vector& operator+=( const VectorBase<RTYPE,false,EXPR_VEC>& rhs ){
00692         const EXPR_VEC& ref = rhs.get_ref() ;
00693         iterator start = begin() ;
00694         int n = size() ;
00695         // TODO: maybe unroll this
00696         stored_type tmp ;
00697         for( int i=0; i<n; i++){
00698             Proxy left = start[i] ;
00699             if( ! traits::is_na<RTYPE>( left ) ){
00700                 left = left + ref[i] ;
00701             }
00702         }
00703         return *this ;
00704     }
00705     
00706 } ; /* Vector */
00707 
00708 template <int RTYPE>
00709 void Vector<RTYPE>::push_back__impl(const stored_type& object, traits::false_type){
00710     int n = size() ;
00711     Vector target( n + 1 ) ;
00712     SEXP names = RCPP_GET_NAMES(RObject::m_sexp) ;
00713     iterator target_it( target.begin() ) ;
00714     iterator it(begin()) ;
00715     iterator this_end(end());
00716     if( names == R_NilValue ){
00717         for( ; it < this_end; ++it, ++target_it ){
00718             *target_it = *it ;
00719         }
00720     } else {
00721         SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n + 1) ) ;
00722         int i = 0 ;
00723         for( ; it < this_end; ++it, ++target_it, i++ ){
00724             *target_it = *it ;
00725             SET_STRING_ELT( newnames, i, STRING_ELT(names, i ) ) ;
00726         }
00727         SET_STRING_ELT( newnames, i, Rf_mkChar("") ) ;
00728         target.attr("names") = newnames ;
00729         UNPROTECT(1) ; /* newnames */
00730     }
00731     *target_it = object;
00732     set_sexp( target.asSexp() ) ;
00733 }       
00734 
00735 template <int RTYPE>
00736 void Vector<RTYPE>::push_back__impl(const stored_type& object, traits::true_type){
00737     SEXP object_sexp = PROTECT( object ) ;
00738     int n = size() ;
00739     Vector target( n + 1 ) ;
00740     SEXP names = RCPP_GET_NAMES(RObject::m_sexp) ;
00741     iterator target_it( target.begin() ) ;
00742     iterator it(begin()) ;
00743     iterator this_end(end());
00744     if( names == R_NilValue ){
00745         for( ; it < this_end; ++it, ++target_it ){
00746             *target_it = *it ;
00747         }
00748     } else {
00749         SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n + 1) ) ;
00750         int i = 0 ;
00751         for( ; it < this_end; ++it, ++target_it, i++ ){
00752             *target_it = *it ;
00753             SET_STRING_ELT( newnames, i, STRING_ELT(names, i ) ) ;
00754         }
00755         SET_STRING_ELT( newnames, i, Rf_mkChar("") ) ;
00756         target.attr("names") = newnames ;
00757         UNPROTECT(1) ; /* newnames */
00758     }
00759     *target_it = object_sexp;
00760     set_sexp( target.asSexp() ) ;
00761     UNPROTECT(1) ;
00762 }       
00763         
00764 template <int RTYPE>
00765 void Vector<RTYPE>::push_back_name__impl(const stored_type& object, const std::string& name, traits::false_type ){
00766     int n = size() ;
00767     Vector target( n + 1 ) ;
00768     iterator target_it( target.begin() ) ;
00769     iterator it(begin()) ;
00770     iterator this_end(end());
00771     SEXP names = RCPP_GET_NAMES(RObject::m_sexp) ;
00772     SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n+1 ) ) ;
00773     int i=0;
00774     if( names == R_NilValue ){
00775         SEXP dummy = PROTECT( Rf_mkChar("") );
00776         for( ; it < this_end; ++it, ++target_it,i++ ){
00777             *target_it = *it ;
00778             SET_STRING_ELT( newnames, i , dummy );
00779         }
00780         UNPROTECT(1) ; /* dummy */
00781     } else {
00782         for( ; it < this_end; ++it, ++target_it, i++ ){
00783             *target_it = *it ;
00784             SET_STRING_ELT( newnames, i, STRING_ELT(names, i ) ) ;
00785         }
00786     }
00787     SET_STRING_ELT( newnames, i, Rf_mkChar( name.c_str() ) );
00788     target.attr("names") = newnames ;
00789                 
00790     *target_it = object;
00791     UNPROTECT(1) ; /* newnames, */
00792     set_sexp( target.asSexp() ) ;
00793 }
00794         
00795 template <int RTYPE>
00796 void Vector<RTYPE>::push_back_name__impl(const stored_type& object, const std::string& name, traits::true_type ){
00797     SEXP object_sexp = PROTECT( object ) ;
00798     int n = size() ;
00799     Vector target( n + 1 ) ;
00800     iterator target_it( target.begin() ) ;
00801     iterator it(begin()) ;
00802     iterator this_end(end());
00803     SEXP names = RCPP_GET_NAMES(RObject::m_sexp) ;
00804     SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n+1 ) ) ;
00805     int i=0;
00806     if( names == R_NilValue ){
00807         SEXP dummy = PROTECT( Rf_mkChar("") );
00808         for( ; it < this_end; ++it, ++target_it,i++ ){
00809             *target_it = *it ;
00810             SET_STRING_ELT( newnames, i , dummy );
00811         }
00812         UNPROTECT(1) ; /* dummy */
00813     } else {
00814         for( ; it < this_end; ++it, ++target_it, i++ ){
00815             *target_it = *it ;
00816             SET_STRING_ELT( newnames, i, STRING_ELT(names, i ) ) ;
00817         }
00818     }
00819     SET_STRING_ELT( newnames, i, Rf_mkChar( name.c_str() ) );
00820     target.attr("names") = newnames ;
00821                 
00822     *target_it = object_sexp;
00823     UNPROTECT(2) ; /* newnames, object_sexp */
00824     set_sexp( target.asSexp() ) ;
00825 }
00826         
00827 template <int RTYPE>
00828 void Vector<RTYPE>::push_front__impl(const stored_type& object, traits::false_type ){
00829     int n = size() ;
00830     Vector target( n+1);
00831     iterator target_it(target.begin());
00832     iterator it(begin());
00833     iterator this_end(end());
00834     *target_it = object ;
00835     ++target_it ;
00836     SEXP names = RCPP_GET_NAMES(RObject::m_sexp) ;
00837     if( names == R_NilValue ){
00838         for( ; it<this_end; ++it, ++target_it){
00839             *target_it = *it ;
00840         }
00841     } else{
00842         SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n + 1) );
00843         int i=1 ;
00844         SET_STRING_ELT( newnames, 0, Rf_mkChar("") ) ;
00845         for( ; it<this_end; ++it, ++target_it, i++){
00846             *target_it = *it ;
00847             SET_STRING_ELT( newnames, i, STRING_ELT(names, i-1 ) ) ;
00848         }
00849         target.attr("names") = newnames ;
00850         UNPROTECT(1) ; /* newnames */
00851     }
00852     set_sexp( target.asSexp() ) ;
00853 }
00854         
00855 template <int RTYPE>
00856 void Vector<RTYPE>::push_front__impl(const stored_type& object, traits::true_type ){
00857     SEXP object_sexp = PROTECT( object ) ;
00858     int n = size() ;
00859     Vector target( n+1);
00860     iterator target_it(target.begin());
00861     iterator it(begin());
00862     iterator this_end(end());
00863     *target_it = object_sexp ;
00864     UNPROTECT(1); /* object_sexp */
00865     ++target_it ;
00866     SEXP names = RCPP_GET_NAMES(RObject::m_sexp) ;
00867     if( names == R_NilValue ){
00868         for( ; it<this_end; ++it, ++target_it){
00869             *target_it = *it ;
00870         }
00871     } else{
00872         SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n + 1) );
00873         int i=1 ;
00874         SET_STRING_ELT( newnames, 0, Rf_mkChar("") ) ;
00875         for( ; it<this_end; ++it, ++target_it, i++){
00876             *target_it = *it ;
00877             SET_STRING_ELT( newnames, i, STRING_ELT(names, i-1 ) ) ;
00878         }
00879         target.attr("names") = newnames ;
00880         UNPROTECT(1) ; /* newnames */
00881     }
00882     set_sexp( target.asSexp() ) ;
00883 }
00884         
00885 template <int RTYPE>
00886 void Vector<RTYPE>::push_front_name__impl(const stored_type& object, const std::string& name, traits::false_type ){
00887     int n = size() ;
00888     Vector target( n + 1 ) ;
00889     iterator target_it( target.begin() ) ;
00890     iterator it(begin()) ;
00891     iterator this_end(end());
00892     SEXP names = RCPP_GET_NAMES(RObject::m_sexp) ;
00893     SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n+1 ) ) ;
00894     int i=1;
00895     SET_STRING_ELT( newnames, 0, Rf_mkChar( name.c_str() ) );
00896     *target_it = object;
00897     ++target_it ;
00898                 
00899     if( names == R_NilValue ){
00900         SEXP dummy = PROTECT( Rf_mkChar("") );
00901         for( ; it < this_end; ++it, ++target_it,i++ ){
00902             *target_it = *it ;
00903             SET_STRING_ELT( newnames, i , dummy );
00904         }
00905         UNPROTECT(1) ; /* dummy */
00906     } else {
00907         for( ; it < this_end; ++it, ++target_it, i++ ){
00908             *target_it = *it ;
00909             SET_STRING_ELT( newnames, i, STRING_ELT(names, i-1 ) ) ;
00910         }
00911     }
00912     target.attr("names") = newnames ;
00913                 
00914     UNPROTECT(1) ; /* newnames, */
00915     set_sexp( target.asSexp() ) ;
00916 }
00917         
00918 template <int RTYPE>
00919 void Vector<RTYPE>::push_front_name__impl(const stored_type& object, const std::string& name, traits::true_type ){
00920     SEXP object_sexp = PROTECT(object) ;
00921     int n = size() ;
00922     Vector target( n + 1 ) ;
00923     iterator target_it( target.begin() ) ;
00924     iterator it(begin()) ;
00925     iterator this_end(end());
00926     SEXP names = RCPP_GET_NAMES(RObject::m_sexp) ;
00927     SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n+1 ) ) ;
00928     int i=1;
00929     SET_STRING_ELT( newnames, 0, Rf_mkChar( name.c_str() ) );
00930     *target_it = object_sexp;
00931     ++target_it ;
00932                 
00933     if( names == R_NilValue ){
00934         SEXP dummy = PROTECT( Rf_mkChar("") );
00935         for( ; it < this_end; ++it, ++target_it,i++ ){
00936             *target_it = *it ;
00937             SET_STRING_ELT( newnames, i , dummy );
00938         }
00939         UNPROTECT(1) ; /* dummy */
00940     } else {
00941         for( ; it < this_end; ++it, ++target_it, i++ ){
00942             *target_it = *it ;
00943             SET_STRING_ELT( newnames, i, STRING_ELT(names, i-1 ) ) ;
00944         }
00945     }
00946     target.attr("names") = newnames ;
00947                 
00948     UNPROTECT(2) ; /* newnames, object_sexp */
00949     set_sexp( target.asSexp() ) ;
00950 }
00951         
00952 template <int RTYPE>
00953 typename Vector<RTYPE>::iterator Vector<RTYPE>::insert__impl( iterator position, const stored_type& object, traits::false_type){
00954     int n = size() ;
00955     Vector target( n+1 ) ;
00956     iterator target_it = target.begin();
00957     iterator it = begin() ;
00958     iterator this_end = end() ;
00959     SEXP names = RCPP_GET_NAMES(RObject::m_sexp) ;
00960     iterator result ;
00961     if( names == R_NilValue ){
00962         for( ; it < position; ++it, ++target_it){
00963             *target_it = *it ;
00964         }
00965         result = target_it;
00966         *target_it = object ; 
00967         ++target_it ;
00968         for( ; it < this_end; ++it, ++target_it ){
00969             *target_it = *it ;
00970         }
00971     } else{
00972         SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n + 1 ) ) ;
00973         int i=0;
00974         for( ; it < position; ++it, ++target_it, i++){
00975             *target_it = *it ;
00976             SET_STRING_ELT( newnames, i, STRING_ELT(names, i ) ) ;
00977         }
00978         result = target_it;
00979         *target_it = object ;
00980         SET_STRING_ELT( newnames, i, ::Rf_mkChar("") ) ;
00981         i++ ;
00982         ++target_it ;
00983         for( ; it < this_end; ++it, ++target_it, i++ ){
00984             *target_it = *it ;
00985             SET_STRING_ELT( newnames, i, STRING_ELT(names, i - 1) ) ;
00986         }
00987         target.attr( "names" ) = newnames ;
00988         UNPROTECT(1) ; /* newmanes */
00989     }
00990     set_sexp( target.asSexp() );
00991     return result ;
00992 }
00993 
00994 template <int RTYPE>
00995 typename Vector<RTYPE>::iterator Vector<RTYPE>::insert__impl( iterator position, const stored_type& object, traits::true_type){
00996     PROTECT( object ) ;
00997     int n = size() ;
00998     Vector target( n+1 ) ;
00999     iterator target_it = target.begin();
01000     iterator it = begin() ;
01001     iterator this_end = end() ;
01002     SEXP names = RCPP_GET_NAMES(RObject::m_sexp) ;
01003     iterator result ;
01004     if( names == R_NilValue ){
01005         for( ; it < position; ++it, ++target_it){
01006             *target_it = *it ;
01007         }
01008         result = target_it;
01009         *target_it = object ; 
01010         ++target_it ;
01011         for( ; it < this_end; ++it, ++target_it ){
01012             *target_it = *it ;
01013         }
01014     } else{
01015         SEXP newnames = PROTECT( ::Rf_allocVector( STRSXP, n + 1 ) ) ;
01016         int i=0;
01017         for( ; it < position; ++it, ++target_it, i++){
01018             *target_it = *it ;
01019             SET_STRING_ELT( newnames, i, STRING_ELT(names, i ) ) ;
01020         }
01021         result = target_it;
01022         *target_it = object ;
01023         SET_STRING_ELT( newnames, i, ::Rf_mkChar("") ) ;
01024         i++ ;
01025         ++target_it ;
01026         for( ; it < this_end; ++it, ++target_it, i++ ){
01027             *target_it = *it ;
01028             SET_STRING_ELT( newnames, i, STRING_ELT(names, i - 1) ) ;
01029         }
01030         target.attr( "names" ) = newnames ;
01031         UNPROTECT(1) ; /* newmanes */
01032     }
01033     set_sexp( target.asSexp() );
01034     UNPROTECT(1); /* object */
01035     return result ;
01036 }
01037         
01038 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines