Skip to content
Open
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
2 changes: 1 addition & 1 deletion data_structures/binary_search_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ export class BinarySearchTree<T> implements Iterable<T> {
}
const values: Iterable<U> = options?.map
? Array.from(unmappedValues, options.map, options.thisArg)
: unmappedValues as U[];
: Array.from(unmappedValues) as unknown as U[];
for (const value of values) result.insert(value);
return result;
}
Expand Down
20 changes: 20 additions & 0 deletions data_structures/binary_search_tree_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,26 @@ Deno.test("BinarySearchTree contains objects", () => {
assertEquals(tree.isEmpty(), true);
});

Deno.test("BinarySearchTree.from() handles empty array-like objects", () => {
const tree = BinarySearchTree.from<number>({ length: 0 });
assertEquals([...tree], []);
assertEquals(tree.size, 0);
});

Deno.test("BinarySearchTree.from() handles array-like objects", () => {
const values = { 0: 3, 1: 1, 2: 3, 3: 2, length: 4 };
const tree = BinarySearchTree.from(values);
assertEquals([...tree], [1, 2, 3]);
assertEquals(tree.size, 3);
});

Deno.test("BinarySearchTree.from() handles array-like objects with a custom comparator", () => {
const values = { 0: 3, 1: 1, 2: 3, 3: 2, length: 4 };
const tree = BinarySearchTree.from(values, { compare: descend });
assertEquals([...tree], [3, 2, 1]);
assertEquals(tree.size, 3);
});

Deno.test("BinarySearchTree.from() handles iterable", () => {
const values: number[] = [-10, 9, -1, 100, 9, 1, 0, 9, -100, 10, -9];
const originalValues: number[] = Array.from(values);
Expand Down
2 changes: 1 addition & 1 deletion data_structures/red_black_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export class RedBlackTree<T> extends BinarySearchTree<T> {
}
const values: Iterable<U> = options?.map
? Array.from(unmappedValues, options.map, options.thisArg)
: unmappedValues as U[];
: Array.from(unmappedValues) as unknown as U[];
for (const value of values) result.insert(value);
return result;
}
Expand Down
20 changes: 20 additions & 0 deletions data_structures/red_black_tree_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,26 @@ Deno.test("RedBlackTree works with object items", () => {
assertEquals(tree.isEmpty(), true);
});

Deno.test("RedBlackTree.from() handles empty array-like objects", () => {
const tree = RedBlackTree.from<number>({ length: 0 });
assertEquals([...tree], []);
assertEquals(tree.size, 0);
});

Deno.test("RedBlackTree.from() handles array-like objects", () => {
const values = { 0: 3, 1: 1, 2: 3, 3: 2, length: 4 };
const tree = RedBlackTree.from(values);
assertEquals([...tree], [1, 2, 3]);
assertEquals(tree.size, 3);
});

Deno.test("RedBlackTree.from() handles array-like objects with a custom comparator", () => {
const values = { 0: 3, 1: 1, 2: 3, 3: 2, length: 4 };
const tree = RedBlackTree.from(values, { compare: descend });
assertEquals([...tree], [3, 2, 1]);
assertEquals(tree.size, 3);
});

Deno.test("RedBlackTree.from() handles Iterable", () => {
const values: number[] = [-10, 9, -1, 100, 9, 1, 0, 9, -100, 10, -9];
const originalValues: number[] = Array.from(values);
Expand Down
Loading