Go to the documentation of this file.00001
00005
00006
00007 #include <cmath>
00008 #include <algorithm>
00009 #include <iostream>
00010
00011 #include "nxyter/DistFunc.h"
00012
00019
00021
00028 nxyter::DistFunc::DistFunc(int cap) :
00029 fMaxEntries(0),
00030 fFull(false),
00031 fSorted(false)
00032 {
00033 fData.reserve(cap);
00034 }
00035
00036
00037
00038 nxyter::DistFunc::~DistFunc()
00039 {
00040 }
00041
00042
00044
00053 void nxyter::DistFunc::setMaxEntries(int max)
00054 {
00055 fMaxEntries = max;
00056 fFull = false;
00057 if (max > 0) fFull = fData.size() > max;
00058 }
00059
00060
00062
00066 void nxyter::DistFunc::addEntry(float val)
00067 {
00068 if (fMaxEntries>0 && fData.size() >= fMaxEntries) fFull = true;
00069 if (fFull) return;
00070 fData.push_back(val);
00071 fSorted = false;
00072 }
00073
00074
00076
00077 float nxyter::DistFunc::operator()(float prob)
00078 {
00079 if (fData.size() == 0) return 0.;
00080
00081 sort();
00082
00083 if (prob < 0.) prob = 0.;
00084 if (prob > 1.) prob = 1.;
00085
00086 float ind_f = float(fData.size()-1) * prob;
00087 int ind_i = int(std::floor(ind_f));
00088 float w_i1 = ind_f - ind_i;
00089 float w_i0 = 1. - w_i1;
00090
00091 float res;
00092 if (ind_i == fData.size()-1 || ind_f == 0.) {
00093 res = fData[ind_i];
00094 } else {
00095 res = w_i0 * fData[ind_i] + w_i1 * fData[ind_i+1];
00096 }
00097
00098 #ifdef never
00099 printf("+++1");
00100 for (int i=0; i<fData.size(); i++) printf(" %6.0f", fData[i]);
00101 printf("\n");
00102
00103 printf("+++2 p=%6.3f n=%5d i_f=%6.1f i_i=%5d d[i]=%4.0f d[+1]=%4.0f res=%6.1f\n",
00104 prob, int(fData.size()), ind_f, ind_i, fData[ind_i],
00105 (ind_i<fData.size()-1) ? fData[ind_i+1] : 0, res);
00106 #endif
00107
00108 return res;
00109
00110 }
00111
00112
00114
00119 float nxyter::DistFunc::operator[](int ind)
00120 {
00121 sort();
00122 return fData.at(ind);
00123 }
00124
00125
00127
00128 float nxyter::DistFunc::getMedian()
00129 {
00130 return (*this)(0.5);
00131 }
00132
00133
00135
00136 float nxyter::DistFunc::getWidth(float cut)
00137 {
00138 return (*this)(0.5+cut) - (*this)(0.5-cut);
00139 }
00140
00141
00143
00144 float nxyter::DistFunc::getMin()
00145 {
00146 if (fData.size() == 0) return 0.;
00147 return (*this)[0];
00148 }
00149
00150
00152
00153 float nxyter::DistFunc::getMax()
00154 {
00155 if (fData.size() == 0) return 0.;
00156 return (*this)[fData.size()-1];
00157 }
00158
00159
00161
00162 void nxyter::DistFunc::sort()
00163 {
00164 if (!fSorted) {
00165 std::sort(fData.begin(), fData.end());
00166 fSorted = true;
00167 }
00168 }
00169