 d40e29d5f6
			
		
	
	d40e29d5f6
	
	
	
		
			
			Increasing counter sizes from int to long for averages. Rename random functions to end with _co instead of _exclusive to indicate range [close, open[, and to allow future suffixes oc, cc and oo.
		
			
				
	
	
		
			121 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			121 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2018 MariaDB Corporation Ab
 | |
|  *
 | |
|  * Use of this software is governed by the Business Source License included
 | |
|  * in the LICENSE.TXT file and at www.mariadb.com/bsl11.
 | |
|  *
 | |
|  * Change Date: 2022-01-01
 | |
|  *
 | |
|  * On the date above, in accordance with the Business Source License, use
 | |
|  * of this software will be governed by version 2 or later of the General
 | |
|  * Public License.
 | |
|  */
 | |
| 
 | |
| #include <maxbase/average.hh>
 | |
| #include <iostream>
 | |
| 
 | |
| namespace maxbase
 | |
| {
 | |
| 
 | |
| void CumulativeAverage::add(double ave, long num_samples)
 | |
| {
 | |
|     m_num_samples += num_samples;
 | |
| 
 | |
|     if (m_num_samples == num_samples)
 | |
|     {
 | |
|         m_ave = ave;
 | |
|     }
 | |
|     else
 | |
|     {
 | |
|         m_ave = (m_ave * (m_num_samples - m_num_last_added)
 | |
|                  + ave * num_samples) / m_num_samples;
 | |
|     }
 | |
|     m_num_last_added = num_samples;
 | |
| }
 | |
| 
 | |
| double CumulativeAverage::average() const
 | |
| {
 | |
|     return m_ave;
 | |
| }
 | |
| 
 | |
| long CumulativeAverage::num_samples() const
 | |
| {
 | |
|     return m_num_samples;
 | |
| }
 | |
| 
 | |
| CumulativeAverage& CumulativeAverage::operator+=(const CumulativeAverage& rhs)
 | |
| {
 | |
|     this->add(rhs.m_ave, rhs.m_num_samples);
 | |
|     return *this;
 | |
| }
 | |
| 
 | |
| CumulativeAverage operator+(const CumulativeAverage& lhs, const CumulativeAverage& rhs)
 | |
| {
 | |
|     return CumulativeAverage(lhs) += rhs;
 | |
| }
 | |
| 
 | |
| void CumulativeAverage::reset()
 | |
| {
 | |
|     m_ave = 0;
 | |
|     m_num_samples = 0;
 | |
|     m_num_last_added = 0;
 | |
| }
 | |
| 
 | |
| EMAverage::EMAverage(double min_alpha, double max_alpha, long sample_max)
 | |
|     : m_min_alpha{min_alpha}
 | |
|     , m_max_alpha{max_alpha}
 | |
|     , m_sample_max{sample_max}
 | |
| {
 | |
| }
 | |
| 
 | |
| void EMAverage::add(double ave, long num_samples)
 | |
| {
 | |
|     // Give more weight to initial samples.
 | |
|     long sample_max = std::min(m_num_samples ? m_num_samples : 1, m_sample_max);
 | |
| 
 | |
|     double alpha = m_min_alpha + m_max_alpha
 | |
|         * std::min(double(num_samples) / sample_max, 1.0);
 | |
| 
 | |
|     m_num_samples += num_samples;
 | |
|     if (m_num_samples == num_samples)
 | |
|     {
 | |
|         m_ave = ave;
 | |
|     }
 | |
|     else
 | |
|     {
 | |
|         m_ave = alpha * ave + (1 - alpha) * m_ave;
 | |
|     }
 | |
| }
 | |
| 
 | |
| void EMAverage::add(const CumulativeAverage& ca)
 | |
| {
 | |
|     add(ca.average(), ca.num_samples());
 | |
| }
 | |
| 
 | |
| double EMAverage::average() const
 | |
| {
 | |
|     return m_ave;
 | |
| }
 | |
| 
 | |
| long EMAverage::num_samples() const
 | |
| {
 | |
|     return m_num_samples;
 | |
| }
 | |
| 
 | |
| void EMAverage::set_sample_max(long sample_max)
 | |
| {
 | |
|     m_sample_max = sample_max;
 | |
| }
 | |
| 
 | |
| long EMAverage::sample_max() const
 | |
| {
 | |
|     return m_sample_max;
 | |
| }
 | |
| 
 | |
| void EMAverage::reset()
 | |
| {
 | |
|     m_ave = 0;
 | |
|     m_num_samples = 0;
 | |
| }
 | |
| }   // maxbase
 |