Rcpp Version 0.12.12
RangeIndexer.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // RangeIndexer.h: Rcpp R/C++ interface class library --
4 //
5 // Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
6 //
7 // This file is part of Rcpp.
8 //
9 // Rcpp is free software: you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Rcpp is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21 
22 #ifndef Rcpp__vector__RangeIndexer_h
23 #define Rcpp__vector__RangeIndexer_h
24 
25 #define UNROLL_LOOP(OP) \
26  typedef typename ::Rcpp::traits::Extractor<RTYPE,NA,T>::type EXT ; \
27  const EXT& input( x.get_ref() ) ; \
28  int __trip_count = (size_) >> 2; \
29  int i=0 ; \
30  for ( ; __trip_count > 0 ; --__trip_count) { \
31  start[i] OP input[i] ; i++ ; \
32  start[i] OP input[i] ; i++ ; \
33  start[i] OP input[i] ; i++ ; \
34  start[i] OP input[i] ; i++ ; \
35  } \
36  switch (size_ - i){ \
37  case 3: \
38  start[i] OP input[i] ; i++ ; \
39  case 2: \
40  start[i] OP input[i] ; i++ ; \
41  case 1: \
42  start[i] OP input[i] ; i++ ; \
43  case 0: \
44  default: \
45  return *this ; \
46  }
47 
48 
49 namespace Rcpp{
50 namespace internal{
51 
52 template <int RTYPE, bool NA, typename VECTOR>
53 class RangeIndexer : public VectorBase<RTYPE, NA, RangeIndexer<RTYPE,NA,VECTOR> > {
54 public:
55  typedef typename VECTOR::Proxy Proxy ;
56  typedef typename VECTOR::iterator iterator ;
57 
58  RangeIndexer( VECTOR& vec_, const Rcpp::Range& range_) :
59  start(vec_.begin() + range_.get_start() ), size_( range_.size() ) {}
60 
61  template <bool NA_, typename T>
63  UNROLL_LOOP(=)
64  }
65 
66  template <bool NA_, typename T>
68  UNROLL_LOOP(+=)
69  }
70 
71  template <bool NA_, typename T>
73  UNROLL_LOOP(*=)
74  }
75 
76  template <bool NA_, typename T>
78  UNROLL_LOOP(-=)
79  }
80 
81  template <bool NA_, typename T>
83  UNROLL_LOOP(/=)
84  }
85 
86  inline Proxy operator[]( R_xlen_t i ) const {
87  return start[i] ;
88  }
89 
90  inline R_xlen_t size() const {
91  return size_ ;
92  }
93 
94 private:
95  iterator start ;
96  R_xlen_t size_ ;
97 } ;
98 
99 }
100 }
101 
102 #undef UNROLL_LOOP
103 
104 #endif
Proxy operator[](R_xlen_t i) const
Definition: RangeIndexer.h:86
RangeIndexer(VECTOR &vec_, const Rcpp::Range &range_)
Definition: RangeIndexer.h:58
RangeIndexer & operator/=(const Rcpp::VectorBase< RTYPE, NA_, T > &x)
Definition: RangeIndexer.h:82
VECTOR::iterator iterator
Definition: RangeIndexer.h:56
RangeIndexer & operator+=(const Rcpp::VectorBase< RTYPE, NA_, T > &x)
Definition: RangeIndexer.h:67
#define UNROLL_LOOP(OP)
Definition: RangeIndexer.h:25
RangeIndexer & operator-=(const Rcpp::VectorBase< RTYPE, NA_, T > &x)
Definition: RangeIndexer.h:77
R_xlen_t size() const
Definition: RangeIndexer.h:90
RangeIndexer & operator=(const Rcpp::VectorBase< RTYPE, NA_, T > &x)
Definition: RangeIndexer.h:62
RangeIndexer & operator*=(const Rcpp::VectorBase< RTYPE, NA_, T > &x)
Definition: RangeIndexer.h:72
Rcpp API.
Definition: algo.h:28