30 #include <suplibxx/str.h> 32 #include "RdbStatsPercentiles.h" 36 const PercentileList& percentiles )
37 :
RdbStats( irdbtable, name ), the_median( 0.0 ), the_percentile( percentiles )
42 :
RdbStats( irdbtable, name ), the_median( 0.0 ) {
48 double RdbStatsPercentiles::calculate_median( ) {
50 std::vector< double >::iterator begin_ptr = data.begin( );
52 size_t num_size = data.size( );
53 size_t num_size_div = num_size / 2;
55 nth_element( begin_ptr, begin_ptr + num_size_div, data.end( ) );
57 if ( num_size & 0001 ) {
61 return data[ num_size_div ];
67 double tmp = data[ num_size_div ];
73 nth_element( begin_ptr, begin_ptr + num_size_div - 1,
74 begin_ptr + num_size_div );
75 tmp += data[ num_size_div - 1 ];
82 double RdbStatsPercentiles::calculate_percentile(
const double percentile ) {
84 std::vector< double >::iterator begin_ptr = data.begin( );
86 size_t n = data.size( );
87 double f = percentile / 100.0;
88 int i = ( ( n - 1.0 ) * f );
89 double delta = ( n - 1.0 ) * f - i;
91 nth_element( begin_ptr, begin_ptr + i + 1, data.end( ) );
92 double data_i_1 = data[ i + 1];
94 nth_element( begin_ptr, begin_ptr + i, data.end( ) );
95 double data_i = data[ i ];
97 double quantile = ( 1.0 - delta ) * data_i + delta * data_i_1;
116 the_median = calculate_median( );
118 for (
size_t ii = 0; ii < the_percentile.size( ); ii++ )
119 the_percentile_result[ ii ] =
120 calculate_percentile( the_percentile[ ii ].percentile );
125 void RdbStatsPercentiles::init( ) {
126 this->RdbStats::init( );
132 void RdbStatsPercentiles::normalize_results(
const double norm ) {
134 this->RdbStats::normalize_results( norm );
135 for (
size_t ii = 0; ii < the_percentile.size( ); ii++ )
136 the_percentile_result[ ii ] /= norm;
140 void RdbStatsPercentiles::set_output_columns( RDB& ordbtable ) {
142 this->RdbStats::set_output_columns( ordbtable );
144 const int mysize( the_percentile.size( ) );
145 the_percentile_result.reserve( mysize );
147 const char* colname_ptr = colname.c_str( );
150 sprintf( str,
"%s_median", colname_ptr );
151 ordbtable.setColumn( str,
"N" );
152 RDBColumn* tmp = ordbtable.getColumn( str );
153 tmp->mapData( &the_median, 1 );
155 for (
int ii = 0; ii < mysize; ii++ ) {
157 std::ostringstream ost;
158 ost << colname_ptr << the_percentile[ ii ].colname ;
159 ordbtable.setColumn( ost.str( ),
"N" );
160 RDBColumn* ptr = ordbtable.getColumn( ost.str( ) );
161 ptr->mapData( &the_percentile_result[ii], 1 );
169 data.push_back( get_value( ) );
The base class to calculate : average, maximum, minimum, num, stddev and sum.
virtual void update_statistics()
Read the column from RDB++, update the statistics for the column.
virtual void update_statistics()
Read the column from RDB++, update the statistics for the column.
RdbStatsPercentiles(RDB &irdbtable, const std::string &name, const PercentileList &percentiles)
–percentiles 12,34.. and –quartile
virtual int calculate_statistics()
Perform the final statistic for the set.
virtual int calculate_statistics()
Perform the final statistic for the set.