File tree Expand file tree Collapse file tree 1 file changed +12
-8
lines changed Expand file tree Collapse file tree 1 file changed +12
-8
lines changed Original file line number Diff line number Diff line change @@ -42,8 +42,6 @@ enum TsortError {
42
42
43
43
impl UError for TsortError { }
44
44
45
-
46
-
47
45
#[ uucore:: main]
48
46
pub fn uumain ( args : impl uucore:: Args ) -> UResult < ( ) > {
49
47
let matches = uucore:: clap_localization:: handle_clap_result ( uu_app ( ) , args) ?;
@@ -65,17 +63,23 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
65
63
66
64
// Create the directed graph from pairs of tokens in the input data.
67
65
let mut g = Graph :: new ( input. to_string_lossy ( ) . to_string ( ) ) ;
68
-
69
- let mut edgetokens = data. split_whitespace ( ) ;
70
-
66
+ // Input is considered to be in the format
67
+ // From1 To1 From2 To2 ...
68
+ // with tokens separated by whitespaces
69
+ let mut edge_tokens = data. split_whitespace ( ) ;
70
+ // Note: this is equivalent to iterating over edge_tokens.chunks(2)
71
+ // but chunks() exists only for slices and would require unnecessary Vec allocation.
72
+ // Itertools::chunks() is not used due to unnecessary overhead for internal RefCells
71
73
loop {
72
- let Some ( a) = edgetokens. next ( ) else {
74
+ // Try take next pair of tokens
75
+ let Some ( from) = edge_tokens. next ( ) else {
76
+ // no more tokens -> end of input. Graph constructed
73
77
break ;
74
78
} ;
75
- let Some ( b ) = edgetokens . next ( ) else {
79
+ let Some ( to ) = edge_tokens . next ( ) else {
76
80
return Err ( TsortError :: NumTokensOdd ( input. to_string_lossy ( ) . to_string ( ) ) . into ( ) ) ;
77
81
} ;
78
- g. add_edge ( a , b ) ;
82
+ g. add_edge ( from , to ) ;
79
83
}
80
84
81
85
g. run_tsort ( ) ;
You can’t perform that action at this time.
0 commit comments