Repository navigation
Expand file tree
/
Copy pathseive.jsx
More file actions
197 lines (184 loc) · 5.5 KB
/
Copy pathseive.jsx
File metadata and controls
197 lines (184 loc) · 5.5 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import React, {Component} from 'react';
import Cells from "./cells";
import Navbar from "./navbar";
import DiscreteSlider from "./slider";
import Menu from "./menu";
import {seive} from "../algorithms/prime";
import Spiral from "./spiral";
class Seive extends Component {
state = {
number: 100,
cells:[],
isRunning:false,
speed:500,
primes:[],
maxPrime:0,
algo:0
}
constructor(props) {
super(props);
}
componentDidMount() {
const cells = getCells(this.state.number);
this.setState({cells});
}
setAlgo = (pos, val) => {
if (pos === 0) {
this.setState({algo: val});
// console.log(this.state.algo);
}
}
render() {
return (
<div>
<Navbar/>
<Menu
onChangeSpeed={this.changeSpeed}
onChangeValues={this.handleValueIncease}
onVisualize = {this.startAlgo}
onRefresh = {this.handleRefresh}
isDisabled = {this.state.isRunning}
setAlgo={this.setAlgo}
/>
{this.state.algo === 0 &&
<Cells
num={this.state.number}
cells={this.state.cells}
/>
}
{this.state.algo === 1 &&
<Spiral
num={this.state.number}
primes={this.state.primes}
maxPrime={this.state.maxPrime}
/>
}
</div>
);
}
changeSpeed = (speed) => {
//console.log(typeof speed);
this.setState({speed:600-speed*10});
}
handleValueIncease = (value) => {
this.setState({number:value});
if( this.state.algo === 0 ){
this.setState({cells:getCells(value),isRunning:false});
}
// console.log(value);
}
handleRefresh = () => {
this.setState({cells:getCells(this.state.number),isRunning:false});
}
startAlgo = () =>{
console.log(this.state.algo);
if( this.state.algo === 0 ){
this.startSeive();
}else if( this.state.algo === 1 ){
this.startSpiral();
}
}
startSpiral = async () =>{
let pprimes = seive(this.state.number*100);
let primes = [];
this.setState({primes:[],maxPrime:pprimes[pprimes.length-1]});
let mod = Math.ceil(this.state.number/10);
for(let i=0;i<pprimes.length;i++){
primes.push(pprimes[i]);
if( i%mod === 0 ){
this.setState({primes});
await sleep(10);
}
}
console.log('done');
}
startSeive = async () => {
const speed = this.state.speed;
this.setState({isRunning:true});
const prime = [];
for(let i = 0;i<=this.state.number;i++){
prime.push(1);
}
prime[0] = prime[1] = 0;
let changedCells = this.state.cells;
let prevCheck = -1;
let counter = 0;
for( let i = 2; i<=this.state.number;i++){
if( prime[i] === 1 ){
// setTimeout(()=>{
changedCells = getNewCellPrimeToggled(changedCells,i-1);
this.setState({cells:changedCells});
//},counter*speed);
await sleep(this.state.speed);
counter++;
for(let j = i*i;j<=this.state.number;j+=i){
//setTimeout(()=>{
if( prevCheck!=-1 ){
changedCells = getNewCellVisitingToggled(changedCells,prevCheck);
}
prevCheck = j-1;
changedCells = getNewCellCheckToggled(changedCells,j-1);
changedCells = getNewCellVisitingToggled(changedCells,prevCheck);
this.setState({cells:changedCells});
// },counter*speed);
await sleep(this.state.speed);
counter++;
prime[j] = 0;
}
}
}
// setTimeout(()=>{
changedCells = getNewCellVisitingToggled(changedCells,prevCheck);
this.setState({cells:changedCells,isRunning:false});
// },counter*speed);
}
}
const getNewCellPrimeToggled = (cells,pos) =>{
const newCells = cells.slice();
const cell = newCells[pos];
const newCell = {
...cell,
isPrime:true
}
newCells[pos] = newCell;
return newCells;
}
const getNewCellVisitingToggled = (cells,pos)=>{
const newCells = cells.slice();
const cell = newCells[pos];
const newCell = {
...cell,
isVisiting:!cell.isVisiting
}
newCells[pos] = newCell;
return newCells;
}
const getNewCellCheckToggled = (cells,pos) =>{
const newCells = cells.slice();
const cell = newCells[pos];
const newCell = {
...cell,
isChecking:true
}
newCells[pos] = newCell;
return newCells;
}
const getCells = (rows)=>{
const cells = [];
for(let cell = 1;cell<=rows;cell++){
cells.push(createCell(cell))
}
return cells;
}
const createCell = (val)=>{
return {
val,
isChecking:false,
isVisiting:false,
isPrime:false
};
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
export default Seive;