init commit
This commit is contained in:
commit
0368f12d60
|
|
@ -0,0 +1,24 @@
|
|||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
.DS_Store
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Vite + TS</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script type="module" src="/src/main.ts"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
{
|
||||
"name": "bitmap-index",
|
||||
"private": false,
|
||||
"version": "0.0.1",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/yazmeyaa/bitmap-index.git"
|
||||
},
|
||||
"main": "./dist/index.umd.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "tsc && vite build",
|
||||
"preview": "vite preview",
|
||||
"predeploy": "npm run dist",
|
||||
"lint": "eslint src --ext .ts",
|
||||
"lint:fix": "eslint src --ext .ts --fix",
|
||||
"format": "prettier --write ./src/**/*.ts"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@jest/globals": "^29.7.0",
|
||||
"@rollup/plugin-typescript": "^11.1.6",
|
||||
"@types/jest": "^29.5.12",
|
||||
"@types/node": "^20.14.9",
|
||||
"@typescript-eslint/eslint-plugin": "^7.15.0",
|
||||
"@typescript-eslint/parser": "^7.15.0",
|
||||
"eslint": "^9.6.0",
|
||||
"eslint-config-prettier": "^9.1.0",
|
||||
"prettier": "^3.3.2",
|
||||
"rollup-plugin-typescript-paths": "^1.5.0",
|
||||
"tslib": "^2.6.3",
|
||||
"typescript": "^5.5.3",
|
||||
"vite": "^5.3.3",
|
||||
"vite-plugin-dts": "^3.9.1",
|
||||
"vite-tsconfig-paths": "^4.3.2"
|
||||
},
|
||||
"dependencies": {}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
|
||||
|
After Width: | Height: | Size: 1.5 KiB |
|
|
@ -0,0 +1,169 @@
|
|||
import { Bitmap } from "./bitmap";
|
||||
|
||||
describe("Bitmap", () => {
|
||||
test("Set", () => {
|
||||
const x = 2;
|
||||
const y = 4;
|
||||
const bitmap = new Bitmap(32);
|
||||
bitmap.set(x);
|
||||
bitmap.set(y);
|
||||
expect(bitmap.contains(x)).toBe(true);
|
||||
expect(bitmap.contains(y)).toBe(true);
|
||||
});
|
||||
|
||||
test("Remove", () => {
|
||||
const x = 2;
|
||||
const bitmap = new Bitmap(32);
|
||||
bitmap.set(x);
|
||||
expect(bitmap.contains(x)).toBe(true);
|
||||
bitmap.remove(x);
|
||||
expect(bitmap.contains(x)).toBe(false);
|
||||
});
|
||||
|
||||
test("Min, Max, MinZero, MaxZero", () => {
|
||||
const bitmap = new Bitmap(32);
|
||||
|
||||
expect(bitmap.min()).toBe(-1);
|
||||
expect(bitmap.max()).toBe(-1);
|
||||
expect(bitmap.minZero()).toBe(0);
|
||||
expect(bitmap.maxZero()).toBe(31);
|
||||
|
||||
bitmap.set(1);
|
||||
expect(bitmap.min()).toBe(1);
|
||||
expect(bitmap.minZero()).toBe(0);
|
||||
expect(bitmap.maxZero()).toBe(31);
|
||||
|
||||
bitmap.set(0);
|
||||
expect(bitmap.min()).toBe(0);
|
||||
expect(bitmap.max()).toBe(1);
|
||||
expect(bitmap.minZero()).toBe(2);
|
||||
expect(bitmap.maxZero()).toBe(31);
|
||||
|
||||
bitmap.set(31);
|
||||
expect(bitmap.min()).toBe(0);
|
||||
expect(bitmap.max()).toBe(31);
|
||||
expect(bitmap.minZero()).toBe(2);
|
||||
expect(bitmap.maxZero()).toBe(30);
|
||||
});
|
||||
|
||||
test("Count", () => {
|
||||
const bitmap = new Bitmap(32);
|
||||
expect(bitmap.count()).toBe(0);
|
||||
bitmap.set(1);
|
||||
bitmap.set(2);
|
||||
bitmap.set(16);
|
||||
|
||||
expect(bitmap.count()).toBe(3);
|
||||
});
|
||||
|
||||
test("Range over bitmap", () => {
|
||||
const bitmap = new Bitmap(16);
|
||||
|
||||
bitmap.set(2);
|
||||
bitmap.set(4);
|
||||
bitmap.set(6);
|
||||
bitmap.set(8);
|
||||
|
||||
const results: number[] = [];
|
||||
|
||||
bitmap.range((x) => {
|
||||
results.push(x);
|
||||
return true;
|
||||
});
|
||||
|
||||
expect(results).toEqual([2, 4, 6, 8]);
|
||||
});
|
||||
|
||||
test("AND operation", () => {
|
||||
const bitmap1 = new Bitmap(32);
|
||||
bitmap1.set(1);
|
||||
bitmap1.set(2);
|
||||
bitmap1.set(3);
|
||||
|
||||
const bitmap2 = new Bitmap(32);
|
||||
bitmap2.set(2);
|
||||
bitmap2.set(3);
|
||||
bitmap2.set(4);
|
||||
|
||||
const result = bitmap1.and(bitmap2);
|
||||
|
||||
expect(result.contains(1)).toBe(false);
|
||||
expect(result.contains(2)).toBe(true);
|
||||
expect(result.contains(3)).toBe(true);
|
||||
expect(result.contains(4)).toBe(false);
|
||||
});
|
||||
|
||||
test("AND NOT operation", () => {
|
||||
const bitmap1 = new Bitmap(32);
|
||||
bitmap1.set(1);
|
||||
bitmap1.set(2);
|
||||
bitmap1.set(3);
|
||||
|
||||
const bitmap2 = new Bitmap(32);
|
||||
bitmap2.set(2);
|
||||
bitmap2.set(3);
|
||||
bitmap2.set(4);
|
||||
|
||||
const result = bitmap1.andNot(bitmap2);
|
||||
|
||||
expect(result.contains(1)).toBe(true);
|
||||
expect(result.contains(2)).toBe(false);
|
||||
expect(result.contains(3)).toBe(false);
|
||||
expect(result.contains(4)).toBe(false);
|
||||
});
|
||||
|
||||
test("OR operation", () => {
|
||||
const bitmap1 = new Bitmap(32);
|
||||
bitmap1.set(1);
|
||||
bitmap1.set(2);
|
||||
bitmap1.set(3);
|
||||
|
||||
const bitmap2 = new Bitmap(32);
|
||||
bitmap2.set(2);
|
||||
bitmap2.set(3);
|
||||
bitmap2.set(4);
|
||||
|
||||
const result = bitmap1.or(bitmap2);
|
||||
|
||||
expect(result.contains(1)).toBe(true);
|
||||
expect(result.contains(2)).toBe(true);
|
||||
expect(result.contains(3)).toBe(true);
|
||||
expect(result.contains(4)).toBe(true);
|
||||
});
|
||||
|
||||
test("XOR operation", () => {
|
||||
const bitmap1 = new Bitmap(32);
|
||||
bitmap1.set(1);
|
||||
bitmap1.set(2);
|
||||
bitmap1.set(3);
|
||||
|
||||
const bitmap2 = new Bitmap(32);
|
||||
bitmap2.set(2);
|
||||
bitmap2.set(3);
|
||||
bitmap2.set(4);
|
||||
|
||||
const result = bitmap1.xor(bitmap2);
|
||||
|
||||
expect(result.contains(1)).toBe(true);
|
||||
expect(result.contains(2)).toBe(false);
|
||||
expect(result.contains(3)).toBe(false);
|
||||
expect(result.contains(4)).toBe(true);
|
||||
});
|
||||
|
||||
test("Filter to keep even numbers", () => {
|
||||
const bitmap = new Bitmap(32);
|
||||
bitmap.set(0);
|
||||
bitmap.set(1);
|
||||
bitmap.set(2);
|
||||
bitmap.set(3);
|
||||
bitmap.set(4);
|
||||
|
||||
bitmap.filter((x) => x % 2 === 0);
|
||||
|
||||
expect(bitmap.contains(0)).toBe(true);
|
||||
expect(bitmap.contains(1)).toBe(false);
|
||||
expect(bitmap.contains(2)).toBe(true);
|
||||
expect(bitmap.contains(3)).toBe(false);
|
||||
expect(bitmap.contains(4)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
|
@ -0,0 +1,190 @@
|
|||
import { IBitmap } from "./types";
|
||||
|
||||
const computeBitsArrayLength = (x: number): number => Math.ceil(x / 8);
|
||||
const getBitPositionIndex = (x: number): number => Math.floor(x / 8);
|
||||
const getBitPosition = (x: number): number => x % 8;
|
||||
const getMask = (bitIdx: number): number => 0b00000001 << bitIdx;
|
||||
|
||||
export class Bitmap implements IBitmap {
|
||||
private bits: Uint8Array;
|
||||
|
||||
constructor(x = 32) {
|
||||
this.bits = new Uint8Array(computeBitsArrayLength(x));
|
||||
}
|
||||
|
||||
protected getBits(): Uint8Array {
|
||||
return this.bits;
|
||||
}
|
||||
|
||||
public grow(x: number): void {
|
||||
const arrLength = computeBitsArrayLength(x);
|
||||
if (arrLength <= this.bits.length) return;
|
||||
const prev = this.bits;
|
||||
this.bits = new Uint8Array(arrLength);
|
||||
this.bits.set(prev);
|
||||
}
|
||||
|
||||
public set(x: number): void {
|
||||
const idx = getBitPositionIndex(x);
|
||||
if (idx >= this.bits.length) this.grow(x);
|
||||
this.bits[idx] |= getMask(getBitPosition(x));
|
||||
}
|
||||
|
||||
public remove(x: number): void {
|
||||
const idx = getBitPositionIndex(x);
|
||||
if (!this.bits[idx]) return;
|
||||
const mask = getMask(getBitPosition(x));
|
||||
this.bits[idx] &= ~mask;
|
||||
}
|
||||
|
||||
public contains(x: number): boolean {
|
||||
const [idx, pos] = [getBitPositionIndex(x), getBitPosition(x)];
|
||||
if (!this.bits[idx]) return false;
|
||||
const mask = getMask(pos);
|
||||
return (this.bits[idx] & mask) === mask;
|
||||
}
|
||||
|
||||
public count(): number {
|
||||
let count = 0;
|
||||
for (const byte of this.bits) {
|
||||
let tmp = byte;
|
||||
while (tmp) {
|
||||
tmp &= tmp - 1;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
public and(bitmap: IBitmap): IBitmap {
|
||||
const otherBits = (bitmap as Bitmap).bits;
|
||||
const result = this.clone() as Bitmap;
|
||||
const minlen = Math.min(this.bits.length, otherBits.length);
|
||||
|
||||
for (let i = 0; i < minlen; i++) {
|
||||
result.bits[i] &= otherBits[i];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public andNot(bitmap: IBitmap): IBitmap {
|
||||
const otherBits = (bitmap as Bitmap).bits;
|
||||
const result = this.clone() as Bitmap;
|
||||
const minlen = Math.min(this.bits.length, otherBits.length);
|
||||
|
||||
for (let i = 0; i < minlen; i++) {
|
||||
result.bits[i] &= ~otherBits[i];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public or(bitmap: IBitmap): IBitmap {
|
||||
const otherBits = (bitmap as Bitmap).bits;
|
||||
const result = this.clone() as Bitmap;
|
||||
const minlen = Math.min(this.bits.length, otherBits.length);
|
||||
|
||||
for (let i = 0; i < minlen; i++) {
|
||||
result.bits[i] |= otherBits[i];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public xor(bitmap: IBitmap): IBitmap {
|
||||
const otherBits = (bitmap as Bitmap).bits;
|
||||
const result = this.clone() as Bitmap;
|
||||
const minlen = Math.min(this.bits.length, otherBits.length);
|
||||
|
||||
for (let i = 0; i < minlen; i++) {
|
||||
result.bits[i] ^= otherBits[i];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public range(fn: (x: number) => boolean): void {
|
||||
let needContinueIterating = true;
|
||||
for (let i = 0; i < this.bits.length; i++) {
|
||||
let x = this.bits[i];
|
||||
for (let j = 0; j < 8; j++) {
|
||||
const bit = (x >> j) & 1;
|
||||
if (bit === 0) continue;
|
||||
needContinueIterating = fn(i * 8 + j);
|
||||
if (!needContinueIterating) break;
|
||||
}
|
||||
if (!needContinueIterating) break;
|
||||
}
|
||||
}
|
||||
public filter(fn: (x: number) => boolean): void {
|
||||
for (let i = 0; i < this.bits.length; i++) {
|
||||
for (let j = 0; j < 8; j++) {
|
||||
const bitIndex = i * 8 + j;
|
||||
if (this.contains(bitIndex) && !fn(bitIndex)) {
|
||||
this.remove(bitIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public clear(): void {
|
||||
for (let i = 0; i < this.bits.length; i++) {
|
||||
this.bits[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public clone(): IBitmap {
|
||||
const clonedBitmap = new Bitmap(this.bits.length * 8);
|
||||
clonedBitmap.bits.set(this.bits);
|
||||
return clonedBitmap;
|
||||
}
|
||||
|
||||
public min(): number {
|
||||
for (let i = 0; i < this.bits.length; i++) {
|
||||
const byte = this.bits[i];
|
||||
if (byte === 0) continue;
|
||||
for (let j = 0; j < 8; j++) {
|
||||
if ((byte & (1 << j)) !== 0) {
|
||||
return i * 8 + j;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public max(): number {
|
||||
let x = -1;
|
||||
for (let i = this.bits.length - 1; i >= 0; i--) {
|
||||
const byte = this.bits[i];
|
||||
if (byte === 0) continue;
|
||||
x = i * 8 + Math.floor(Math.log2(byte));
|
||||
break;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
public minZero(): number {
|
||||
for (let i = 0; i < this.bits.length; i++) {
|
||||
const byte = this.bits[i];
|
||||
if (byte === 0xff) continue;
|
||||
for (let j = 0; j < 8; j++) {
|
||||
if ((byte & (1 << j)) === 0) {
|
||||
return i * 8 + j;
|
||||
}
|
||||
}
|
||||
}
|
||||
return this.bits.length * 8;
|
||||
}
|
||||
|
||||
public maxZero(): number {
|
||||
for (let i = this.bits.length - 1; i >= 0; i--) {
|
||||
const byte = this.bits[i];
|
||||
if (byte === 0xff) continue;
|
||||
for (let j = 7; j >= 0; j--) {
|
||||
if ((byte & (1 << j)) === 0) {
|
||||
return i * 8 + j;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
export { Bitmap } from "./bitmap";
|
||||
export type { IBitmap } from "./types";
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
export interface IBitmap {
|
||||
grow(x: number): void;
|
||||
set(x: number): void;
|
||||
remove(x: number): void;
|
||||
contains(x: number): boolean;
|
||||
count(): number;
|
||||
and(bitmap: IBitmap): IBitmap;
|
||||
andNot(bitmap: IBitmap): IBitmap;
|
||||
or(bitmap: IBitmap): IBitmap;
|
||||
xor(bitmap: IBitmap): IBitmap;
|
||||
range(fn: (x: number) => boolean): void;
|
||||
filter(fn: (x: number) => boolean): void;
|
||||
clear(): void;
|
||||
clone(): IBitmap;
|
||||
min(): number;
|
||||
max(): number;
|
||||
minZero(): number;
|
||||
maxZero(): number;
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2020",
|
||||
"useDefineForClassFields": true,
|
||||
"module": "ESNext",
|
||||
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
||||
"skipLibCheck": true,
|
||||
|
||||
/* Bundler mode */
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"moduleDetection": "force",
|
||||
"noEmit": true,
|
||||
|
||||
/* Linting */
|
||||
"strict": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"noFallthroughCasesInSwitch": true
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
import { defineConfig } from "vite";
|
||||
import path from "path";
|
||||
import dts from 'vite-plugin-dts';
|
||||
|
||||
const libConfig = defineConfig({
|
||||
build: {
|
||||
lib: {
|
||||
entry: path.resolve(__dirname, "src/index.ts"),
|
||||
fileName: "index",
|
||||
name: 'bitmap-index'
|
||||
},
|
||||
},
|
||||
plugins: [dts({exclude: "**/*.test.ts"})],
|
||||
});
|
||||
|
||||
export default libConfig
|
||||
Loading…
Reference in New Issue