Skip to content

Commit 77f4a47

Browse files
authored
Update private.rs
* Use `position()` on `iter()` instead of for loop in `code_value` * Use arithmetic operator in `normalize_longitude` * Use `powi` for integer power in `compute_latitude_precision`
1 parent ecf5d75 commit 77f4a47

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

rust/src/private.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,19 @@ use interface::encode;
88
use geo::Point;
99

1010
pub fn code_value(chr: char) -> usize {
11-
for (i, c) in CODE_ALPHABET.iter().enumerate() {
12-
if chr == *c {
13-
return i;
14-
}
15-
}
1611
// We assume this function is only called by other functions that have
1712
// already ensured that the characters in the passed-in code are all valid
1813
// and have all been "treated" (upper-cased, padding and '+' stripped)
19-
//
20-
// If that is the case, we will always return above.
21-
unreachable!();
14+
CODE_ALPHABET.iter().position(|&x| x == char).unwrap()
2215
}
2316

2417
pub fn normalize_longitude(value: f64) -> f64 {
2518
let mut result: f64 = value;
2619
while result >= LONGITUDE_MAX {
27-
result = result - LONGITUDE_MAX * 2f64;
20+
result -= LONGITUDE_MAX * 2f64;
2821
}
2922
while result < -LONGITUDE_MAX{
30-
result = result + LONGITUDE_MAX * 2f64;
23+
result += LONGITUDE_MAX * 2f64;
3124
}
3225
result
3326
}
@@ -40,7 +33,7 @@ pub fn compute_latitude_precision(code_length: usize) -> f64 {
4033
if code_length <= PAIR_CODE_LENGTH {
4134
return ENCODING_BASE.powf((code_length as f64 / -2f64 + 2f64).floor())
4235
}
43-
ENCODING_BASE.powf(-3f64) / GRID_ROWS.powf(code_length as f64 - PAIR_CODE_LENGTH as f64)
36+
ENCODING_BASE.powi(-3f64) / GRID_ROWS.powf(code_length as f64 - PAIR_CODE_LENGTH as f64)
4437
}
4538

4639
pub fn prefix_by_reference(pt: Point<f64>, code_length: usize) -> String {

0 commit comments

Comments
 (0)