Rcpp Version 1.0.9
DottedPairImpl.h
Go to the documentation of this file.
1 // Copyright (C) 2013 Romain Francois
2 //
3 // This file is part of Rcpp.
4 //
5 // Rcpp is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // Rcpp is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
17 
18 #ifndef Rcpp_api_meat_DottedPairImpl_h
19 #define Rcpp_api_meat_DottedPairImpl_h
20 
21 namespace Rcpp{
22 
23  template <typename CLASS>
24  template <typename T>
25  void DottedPairImpl<CLASS>::push_front( const T& object){
26  CLASS& ref = static_cast<CLASS&>(*this) ;
27  ref.set__( grow(object, ref.get__() ) ) ;
28  }
29 
30  template <typename CLASS>
31  template <typename T>
32  void DottedPairImpl<CLASS>::push_back( const T& object){
33  CLASS& ref = static_cast<CLASS&>(*this) ;
34  if( ref.isNULL() ){
35  ref.set__( grow( object, ref.get__() ) ) ;
36  } else {
37  SEXP x = ref.get__() ;
38  /* traverse the pairlist */
39  while( !Rf_isNull(CDR(x)) ){
40  x = CDR(x) ;
41  }
42  Shield<SEXP> tail( grow( object, R_NilValue ) );
43  SETCDR( x, tail ) ;
44  }
45  }
46 
47  template <typename CLASS>
48  template <typename T>
49  void DottedPairImpl<CLASS>::insert( const size_t& index, const T& object) {
50  CLASS& ref = static_cast<CLASS&>(*this) ;
51  if( index == 0 ) {
52  push_front( object ) ;
53  } else {
54  if( ref.isNULL( ) ) {
55  throw index_out_of_bounds("Object being inserted into Dotted Pair is null.");
56  }
57 
58  if( static_cast<R_xlen_t>(index) > ::Rf_xlength(ref.get__()) ) {
59  const char* fmt = "Dotted Pair index is out of bounds: "
60  "[index=%i; extent=%i].";
61 
62  throw index_out_of_bounds(fmt,
63  static_cast<R_xlen_t>(index),
64  ::Rf_xlength(ref.get__()) ) ;
65  }
66 
67  size_t i=1;
68  SEXP x = ref.get__() ;
69  while( i < index ){
70  x = CDR(x) ;
71  i++;
72  }
73  Shield<SEXP> tail( grow( object, CDR(x) ) );
74  SETCDR( x, tail ) ;
75  }
76  }
77 
78  template <typename CLASS>
79  template <typename T>
80  void DottedPairImpl<CLASS>::replace( const int& index, const T& object ) {
81  CLASS& ref = static_cast<CLASS&>(*this) ;
82  if( static_cast<R_xlen_t>(index) >= ::Rf_xlength(ref.get__()) ) {
83  const char* fmt = "Dotted Pair index is out of bounds: "
84  "[index=%i; extent=%i].";
85 
86  throw index_out_of_bounds(fmt,
87  static_cast<R_xlen_t>(index),
88  ::Rf_xlength(ref.get__()) ) ;
89  }
90 
91  Shield<SEXP> x( pairlist( object ) );
92  SEXP y = ref.get__() ;
93  int i=0;
94  while( i<index ){ y = CDR(y) ; i++; }
95 
96  SETCAR( y, CAR(x) );
97  SET_TAG( y, TAG(x) );
98  }
99 
100  template <typename CLASS>
101  void DottedPairImpl<CLASS>::remove( const size_t& index ) {
102  CLASS& ref = static_cast<CLASS&>(*this) ;
103  if( static_cast<R_xlen_t>(index) >= Rf_xlength(ref.get__()) ) {
104  const char* fmt = "Dotted Pair index is out of bounds: "
105  "[index=%i; extent=%i].";
106 
107  throw index_out_of_bounds(fmt,
108  static_cast<R_xlen_t>(index),
109  ::Rf_xlength(ref.get__()) ) ;
110  }
111 
112  if( index == 0 ){
113  ref.set__( CDR( ref.get__() ) ) ;
114  } else{
115  SEXP x = ref.get__() ;
116  size_t i=1;
117  while( i<index ){ x = CDR(x) ; i++; }
118  SETCDR( x, CDDR(x) ) ;
119  }
120  }
121 
122 
123 
124 } // namespace Rcpp
125 
126 #endif
void push_back(const T &object)
void replace(const int &index, const T &object)
void remove(const size_t &index)
void insert(const size_t &index, const T &object)
void push_front(const T &object)
Rcpp API.
Definition: algo.h:28
SEXP pairlist()
Definition: grow.h:30
StretchyList_Impl & push_front(const T &obj)
Definition: StretchyList.h:63
sugar::Tail< RTYPE, NA, T > tail(const VectorBase< RTYPE, NA, T > &t, R_xlen_t n)
Definition: tail.h:56
SEXP grow(SEXP head, SEXP tail)
Definition: grow.h:34