-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.cpp
57 lines (57 loc) · 1.44 KB
/
array.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
#include "lifealgo.h"
#include <algorithm>
#include <cstdlib>
using namespace std ;
class arrayalgo : 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 arrayalgofactory : public lifealgofactory {
public:
arrayalgofactory() ;
virtual lifealgo *createInstance() {
return new arrayalgo() ;
}
} factory ;
arrayalgofactory::arrayalgofactory() {
registerAlgo("array", &factory) ;
}
void arrayalgo::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 arrayalgo::setcell(int x, int y) {
u0[y * w + x] = 1 ;
}
int arrayalgo::getpopulation() {
int r = 0 ;
for (int i=0; i<wh; i++)
r += u0[i] ;
return r ;
}
void arrayalgo::swap() { ::swap(u0, u1) ; }
int arrayalgo::nextstep(int id, int nid, int) {
int pop = 0 ;
int ylo = (h - 2) * id / nid + 1 ;
int yhi = (h - 2) * (id + 1) / nid + 1 ;
for (int y=ylo; y<yhi; y++) {
unsigned char *r = u0 + y*w + 1 ;
unsigned char *wr = u1 + y*w + 1 ;
for (int x=1; x+1<w; x++, r++, wr++) {
int n = r[-w-1]+r[-w]+r[-w+1]+r[-1]+r[1]+r[w-1]+r[w]+r[w+1] ;
*wr = (n == 3 || (n == 2 && *r)) ;
pop += *wr ;
}
}
return pop ;
}