TPIE

11a2c2d
map.h
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2011, 2012, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
20 #ifndef __TPIE_PIPELINING_MAP_H__
21 #define __TPIE_PIPELINING_MAP_H__
22 
23 #include <tpie/pipelining/node.h>
24 #include <tpie/pipelining/pipe_base.h>
25 #include <tpie/pipelining/factory_helpers.h>
26 #include <tpie/pipelining/node_name.h>
27 #include <type_traits>
28 
29 namespace tpie::pipelining {
30 namespace bits {
31 
32 template <typename T>
34 
35 template <typename C, typename R, typename A>
36 struct unary_traits_imp<R(C::*)(A)> {
37  typedef A argument_type;
38  typedef R return_type;
39 };
40 
41 template <typename C, typename R, typename A>
42 struct unary_traits_imp<R(C::*)(A) const > {
43  typedef A argument_type;
44  typedef R return_type;
45 };
46 
47 template <typename R, typename A>
48 struct unary_traits_imp<R(*)(A)> {
49  typedef A argument_type;
50  typedef R return_type;
51 };
52 
53 template <typename T>
54 struct unary_traits: public unary_traits_imp<decltype(&T::operator()) > {};
55 
56 template <typename dest_t, typename F>
57 class map_t: public node {
58 private:
59  F functor;
60  dest_t dest;
61 public:
62  typedef typename std::decay<typename unary_traits<F>::argument_type>::type item_type;
63 
64  map_t(dest_t dest, const F & functor):
65  functor(functor), dest(std::move(dest)) {
66  set_name(bits::extract_pipe_name(typeid(F).name()), PRIORITY_NO_NAME);
67  }
68 
69  void push(const item_type & item) {
70  dest.push(functor(item));
71  }
72 };
73 
74 template <typename dest_t, typename F>
75 class map_temp_t: public node {
76 private:
77  F functor;
78  dest_t dest;
79 public:
80  map_temp_t(dest_t dest, const F & functor):
81  functor(functor), dest(std::move(dest)) {
82  set_name(bits::extract_pipe_name(typeid(F).name()), PRIORITY_NO_NAME);
83  }
84 
85  template <typename T>
86  void push(const T & item) {
87  dest.push(functor(item));
88  }
89 };
90 
91 template <typename F>
92 class map_sink_t: public node {
93 private:
94  F functor;
95 public:
96  typedef typename std::decay<typename unary_traits<F>::argument_type>::type item_type;
97 
98  map_sink_t(const F & functor):
99  functor(functor) {
100  set_name(bits::extract_pipe_name(typeid(F).name()), PRIORITY_NO_NAME);
101  }
102 
103  void push(const item_type & item) {
104  functor(item);
105  }
106 };
107 
108 template <typename src_t, typename F>
109 class pull_map_t: public node {
110 private:
111  F functor;
112  src_t src;
113 public:
114  pull_map_t(src_t src, const F & functor):
115  functor(functor), src(std::move(src)) {
116  set_name(bits::extract_pipe_name(typeid(F).name()), PRIORITY_NO_NAME);
117  }
118 
119  auto pull() {
120  return functor(src.pull());
121  }
122 
123  bool can_pull() {
124  return src.can_pull();
125  }
126 };
127 
128 
129 template <typename T>
131  typedef char yes[1];
132  typedef char no[2];
133 
134  // This does not seem to work as well as it should
135  // template <typename C>
136  // static yes& test(typename unary_traits_imp<decltype(&C::operator())>::argument_type *);
137 
138  template <typename C>
139  static yes& test(decltype(&C::operator()) *);
140 
141  template <typename>
142  static no& test(...);
143  static const bool value = sizeof(test<T>(nullptr)) == sizeof(yes);
144 };
145 
146 } //namespace bits
147 
153 template <typename F, typename = typename std::enable_if<bits::has_argument_type<F>::value>::type>
155  return {functor};
156 }
157 
158 template <typename F, typename = typename std::enable_if<!bits::has_argument_type<F>::value>::type>
159 pipe_middle<tfactory<bits::map_temp_t, Args<F>, F> > map(const F & functor) {
160  return {functor};
161 }
162 
163 template <typename F>
164 pipe_end<termfactory<bits::map_sink_t<F>, F> > map_sink(const F & functor) {
165  return {functor};
166 }
167 
168 template <typename F>
169 pullpipe_middle<tfactory<bits::pull_map_t, Args<F>, F>> pull_map(const F & functor) {
170  return {functor};
171 }
172 
173 } //namespace terrastream::pipelining
174 
175 #endif //__TPIE_PIPELINING_MAP_H__
tpie::pipelining
pipelining/factory_base.h Base class of pipelining factories
Definition: ami_glue.h:23
tpie::pipelining::bits::map_sink_t
Definition: map.h:92
tpie::pipelining::bits::has_argument_type
Definition: map.h:130
tpie::pipelining::map
pipe_middle< tfactory< bits::map_t, Args< F >, F > > map(const F &functor)
Pipelining nodes that applies to given functor to items in the stream.
Definition: map.h:154
tpie::pipelining::bits::map_temp_t
Definition: map.h:75
tpie::pipelining::node::set_name
void set_name(const std::string &name, priority_type priority=PRIORITY_USER)
Set this node's name.
tpie::pipelining::pipe_middle
Definition: pipe_base.h:243
tpie::pipelining::bits::unary_traits
Definition: map.h:54
tpie::pipelining::bits::unary_traits_imp
Definition: map.h:33
tpie::pipelining::bits::pull_map_t
Definition: map.h:109
tpie::pipelining::node
Base class of all nodes.
Definition: node.h:77
tpie::pipelining::bits::map_t
Definition: map.h:57