1- //hallo
1+ /**
2+ * File created by Philip Hoang 06.2.18
3+ * based on QuickFind.Java
4+ */
5+ ///<reference path="controller.ts"/>
6+
7+ const DELAY : number = 100 ;
8+ let arr : number [ ] ;
9+ let pause : boolean ;
10+ let name : string = "Quick Find" ;
11+
12+ // noninspection JSAnnotator
13+ function constructor ( size : number ) {
14+ arr = new Array ( size ) ;
15+ for ( let i = 0 ; i < size ; i ++ ) {
16+ arr [ i ] = i ;
17+ }
18+
19+ pause = false ;
20+ }
21+
22+ function union ( aIndex : number , bIndex : number ) {
23+ let aRoot : number = arr [ aIndex ] ;
24+ let bRoot : number = arr [ bIndex ] ;
25+
26+ if ( aRoot == bRoot ) {
27+ delay ( getDelayTime ( ) ) ;
28+ control . setSelectedIndex ( aIndex , false ) ;
29+ control . setSelectedIndex ( bIndex , false ) ;
30+ return ;
31+ }
32+
33+ control . saveState ( arr ) ;
34+
35+ for ( let i = 0 ; i < arr . length ; i ++ ) {
36+
37+ while ( pause ) {
38+ delay ( getDelayTime ( ) * 0.2 ) ;
39+ }
40+
41+ control . setArrow ( i ) ;
42+ delay ( getDelayTime ( ) * 0.5 )
43+ if ( arr [ i ] == aRoot ) {
44+ control . setValueAtIndex ( i , bRoot ) ;
45+ control . connectNodes ( i , bRoot ) ;
46+ arr [ i ] = bRoot ;
47+ delay ( getDelayTime ( ) * 0.5 )
48+ }
49+ }
50+
51+ control . setArrow ( - 1 ) ;
52+ control . setSelectedIndex ( bIndex , false ) ;
53+ control . setSelectedIndex ( aIndex , false ) ;
54+ }
55+
56+ function connected ( aIndex : number , bIndex : number ) {
57+ let connected : boolean = simpleFind ( aIndex , "orange" ) == simpleFind ( bIndex , "orange" ) ;
58+
59+ if ( connected ) {
60+ control . highlightNode ( arr [ aIndex ] , "green" ) ;
61+ control . highlightNode ( arr [ bIndex ] , "green" ) ;
62+ control . checkMark ( aIndex , bIndex , true ) ;
63+ } else
64+ control . redCross ( aIndex , bIndex , true ) ;
65+
66+ delay ( getDelayTime ( ) * 2 ) ;
67+ removeHighlighFromRoot ( aIndex ) ;
68+ removeHighlighFromRoot ( bIndex ) ;
69+ control . checkMark ( aIndex , bIndex , false ) ;
70+ control . redCross ( aIndex , bIndex , false ) ;
71+
72+ return connected
73+ }
74+
75+ function getName ( ) {
76+ return name ;
77+ }
78+
79+ function removeHighlighFromRoot ( pIndex : number ) {
80+ control . removeHighlight ( arr [ pIndex ] ) ;
81+ }
82+
83+ function delay ( delayTime : number ) {
84+ let start = new Date ( ) . getTime ( ) ;
85+ for ( let i = 0 ; i < 1e7 ; i ++ ) {
86+ if ( ( new Date ( ) . getTime ( ) - start ) > delayTime )
87+ break ;
88+ }
89+ }
90+
91+ function find ( pIndex : number ) {
92+ let root = simpleFind ( pIndex , "green" ) ;
93+
94+ delay ( getDelayTime ( ) ) ;
95+ control . removeHighlight ( root ) ;
96+ control . setSelectedIndex ( pIndex , false ) ;
97+
98+ return root ;
99+ }
100+
101+ function simpleFind ( pIndex : number , color : string ) {
102+ let root : number = arr [ pIndex ] ;
103+
104+ if ( pIndex != root ) {
105+ control . highlightNode ( pIndex , "orange" ) ;
106+ delay ( getDelayTime ( ) ) ;
107+ control . removeHighlight ( pIndex ) ;
108+ }
109+
110+ control . highlightNode ( root , color ) ;
111+
112+ return root ;
113+ }
114+
115+ function getArray ( ) {
116+ return arr ;
117+ }
118+
119+ function setArray ( array : number [ ] ) {
120+ arr = array ;
121+ }
122+
123+ function isPause ( ) {
124+ return pause ;
125+ }
126+
127+ function invertPause ( ) {
128+ pause = ! pause ;
129+ }
130+
131+ function connectedNoGUIUpdate ( a : number , b : number ) {
132+ return arr [ a ] == arr [ b ] ;
133+ }
134+
135+ function getDelayTime ( ) {
136+ return DELAY_TIME + control . getSpeed ( ) * 50 ;
137+ }
0 commit comments