flow  3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
math.h
Go to the documentation of this file.
1 #if !defined(FLOW_MATH_H)
2  #define FLOW_MATH_H
3 
4 #include "node.h"
5 
6 #include <algorithm>
7 #include <memory>
8 #include <utility>
9 #include <vector>
10 
14 
18 namespace flow { namespace samples { namespace math {
19 
21 template<typename T>
22 class adder : public transformer<T, T>
23 {
24 public:
29  adder(size_t ins = 2, const std::string& name_r = "adder") : node(name_r), transformer<T, T>(name_r, ins, 1) {}
30 
31  virtual ~adder() {}
32 
37  virtual void ready(size_t n)
38  {
39  // Check that a packet is ready at all inputs. If not, return and try again when another packet arrives.
40  for(size_t i = 0; i != consumer<T>::ins(); ++i)
41  {
42  if(!consumer<T>::input(i).peek())
43  {
44  return;
45  }
46  }
47 
48  // Gather the terms in a container.
49  std::vector<std::unique_ptr<packet<T>>> terms;
50 
51  for(auto& inpin : consumer<T>::inputs())
52  {
53  terms.emplace_back(move(inpin.pop()));
54  }
55 
56  // Start the sum as equal to the first term.
57  T sum(terms[0]->data());
58 
59  // Add to the sum the value of all other packets.
60  std::for_each(terms.begin() + 1, terms.end(), [&sum](const std::unique_ptr<packet<T>>& packet_up_r){
61  sum += packet_up_r->data();
62  });
63 
64  // Make a packet with the sum data.
65  std::unique_ptr<packet<T>> sum_up(new packet<T>(sum));
66 
67  // Output it.
68  producer<T>::output(0).push(std::move(sum_up));
69  }
70 };
71 
73 template<typename T>
74 class const_adder : public transformer<T, T>
75 {
76  const T d_addend;
77 
78 public:
81  const_adder(const T& addend, const std::string& name_r = "const_adder") : node(name_r), transformer<T, T>(name_r, 1, 1), d_addend(addend) {}
82 
83  virtual ~const_adder() {}
84 
86  virtual void ready(size_t)
87  {
88  std::unique_ptr<packet<T>> packet_p = consumer<T>::input(0).pop();
89 
90  packet_p->data() += d_addend;
91 
92  producer<T>::output(0).push(std::move(packet_p));
93  }
94 };
95 
96 }}}
97 
98 #endif
99 
100 /*
101  (C) Copyright Thierry Seegers 2010-2012. Distributed under the following license:
102 
103  Boost Software License - Version 1.0 - August 17th, 2003
104 
105  Permission is hereby granted, free of charge, to any person or organization
106  obtaining a copy of the software and accompanying documentation covered by
107  this license (the "Software") to use, reproduce, display, distribute,
108  execute, and transmit the Software, and to prepare derivative works of the
109  Software, and to permit third-parties to whom the Software is furnished to
110  do so, all subject to the following:
111 
112  The copyright notices in the Software and this entire statement, including
113  the above license grant, this restriction and the following disclaimer,
114  must be included in all copies of the Software, in whole or in part, and
115  all derivative works of the Software, unless such copies or derivative
116  works are solely in the form of machine-executable object code generated by
117  a source language processor.
118 
119  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
120  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
121  FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
122  SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
123  FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
124  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
125  DEALINGS IN THE SOFTWARE.
126 */