Skip to content

Commit

Permalink
refactor(data_structures): prepare for noUncheckedIndexedAccess (de…
Browse files Browse the repository at this point in the history
…noland#4146)

* refactor(data_structures): prepare for `noUncheckedIndexedAccess`

* tweak

---------

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
  • Loading branch information
syhol and iuioiua committed Jan 10, 2024
1 parent 50bfa28 commit f2324e5
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 269 deletions.
18 changes: 9 additions & 9 deletions data_structures/binary_heap.ts
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
16 changes: 8 additions & 8 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 @@ -95,13 +95,13 @@ Deno.test("BinaryHeap contains objects", () => {

const expected: number[] = [-100, -10, -9, -1, 0, 1, 9, 10, 100];
const expectedValue: number[] = [6, 0, 8, 2, 5, 4, 1, 7, 3];
for (let i = 0; i < ids.length; i++) {
for (const [i, value] of expectedValue.entries()) {
assertEquals(heap.length, ids.length - i);
assertEquals(heap.isEmpty(), false);

const expectedContainer = {
id: expected[i],
values: [expectedValue[i] - 1, expectedValue[i], expectedValue[i] + 1],
values: [value - 1, value, value + 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

0 comments on commit f2324e5

Please sign in to comment.