LazyUpdate reference example

git-svn-id: svn://ultimatepp.org/upp/trunk@968 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2009-03-17 13:57:45 +00:00
parent c271c33440
commit 3325d89c50
3 changed files with 49 additions and 0 deletions

View file

@ -0,0 +1,35 @@
#include <Core/Core.h>
using namespace Upp;
struct DblSum {
Vector<double> d;
mutable double sum;
LazyUpdate dirty;
public:
void Add(double x) {
d.Add(x);
dirty.Invalidate();
}
double Sum() const {
if(dirty.BeginUpdate()) {
Vector<int> o = GetSortOrder(d);
sum = 0;
for(int i = 0; i < o.GetCount(); i++)
sum += d[o[i]];
dirty.EndUpdate();
}
return sum;
}
};
CONSOLE_APP_MAIN
{
DblSum x;
x.Add(100);
for(int i = 0; i < 1000*1000; i++)
x.Add(1e-10);
Cout() << x.Sum() << "\n";
}

View file

@ -0,0 +1,10 @@
uses
Core;
file
LazyUpdate.cpp;
mainconfig
"" = "",
"" = "MT";

View file

@ -0,0 +1,4 @@
#ifndef _LazyUpdate_icpp_init_stub
#define _LazyUpdate_icpp_init_stub
#include "Core/init"
#endif