-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.cpp
75 lines (75 loc) · 1.99 KB
/
list.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "lifealgo.h"
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std ;
class listalgo : 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 sorted ;
vector<pair<int, int> > pts, pts2 ;
} ;
static class listalgofactory : public lifealgofactory {
public:
listalgofactory() ;
virtual lifealgo *createInstance() {
return new listalgo() ;
}
} factory ;
listalgofactory::listalgofactory() {
registerAlgo("list", &factory) ;
}
void listalgo::init(int w_, int h_) {
}
void listalgo::setcell(int x, int y) {
sorted = 0 ;
pts.push_back(make_pair(x, y)) ;
}
int listalgo::getpopulation() {
sorted = 0 ;
return pts.size() ;
}
void listalgo::swap() { }
int listalgo::nextstep(int id, int nid, int) {
if (nid != 1)
error("! multithreading not yet supported") ;
pts2.clear() ;
for (size_t i=0; i<pts.size(); i++) {
int x = pts[i].first ;
int y = pts[i].second ;
pts2.push_back(make_pair(x-1, y-1)) ;
pts2.push_back(make_pair(x-1, y)) ;
pts2.push_back(make_pair(x-1, y+1)) ;
pts2.push_back(make_pair(x, y-1)) ;
pts2.push_back(make_pair(x, y+1)) ;
pts2.push_back(make_pair(x+1, y-1)) ;
pts2.push_back(make_pair(x+1, y)) ;
pts2.push_back(make_pair(x+1, y+1)) ;
}
sort(pts2.begin(), pts2.end()) ;
if (!sorted) {
sort(pts.begin(), pts.end()) ;
sorted = 1 ;
}
size_t w = 0 ;
size_t rd = 0 ;
size_t at = 0 ;
while (at < pts2.size()) {
int cnt = 1 ;
while (at + cnt < pts2.size() && pts2[at+cnt] == pts2[at])
cnt++ ;
while (rd < pts.size() && pts[rd] < pts2[at])
rd++ ;
if (cnt == 3 || (cnt == 2 && rd < pts.size() && pts[rd] == pts2[at]))
pts2[w++] = pts2[at] ;
at += cnt ;
}
pts2.resize(w) ;
::swap(pts, pts2) ;
return pts.size() ;
}