Repository navigation
Expand file tree
/
Copy pathcode.js
More file actions
70 lines (59 loc) · 1.81 KB
/
Copy pathcode.js
File metadata and controls
70 lines (59 loc) · 1.81 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
logger._print( 'original array = [' + D.join(', ') + ']' );
var N = D.length;
var writes = 0;
var pos;
var item;
var temp;
for( cycleStart=0; cycleStart<=N-2; cycleStart++ ){
item = D[cycleStart];
pos = cycleStart;
tracer._select(cycleStart);
for( i=cycleStart+1; i<=N-1; i++ ){
tracer._select(i)._wait()._deselect(i);
if( D[i]<item ){
pos++;
}
}
if( pos == cycleStart ){
tracer._deselect(cycleStart);
continue;
}
while( item == D[pos] ){
pos++;
}
temp = D[pos];
D[pos] = item;
item = temp;
if( pos !== cycleStart ){
logger._print( 'Rewrite '+D[pos]+' to index '+pos+'; the next value to rewrite is '+item );
}else{
logger._print( 'Rewrite '+D[pos]+' to index '+pos);
}
tracer._select(pos)._wait()._deselect(pos);
tracer._notify(pos, D[pos])._notify(cycleStart, D[cycleStart])._wait()._denotify(pos)._denotify(cycleStart);
while( pos != cycleStart ){
pos = cycleStart;
for( i=cycleStart+1; i<=N-1; i++ ){
tracer._select(i)._wait()._deselect(i);
if( D[i]<item ){
pos++;
}
}
while( item == D[pos] ){
pos++;
}
temp = D[pos];
D[pos] = item;
item = temp;
if( pos !== cycleStart ){
logger._print( 'Rewrite '+D[pos]+' to index '+pos+'; the next value to rewrite is '+item );
}else{
logger._print( 'Rewrite '+D[pos]+' to index '+pos);
}
tracer._select(pos)._wait()._deselect(pos);
tracer._notify(pos, D[pos])._notify(cycleStart, D[cycleStart])._wait()._denotify(pos)._denotify(cycleStart);
writes++;
}
writes++;
}
logger._print( 'Number of writes performed is ' + writes );