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

refactor(data_structures): prepare for noUncheckedIndexedAccess #4146

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 9 additions & 9 deletions data_structures/binary_heap.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file "data_structures/binary_heap.ts" might need a bit of a closer review. The implementation seems sound for now but this won't help us catch issues in future. Are you happy leaving them as non-null assertions for now?

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { descend } from "./comparators.ts";

/** Swaps the values at two indexes in an array. */
function swap<T>(array: T[], a: number, b: number) {
const temp: T = array[a];
array[a] = array[b];
array[b] = temp;
const temp = array[a];
array[a] = array[b]!;
array[b] = temp!;
}

/** Returns the parent index for a child index. */
Expand Down Expand Up @@ -132,11 +132,11 @@ export class BinaryHeap<T> implements Iterable<T> {
let right: number = 2 * (parent + 1);
let left: number = right - 1;
while (left < size) {
const greatestChild =
right === size || this.compare(this.#data[left], this.#data[right]) <= 0
? left
: right;
if (this.compare(this.#data[greatestChild], this.#data[parent]) < 0) {
const greatestChild = right === size ||
this.compare(this.#data[left]!, this.#data[right]!) <= 0
? left
: right;
if (this.compare(this.#data[greatestChild]!, this.#data[parent]!) < 0) {
swap(this.#data, parent, greatestChild);
parent = greatestChild;
} else {
Expand All @@ -155,7 +155,7 @@ export class BinaryHeap<T> implements Iterable<T> {
let parent: number = getParentIndex(index);
this.#data.push(value);
while (
index !== 0 && this.compare(this.#data[index], this.#data[parent]) < 0
index !== 0 && this.compare(this.#data[index]!, this.#data[parent]!) < 0
) {
swap(this.#data, parent, index);
index = parent;
Expand Down
14 changes: 7 additions & 7 deletions data_structures/binary_heap_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Deno.test("BinaryHeap works with default descend comparator", () => {
assertEquals(maxHeap.length, 0);
assertEquals(maxHeap.isEmpty(), true);
assertEquals(maxHeap.peek(), undefined);
for (let i = 0; i < values.length; i++) {
assertEquals(maxHeap.push(values[i]), i + 1);
for (const [i, value] of values.entries()) {
assertEquals(maxHeap.push(value), i + 1);
}
assertEquals(maxHeap.length, values.length);
assertEquals(maxHeap.isEmpty(), false);
Expand Down Expand Up @@ -50,8 +50,8 @@ Deno.test("BinaryHeap works with ascend comparator", () => {
assertEquals(minHeap.length, 0);
assertEquals(minHeap.isEmpty(), true);
assertEquals(minHeap.peek(), undefined);
for (let i = 0; i < values.length; i++) {
assertEquals(minHeap.push(values[i]), i + 1);
for (const [i, value] of values.entries()) {
assertEquals(minHeap.push(value), i + 1);
}
assertEquals(minHeap.length, values.length);
assertEquals(minHeap.isEmpty(), false);
Expand Down Expand Up @@ -85,8 +85,8 @@ Deno.test("BinaryHeap contains objects", () => {
) => ascend(a.id, b.id));
const ids: number[] = [-10, 9, -1, 100, 1, 0, -100, 10, -9];

for (let i = 0; i < ids.length; i++) {
const newContainer: Container = { id: ids[i], values: [] };
for (const [i, id] of ids.entries()) {
const newContainer: Container = { id, values: [] };
assertEquals(heap.push(newContainer), i + 1);
newContainer.values.push(i - 1, i, i + 1);
assertEquals(heap.length, i + 1);
Expand All @@ -101,7 +101,7 @@ Deno.test("BinaryHeap contains objects", () => {

const expectedContainer = {
id: expected[i],
values: [expectedValue[i] - 1, expectedValue[i], expectedValue[i] + 1],
values: [expectedValue[i]! - 1, expectedValue[i], expectedValue[i]! + 1],
};
assertEquals(heap.peek(), expectedContainer);
assertEquals(heap.pop(), expectedContainer);
Expand Down
2 changes: 1 addition & 1 deletion data_structures/binary_search_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ export class BinarySearchTree<T> implements Iterable<T> {
nodes.push(node);
node = node.left;
} else {
const lastNode: BinarySearchNode<T> = nodes[nodes.length - 1];
const lastNode: BinarySearchNode<T> = nodes.at(-1)!;
if (lastNode.right && lastNode.right !== lastNodeVisited) {
node = lastNode.right;
} else {
Expand Down
Loading