Skip to content

Commit 335d255

Browse files
committed
fix(Entity): Simplify target index finder for sorted entities
1 parent bdae25f commit 335d255

File tree

1 file changed

+9
-23
lines changed

1 file changed

+9
-23
lines changed

modules/entity/src/sorted_state_adapter.ts

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -88,35 +88,21 @@ export function createSortedStateAdapter<T>(
8888
}
8989
}
9090

91-
function findTargetIndex(
92-
state: R,
93-
model: T,
94-
left = 0,
95-
right = state.ids.length - 1
96-
) {
97-
if (right === -1) {
91+
function findTargetIndex(state: R, model: T) {
92+
if (state.ids.length === 0) {
9893
return 0;
9994
}
10095

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

103-
while (true) {
104-
middle = Math.floor((left + right) / 2);
105-
106-
const result = sort(state.entities[state.ids[middle]], model);
107-
108-
if (result === 0) {
109-
return middle;
110-
} else if (result < 0) {
111-
left = middle + 1;
112-
} else {
113-
right = middle - 1;
114-
}
115-
116-
if (left > right) {
117-
return state.ids.length - 1;
100+
if (isSmaller) {
101+
return i;
118102
}
119103
}
104+
105+
return state.ids.length - 1;
120106
}
121107

122108
return {

0 commit comments

Comments
 (0)