11#![ allow( non_snake_case) ]
22use std:: thread;
33use std:: env;
4- use std:: process:: Command ;
54use std:: sync:: mpsc:: SyncSender ;
65use std:: sync:: mpsc:: sync_channel;
76use std:: fs:: File ;
87use std:: io:: prelude:: * ;
98use std:: time:: Instant ;
109
11- const maxThreads: i8 = 8 ;
1210
1311struct CompleteTask {
1412 startNode : i16 ,
1513 paths : Vec < Vec < i16 > > ,
1614}
1715
1816fn main ( ) {
17+ let mut argI: i8 = 0 ;
1918 let mut _totalNodes: String = "0" . to_string ( ) ;
19+ let mut _maxThreads: String = "8" . to_string ( ) ;
2020 for argument in env:: args ( ) {
21- _totalNodes = argument. to_string ( ) ;
21+ if argI == 1 {
22+ _totalNodes = argument. to_string ( ) ;
23+ } else if argI == 2 {
24+ _maxThreads = argument. to_string ( ) ;
25+ }
26+ argI += 1 ;
2227 }
2328 println ! ( "You have chosen to order numbers upto {}" , _totalNodes) ;
29+ println ! ( "You have chosen to hava a max number of threads of {}" , _maxThreads) ;
2430 let totalNodes: i16 = _totalNodes. parse :: < i16 > ( ) . unwrap ( ) ;
31+ let maxThreads: i8 = _maxThreads. parse :: < i8 > ( ) . unwrap ( ) ;
2532 let now = Instant :: now ( ) ;
2633
27- let paths = getCore ( totalNodes, now) ;
34+ let paths = getCore ( totalNodes, maxThreads , now) ;
2835
2936 let endTime = now. elapsed ( ) . as_secs ( ) ;
3037 printResult ( paths, totalNodes) ;
3138 println ! ( "It took aproximately {} seconds to compute" , endTime) ;
3239}
3340
34- fn getCore ( totalNodes : i16 , now : Instant ) -> Vec < Vec < i16 > > {
41+ fn getCore ( totalNodes : i16 , maxThreads : i8 , now : Instant ) -> Vec < Vec < i16 > > {
3542 let connections: Vec < [ i16 ; 2 ] > = findConnections ( totalNodes) ;
3643 let ( sender, reciever) = sync_channel ( maxThreads as usize ) ;
3744 let mut paths: Vec < Vec < i16 > > = vec ! [ ] ;
@@ -67,7 +74,7 @@ fn startCalcAsync(startNode:i16, connections:Vec<[i16; 2]>, totalNodes:i16, send
6774 path. push ( startNode) ;
6875
6976 thread:: spawn ( move || {
70- let paths = doThread ( connections, path, totalNodes) ;
77+ let paths = doThread ( connections, & mut path. clone ( ) , totalNodes) ;
7178 sender. send ( CompleteTask {
7279 startNode : startNode,
7380 paths : paths,
@@ -84,7 +91,7 @@ fn printResult(paths:Vec<Vec<i16>>, totalNodes:i16){
8491 return ;
8592 } ,
8693 } ;
87- let mut completePaths: i16 = 0 ;
94+ let mut completePaths: i32 = 0 ;
8895 for path in paths. clone ( ) {
8996 if path. len ( ) == totalNodes as usize {
9097 write ! ( file, "A working path is " ) ;
@@ -102,8 +109,7 @@ fn printResult(paths:Vec<Vec<i16>>, totalNodes:i16){
102109 println ! ( "All working paths have been outputted to Output.txt" ) ;
103110}
104111
105- fn doThread ( connections : Vec < [ i16 ; 2 ] > , _path : Vec < i16 > , totalNodes : i16 ) -> Vec < Vec < i16 > > {
106- let mut path = _path. clone ( ) ;
112+ fn doThread ( connections : Vec < [ i16 ; 2 ] > , path : & mut Vec < i16 > , totalNodes : i16 ) -> Vec < Vec < i16 > > {
107113 let mut paths: Vec < Vec < i16 > > = Vec :: with_capacity ( totalNodes as usize ) ;
108114
109115 loop {
@@ -128,7 +134,7 @@ fn doThread(connections:Vec<[i16; 2]>, _path:Vec<i16>, totalNodes:i16) -> Vec<Ve
128134 let mut newPath = path. clone ( ) ;
129135 newPath. push ( connectedTo) ;
130136 let newConnections = connections. clone ( ) ;
131- paths. append ( & mut doThread ( newConnections, newPath, totalNodes) ) ;
137+ paths. append ( & mut doThread ( newConnections, & mut newPath, totalNodes) ) ;
132138 }
133139 }
134140 }
@@ -139,7 +145,7 @@ fn doThread(connections:Vec<[i16; 2]>, _path:Vec<i16>, totalNodes:i16) -> Vec<Ve
139145 }
140146 }
141147
142- paths. push ( path) ;
148+ paths. push ( path. clone ( ) ) ;
143149 return paths;
144150}
145151
0 commit comments