TPIE

11a2c2d
ami_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, 2015 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_AMI_GLUE_H__
21 #define __TPIE_PIPELINING_AMI_GLUE_H__
22 
23 namespace tpie::pipelining {
24 
25 namespace bits {
26 
27 template <typename dest_t>
28 class ami_input_t : public node {
29 public:
30  typedef typename dest_t::item_type item_type;
31  ami_input_t(dest_t && dest, typename tpie::ami::stream<item_type> & stream)
32  : stream(stream), dest(std::move(dest)) {}
33 
34  void propagate() override {
35  forward("items", (stream_size_type)stream.stream_len());
36  set_steps(stream.stream_len());
37  }
38 
39  void begin() override {
40  stream.seek(0);
41  }
42 
43  void go() override {
44  item_type *item;
45  while (stream.read_item(&item) == tpie::ami::NO_ERROR) {
46  dest.push(*item);
47  step(1);
48  }
49  }
50 
51 private:
52 
53  typename tpie::ami::stream<item_type> & stream;
54  dest_t dest;
55 };
56 
57 template <typename dest_t>
58 class ami_input_stack_t : public node {
59 public:
60  typedef typename dest_t::item_type item_type;
61  ami_input_stack_t(dest_t && dest, typename tpie::ami::stack<item_type> & stack)
62  : stack(stack), dest(std::move(dest)) {}
63 
64  void propagate() override {
65  forward("items", (stream_size_type) stack.size());
66  set_steps(stack.size());
67  }
68 
69  void go() override {
70  const item_type *item;
71  while (stack.pop(&item) == tpie::ami::NO_ERROR) {
72  dest.push(*item);
73  step(1);
74  }
75  }
76 
77 private:
78 
80  dest_t dest;
81 };
82 
83 template <typename T>
84 class ami_pull_input_stack_t : public node {
85 public:
86  typedef T item_type;
88  : stack(stack) {}
89 
90  void propagate() override {
91  forward("items", (stream_size_type) stack.size());
92  set_steps(stack.size());
93  }
94 
95  bool can_pull() {
96  return !stack.is_empty();
97  }
98 
99  T pull() {
100  const item_type *item;
101  stack.pop(&item);
102  return *item;
103  }
104 
105 private:
106 
108 };
109 
115 template <typename T>
116 class ami_output_t : public node {
117 public:
118  typedef T item_type;
119 
120  ami_output_t(tpie::ami::stream<T> & stream) : stream(stream) {
121  set_name("Write", PRIORITY_INSIGNIFICANT);
122  }
123 
124  void push(const T & item) {
125  stream.write_item(item);
126  }
127 private:
128  tpie::ami::stream<T> & stream;
129 };
130 
131 } // namespace bits
132 
138 template<typename T>
142 }
143 
149 template<typename T>
150 inline pipe_begin<factory<bits::ami_input_stack_t, tpie::ami::stack<T> &> >
153 }
154 
159 template<typename T>
160 inline pullpipe_begin<termfactory<bits::pull_input_t<T>, tpie::ami::stack<T> &> >
163 }
164 
169 template <typename T>
172 }
173 
174 } // namespace tpie::pipelining
175 
176 #endif
tpie::pipelining
pipelining/factory_base.h Base class of pipelining factories
Definition: ami_glue.h:23
tpie::stack::pop
const T & pop()
Pops one item from the stack.
Definition: stack.h:113
tpie::pipelining::factory
Definition: factory_helpers.h:35
tpie::pipelining::bits::ami_input_t::go
void go() override
For initiator nodes, execute this phase by pushing all items to be pushed.
Definition: ami_glue.h:43
tpie::pipelining::bits::ami_input_stack_t::propagate
void propagate() override
Propagate stream metadata.
Definition: ami_glue.h:64
tpie::pipelining::pipe_begin
Definition: pipe_base.h:331
tpie::pipelining::pipe_end
Definition: pipe_base.h:212
tpie::pipelining::bits::ami_pull_input_stack_t::propagate
void propagate() override
Propagate stream metadata.
Definition: ami_glue.h:90
tpie::pipelining::bits::ami_input_stack_t
Definition: ami_glue.h:58
tpie::pipelining::ami_output
pipe_end< termfactory< bits::ami_output_t< T >, tpie::ami::stream< T > & > > ami_output(tpie::ami::stream< T > &fs)
A pipelining node that writes the pushed items to an ami stream.
Definition: ami_glue.h:170
tpie::pipelining::ami_pull_input_stack
pullpipe_begin< termfactory< bits::pull_input_t< T >, tpie::ami::stack< T > & > > ami_pull_input_stack(tpie::ami::stack< T > &fs)
A pipelining pull-node that reads items from the given ami::stack.
Definition: ami_glue.h:161
tpie::stack::size
stream_size_type size() const
Returns the number of items currently on the stack.
Definition: stack.h:132
tpie::pipelining::bits::ami_output_t
Definition: ami_glue.h:116
tpie::ami::NO_ERROR
@ NO_ERROR
No error occurred.
Definition: err.h:47
tpie::stack
An implementation of an external-memory stack.
Definition: stack.h:40
tpie::pipelining::bits::ami_pull_input_stack_t
Definition: ami_glue.h:84
tpie::pipelining::bits::ami_input_stack_t::go
void go() override
For initiator nodes, execute this phase by pushing all items to be pushed.
Definition: ami_glue.h:69
tpie::pipelining::node::forward
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:563
tpie::pipelining::node::set_name
void set_name(const std::string &name, priority_type priority=PRIORITY_USER)
Set this node's name.
tpie::ami::stream< item_type >
tpie::pipelining::termfactory
Definition: factory_helpers.h:78
tpie::pipelining::node::step
void step(stream_size_type steps=1)
Step the progress indicator.
Definition: node.h:653
tpie::pipelining::node::set_steps
void set_steps(stream_size_type steps)
Called by implementers that intend to call step().
tpie::pipelining::bits::ami_input_t::propagate
void propagate() override
Propagate stream metadata.
Definition: ami_glue.h:34
tpie::pipelining::input
pipe_begin< factory< bits::input_t, file_stream< T > &, stream_options > > input(file_stream< T > &fs, stream_options options=stream_options())
Pipelining nodes that pushes the contents of the given file stream to the next node in the pipeline.
Definition: file_stream.h:378
tpie::pipelining::node
Base class of all nodes.
Definition: node.h:77
tpie::pipelining::bits::ami_input_t::begin
void begin() override
Begin pipeline processing phase.
Definition: ami_glue.h:39
tpie::pipelining::bits::ami_input_t
Definition: ami_glue.h:28
tpie::pipelining::item_type
pipe_middle< tfactory< bits::item_type_t, Args< T > > > item_type()
Create item type defining identity pipe node.
Definition: helpers.h:654
tpie::ami::stack< item_type >
tpie::pipelining::ami_input
pipe_begin< factory< bits::ami_input_t, tpie::ami::stream< T > & > > ami_input(tpie::ami::stream< T > &input)
Pipelining nodes that pushes the contents of the given ami::stream to the next node in the pipeline.
Definition: ami_glue.h:140
tpie::pipelining::ami_input_stack
pipe_begin< factory< bits::ami_input_stack_t, tpie::ami::stack< T > & > > ami_input_stack(tpie::ami::stack< T > &input)
Pipelining nodes that pushes the contents of the given ami::stack to the next node in the pipeline.
Definition: ami_glue.h:151