Rcpp Version 0.10.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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 
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  // TODO: check if the indexer is valid
59  RangeIndexer( VECTOR& vec_, const Rcpp::Range& range_) :
60  start(vec_.begin() + range_.get_start() ), size_( range_.size() ) {}
61 
62  // TODO: size exceptions
63  template <bool NA_, typename T>
65  UNROLL_LOOP(=)
66  }
67 
68  template <bool NA_, typename T>
70  UNROLL_LOOP(+=)
71  }
72 
73  template <bool NA_, typename T>
75  UNROLL_LOOP(*=)
76  }
77 
78  template <bool NA_, typename T>
80  UNROLL_LOOP(-=)
81  }
82 
83  template <bool NA_, typename T>
85  UNROLL_LOOP(/=)
86  }
87 
88  inline Proxy operator[]( int i ) const {
89  return start[i] ;
90  }
91 
92  inline int size() const {
93  return size_ ;
94  }
95 
96 private:
98  int size_ ;
99 } ;
100 
101 }
102 
103 #undef UNROLL_LOOP
104 
105 #endif