1+ import React , { Component } from 'react' ;
2+ import Cells from "./cells" ;
3+ import Navbar from "./navbar" ;
4+ import DiscreteSlider from "./slider" ;
5+ import Menu from "./menu" ;
6+
7+ class Seive extends Component {
8+ state = {
9+ number : 100 ,
10+ cells :[ ] ,
11+ isRunning :false ,
12+ speed :500
13+ }
14+
15+ constructor ( props ) {
16+ super ( props ) ;
17+ }
18+ componentDidMount ( ) {
19+ const cells = getCells ( this . state . number ) ;
20+ this . setState ( { cells} ) ;
21+ }
22+
23+ render ( ) {
24+ return (
25+ < div >
26+ < Navbar />
27+ < Menu
28+ onChangeSpeed = { this . changeSpeed }
29+ onChangeValues = { this . handleValueIncease }
30+ onVisualize = { this . startSeive }
31+ onRefresh = { this . handleRefresh }
32+ isDisabled = { this . state . isRunning }
33+ />
34+ < Cells
35+ num = { this . state . number }
36+ cells = { this . state . cells }
37+ />
38+
39+ </ div >
40+ ) ;
41+ }
42+
43+ changeSpeed = ( speed ) => {
44+ //console.log(typeof speed);
45+ this . setState ( { speed :600 - speed * 10 } ) ;
46+ }
47+ handleValueIncease = ( value ) => {
48+ this . setState ( { number :value } ) ;
49+ this . setState ( { cells :getCells ( value ) , isRunning :false } ) ;
50+ console . log ( value ) ;
51+ }
52+ handleRefresh = ( ) => {
53+ this . setState ( { cells :getCells ( this . state . number ) , isRunning :false } ) ;
54+ }
55+ startSeive = ( ) => {
56+ const speed = this . state . speed ;
57+ this . setState ( { isRunning :true } ) ;
58+ const prime = [ ] ;
59+ for ( let i = 0 ; i <= this . state . number ; i ++ ) {
60+ prime . push ( 1 ) ;
61+ }
62+ prime [ 0 ] = prime [ 1 ] = 0 ;
63+ let changedCells = this . state . cells ;
64+ let prevCheck = - 1 ;
65+ let counter = 0 ;
66+ for ( let i = 2 ; i <= this . state . number ; i ++ ) {
67+ if ( prime [ i ] === 1 ) {
68+ setTimeout ( ( ) => {
69+ changedCells = getNewCellPrimeToggled ( changedCells , i - 1 ) ;
70+ this . setState ( { cells :changedCells } ) ;
71+ } , counter * speed ) ;
72+ counter ++ ;
73+ for ( let j = i * i ; j <= this . state . number ; j += i ) {
74+ setTimeout ( ( ) => {
75+ if ( prevCheck != - 1 ) {
76+ changedCells = getNewCellVisitingToggled ( changedCells , prevCheck ) ;
77+ }
78+ prevCheck = j - 1 ;
79+ changedCells = getNewCellCheckToggled ( changedCells , j - 1 ) ;
80+ changedCells = getNewCellVisitingToggled ( changedCells , prevCheck ) ;
81+ this . setState ( { cells :changedCells } ) ;
82+ } , counter * speed ) ;
83+ counter ++ ;
84+ prime [ j ] = 0 ;
85+ }
86+ }
87+ }
88+ setTimeout ( ( ) => {
89+ changedCells = getNewCellVisitingToggled ( changedCells , prevCheck ) ;
90+ this . setState ( { cells :changedCells , isRunning :false } ) ;
91+ } , counter * speed ) ;
92+ }
93+ }
94+
95+ const getNewCellPrimeToggled = ( cells , pos ) => {
96+ const newCells = cells . slice ( ) ;
97+ const cell = newCells [ pos ] ;
98+ const newCell = {
99+ ...cell ,
100+ isPrime :true
101+ }
102+ newCells [ pos ] = newCell ;
103+ return newCells ;
104+ }
105+
106+ const getNewCellVisitingToggled = ( cells , pos ) => {
107+ const newCells = cells . slice ( ) ;
108+ const cell = newCells [ pos ] ;
109+ const newCell = {
110+ ...cell ,
111+ isVisiting :! cell . isVisiting
112+ }
113+ newCells [ pos ] = newCell ;
114+ return newCells ;
115+ }
116+
117+ const getNewCellCheckToggled = ( cells , pos ) => {
118+ const newCells = cells . slice ( ) ;
119+ const cell = newCells [ pos ] ;
120+ const newCell = {
121+ ...cell ,
122+ isChecking :true
123+ }
124+ newCells [ pos ] = newCell ;
125+ return newCells ;
126+ }
127+
128+ const getCells = ( rows ) => {
129+ const cells = [ ] ;
130+ for ( let cell = 1 ; cell <= rows ; cell ++ ) {
131+ cells . push ( createCell ( cell ) )
132+ }
133+ return cells ;
134+ }
135+ const createCell = ( val ) => {
136+ return {
137+ val,
138+ isChecking :false ,
139+ isVisiting :false ,
140+ isPrime :false
141+ } ;
142+ }
143+
144+ export default Seive ;
0 commit comments