Skip to content

Commit

Permalink
binarySearch
Browse files Browse the repository at this point in the history
  • Loading branch information
mbraak committed Dec 4, 2024
1 parent d04015c commit 91df7a4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 16 deletions.
27 changes: 27 additions & 0 deletions src/dragAndDropHandler/binarySearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function binarySearch<T>(items: T[], compareFn: (a: T) => number): null | T {
let low = 0;
let high = items.length;

while (low < high) {
const mid = (low + high) >> 1;
const item = items[mid];

if (item === undefined) {
return null;
}

const compareResult = compareFn(item);

if (compareResult > 0) {
high = mid;
} else if (compareResult < 0) {
low = mid + 1;
} else {
return item;
}
}

return null;
}

export default binarySearch;
22 changes: 6 additions & 16 deletions src/dragAndDropHandler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Node } from "../node";
import NodeElement from "../nodeElement";
import { getPositionName, Position } from "../position";
import { getElementPosition } from "../util";
import binarySearch from "./binarySearch";
import DragElement from "./dragElement";
import generateHitAreas from "./generateHitAreas";
import { DropHint, HitArea } from "./types";
Expand Down Expand Up @@ -143,26 +144,15 @@ export class DragAndDropHandler {
return null;
}

let low = 0;
let high = this.hitAreas.length;
while (low < high) {
const mid = (low + high) >> 1;
const area = this.hitAreas[mid];

if (!area) {
return null;
}

return binarySearch<HitArea>(this.hitAreas, (area) => {
if (y < area.top) {
high = mid;
return 1;
} else if (y > area.bottom) {
low = mid + 1;
return -1;
} else {
return area;
return 0;
}
}

return null;
});
}

private generateHitAreas(): void {
Expand Down
31 changes: 31 additions & 0 deletions src/test/dragAndDropHandler/binarySearch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import binarySearch from "../../dragAndDropHandler/binarySearch";

it("returns null when the array is empty", () => {
const compareFn = (_item: number) => 0;

const result = binarySearch<number>([], compareFn);
expect(result).toBeNull();
});

it("finds a value", () => {
const compareFn = (item: number) => item - 5;

const result = binarySearch<number>([1, 5, 7, 9], compareFn);
expect(result).toEqual(5);
});

it("returns null when the value doesn't exist", () => {
const compareFn = (item: number) => item - 6;

const result = binarySearch<number>([1, 5, 7, 9], compareFn);
expect(result).toBeNull();
});

it("handles undefined values in the array", () => {
const compareFn = (item: number) => item - 6;
const array = [1, 5, 7, 9];
(array as any)[1] = undefined;

Check failure on line 27 in src/test/dragAndDropHandler/binarySearch.test.ts

View workflow job for this annotation

GitHub Actions / runner-job

Unsafe member access [1] on an `any` value

const result = binarySearch<number>(array, compareFn);
expect(result).toBeNull();
});

0 comments on commit 91df7a4

Please sign in to comment.