mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-05-15 14:16:07 -06:00
LazyUpdate reference example
git-svn-id: svn://ultimatepp.org/upp/trunk@968 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
c271c33440
commit
3325d89c50
3 changed files with 49 additions and 0 deletions
35
reference/LazyUpdate/LazyUpdate.cpp
Normal file
35
reference/LazyUpdate/LazyUpdate.cpp
Normal 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";
|
||||
}
|
||||
10
reference/LazyUpdate/LazyUpdate.upp
Normal file
10
reference/LazyUpdate/LazyUpdate.upp
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
uses
|
||||
Core;
|
||||
|
||||
file
|
||||
LazyUpdate.cpp;
|
||||
|
||||
mainconfig
|
||||
"" = "",
|
||||
"" = "MT";
|
||||
|
||||
4
reference/LazyUpdate/init
Normal file
4
reference/LazyUpdate/init
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
#ifndef _LazyUpdate_icpp_init_stub
|
||||
#define _LazyUpdate_icpp_init_stub
|
||||
#include "Core/init"
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue