@@ -2,10 +2,11 @@ use halfbrown::HashMap;
22use smallvec:: { smallvec, SmallVec } ;
33use std:: collections:: BinaryHeap ;
44use std:: fmt;
5+ use std:: fmt:: Write ;
56
67use crate :: { score:: Score , score_cache:: ScoreCache } ;
78
8- const NON_WINNING_SCORES_COUNT : usize = ( 3 as usize ) . pow ( 5 ) - 1 ;
9+ const NON_WINNING_SCORES_COUNT : usize = 3_usize . pow ( 5 ) - 1 ;
910const SMALLVEC_MAX : usize = 4 ;
1011
1112type WordIndex = u16 ;
@@ -50,18 +51,18 @@ struct Guess<'a> {
5051
5152impl < ' a > Ord for Guess < ' a > {
5253 fn cmp ( & self , other : & Self ) -> std:: cmp:: Ordering {
53- return other. avg_score . partial_cmp ( & self . avg_score ) . unwrap ( ) ;
54+ other. avg_score . partial_cmp ( & self . avg_score ) . unwrap ( )
5455 }
5556}
5657impl < ' a > PartialOrd for Guess < ' a > {
5758 fn partial_cmp ( & self , other : & Self ) -> Option < std:: cmp:: Ordering > {
58- return Some ( self . cmp ( other) ) ;
59+ Some ( self . cmp ( other) )
5960 }
6061}
6162impl < ' a > Eq for Guess < ' a > { }
6263impl < ' a > PartialEq for Guess < ' a > {
6364 fn eq ( & self , other : & Self ) -> bool {
64- return self . guess == other. guess ;
65+ self . guess == other. guess
6566 }
6667}
6768
@@ -121,37 +122,33 @@ impl<'a> Game<'a> {
121122 }
122123 }
123124
124- pub fn print_current_best_score ( & self , words : & ' a Vec < String > ) -> String {
125+ pub fn print_current_best_score ( & self , words : & ' a [ String ] ) -> String {
125126 if let Game :: Large ( g) = self {
126127 if let OptimizationStatus :: InProgress ( ref heap) = g. optimization {
127128 let mut result = String :: new ( ) ;
128129 for c in heap. iter ( ) . take ( 3 ) {
129- result . push_str ( & format ! ( "{}-{} " , words[ c. guess as usize ] , c. avg_score) ) ;
130+ write ! ( result , "{}-{} " , words[ c. guess as usize ] , c. avg_score) . unwrap ( ) ;
130131 }
131132 return result;
132133 }
133134 }
134135 String :: from ( "optimization is over" )
135136 }
136137
137- pub fn print_tree ( & self , words : & ' a Vec < String > ) -> String {
138+ pub fn print_tree ( & self , words : & ' a [ String ] ) -> String {
138139 match self {
139140 Game :: Single ( w) => format ! ( "{} !" , words[ * w as usize ] ) ,
140141 Game :: Double ( w1, w2) => format ! ( "{} or {} !" , words[ * w1 as usize ] , words[ * w2 as usize ] ) ,
141142 Game :: Large ( g) => {
142143 if let OptimizationStatus :: Done ( ref guess) = g. optimization {
143144 let word = & words[ guess. guess as usize ] ;
144- if guess. subgames . len ( ) == 0 {
145- word. clone ( ) + "!"
146- } else {
147- let mut lines: Vec < String > = vec ! [ ] ;
148- for ( score, sub) in guess. subgames . iter ( ) {
149- for l in sub. print_tree ( words) . lines ( ) {
150- lines. push ( format ! ( "{} {} {}" , word, score, l) ) ;
151- }
145+ let mut result = String :: new ( ) ;
146+ for ( score, sub) in guess. subgames . iter ( ) {
147+ for l in sub. print_tree ( words) . lines ( ) {
148+ writeln ! ( result, "{} {} {}" , word, score, l) . unwrap ( ) ;
152149 }
153- lines. join ( "\n " )
154150 }
151+ result
155152 } else {
156153 panic ! ( "optimization not done" )
157154 }
@@ -164,11 +161,7 @@ impl<'a> Game<'a> {
164161 Game :: Single ( _) => true ,
165162 Game :: Double ( _, _) => true ,
166163 Game :: Large ( g) => {
167- if let OptimizationStatus :: Done ( _) = g. optimization {
168- true
169- } else {
170- false
171- }
164+ matches ! ( g. optimization, OptimizationStatus :: Done ( _) )
172165 }
173166 }
174167 }
0 commit comments