|
Rcpp Version 0.9.10
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- 00002 // 00003 // LazyVector.h: Rcpp R/C++ interface class library -- lazy vectors 00004 // 00005 // Copyright (C) 2010 - 2011 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__LazyVector_h 00023 #define Rcpp__vector__LazyVector_h 00024 00025 namespace internal{ 00026 00027 template <typename VECTOR> 00028 class LazyVector{ 00029 public: 00030 typedef typename VECTOR::r_type r_type ; 00031 typedef typename Rcpp::traits::storage_type< r_type::value >::type stored_type ; 00032 00033 LazyVector( const VECTOR& vec_ ) : vec(vec_), n(vec_.size()), data(n), known(n,false){} 00034 00035 inline stored_type operator[]( int i) const { 00036 stored_type res ; 00037 if( ! known[i] ) { 00038 data[i] = res = vec[i] ; 00039 known[i] = true ; 00040 } else { 00041 res = data[i] ; 00042 } 00043 return res ; 00044 } 00045 00046 private: 00047 const VECTOR& vec ; 00048 int n ; 00049 mutable std::vector<stored_type> data ; 00050 mutable std::vector<bool> known ; 00051 } ; 00052 00053 template <int RTYPE> 00054 class LazyVector< Rcpp::Vector<RTYPE> >{ 00055 public: 00056 typedef Rcpp::Vector<RTYPE> VECTOR ; 00057 typedef typename VECTOR::Proxy Proxy ; 00058 00059 LazyVector( const VECTOR& vec_) : vec(vec_){} 00060 inline Proxy operator[]( int i) const { return vec[i] ; } 00061 00062 private: 00063 const VECTOR& vec ; 00064 } ; 00065 00066 00067 } // internal 00068 00069 #endif