Skip to content

Commit 0878244

Browse files
committed
updated encoding
1 parent 5befbc1 commit 0878244

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

src/PolylineAlgorithm/PolylineEncoding.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public static int Normalize(double value, CoordinateValueType type) {
198198
}
199199

200200
// Fast return if the value is zero, return 0 as the normalized value.
201-
if (value == 0.0) {
201+
if (value == default) {
202202
return 0;
203203
}
204204

@@ -221,19 +221,24 @@ public static int Normalize(double value, CoordinateValueType type) {
221221
/// integer.
222222
/// </param>
223223
/// <returns>
224-
/// The number of characters required to represent the <paramref name="variance"/> value, based on its magnitude.
225-
/// Returns a value between 1 and 6 inclusive.
224+
/// The number of characters required to represent the <paramref name="variance"/> value, based on its magnitude.
226225
/// </returns>
227226

228-
public static int GetCharCount(int variance) => variance switch {
229-
// DO NOT CHANGE THE ORDER. We are skipping inside exclusive ranges as those are covered by previous statements.
230-
>= -16 and <= +15 => 1,
231-
>= -512 and <= +511 => 2,
232-
>= -16384 and <= +16383 => 3,
233-
>= -524288 and <= +524287 => 4,
234-
>= -16777216 and <= +16777215 => 5,
235-
_ => 6,
236-
};
227+
public static int GetCharCount(int variance) {
228+
int rem = variance << 1;
229+
if (variance < 0) {
230+
rem = ~rem;
231+
}
232+
233+
int count = 1;
234+
235+
while (rem >= Defaults.Algorithm.Space) {
236+
rem >>= Defaults.Algorithm.ShiftLength;
237+
count++;
238+
}
239+
240+
return count;
241+
}
237242

238243
/// <summary>
239244
/// Validates whether the specified normalized value falls within the acceptable range for the given value type.

0 commit comments

Comments
 (0)