Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
dqbd committed Oct 2, 2024
1 parent 9ce1658 commit 8685e3c
Showing 1 changed file with 38 additions and 29 deletions.
67 changes: 38 additions & 29 deletions js/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,36 @@ type BPEMergeNode = {
end: number;
};

function compareNode (a:BPEMergeNode, b:BPEMergeNode) {
function compareNode(a: BPEMergeNode, b: BPEMergeNode) {
return a.rank - b.rank || a.start - b.start;
}

// Helper function to swap elements at two indices
function swap(heap:BPEMergeNode[], i:number, j:number) {
function swap(heap: BPEMergeNode[], i: number, j: number) {
const temp = heap[i];
heap[i] = heap[j];
heap[j] = temp;
}

// standard binary heap push, generated by gpt4
function heapPush(heap:BPEMergeNode[], part:BPEMergeNode) {
function heapPush(heap: BPEMergeNode[], part: BPEMergeNode) {
heap.push(part); // Add the new element to the end
let currentIndex = heap.length - 1;
let parentIndex = Math.floor((currentIndex - 1) / 2);

// Bubble the new element up to its correct position
while (currentIndex > 0 && compareNode(heap[currentIndex], heap[parentIndex]) < 0) {
while (
currentIndex > 0 &&
compareNode(heap[currentIndex], heap[parentIndex]) < 0
) {
swap(heap, currentIndex, parentIndex);
currentIndex = parentIndex;
parentIndex = Math.floor((currentIndex - 1) / 2);
}
}

// standard heap pop, also ai generated
function heapPop(heap:BPEMergeNode[]) {
function heapPop(heap: BPEMergeNode[]) {
if (heap.length === 0) {
return undefined; // Return undefined if the heap is empty
}
Expand All @@ -60,11 +63,17 @@ function heapPop(heap:BPEMergeNode[]) {
let rightChildIndex = 2 * currentIndex + 2;
let smallestIndex = currentIndex;

if (leftChildIndex < heap.length && compareNode(heap[leftChildIndex], heap[smallestIndex]) < 0) {
if (
leftChildIndex < heap.length &&
compareNode(heap[leftChildIndex], heap[smallestIndex]) < 0
) {
smallestIndex = leftChildIndex;
}

if (rightChildIndex < heap.length && compareNode(heap[rightChildIndex], heap[smallestIndex]) < 0) {
if (
rightChildIndex < heap.length &&
compareNode(heap[rightChildIndex], heap[smallestIndex]) < 0
) {
smallestIndex = rightChildIndex;
}

Expand All @@ -84,17 +93,20 @@ function bytePairMerge(
piece: Uint8Array,
ranks: Map<string, number>
): Array<{ start: number; end: number }> {
const parts: BPEMergeNode[] = Array.from({ length: piece.length }, (_, i) => ({
start: i,
end: i + 1,
rank: 0,
deleted: false,
updated: false,
updatedRank: 0,
removed: true,
listNext: null,
listPrev: null
}));
const parts: BPEMergeNode[] = Array.from(
{ length: piece.length },
(_, i) => ({
start: i,
end: i + 1,
rank: 0,
deleted: false,
updated: false,
updatedRank: 0,
removed: true,
listNext: null,
listPrev: null,
})
);

if (parts.length === 0) {
return [];
Expand All @@ -106,12 +118,11 @@ function bytePairMerge(
parts[i].listNext = parts[i + 1] ?? null;
}

const heap:BPEMergeNode[] = []
const heap: BPEMergeNode[] = [];
for (let i = 0; i < parts.length - 1; ++i) {
const slice = piece.slice(parts[i].start, parts[i + 1].end);
const rank = ranks.get(slice.join(","));
if (rank == null)
continue;
if (rank == null) continue;
const part = parts[i];
part.removed = false;
part.rank = rank;
Expand All @@ -120,14 +131,13 @@ function bytePairMerge(

while (heap.length > 0) {
const part = heapPop(heap);
if (!part)
break;
if (!part) break;

// remove deleted nodes from heap
if (part.deleted) {
part.deleted = false;
part.removed = true;
continue
continue;
}

// reinsert updated nodes
Expand All @@ -139,12 +149,11 @@ function bytePairMerge(
}

// mark node as removed from heap
part.removed = true
part.removed = true;

// delete next part and collapse node
part.end = part.listNext?.end ?? piece.length;
if (part.listNext)
part.listNext.deleted = true;
if (part.listNext) part.listNext.deleted = true;
part.listNext = part.listNext?.listNext ?? null;

// update rank
Expand All @@ -166,9 +175,9 @@ function bytePairMerge(
if (prevRank != null) {
if (prevRank !== part.listPrev.rank) {
if (part.listPrev.removed) {
part.listPrev.removed = false
part.listPrev.removed = false;
part.listPrev.rank = prevRank;
heapPush(heap, part)
heapPush(heap, part);
} else {
part.listPrev.updated = true;
part.listPrev.updatedRank = prevRank;
Expand Down

0 comments on commit 8685e3c

Please sign in to comment.