Unlisted
Edited
Nov 21, 2023
Insert cell
Insert cell
// todo: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry
// to clean up after done (also, make deinit idempotent so the user is allowed to call it explicitly)
Insert cell
viewof reload = Inputs.button("Reload")
Insert cell
zig = {
return WebAssembly.instantiate(wasmBytes, { env: {} }).then((results) => {
results.instance.exports.init();
return results.instance.exports;
});
}
Insert cell
bv = {
const builder = new ZigDenseBitVecBuilder(100);
builder.one(10);
builder.one(15);
return builder.build();
}
Insert cell
bv.rank1(10)
Insert cell
bv.rank1(11)
Insert cell
bv.rank1(21)
Insert cell
bv.rank1(10001)
Insert cell
bv.rank1(1e100) // 🐛 hmm...
Insert cell
class ZigDenseBitVecBuilder {
constructor(universeSize) {
this.builder = zig.DenseBitVecBuilder_init(universeSize);
}
one(index, count = 1) {
zig.DenseBitVecBuilder_one(this.builder, index, count);
}
build({
rank1SamplesPow2 = 10,
select0SamplesPow2 = 10,
select1SamplesPow2 = 10
} = {}) {
const bv = zig.DenseBitVecBuilder_build(
this.builder,
rank1SamplesPow2,
select0SamplesPow2,
select1SamplesPow2
);
return new ZigDenseBitVec(bv);
}
}
Insert cell
class ZigDenseBitVec {
constructor(bv) {
this.bv = bv;
}
rank1(index) {
return zig.DenseBitVec_rank1(this.bv, index);
}
rank0(index) {
return zig.DenseBitVec_rank0(this.bv, index);
}
select1(n) {
return zig.DenseBitVec_select1(this.bv, n);
}
select0(n) {
return zig.DenseBitVec_select0(this.bv, n);
}
trySelect1(n) {
return zig.DenseBitVec_select1(this.bv, n);
}
trySelect0(n) {
return zig.DenseBitVec_select0(this.bv, n);
}
get(index) {
return zig.DenseBitVec_get(this.bv, index);
}
get universeSize() {
return zig.DenseBitVec_universe_size(this.bv);
}
get numOnes() {
return zig.DenseBitVec_num_ones(this.bv);
}
get numZeros() {
return zig.DenseBitVec_num_zeros(this.bv);
}
get hasMultiplicity() {
return zig.DenseBitVec_has_multiplicity(this.bv);
}
get numUniqueOnes() {
return zig.DenseBitVec_num_unique_ones(this.bv);
}
get numUniqueZeros() {
return zig.DenseBitVec_num_unique_zeros(this.bv);
}
deinit() {
return zig.DenseBitVec_deinit(this.bv);
}
}
Insert cell
// zig = {
// let instance;
// const imports = {
// env: {
// consoleLog: (location, size) => consoleLog(instance, location, size)
// }
// };
// return WebAssembly.instantiate(wasmBytes, imports).then((results) => {
// instance = results.instance;
// results.instance.exports.init();
// return results.instance.exports;
// });
// }
Insert cell
consoleLog = (instance, location, size) => {
const buffer = new Uint8Array(instance.exports.memory.buffer, location, size);
const decoder = new TextDecoder();
const string = decoder.decode(buffer);
console.log(string);
}
Insert cell
wasmBytes = FileAttachment("main.wasm").arrayBuffer()
// Uncomment the below instead to use with esbuild serving functionality:
// (reload,
// Generators.observe((notify) => {
// const host = "https://0.0.0.0:4242";
// const f = () => {
// console.clear();
// const url = `${host}/main.wasm?${Math.random()}`;
// // console.log(url);
// return fetch(url).then((res) => res.arrayBuffer());
// };

// // todo: needs better handling but this is trying to work around the stream of errors
// // when i restart the esbuild server

// // todo: try reconnecting once a second
// let source = new EventSource(`${host}/esbuild`);
// source.addEventListener("change", () => {
// notify(f());
// });

// source.addEventListener("error", (e) => {
// console.log("error", e);
// notify(null); // closed due to error
// source.close();
// source = null;
// // e.preventDefault();
// // e.stopPropagation();
// });

// notify(f());
// }))
Insert cell
Insert cell
// hi
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more