-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup2.cpp
61 lines (61 loc) · 1.64 KB
/
lookup2.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
60
61
#include "lifealgo.h"
#include <algorithm>
#include <cstdlib>
using namespace std ;
class lookup2algo : public lifealgo {
public:
virtual void init(int w, int h) ;
virtual void setcell(int x, int y) ;
virtual int getpopulation() ;
virtual int nextstep(int, int, int) ;
virtual void swap() ;
int w, h ;
long long wh ;
unsigned char *u0, *u1 ;
} ;
static class lookup2algofactory : public lifealgofactory {
public:
lookup2algofactory() ;
virtual lifealgo *createInstance() {
return new lookup2algo() ;
}
} factory ;
lookup2algofactory::lookup2algofactory() {
registerAlgo("lookup2", &factory) ;
}
void lookup2algo::init(int w_, int h_) {
w = w_ ;
h = h_ ;
wh = w * h ;
u0 = (unsigned char *)calloc(w, h) ;
u1 = (unsigned char *)calloc(w, h) ;
}
void lookup2algo::setcell(int x, int y) {
u0[y * w + x] = 1 ;
}
int lookup2algo::getpopulation() {
int r = 0 ;
for (int i=0; i<wh; i++)
r += u0[i] ;
return r ;
}
static unsigned char tab[] =
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 } ;
void lookup2algo::swap() { ::swap(u0, u1) ; }
int lookup2algo::nextstep(int id, int n, int) {
int pop = 0 ;
int loy = id * (h - 2) / n + 1 ;
int hiy = (id + 1) * (h - 2) / n + 1 ;
for (int y=loy; y<hiy; y++) {
unsigned char *rp = u0 + (y-1)*w + 1 ;
unsigned char *r = u0 + y*w + 1 ;
unsigned char *rn = u0 + (y+1)*w + 1 ;
unsigned char *wr = u1 + y*w + 1 ;
for (int x=1; x+1<w; x++, r++, rp++, rn++, wr++) {
int n = tab[rp[-1]+rp[0]+rp[+1]+r[-1]+r[1]+rn[-1]+rn[0]+rn[1]+9* *r] ;
pop += n ;
*wr = n ;
}
}
return pop ;
}