TPIE

11a2c2d
numeric.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_NUMERIC_H__
21 #define __TPIE_PIPELINING_NUMERIC_H__
22 
23 #include <iostream>
24 #include <tpie/pipelining/node.h>
25 #include <tpie/pipelining/pipe_base.h>
26 #include <tpie/pipelining/factory_helpers.h>
27 
28 namespace tpie::pipelining {
29 namespace bits {
30 
31 template <typename dest_t>
32 class linear_t : public node {
33 public:
34  typedef typename push_type<dest_t>::type item_type;
35 
36  linear_t(dest_t dest, item_type factor, item_type term) : factor(factor), term(term), dest(std::move(dest)) {
37  add_push_destination(this->dest);
38  set_name("Linear transform", PRIORITY_INSIGNIFICANT);
39  }
40  void push(const item_type & item) {
41  dest.push(item*factor+term);
42  }
43 private:
44  item_type factor, term;
45  dest_t dest;
46 };
47 
48 template <typename dest_t>
49 class range_t : public node {
50 public:
51  typedef typename push_type<dest_t>::type item_type;
52 
53  range_t(dest_t dest, item_type from, item_type to, item_type increment) : from(from), to(to), increment(increment), dest(std::move(dest)) {}
54 
55  void propagate() {
56  stream_size_type items = (from - to) / increment;
57  set_steps(items);
58  forward("items", items);
59  }
60 
61  void go() override {
62  for (item_type i=from; i < to; i += increment) {
63  dest.push(i);
64  step(1);
65  }
66  }
67 
68 private:
69  item_type from, to, increment;
70  dest_t dest;
71 };
72 
73 } // namespace bits
74 
81 template <typename T>
82 inline pipe_middle<factory<bits::linear_t, T, T> >
83 linear(T factor, T term) {
84  return factory<bits::linear_t, T, T>(factor, term);
85 }
86 
87 template <typename T>
88 inline pipe_begin<factory<bits::range_t, T, T, T> >
89 range(T from, T to, T increment = 1) {
90  return factory<bits::range_t, T, T, T>(from, to, increment);
91 }
92 
93 } // namespace tpie::pipelining
94 
95 #endif //__TPIE_PIPELINING_NUMERIC_H__
tpie::pipelining
pipelining/factory_base.h Base class of pipelining factories
Definition: ami_glue.h:23
tpie::pipelining::factory
Definition: factory_helpers.h:35
tpie::pipelining::bits::range_t
Definition: numeric.h:49
tpie::pipelining::push_type
Class to deduce the item_type of a node of type T.
Definition: node_traits.h:158
tpie::pipelining::bits::linear_t
Definition: numeric.h:32
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::add_push_destination
void add_push_destination(const node_token &dest)
Called by implementers to declare a push destination.
tpie::pipelining::node::set_name
void set_name(const std::string &name, priority_type priority=PRIORITY_USER)
Set this node's name.
tpie::pipelining::bits::range_t::go
void go() override
For initiator nodes, execute this phase by pushing all items to be pushed.
Definition: numeric.h:61
tpie::pipelining::bits::range_t::propagate
void propagate()
Propagate stream metadata.
Definition: numeric.h:55
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::node
Base class of all nodes.
Definition: node.h:77
tpie::pipelining::linear
pipe_middle< factory< bits::linear_t, T, T > > linear(T factor, T term)
A pipelining node that transforms the items by applying a linear function to them.
Definition: numeric.h:83
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