<s> """<STR_LIT>""" <EOL> from collections import defaultdict <EOL> from . import ast <EOL> from . import util <EOL> from . tiler import ASTPattern , SimplePattern , tile <EOL> def select_rewrite_expression ( settings , name , exprs ) : <EOL> """<STR_LIT>""" <EOL> sets = [ e . left . value for e in exprs ] <EOL> counts = defaultdict ( int ) <EOL> total = <NUM_LIT:0> <EOL> for s in sets : <EOL> for item in s : <EOL> counts [ item ] += <NUM_LIT:1> <EOL> total += <NUM_LIT:1> <EOL> scores = [ ] <EOL> total = float ( total ) <EOL> for idx , s in enumerate ( sets ) : <EOL> freqs = [ counts [ i ] / total for i in s ] <EOL> hm = util . harmonic_mean ( freqs ) <EOL> scores . append ( ( hm , exprs [ idx ] ) ) <EOL> sorted ( scores , key = lambda p : p [ <NUM_LIT:0> ] , reverse = True ) <EOL> if scores [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] < settings . min_density : <EOL> return None <EOL> return scores [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> def contains_rewrite ( node , name , expr , assumed_result ) : <EOL> """<STR_LIT>""" <EOL> expr_set = expr . left . value <EOL> def replace_func ( pattern , node ) : <EOL> if assumed_result is True : <EOL> set_prime = node . left . value & expr_set <EOL> if set_prime == expr_set : <EOL> return ast . Constant ( True ) <EOL> elif len ( set_prime ) == <NUM_LIT:0> : <EOL> return ast . Constant ( False ) <EOL> set_prime_diff = expr_set - node . left . value <EOL> if len ( set_prime_diff ) < len ( set_prime ) : <EOL> node . left . value = set_prime_diff <EOL> return ast . NegateOperator ( node ) <EOL> else : <EOL> set_prime = node . left . value - expr_set <EOL> if len ( set_prime ) == <NUM_LIT:0> : <EOL> return ast . Constant ( False ) <EOL> node . left . value = set_prime <EOL> return None <EOL> pattern = SimplePattern ( "<STR_LIT>" , <EOL> "<STR_LIT>" , ASTPattern ( expr . right ) ) <EOL> return tile ( node , [ pattern ] , replace_func ) </s>
