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

Optimize scrolling performance with Binary Indexed Tree #565

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
66 changes: 66 additions & 0 deletions src/core/bit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// https://en.wikipedia.org/wiki/Fenwick_tree

import { append, clz32 } from "./utils";

declare const bitSymbol: unique symbol;
/** @internal */
export type BIT = { [bitSymbol]: never } & number[];

/** @internal */
export const init = (array: readonly number[]): BIT => {
const length = array.length + 1;
const bit = append([], length, 0);

for (let i = 1; i < length; i++) {
bit[i]! += array[i - 1]!;
const parent = i + (i & -i);
if (parent < length) {
bit[parent]! += bit[i]!;
}
}
return bit as BIT;
};

/** @internal */
export const push = (bit: BIT, value: number) => {
const length = bit.length;
const k = length & -length;
for (let i = 1; i < k; i <<= 1) {
value += bit[length - i]!;
}
bit.push(value);
};

/** @internal */
export const get = (bit: BIT, i: number): number => {
let sum = 0;
for (; i > 0; i -= i & -i) {
sum += bit[i]!;
}
return sum;
};

/** @internal */
export const add = (bit: BIT, i: number, delta: number) => {
for (; i < bit.length; i += i & -i) {
bit[i]! += delta;
}
};

/** @internal */
export const lowerBound = (bit: BIT, value: number): number => {
if (value <= 0) {
return 0;
} else {
const length = bit.length;
let index = 0;
for (let t = 1 << (31 - clz32(length - 1)); t > 0; t >>= 1) {
const nextIndex = index + t;
if (nextIndex < length && bit[nextIndex]! <= value) {
value -= bit[nextIndex]!;
index = nextIndex;
}
}
return index;
}
};
Loading
Loading