|
| 1 | +// Copyright (C) 2013 Romain Francois |
| 2 | +// |
| 3 | +// This file is part of Rcpp11. |
| 4 | +// |
| 5 | +// Rcpp11 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 | +// Rcpp11 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 Rcpp11. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +#ifndef Rcpp__sugar__transform_h |
| 19 | +#define Rcpp__sugar__transform_h |
| 20 | + |
| 21 | +namespace Rcpp{ |
| 22 | +namespace sugar{ |
| 23 | + |
| 24 | + template <int RTYPE, bool needs_cast, typename storage_type, typename value_type, typename result_type, typename InputIterator, typename Func > |
| 25 | + class Transform : public Rcpp::VectorBase< RTYPE, true, Transform<RTYPE,needs_cast, storage_type, value_type, result_type, InputIterator, Func> > { |
| 26 | + public: |
| 27 | + Transform( InputIterator begin, Func func_, size_t n_ ): n(n_), it(begin), func(func_) {} |
| 28 | + |
| 29 | + inline storage_type operator[]( int i ) const { |
| 30 | + return func( *(it + i) ) ; |
| 31 | + } |
| 32 | + inline int size() const { return n ; } |
| 33 | + |
| 34 | + private: |
| 35 | + size_t n ; |
| 36 | + InputIterator it ; |
| 37 | + Func func ; |
| 38 | + } ; |
| 39 | + |
| 40 | + template <int RTYPE, typename storage_type, typename value_type, typename result_type, typename InputIterator, typename Func > |
| 41 | + class Transform<RTYPE,true,storage_type,value_type,result_type,InputIterator,Func> : public Rcpp::VectorBase< RTYPE, true, Transform<RTYPE, true, storage_type, value_type, result_type, InputIterator, Func> > { |
| 42 | + public: |
| 43 | + Transform( InputIterator begin, Func func_, size_t n_ ): n(n_), it(begin), func(func_) {} |
| 44 | + |
| 45 | + inline storage_type operator[]( int i ) const { |
| 46 | + return Rcpp::internal::caster<result_type,storage_type>( func( *(it + i) ) ) ; |
| 47 | + } |
| 48 | + inline int size() const { return n ; } |
| 49 | + |
| 50 | + private: |
| 51 | + size_t n ; |
| 52 | + InputIterator it ; |
| 53 | + Func func ; |
| 54 | + } ; |
| 55 | + |
| 56 | + |
| 57 | + template <typename InputIterator, typename Func> |
| 58 | + struct import_transform_type { |
| 59 | + typedef typename std::iterator_traits<InputIterator>::value_type value_type ; |
| 60 | + typedef typename std::result_of<Func(value_type)>::type result_type ; |
| 61 | + |
| 62 | + const static int RTYPE = Rcpp::traits::r_sexptype_traits<result_type>::rtype ; |
| 63 | + typedef typename Rcpp::traits::storage_type<RTYPE>::type storage_type ; |
| 64 | + typedef typename std::is_same<result_type, storage_type>::type needs_cast_type ; |
| 65 | + |
| 66 | + typedef Transform<RTYPE,!needs_cast_type::value,storage_type,value_type, result_type, InputIterator, Func> type ; |
| 67 | + } ; |
| 68 | + |
| 69 | +} // sugar |
| 70 | + |
| 71 | +template <typename InputIterator, typename Func> |
| 72 | +inline typename sugar::import_transform_type<InputIterator,Func>::type |
| 73 | +transform( InputIterator begin, InputIterator end, Func func ){ |
| 74 | + return typename sugar::import_transform_type<InputIterator,Func>::type( begin, func, std::distance(begin, end) ) ; |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +} // Rcpp |
| 80 | +#endif |
| 81 | + |
0 commit comments