Skip to content

Commit 01439a3

Browse files
split import and transform
1 parent 0f66eab commit 01439a3

3 files changed

Lines changed: 82 additions & 54 deletions

File tree

inst/include/Rcpp/sugar/functions/functions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,6 @@
7777
#include <Rcpp/sugar/functions/DelayedCall.h>
7878
#include <Rcpp/sugar/functions/replicate.h>
7979
#include <Rcpp/sugar/functions/import.h>
80+
#include <Rcpp/sugar/functions/transform.h>
8081

8182
#endif

inst/include/Rcpp/sugar/functions/import.h

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -51,40 +51,6 @@ namespace sugar{
5151
InputIterator it ;
5252
} ;
5353

54-
template <int RTYPE, bool needs_cast, typename storage_type, typename value_type, typename result_type, typename InputIterator, typename Func >
55-
class ImportTransform : public Rcpp::VectorBase< RTYPE, true, ImportTransform<RTYPE,needs_cast, storage_type, value_type, result_type, InputIterator, Func> > {
56-
public:
57-
ImportTransform( InputIterator begin, Func func_, size_t n_ ): n(n_), it(begin), func(func_) {}
58-
59-
inline storage_type operator[]( int i ) const {
60-
return func( *(it + i) ) ;
61-
}
62-
inline int size() const { return n ; }
63-
64-
private:
65-
size_t n ;
66-
InputIterator it ;
67-
Func func ;
68-
} ;
69-
70-
template <int RTYPE, typename storage_type, typename value_type, typename result_type, typename InputIterator, typename Func >
71-
class ImportTransform<RTYPE,true,storage_type,value_type,result_type,InputIterator,Func> : public Rcpp::VectorBase< RTYPE, true, ImportTransform<RTYPE, true, storage_type, value_type, result_type, InputIterator, Func> > {
72-
public:
73-
ImportTransform( InputIterator begin, Func func_, size_t n_ ): n(n_), it(begin), func(func_) {}
74-
75-
inline storage_type operator[]( int i ) const {
76-
return Rcpp::internal::caster<result_type,storage_type>( func( *(it + i) ) ) ;
77-
}
78-
inline int size() const { return n ; }
79-
80-
private:
81-
size_t n ;
82-
InputIterator it ;
83-
Func func ;
84-
} ;
85-
86-
87-
8854
template <typename InputIterator>
8955
struct import_type {
9056
typedef typename std::iterator_traits<InputIterator>::value_type value_type ;
@@ -95,18 +61,6 @@ namespace sugar{
9561
typedef Import<RTYPE,! needs_cast_type::value,storage_type,value_type, InputIterator> type ;
9662
} ;
9763

98-
template <typename InputIterator, typename Func>
99-
struct import_transform_type {
100-
typedef typename std::iterator_traits<InputIterator>::value_type value_type ;
101-
typedef typename std::result_of<Func(value_type)>::type result_type ;
102-
103-
const static int RTYPE = Rcpp::traits::r_sexptype_traits<result_type>::rtype ;
104-
typedef typename Rcpp::traits::storage_type<RTYPE>::type storage_type ;
105-
typedef typename std::is_same<result_type, storage_type>::type needs_cast_type ;
106-
107-
typedef ImportTransform<RTYPE,!needs_cast_type::value,storage_type,value_type, result_type, InputIterator, Func> type ;
108-
} ;
109-
11064
} // sugar
11165

11266
template <typename InputIterator>
@@ -115,14 +69,6 @@ import( InputIterator begin, InputIterator end ){
11569
return typename sugar::import_type<InputIterator>::type( begin, std::distance(begin, end) ) ;
11670
}
11771

118-
template <typename InputIterator, typename Func>
119-
inline typename sugar::import_transform_type<InputIterator,Func>::type
120-
transform( InputIterator begin, InputIterator end, Func func ){
121-
return typename sugar::import_transform_type<InputIterator,Func>::type( begin, func, std::distance(begin, end) ) ;
122-
}
123-
124-
125-
12672
} // Rcpp
12773
#endif
12874

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)