-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharray3.cpp
More file actions
60 lines (60 loc) · 1.54 KB
/
array3.cpp
File metadata and controls
60 lines (60 loc) · 1.54 KB
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
#include "lifealgo.h"
#include <algorithm>
#include <cstdlib>
using namespace std ;
class array3algo : 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 array3algofactory : public lifealgofactory {
public:
array3algofactory() ;
virtual lifealgo *createInstance() {
return new array3algo() ;
}
} factory ;
array3algofactory::array3algofactory() {
registerAlgo("array3", &factory) ;
}
void array3algo::init(int w_, int h_) {
w = w_ ;
h = h_ ;
wh = w * h ;
u0 = (unsigned char *)calloc(1, wh+w+16) ;
u1 = (unsigned char *)calloc(1, wh+2*w+16)+w ;
}
void array3algo::setcell(int x, int y) {
u0[y * w + x] = 1 ;
}
int array3algo::getpopulation() {
int r = 0 ;
for (int i=0; i<wh; i++)
r += u0[i] ;
return r ;
}
void array3algo::swap() {}
int array3algo::nextstep(int id, int nid, int) {
if (nid != 1)
error("! multithreading not supported yet") ;
int pop = 0 ;
u1[0] = 0 ;
for (int i=1; i<=wh+w; i++)
u1[i] = u1[i-1] + u0[i-1] ;
for (int i=0; i<wh; i++)
u1[i+w] += u1[i] ;
for (int y=1; y+1<h; y++) {
for (int x=1; x+1<w; x++) {
unsigned char n = u1[(y+1)*w+(x+2)] - u1[(y+1)*w+(x-1)] -
u1[(y-2)*w+(x+2)] + u1[(y-2)*w+(x-1)] ;
pop += u0[y*w+x] = (n == 3 || (n == 4 && u0[y*w+x])) ;
}
}
return pop ;
}