Rcpp Version 1.0.9
LazyVector.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 //
3 // LazyVector.h: Rcpp R/C++ interface class library -- lazy vectors
4 //
5 // Copyright (C) 2010 - 2012 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__LazyVector_h
23 #define Rcpp__vector__LazyVector_h
24 
25 namespace Rcpp{
26 namespace internal{
27 
28 template <typename VECTOR>
29 class LazyVector{
30 public:
31  typedef typename VECTOR::r_type r_type ;
33 
34  LazyVector( const VECTOR& vec_ ) : vec(vec_), n(vec_.size()), data(n), known(n,false){}
35 
36  inline stored_type operator[]( R_xlen_t i) const {
37  stored_type res ;
38  if( ! known[i] ) {
39  data[i] = res = vec[i] ;
40  known[i] = true ;
41  } else {
42  res = data[i] ;
43  }
44  return res ;
45  }
46 
47 private:
48  const VECTOR& vec ;
49  R_xlen_t n ;
50  mutable std::vector<stored_type> data ;
51  mutable std::vector<bool> known ;
52 } ;
53 
54 template <int RTYPE>
55 class LazyVector< Rcpp::Vector<RTYPE> >{
56 public:
58  typedef typename VECTOR::Proxy Proxy ;
59 
60  LazyVector( const VECTOR& vec_) : vec(vec_){}
61  inline Proxy operator[]( R_xlen_t i) const { return vec[i] ; }
62 
63 private:
64  const VECTOR& vec ;
65 } ;
66 
67 
68 } // internal
69 } // Rcpp
70 #endif
traits::r_vector_proxy< RTYPE, StoragePolicy >::type Proxy
Definition: Vector.h:42
Rcpp::traits::storage_type< r_type::value >::type stored_type
Definition: LazyVector.h:32
VECTOR::r_type r_type
Definition: LazyVector.h:31
LazyVector(const VECTOR &vec_)
Definition: LazyVector.h:34
stored_type operator[](R_xlen_t i) const
Definition: LazyVector.h:36
std::vector< bool > known
Definition: LazyVector.h:51
std::vector< stored_type > data
Definition: LazyVector.h:50
Rcpp API.
Definition: algo.h:28