TPIE

2362a60
reverse.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_REVERSE_H__
21 #define __TPIE_PIPELINING_REVERSE_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/stack.h>
27 #include <stack>
28 
29 namespace tpie {
30 
31 namespace pipelining {
32 
33 namespace bits {
34 
38 template <typename T>
39 class reverser_input_t: public node {
40 public:
41  typedef T item_type;
42 
43  inline reverser_input_t(const node_token & token, std::shared_ptr<node> output=std::shared_ptr<node>())
44  : node(token), m_output(output)
45  {
46  set_name("Store items", PRIORITY_SIGNIFICANT);
49  set_plot_options(PLOT_BUFFERED | PLOT_SIMPLIFIED_HIDE);
50  }
51 
52  void begin() override {
53  m_stack.construct();
54  }
55 
59  void push(const item_type & t) {
60  m_stack->push(t);
61  }
62 
63  void end() override {
64  forward("stack", &m_stack, 1);
65  }
66 private:
68  std::shared_ptr<node> m_output;
69 };
70 
74 
75 template <typename T>
77 public:
78  typedef T item_type;
79 
80  inline internal_reverser_input_t(const node_token & token, std::shared_ptr<node> output=std::shared_ptr<node>())
81  : node(token), m_output(output)
82  {
83  set_name("Store items", PRIORITY_SIGNIFICANT);
84  set_minimum_memory(sizeof(std::stack<item_type>));
85  set_plot_options(PLOT_BUFFERED | PLOT_SIMPLIFIED_HIDE);
86  }
87 
88  virtual void propagate() override {
89  m_stack = tpie_new<std::stack<item_type> >();
90  forward("stack", m_stack, 1);
91  }
92 
96  void push(const item_type & t) {
97  m_stack->push(t);
98  }
99 private:
100  std::stack<item_type> * m_stack;
101  std::shared_ptr<node> m_output;
102 };
103 
107 template <typename dest_t>
108 class reverser_output_t: public node {
109 public:
110  typedef typename push_type<dest_t>::type item_type;
111 
112  reverser_output_t(dest_t dest, const node_token & input_token)
113  : dest(std::move(dest))
114  {
115  add_dependency(input_token);
116  add_push_destination(this->dest);
117  set_name("Output reversed", PRIORITY_INSIGNIFICANT);
118  set_minimum_memory(this->m_stack->memory_usage());
119  set_minimum_resource_usage(FILES, 1);
120  set_plot_options(PLOT_BUFFERED);
121  }
122 
123  void propagate() override {
124  m_stack_ptr = fetch<tpie::maybe<stack<item_type> > *>("stack");
125  m_stack = &**m_stack_ptr;
126  forward("items", m_stack->size());
127  set_steps(m_stack->size());
128  }
129 
130  void go() override {
131  while (!m_stack->empty()) {
132  dest.push(m_stack->pop());
133  step();
134  }
135  }
136 
137  void end() override {
138  m_stack_ptr->destruct();
139  }
140 private:
141  dest_t dest;
142  tpie::maybe<stack<item_type> > * m_stack_ptr;
143  stack<item_type> * m_stack;
144 };
145 
149 template <typename dest_t>
151 public:
152  typedef typename push_type<dest_t>::type item_type;
153 
154  internal_reverser_output_t(dest_t dest, const node_token & input_token)
155  : dest(std::move(dest))
156  {
157  add_dependency(input_token);
158  add_push_destination(this->dest);
159  set_name("Output reversed", PRIORITY_INSIGNIFICANT);
160  set_minimum_memory(sizeof(std::stack<item_type>));
161  set_plot_options(PLOT_BUFFERED);
162  }
163 
164  virtual void propagate() override {
165  m_stack = fetch<std::stack<item_type> *>("stack");
166  forward("items", m_stack->size());
167  set_steps(m_stack->size());
168  }
169 
170  virtual void go() override {
171  while (!m_stack->empty()) {
172  dest.push(m_stack->pop());
173  step();
174  }
175  }
176 
177  virtual void end() override {
178  tpie_delete(m_stack);
179  }
180 private:
181  dest_t dest;
182  std::stack<item_type> * m_stack;
183 };
184 
188 template <typename T>
189 class reverser_pull_output_t : public node {
190 public:
191  typedef T item_type;
192 
193  reverser_pull_output_t(const node_token & input_token) {
194  add_dependency(input_token);
195  set_name("Input items to reverse", PRIORITY_INSIGNIFICANT);
197  set_plot_options(PLOT_BUFFERED);
198  }
199 
200  void propagate() override {
201  m_stack_ptr = fetch<tpie::maybe<stack<item_type> > *>("stack");
202  m_stack = &**m_stack_ptr;
203  forward("items", m_stack->size());
204  }
205 
209  bool can_pull() const {
210  return !m_stack->empty();
211  }
212 
216  T pull() {
217  return m_stack->pop();
218  }
219 
220  void end() override {
221  m_stack_ptr->destruct();
222  }
223 private:
224  tpie::maybe<stack<item_type> > * m_stack_ptr;
225  stack<item_type> * m_stack;
226 };
227 
231 template <typename T>
233 public:
234  typedef T item_type;
235 
236  internal_reverser_pull_output_t(const node_token & input_token) {
237  add_dependency(input_token);
238  set_name("Input items to reverse", PRIORITY_INSIGNIFICANT);
239  set_minimum_memory(sizeof(std::stack<item_type>));
240  set_plot_options(PLOT_BUFFERED);
241  }
242 
243  virtual void propagate() override {
244  m_stack = fetch<std::stack<item_type> *>("stack");
245  forward("items", m_stack->size());
246  }
247 
251  bool can_pull() const {
252  return !m_stack->empty();
253  }
254 
258  T pull() {
259  T r = m_stack->top();
260  m_stack->pop();
261  return r;
262  }
263 
264  virtual void end() override {
265  tpie_delete(m_stack);
266  }
267 private:
268  std::stack<item_type> * m_stack;
269 };
270 
271 } // namespace bits
272 
277 template <typename T>
279 public:
280  typedef T item_type;
283 private:
288 public:
289  passive_reverser() {}
290 
291  inline input_t raw_input() {
292  return input_t(input_token);
293  }
294 
295  inline output_t raw_output() {
296  return output_t(input_token);
297  }
298 
302  inline inputpipe_t input() {
303  return inputfact_t(input_token);
304  }
305 
309  inline outputpipe_t output() {
310  return outputfact_t(input_token);
311  }
312 private:
313  node_token input_token;
314 
316  passive_reverser & operator=(const passive_reverser &);
317 };
318 
319 
324 template <typename T>
326 public:
327  typedef T item_type;
330 private:
335 public:
337 
338  inline input_t raw_input() {
339  return input_t(input_token);
340  }
341 
342  inline output_t raw_output() {
343  return output_t(input_token);
344  }
345 
349  inline inputpipe_t input() {
350  return inputfact_t(input_token);
351  }
352 
356  inline outputpipe_t output() {
357  return outputfact_t(input_token);
358  }
359 private:
360  node_token input_token;
361 
364 };
365 
371 
377 
378 } // namespace pipelining
379 
380 } // namespace tpie
381 
382 #endif // __TPIE_PIPELINING_REVERSE_H__
virtual void go() override
For initiator nodes, execute this phase by pushing all items to be pushed.
Definition: reverse.h:170
Output node for reverser stored in internal memory.
Definition: reverse.h:150
bool can_pull() const
Whether an item can be pulled from the node.
Definition: reverse.h:251
virtual void end() override
End pipeline processing phase.
Definition: reverse.h:264
pipe_middle< split_factory< bits::internal_reverser_input_t, node, bits::internal_reverser_output_t > > internal_reverser
Constructs a reverser node stored in internal memory.
Definition: reverse.h:376
void push(const item_type &t)
Pushes an item to the node.
Definition: reverse.h:59
Output node for reverser stored in external memory.
Definition: reverse.h:108
A passive reverser stored in external memory.
Definition: reverse.h:278
outputpipe_t output()
Returns a termfactory for the output nodes.
Definition: reverse.h:309
const T & pop()
Pops one item from the stack.
Definition: stack.h:113
pipe_middle< split_factory< bits::reverser_input_t, node, bits::reverser_output_t > > reverser
Constructs a reverser node stored in external memory.
Definition: reverse.h:370
input node for reverser stored in external memory
Definition: reverse.h:39
An implementation of an external-memory stack.
Definition: stack.h:40
void end() override
End pipeline processing phase.
Definition: reverse.h:137
void go() override
For initiator nodes, execute this phase by pushing all items to be pushed.
Definition: reverse.h:130
T pull()
Pulls an item from the node.
Definition: reverse.h:216
Base class of all nodes.
Definition: node.h:78
inputpipe_t input()
Returns a termfactory for the input nodes.
Definition: reverse.h:349
void propagate() override
Propagate stream metadata.
Definition: reverse.h:123
void add_push_destination(const node_token &dest)
Called by implementers to declare a push destination.
Class to deduce the item_type of a node of type T.
Definition: node_traits.h:152
virtual void propagate() override
Propagate stream metadata.
Definition: reverse.h:164
virtual void propagate() override
Propagate stream metadata.
Definition: reverse.h:243
void forward(std::string key, T value, memory_size_type k=std::numeric_limits< memory_size_type >::max())
Called by implementers to forward auxiliary data to successors.
Definition: node.h:564
void end() override
End pipeline processing phase.
Definition: reverse.h:63
void set_name(const std::string &name, priority_type priority=PRIORITY_USER)
Set this node's name.
void set_minimum_resource_usage(resource_type type, memory_size_type usage)
Called by implementers to declare minimum resource requirements.
void set_minimum_memory(memory_size_type minimumMemory)
Called by implementers to declare minimum memory requirements.
Definition: node.h:207
Node factory for variadic argument terminators.
void set_plot_options(flags< PLOT > options)
Set options specified for plot(), as a combination of node::PLOT values.
Definition: node.h:459
void begin() override
Begin pipeline processing phase.
Definition: reverse.h:52
void add_dependency(const node_token &dest)
Called by implementers to declare a node dependency, that is, a requirement that another node has end...
void propagate() override
Propagate stream metadata.
Definition: reverse.h:200
void end() override
End pipeline processing phase.
Definition: reverse.h:220
void step(stream_size_type steps=1)
Step the progress indicator.
Definition: node.h:654
A passive reverser stored in internal memory.
Definition: reverse.h:325
bool empty() const
Returns whether the stack is empty or not.
Definition: stack.h:139
Output node for passive reverser stored in external memory.
Definition: reverse.h:189
A pipe_middle class pushes input down the pipeline.
Definition: pipe_base.h:241
Output node for passive reverser stored in internal memory.
Definition: reverse.h:232
static memory_size_type memory_usage(float blockFactor=1.0)
Compute the memory used by a stack.
Definition: stack.h:146
void tpie_delete(T *p)
Delete an object allocated with tpie_new.
Definition: memory.h:301
pipe_end< termfactory< bits::output_t< T >, file_stream< T > & > > output(file_stream< T > &fs)
A pipelining node that writes the pushed items to a file stream.
Definition: file_stream.h:431
input node for reverser stored in internal memory
Definition: reverse.h:76
void set_steps(stream_size_type steps)
Called by implementers that intend to call step().
outputpipe_t output()
Returns a termfactory for the output nodes.
Definition: reverse.h:356
node()
Default constructor, using a new node_token.
void push(const item_type &t)
Pushes an item to the node.
Definition: reverse.h:96
inputpipe_t input()
Returns a termfactory for the input nodes.
Definition: reverse.h:302
virtual void propagate() override
Propagate stream metadata.
Definition: reverse.h:88
bool can_pull() const
Whether an item can be pulled from the node.
Definition: reverse.h:209
External memory stack.
virtual void end() override
End pipeline processing phase.
Definition: reverse.h:177