Embedded Template Library  1.0
cumulative_moving_average.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 https://github.com/ETLCPP/etl
8 https://www.etlcpp.com
9 
10 Copyright(c) 2018 jwellbelove
11 
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files(the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions :
18 
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29 ******************************************************************************/
30 
31 #ifndef ETL_CUMULATIVE_MOVING_AVERAGE_INCLUDED
32 #define ETL_CUMULATIVE_MOVING_AVERAGE_INCLUDED
33 
34 #include "type_traits.h"
35 #include "iterator.h"
36 
37 namespace etl
38 {
39  namespace private_cumulative_moving_average
40  {
41  //***************************************************
44  //***************************************************
45  template <typename TCMA>
46  class add_insert_iterator : public etl::iterator<ETL_OR_STD::output_iterator_tag, void, void, void, void>
47  {
48  public:
49 
50  //***********************************
51  explicit add_insert_iterator(TCMA& cma) ETL_NOEXCEPT
52  : p_cma(&cma)
53  {
54  }
55 
56  //***********************************
57  add_insert_iterator& operator*() ETL_NOEXCEPT
58  {
59  return *this;
60  }
61 
62  //***********************************
63  add_insert_iterator& operator++() ETL_NOEXCEPT
64  {
65  return *this;
66  }
67 
68  //***********************************
69  add_insert_iterator& operator++(int) ETL_NOEXCEPT
70  {
71  return *this;
72  }
73 
74  //***********************************
75  add_insert_iterator& operator =(typename TCMA::value_type value)
76  {
77  p_cma->add(value);
78  return *this;
79  }
80 
81  private:
82 
83  TCMA* p_cma;
84  };
85  }
86 
87  //***************************************************************************
92  //***************************************************************************
93  template <typename T,
94  const size_t SAMPLE_SIZE,
95  const size_t SCALING = 1U,
96  const bool IsIntegral = etl::is_integral<T>::value,
97  const bool IsFloat = etl::is_floating_point<T>::value>
99 
100  //***************************************************************************
106  //***************************************************************************
107  template <typename T, const size_t SAMPLE_SIZE_, const size_t SCALING_>
108  class cumulative_moving_average<T, SAMPLE_SIZE_, SCALING_, true, false>
109  {
110  private:
111 
113 
114  typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>::type scale_t;
115  typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>::type sample_t;
116 
117  static const sample_t SAMPLES = static_cast<sample_t>(SAMPLE_SIZE_);
118  static const scale_t SCALE = static_cast<scale_t>(SCALING_);
119 
120  public:
121 
122  typedef T value_type;
124 
125  static const size_t SAMPLE_SIZE = SAMPLE_SIZE_;
126  static const size_t SCALING = SCALING_;
127 
128  //*************************************************************************
131  //*************************************************************************
132  cumulative_moving_average(const T initial_value)
133  : average(initial_value * SCALE)
134  {
135  }
136 
137  //*************************************************************************
140  //*************************************************************************
141  void clear(const T initial_value)
142  {
143  average = (initial_value * SCALE);
144  }
145 
146  //*************************************************************************
149  //*************************************************************************
150  void add(T new_value)
151  {
152  average *= SAMPLES;
153  average += SCALE * new_value;
154  average /= SAMPLES + sample_t(1);
155  }
156 
157  //*************************************************************************
160  //*************************************************************************
161  T value() const
162  {
163  return average;
164  }
165 
166  //*************************************************************************
169  //*************************************************************************
171  {
172  return add_insert_iterator(*this);
173  }
174 
175  private:
176 
177  T average;
178  };
179 
180  //***************************************************************************
185 //***************************************************************************
186  template <typename T, const size_t SCALING_>
187  class cumulative_moving_average<T, 0, SCALING_, true, false>
188  {
190 
191  typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>::type scale_t;
192  typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>::type sample_t;
193 
194  static const scale_t SCALE = static_cast<scale_t>(SCALING_);
195 
196  public:
197 
198  typedef T value_type;
200 
201  static const size_t SCALING = SCALING_;
202 
203  //*************************************************************************
206  //*************************************************************************
207  cumulative_moving_average(const T initial_value, const size_t sample_size)
208  : average(initial_value * SCALE)
209  , samples(sample_size)
210  {
211  }
212 
213  //*************************************************************************
216  //*************************************************************************
217  void clear(const T initial_value)
218  {
219  average = (initial_value * SCALE);
220  }
221 
222  //*************************************************************************
225  //*************************************************************************
226  void set_sample_size(const size_t sample_size)
227  {
228  samples = sample_t(sample_size);
229  }
230 
231  //*************************************************************************
234  //*************************************************************************
235  void add(T new_value)
236  {
237  average *= samples;
238  average += SCALE * new_value;
239  average /= samples + sample_t(1);
240  }
241 
242  //*************************************************************************
245  //*************************************************************************
246  T value() const
247  {
248  return average;
249  }
250 
251  //*************************************************************************
254  //*************************************************************************
256  {
257  return add_insert_iterator(*this);
258  }
259 
260  private:
261 
262  T average;
263  sample_t samples;
264  };
265 
266  //***************************************************************************
271  //***************************************************************************
272  template <typename T, const size_t SAMPLE_SIZE_>
273  class cumulative_moving_average<T, SAMPLE_SIZE_, 1U, false, true>
274  {
276 
277  public:
278 
279  typedef T value_type;
281 
282  static const size_t SAMPLE_SIZE = SAMPLE_SIZE_;
283 
284  //*************************************************************************
287  //*************************************************************************
288  cumulative_moving_average(const T initial_value)
289  : reciprocal_samples_plus_1(T(1.0) / T(SAMPLE_SIZE_ + 1U))
290  , average(initial_value)
291  {
292  }
293 
294  //*************************************************************************
297  //*************************************************************************
298  void clear(const T initial_value)
299  {
300  average = initial_value;
301  }
302 
303  //*************************************************************************
306  //*************************************************************************
307  void add(const T new_value)
308  {
309  average += (new_value - average) * reciprocal_samples_plus_1;
310  }
311 
312  //*************************************************************************
315  //*************************************************************************
316  T value() const
317  {
318  return average;
319  }
320 
321  //*************************************************************************
324  //*************************************************************************
326  {
327  return add_insert_iterator(*this);
328  }
329 
330  private:
331 
332  const T reciprocal_samples_plus_1;
333  T average;
334  };
335 
336  //***************************************************************************
341  //***************************************************************************
342  template <typename T>
343  class cumulative_moving_average<T, 0U, 1U, false, true>
344  {
346 
347  public:
348 
349  typedef T value_type;
351 
352  //*************************************************************************
355  //*************************************************************************
356  cumulative_moving_average(const T initial_value, const size_t sample_size)
357  : reciprocal_samples_plus_1(T(1.0) / T(sample_size + 1U))
358  , average(initial_value)
359  {
360  }
361 
362  //*************************************************************************
365  //*************************************************************************
366  void clear(const T initial_value)
367  {
368  average = initial_value;
369  }
370 
371  //*************************************************************************
374  //*************************************************************************
375  void set_sample_size(const size_t sample_size)
376  {
377  reciprocal_samples_plus_1 = T(1.0) / (T(sample_size) + T(1));
378  }
379 
380  //*************************************************************************
383  //*************************************************************************
384  void add(const T new_value)
385  {
386  average += (new_value - average) * reciprocal_samples_plus_1;
387  }
388 
389  //*************************************************************************
392  //*************************************************************************
393  T value() const
394  {
395  return average;
396  }
397 
398  //*************************************************************************
401  //*************************************************************************
403  {
404  return add_insert_iterator(*this);
405  }
406 
407  private:
408 
409  T reciprocal_samples_plus_1;
410  T average;
411  };
412 }
413 
414 #endif
Definition: cumulative_moving_average.h:188
void set_sample_size(const size_t sample_size)
Definition: cumulative_moving_average.h:226
T value() const
Definition: cumulative_moving_average.h:246
void clear(const T initial_value)
Definition: cumulative_moving_average.h:217
add_insert_iterator input()
Definition: cumulative_moving_average.h:255
cumulative_moving_average(const T initial_value, const size_t sample_size)
Definition: cumulative_moving_average.h:207
void add(T new_value)
Definition: cumulative_moving_average.h:235
Definition: cumulative_moving_average.h:344
T value() const
Definition: cumulative_moving_average.h:393
void clear(const T initial_value)
Definition: cumulative_moving_average.h:366
void add(const T new_value)
Definition: cumulative_moving_average.h:384
cumulative_moving_average(const T initial_value, const size_t sample_size)
Definition: cumulative_moving_average.h:356
add_insert_iterator input()
Definition: cumulative_moving_average.h:402
void set_sample_size(const size_t sample_size)
Definition: cumulative_moving_average.h:375
add_insert_iterator input()
Definition: cumulative_moving_average.h:325
T value() const
Definition: cumulative_moving_average.h:316
void add(const T new_value)
Definition: cumulative_moving_average.h:307
cumulative_moving_average(const T initial_value)
Definition: cumulative_moving_average.h:288
void clear(const T initial_value)
Definition: cumulative_moving_average.h:298
void clear(const T initial_value)
Definition: cumulative_moving_average.h:141
cumulative_moving_average(const T initial_value)
Definition: cumulative_moving_average.h:132
T value() const
Definition: cumulative_moving_average.h:161
void add(T new_value)
Definition: cumulative_moving_average.h:150
add_insert_iterator input()
Definition: cumulative_moving_average.h:170
Definition: cumulative_moving_average.h:98
Definition: cumulative_moving_average.h:47
conditional
Definition: type_traits_generator.h:1202
is_floating_point
Definition: type_traits_generator.h:971
is_integral
Definition: type_traits_generator.h:941
Definition: absolute.h:37
iterator
Definition: iterator.h:422