-
Notifications
You must be signed in to change notification settings - Fork 0
/
pct.cpp
60 lines (51 loc) · 925 Bytes
/
pct.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* File: pct.cpp
* Date: Dec 2014
* Author: Qing Wang
* Description: A pre-computed table to speed up complex calculations.
*/
#pragma once
#include <cstdint>
/*
* PCT for func(i+delta)
*/
class PCT
{
public:
uint32_t len;
double (*func)(double);
double delta;
double * x;
PCT() : len(0), func(NULL), delta(0), x(NULL) {}
PCT(uint32_t _len, double (*_func)(double), double _delta)
: len(0), func(NULL), delta(0), x(NULL)
{
make(_len,_func,_delta);
}
~PCT() { destroy(); }
void destroy()
{
if(NULL!=x) {
delete[] x;
x = NULL;
}
}
void make(uint32_t _len, double (*_func)(double), double _delta)
{
len = _len;
func = _func;
delta = _delta;
destroy();
x = new double[len];
for(uint32_t i=0;i<len;i++)
x[i] = i + delta;
for(uint32_t i=0;i<len;i++)
x[i] = func(x[i]);
}
double operator()(uint32_t i)
{
if(i<len)
return x[i];
return func(i+delta);
}
};