TPIE

v1.1rc1-6-g0c97303
std_glue.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_STD_GLUE_H__
21 #define __TPIE_PIPELINING_STD_GLUE_H__
22 
23 #include <vector>
24 
25 #include <tpie/pipelining/node.h>
26 #include <tpie/pipelining/pipe_base.h>
27 #include <tpie/pipelining/factory_helpers.h>
28 
29 namespace tpie {
30 
31 namespace pipelining {
32 
33 namespace bits {
34 
35 template <typename dest_t>
36 class input_vector_t : public node {
37 public:
38  typedef typename dest_t::item_type item_type;
39 
40  inline input_vector_t(const dest_t & dest, const std::vector<item_type> & input) : dest(dest), input(input) {
42  set_name("Read", PRIORITY_INSIGNIFICANT);
43  }
44 
45  virtual void propagate() override {
46  forward("items", static_cast<stream_size_type>(input.size()));
47  set_steps(input.size());
48  }
49 
50  virtual void go() override {
51  typedef typename std::vector<item_type>::const_iterator IT;
52  for (IT i = input.begin(); i != input.end(); ++i) {
53  dest.push(*i);
54  step();
55  }
56  }
57 private:
58  dest_t dest;
59  const std::vector<item_type> & input;
60 };
61 
62 template <typename T>
63 class output_vector_t : public node {
64 public:
65  typedef T item_type;
66 
67  inline output_vector_t(std::vector<T> & output) : output(output) {
68  set_name("Write", PRIORITY_INSIGNIFICANT);
69  }
70 
71  inline void push(const T & item) {
72  output.push_back(item);
73  }
74 private:
75  std::vector<item_type> & output;
76 };
77 
78 template <typename F>
79 class lambda_t {
80 public:
81  template <typename dest_t>
82  class type: public node {
83  public:
84  typedef typename F::argument_type item_type;
85 
86  type(const dest_t & dest, const F & f): f(f), dest(dest) {
87  set_name("Lambda", PRIORITY_INSIGNIFICANT);
88  }
89 
90  void push(const item_type & item) {
91  dest.push(f(item));
92  }
93  private:
94  F f;
95  dest_t dest;
96  };
97 };
98 
99 template <typename F>
101 public:
102  template <typename dest_t>
103  class type: public node {
104  public:
105  typedef typename F::argument_type item_type;
106 
107  type(const dest_t & dest, const F & f): f(f), dest(dest) {
108  set_name("Lambda", PRIORITY_INSIGNIFICANT);
109  }
110 
111  void push(const item_type & item) {
112  typename F::result_type t=f(item);
113  if (t.second) dest.push(t.first);
114  }
115  private:
116  F f;
117  dest_t dest;
118  };
119 };
120 
121 
122 } // namespace bits
123 
124 template<typename T>
125 inline pipe_begin<factory_1<bits::input_vector_t, const std::vector<T> &> > input_vector(const std::vector<T> & input) {
127 }
128 
129 template <typename T>
130 inline pipe_end<termfactory_1<bits::output_vector_t<T>, std::vector<T> &> > output_vector(std::vector<T> & output) {
131  return termfactory_1<bits::output_vector_t<T>, std::vector<T> &>(output);
132 }
133 
134 template <typename F>
135 inline pipe_middle<tempfactory_1<bits::lambda_t<F>, F> > lambda(const F & f) {
136  return tempfactory_1<bits::lambda_t<F>, F>(f);
137 }
138 
139 template <typename F>
140 inline pipe_middle<tempfactory_1<bits::exclude_lambda_t<F>, F> > exclude_lambda(const F & f) {
141  return tempfactory_1<bits::exclude_lambda_t<F>, F>(f);
142 }
143 
144 
145 } // namespace pipelining
146 
147 } // namespace tpie
148 
149 #endif
virtual void go() override
For initiator nodes, execute this phase by pushing all items to be pushed.
Definition: std_glue.h:50
Base class of all nodes.
Definition: node.h:58
void add_push_destination(const node_token &dest)
Called by implementers to declare a push destination.
Definition: node.h:352
void set_name(const std::string &name, priority_type priority=PRIORITY_USER)
Set this node's name.
Definition: node.h:241
Node factory for 1-argument generator.
void step(stream_size_type steps=1)
Step the progress indicator.
Definition: node.h:586
void set_steps(stream_size_type steps)
Called by implementers that intend to call step().
Definition: node.h:566
void forward(std::string key, T value, bool explicitForward=true)
Called by implementers to forward auxiliary data to successors.
Definition: node.h:461
virtual void propagate() override
Propagate stream metadata.
Definition: std_glue.h:45