TPIE

v1.1rc1-6-g0c97303
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 
28 namespace tpie {
29 
30 namespace pipelining {
31 
32 template <typename T>
34 public:
35  typedef std::vector<T> buf_t;
36 
37  class sink_t : public node {
38  public:
39  typedef T item_type;
40 
41  inline sink_t(buf_t & buffer, const node_token & token)
42  : node(token)
43  , buffer(buffer)
44  {
45  it = buffer.begin();
46  set_name("Input items to reverse", PRIORITY_INSIGNIFICANT);
47  }
48 
49  inline void push(const T & item) {
50  *it++ = item;
51  }
52 
53  private:
54  buf_t & buffer;
55  typename buf_t::iterator it;
56  };
57 
58  template <typename dest_t>
59  class source_t : public node {
60  public:
61  typedef T item_type;
62 
63  inline source_t(const dest_t & dest, const buf_t & buffer, const node_token & sink)
64  : dest(dest)
65  , buffer(buffer)
66  , it(buffer.rbegin())
67  {
69  add_dependency(sink);
70  set_name("Output reversed items", PRIORITY_INSIGNIFICANT);
71  }
72 
73  virtual void propagate() override {
74  forward("items", static_cast<stream_size_type>(buffer.size()));
75  }
76 
77  inline void go(progress_indicator_base & pi) {
78  pi.init(buffer.size());
79  while (it != buffer.rend()) {
80  dest.push(*it++);
81  pi.step();
82  }
83  pi.done();
84  }
85 
86  private:
87  dest_t dest;
88  const buf_t & buffer;
89  typename buf_t::const_reverse_iterator it;
90 
91  source_t & operator=(const source_t & other);
92  };
93 
94  inline passive_reverser(size_t buffer_size)
95  : buffer(buffer_size)
96  {
97  }
98 
99  inline pipe_end<termfactory_2<sink_t, buf_t &, const node_token &> >
100  sink() {
101  return termfactory_2<sink_t, buf_t &, const node_token &>(buffer, sink_token);
102  }
103 
104  inline pipe_begin<factory_2<source_t, const buf_t &, const node_token &> >
105  source() {
106  return factory_2<source_t, const buf_t &, const node_token &>(buffer, sink_token);
107  }
108 
109 private:
110  buf_t buffer;
111  node_token sink_token;
112 };
113 
114 namespace bits {
115 
116 template <typename T>
117 class reverser_input_t: public node {
118 public:
119  typedef T item_type;
120 
121  inline reverser_input_t(const node_token & token)
122  : node(token)
123  {
124  set_name("Store items", PRIORITY_SIGNIFICANT);
125  set_minimum_memory(this->the_stack->memory_usage());
126  }
127 
128  virtual void propagate() override {
129  the_stack = tpie_new<stack<item_type> >();
130  forward("stack", the_stack);
131  }
132 
133  void push(const T & t) {
134  the_stack->push(t);
135  }
136 
137  stack<T> * the_stack;
138 };
139 
140 template <typename dest_t>
141 class reverser_output_t: public node {
142 public:
143  typedef typename dest_t::item_type item_type;
144 
145  reverser_output_t(const dest_t & dest, const node_token & input_token)
146  : dest(dest)
147  {
148  add_dependency(input_token);
149  add_push_destination(dest);
150  set_name("Output reversed", PRIORITY_INSIGNIFICANT);
151  set_minimum_memory(this->the_stack->memory_usage());
152  }
153 
154  virtual void propagate() override {
155  the_stack = fetch<stack<item_type> *>("stack");
156  forward("items", the_stack->size());
157  set_steps(the_stack->size());
158  }
159 
160  virtual void go() override {
161  while (!the_stack->empty()) {
162  dest.push(the_stack->pop());
163  step();
164  }
165  }
166 
167  virtual void end() override {
168  tpie_delete(the_stack);
169  }
170 
171  dest_t dest;
172  stack<item_type> * the_stack;
173 };
174 
175 
176 template <typename dest_t>
177 class reverser_t: public node {
178 public:
179  typedef typename dest_t::item_type item_type;
180 
183 
184  inline reverser_t(const dest_t & dest)
185  : input_token()
186  , input(input_token)
187  , output(dest, input_token)
188  {
189  add_push_destination(input);
190  set_name("Reverser", PRIORITY_INSIGNIFICANT);
191  }
192 
193  inline reverser_t(const reverser_t & o)
194  : node(o)
195  , input_token(o.input_token)
196  , input(o.input)
197  , output(o.output)
198  {
199  }
200 
201  void push(const item_type & i) {input.push(i);}
202 private:
203  node_token input_token;
204 
205  input_t input;
206  output_t output;
207 };
208 
209 } // namespace bits
210 
212 
213 } // namespace pipelining
214 
215 } // namespace tpie
216 
217 #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:160
virtual void propagate() override
Propagate stream metadata.
Definition: reverse.h:73
The base class for indicating the progress of some task.
const T & pop()
Pops one item from the stack.
Definition: stack.h:110
virtual void done()
Advance the indicator to the end.
An implementation of an external-memory stack.
Definition: stack.h:39
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
virtual void propagate() override
Propagate stream metadata.
Definition: reverse.h:128
stream_size_type size() const
Returns the number of items currently on the stack.
Definition: stack.h:129
void set_name(const std::string &name, priority_type priority=PRIORITY_USER)
Set this node's name.
Definition: node.h:241
void set_minimum_memory(memory_size_type minimumMemory)
Called by implementers to declare minimum memory requirements.
Definition: node.h:421
void step(stream_size_type step=1)
Record an increment to the indicator and advance the indicator.
void add_dependency(const node_token &dest)
Called by implementers to declare a node dependency, that is, a requirement that another node has end...
Definition: node.h:404
void step(stream_size_type steps=1)
Step the progress indicator.
Definition: node.h:586
bool empty() const
Returns whether the stack is empty or not.
Definition: stack.h:136
A pipe_middle class pushes input down the pipeline.
Definition: pipe_base.h:119
static memory_size_type memory_usage(float blockFactor=1.0)
Compute the memory used by a stack.
Definition: stack.h:143
void tpie_delete(T *p)
Delete an object allocated with tpie_new.
Definition: memory.h:380
void set_steps(stream_size_type steps)
Called by implementers that intend to call step().
Definition: node.h:566
void go(progress_indicator_base &pi)
Deprecated go()-implementation signature.
Definition: reverse.h:77
node()
Default constructor, using a new node_token.
Definition: node.h:296
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 end() override
End pipeline processing phase.
Definition: reverse.h:167
virtual void propagate() override
Propagate stream metadata.
Definition: reverse.h:154
External memory stack.
Node factory for 0-argument generator.
virtual void init(stream_size_type range=0)
Initialize progress indicator.