Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes bugs in findStopLessThanOrEqualTo algorithm #8134

Merged
merged 9 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/style-spec/expression/stops.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ export type Stops = Array<[number, Expression]>;
* @private
*/
export function findStopLessThanOrEqualTo(stops: Array<number>, input: number) {
const n = stops.length;
const lastIndex = stops.length - 1;
let lowerIndex = 0;
let upperIndex = n - 1;
let upperIndex = lastIndex;
let currentIndex = 0;
let currentValue, upperValue;
let currentValue, nextValue;

while (lowerIndex <= upperIndex) {
currentIndex = Math.floor((lowerIndex + upperIndex) / 2);
currentValue = stops[currentIndex];
upperValue = stops[currentIndex + 1];
if (input === currentValue || input > currentValue && input < upperValue) { // Search complete
return currentIndex;
} else if (currentValue < input) {
nextValue = stops[currentIndex + 1];

if (currentValue <= input) {
Mike96Angelo marked this conversation as resolved.
Show resolved Hide resolved
if (currentIndex === lastIndex || input < nextValue) { // Search complete
return currentIndex;
}

lowerIndex = currentIndex + 1;
} else if (currentValue > input) {
upperIndex = currentIndex - 1;
Expand All @@ -32,5 +35,5 @@ export function findStopLessThanOrEqualTo(stops: Array<number>, input: number) {
}
}

return Math.max(currentIndex - 1, 0);
return 0;
}
33 changes: 3 additions & 30 deletions src/style-spec/function/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as interpolate from '../util/interpolate';
import Interpolate from '../expression/definitions/interpolate';
import Formatted from '../expression/types/formatted';
import { supportsInterpolation } from '../util/properties';
import { findStopLessThanOrEqualTo } from '../expression/stops';

export function isFunction(value) {
return typeof value === 'object' && value !== null && !Array.isArray(value);
Expand Down Expand Up @@ -145,7 +146,7 @@ function evaluateIntervalFunction(parameters, propertySpec, input) {
if (input <= parameters.stops[0][0]) return parameters.stops[0][1];
if (input >= parameters.stops[n - 1][0]) return parameters.stops[n - 1][1];

const index = findStopLessThanOrEqualTo(parameters.stops, input);
const index = findStopLessThanOrEqualTo(parameters.stops.map((stop) => stop[0]), input);

return parameters.stops[index][1];
}
Expand All @@ -160,7 +161,7 @@ function evaluateExponentialFunction(parameters, propertySpec, input) {
if (input <= parameters.stops[0][0]) return parameters.stops[0][1];
if (input >= parameters.stops[n - 1][0]) return parameters.stops[n - 1][1];

const index = findStopLessThanOrEqualTo(parameters.stops, input);
const index = findStopLessThanOrEqualTo(parameters.stops.map((stop) => stop[0]), input);
const t = interpolationFactor(
input, base,
parameters.stops[index][0],
Expand Down Expand Up @@ -203,34 +204,6 @@ function evaluateIdentityFunction(parameters, propertySpec, input) {
return coalesce(input, parameters.default, propertySpec.default);
}

/**
* Returns the index of the last stop <= input, or 0 if it doesn't exist.
*
* @private
*/
function findStopLessThanOrEqualTo(stops, input) {
const n = stops.length;
let lowerIndex = 0;
let upperIndex = n - 1;
let currentIndex = 0;
let currentValue, upperValue;

while (lowerIndex <= upperIndex) {
currentIndex = Math.floor((lowerIndex + upperIndex) / 2);
currentValue = stops[currentIndex][0];
upperValue = stops[currentIndex + 1][0];
if (input === currentValue || input > currentValue && input < upperValue) { // Search complete
return currentIndex;
} else if (currentValue < input) {
lowerIndex = currentIndex + 1;
} else if (currentValue > input) {
upperIndex = currentIndex - 1;
}
}

return Math.max(currentIndex - 1, 0);
}

/**
* Returns a ratio that can be used to interpolate between exponential function
* stops.
Expand Down
27 changes: 27 additions & 0 deletions test/unit/style-spec/stops.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { test } from 'mapbox-gl-js-test';
import { findStopLessThanOrEqualTo } from '../../../src/style-spec/expression/stops';

test('findStopLessThanOrEqualTo', (t) => {
test('When the input > all stops it returns the last stop.', (t) => {
const index = findStopLessThanOrEqualTo([0, 1, 2, 3, 4, 5, 6, 7], 8);
t.equal(index, 7);
t.end();
});

test('When more than one stop has the same value it always returns the last stop', (t) => {
let index;

index = findStopLessThanOrEqualTo([0.5, 0.5], 0.5);
t.equal(index, 1);

index = findStopLessThanOrEqualTo([0.5, 0.5, 0.5], 0.5);
t.equal(index, 2);

index = findStopLessThanOrEqualTo([0.4, 0.5, 0.5, 0.6, 0.7], 0.5);
t.equal(index, 2);

t.end();
});

t.end();
});