| 1 |
#include "protoAverage.h" |
| 2 |
#include "protoDebug.h" |
| 3 |
#include "math.h" |
| 4 |
#include <limits> |
| 5 |
using std::numeric_limits; |
| 6 |
|
| 7 |
ProtoAverage::ProtoAverage() : count(0) |
| 8 |
{ |
| 9 |
} |
| 10 |
ProtoAverage::~ProtoAverage() |
| 11 |
{ |
| 12 |
} |
| 13 |
void |
| 14 |
ProtoAverage::Reset() |
| 15 |
{ |
| 16 |
list.Destroy(); |
| 17 |
count = 0; |
| 18 |
} |
| 19 |
bool |
| 20 |
ProtoAverage::AddNumber(double number) |
| 21 |
{ |
| 22 |
count++; |
| 23 |
//if its the first entry just make new Item and add it to the list |
| 24 |
if(list.IsEmpty()) |
| 25 |
{ |
| 26 |
AverageItem* newItem = new AverageItem(number); |
| 27 |
list.Prepend(*newItem); |
| 28 |
return true; |
| 29 |
} |
| 30 |
//check the depth of the last value |
| 31 |
if(list.GetHead()->GetDepth()!=0)//first entry is not a single value so just prepend the item |
| 32 |
{ |
| 33 |
AverageItem* newItem = new AverageItem(number); |
| 34 |
list.Prepend(*newItem); |
| 35 |
} else { //the head entry was a single value so we need to bubble back the averages |
| 36 |
AverageList::Iterator iterator(list); |
| 37 |
AverageItem tempItem(number); |
| 38 |
AverageItem* firstItemPtr = &tempItem; |
| 39 |
AverageItem* secondItemPtr = list.GetHead(); |
| 40 |
while(firstItemPtr->GetDepth()==secondItemPtr->GetDepth()) |
| 41 |
{ |
| 42 |
if(!secondItemPtr->Merge(*firstItemPtr)) |
| 43 |
{ |
| 44 |
TRACE("failed merging numbers!\n"); |
| 45 |
return false; |
| 46 |
} |
| 47 |
//if first item is the head destroy it |
| 48 |
if(firstItemPtr==list.GetHead()) |
| 49 |
{ |
| 50 |
firstItemPtr = list.RemoveHead(); |
| 51 |
delete firstItemPtr; |
| 52 |
} |
| 53 |
//fix the pointers |
| 54 |
iterator.Reset(); |
| 55 |
firstItemPtr = iterator.GetNextItem(); |
| 56 |
secondItemPtr = iterator.GetNextItem(); |
| 57 |
if(secondItemPtr==NULL) |
| 58 |
{ |
| 59 |
//we bubbled all the way to one value |
| 60 |
return true; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
return true; |
| 65 |
} |
| 66 |
double |
| 67 |
ProtoAverage::GetAverage() |
| 68 |
{ |
| 69 |
//iterate over list in reverse and get the weighted average |
| 70 |
AverageList::Iterator iterator(list); |
| 71 |
AverageItem* item = iterator.GetNextItem(); |
| 72 |
if(item!=NULL) |
| 73 |
{ |
| 74 |
ProtoAverage::AverageItem tempItem(item->GetValue());//used for storing the moving average as we go |
| 75 |
tempItem.SetDepth(item->GetDepth()); |
| 76 |
double x , y, z; |
| 77 |
double dpx=0; |
| 78 |
double dpy=0; |
| 79 |
double dpz=0; |
| 80 |
double numeral; |
| 81 |
double denom; |
| 82 |
while((item = iterator.GetNextItem())) |
| 83 |
{ |
| 84 |
x = tempItem.GetValue(); |
| 85 |
dpx = tempItem.GetDepth(); |
| 86 |
y = item->GetValue(); |
| 87 |
dpy = item->GetDepth(); |
| 88 |
|
| 89 |
numeral = (x + y * pow(2,dpy-dpx)); |
| 90 |
if(numeral==numeric_limits<double>::infinity()){ |
| 91 |
//we need to go under instead of over |
| 92 |
numeral = (x * pow(2,dpx-dpy) + y); |
| 93 |
denom = (pow(2,dpx-dpy)+1); |
| 94 |
} |
| 95 |
else |
| 96 |
{ |
| 97 |
denom = (1+pow(2,dpy-dpx)); |
| 98 |
} |
| 99 |
z = numeral/denom; //z is the new average |
| 100 |
dpz = log(denom)/log(2)+dpx; //dpz is the weighted depth of the new average |
| 101 |
dpz = log(1+pow(2,dpy-dpx))/log(2)+dpx; //dpz is the weighted depth of the new average |
| 102 |
|
| 103 |
tempItem.SetValue(z); |
| 104 |
tempItem.SetDepth(dpz); |
| 105 |
} |
| 106 |
return tempItem.GetValue(); |
| 107 |
} |
| 108 |
return 0; |
| 109 |
} |
| 110 |
unsigned long |
| 111 |
ProtoAverage::GetCount() const |
| 112 |
{ |
| 113 |
return count; |
| 114 |
} |
| 115 |
void |
| 116 |
ProtoAverage::Print() |
| 117 |
{ |
| 118 |
AverageList::Iterator iterator(list); |
| 119 |
AverageItem* item; |
| 120 |
while((item = iterator.GetNextItem())) |
| 121 |
{ |
| 122 |
TRACE("(%f,%f) ",item->GetValue(),item->GetDepth()); |
| 123 |
} |
| 124 |
TRACE("\n"); |
| 125 |
} |
| 126 |
bool |
| 127 |
ProtoAverage::AverageItem::Merge(const ProtoAverage::AverageItem &otherItem) |
| 128 |
{ |
| 129 |
//verify that they are at the same depth |
| 130 |
double tempvalue = 0; |
| 131 |
if(otherItem.GetDepth()!=GetDepth()) |
| 132 |
{ |
| 133 |
return false; |
| 134 |
} |
| 135 |
tempvalue = (value + otherItem.GetValue())/2; |
| 136 |
if(tempvalue==numeric_limits<double>::infinity()) |
| 137 |
{ |
| 138 |
//lets try dividing first |
| 139 |
value = value/2 + otherItem.GetValue()/2; |
| 140 |
} else { |
| 141 |
value = tempvalue; |
| 142 |
} |
| 143 |
|
| 144 |
if(value==numeric_limits<double>::infinity()) |
| 145 |
{ |
| 146 |
TRACE("still getting inf\n"); |
| 147 |
TRACE("%f\n\n",value/2); |
| 148 |
TRACE("%f\n\n",otherItem.GetValue()/2); |
| 149 |
TRACE("%f\n\n",value/2+otherItem.GetValue()/2); |
| 150 |
return false; |
| 151 |
} |
| 152 |
depth = depth+1; |
| 153 |
return true; |
| 154 |
} |
| 155 |
void |
| 156 |
ProtoAverage::AverageItem::SetValue(double v) |
| 157 |
{ |
| 158 |
value = v; |
| 159 |
return; |
| 160 |
} |
| 161 |
void |
| 162 |
ProtoAverage::AverageItem::SetDepth(double d) |
| 163 |
{ |
| 164 |
depth = d; |
| 165 |
return; |
| 166 |
} |