Skip to content

Commit

Permalink
fix(Entity): Simplify target index finder for sorted entities
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRyanDev committed Aug 18, 2017
1 parent bdae25f commit 335d255
Showing 1 changed file with 9 additions and 23 deletions.
32 changes: 9 additions & 23 deletions modules/entity/src/sorted_state_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,35 +88,21 @@ export function createSortedStateAdapter<T>(
}
}

function findTargetIndex(
state: R,
model: T,
left = 0,
right = state.ids.length - 1
) {
if (right === -1) {
function findTargetIndex(state: R, model: T) {
if (state.ids.length === 0) {
return 0;
}

let middle: number;
for (let i = 0; i < state.ids.length; i++) {
const entity = state.entities[state.ids[i]];
const isSmaller = sort(model, entity) < 0;

while (true) {
middle = Math.floor((left + right) / 2);

const result = sort(state.entities[state.ids[middle]], model);

if (result === 0) {
return middle;
} else if (result < 0) {
left = middle + 1;
} else {
right = middle - 1;
}

if (left > right) {
return state.ids.length - 1;
if (isSmaller) {
return i;
}
}

return state.ids.length - 1;
}

return {
Expand Down

0 comments on commit 335d255

Please sign in to comment.