1
- use crate :: { platform:: prelude:: * , util:: not_nan:: NotNaN } ;
1
+ use crate :: {
2
+ platform:: prelude:: * ,
3
+ util:: not_nan:: { NotNaN , PositiveNotNaN , PositiveNotNaNNotZero } ,
4
+ } ;
2
5
use alloc:: collections:: BinaryHeap ;
3
6
4
7
/// With a Fuzzy List, you can implement a fuzzy searching algorithm. The list
@@ -50,7 +53,7 @@ impl FuzzyList {
50
53
if heap. len ( ) >= max {
51
54
heap. pop ( ) ;
52
55
}
53
- heap. push ( ( NotNaN ( -score) , element) ) ;
56
+ heap. push ( ( -score, element) ) ;
54
57
}
55
58
}
56
59
@@ -60,7 +63,7 @@ impl FuzzyList {
60
63
if heap. len ( ) >= max {
61
64
heap. pop ( ) ;
62
65
}
63
- heap. push ( ( NotNaN ( -score) , element) ) ;
66
+ heap. push ( ( -score, element) ) ;
64
67
}
65
68
}
66
69
} else {
@@ -69,7 +72,7 @@ impl FuzzyList {
69
72
if heap. len ( ) >= max {
70
73
heap. pop ( ) ;
71
74
}
72
- heap. push ( ( NotNaN ( -score) , element) ) ;
75
+ heap. push ( ( -score, element) ) ;
73
76
}
74
77
}
75
78
}
@@ -78,53 +81,53 @@ impl FuzzyList {
78
81
}
79
82
}
80
83
81
- fn match_against ( pattern : & str , text : & str ) -> Option < f64 > {
82
- let ( mut current_score, mut total_score) = ( 0.0 , 0.0 ) ;
84
+ fn match_against ( pattern : & str , text : & str ) -> Option < NotNaN > {
85
+ let [ mut current_score, mut total_score] = [ PositiveNotNaN :: ZERO ; 2 ] ;
83
86
let mut pattern_chars = pattern. chars ( ) ;
84
87
let mut pattern_char = pattern_chars. next ( ) ;
85
88
86
89
for c in text. chars ( ) {
87
90
if pattern_char == Some ( c) {
88
91
pattern_char = pattern_chars. next ( ) ;
89
- current_score = 1.0 + 2.0 * current_score ;
92
+ current_score = current_score * PositiveNotNaNNotZero :: TWO + PositiveNotNaN :: ONE ;
90
93
} else {
91
- current_score = 0.0 ;
94
+ current_score = PositiveNotNaN :: ZERO ;
92
95
}
93
- total_score += current_score;
96
+ total_score = total_score + current_score;
94
97
}
95
98
96
99
if pattern_char. is_none ( ) {
97
- if pattern == text {
98
- Some ( f64 :: INFINITY )
100
+ Some ( if pattern == text {
101
+ NotNaN :: INFINITY
99
102
} else {
100
- Some ( total_score)
101
- }
103
+ total_score. into ( )
104
+ } )
102
105
} else {
103
106
None
104
107
}
105
108
}
106
109
107
- fn match_against_ascii ( pattern : & str , text : & str ) -> Option < f64 > {
108
- let ( mut current_score, mut total_score) = ( 0.0 , 0.0 ) ;
110
+ fn match_against_ascii ( pattern : & str , text : & str ) -> Option < NotNaN > {
111
+ let [ mut current_score, mut total_score] = [ PositiveNotNaN :: ZERO ; 2 ] ;
109
112
let mut pattern_chars = pattern. bytes ( ) ;
110
113
let mut pattern_char = pattern_chars. next ( ) ;
111
114
112
115
for c in text. bytes ( ) {
113
116
if pattern_char == Some ( c) {
114
117
pattern_char = pattern_chars. next ( ) ;
115
- current_score = 1.0 + 2.0 * current_score ;
118
+ current_score = current_score * PositiveNotNaNNotZero :: TWO + PositiveNotNaN :: ONE ;
116
119
} else {
117
- current_score = 0.0 ;
120
+ current_score = PositiveNotNaN :: ZERO ;
118
121
}
119
- total_score += current_score;
122
+ total_score = total_score + current_score;
120
123
}
121
124
122
125
if pattern_char. is_none ( ) {
123
- if pattern == text {
124
- Some ( f64 :: INFINITY )
126
+ Some ( if pattern == text {
127
+ NotNaN :: INFINITY
125
128
} else {
126
- Some ( total_score)
127
- }
129
+ total_score. into ( )
130
+ } )
128
131
} else {
129
132
None
130
133
}
0 commit comments