TPIE

2362a60
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 {
29 
30 namespace pipelining {
31 
32 namespace bits {
33 
34 template <typename dest_t>
35 class linear_t : public node {
36 public:
37  typedef typename push_type<dest_t>::type item_type;
38 
39  inline linear_t(dest_t dest, item_type factor, item_type term) : factor(factor), term(term), dest(std::move(dest)) {
40  add_push_destination(this->dest);
41  set_name("Linear transform", PRIORITY_INSIGNIFICANT);
42  }
43  inline void push(const item_type & item) {
44  dest.push(item*factor+term);
45  }
46 private:
47  item_type factor, term;
48  dest_t dest;
49 };
50 
51 template <typename dest_t>
52 class range_t : public node {
53 public:
54  typedef typename push_type<dest_t>::type item_type;
55 
56  range_t(dest_t dest, item_type from, item_type to, item_type increment) : from(from), to(to), increment(increment), dest(std::move(dest)) {}
57 
58  void propagate() {
59  stream_size_type items = (from - to) / increment;
60  set_steps(items);
61  forward("items", items);
62  }
63 
64  virtual void go() override {
65  for (item_type i=from; i < to; i += increment) {
66  dest.push(i);
67  step(1);
68  }
69  }
70 
71 private:
72  item_type from, to, increment;
73  dest_t dest;
74 };
75 
76 } // namespace bits
77 
84 template <typename T>
85 inline pipe_middle<factory<bits::linear_t, T, T> >
86 linear(T factor, T term) {
87  return factory<bits::linear_t, T, T>(factor, term);
88 }
89 
90 template <typename T>
91 inline pipe_begin<factory<bits::range_t, T, T, T> >
92 range(T from, T to, T increment = 1) {
93  return factory<bits::range_t, T, T, T>(from, to, increment);
94 }
95 
96 
97 } // namespace pipelining
98 
99 } // namespace tpie
100 
101 #endif
virtual void go() override
For initiator nodes, execute this phase by pushing all items to be pushed.
Definition: numeric.h:64
Base class of all nodes.
Definition: node.h:78
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
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 set_name(const std::string &name, priority_type priority=PRIORITY_USER)
Set this node's name.
void propagate()
Propagate stream metadata.
Definition: numeric.h:58
void step(stream_size_type steps=1)
Step the progress indicator.
Definition: node.h:654
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:86
Node factory for variadic argument generators.
void set_steps(stream_size_type steps)
Called by implementers that intend to call step().