Skip to content

Commit

Permalink
[1.1.0] New BitSet method - entries
Browse files Browse the repository at this point in the history
- README now includes badges
- README example now demonstrates entries()
  • Loading branch information
iluha168 committed Oct 22, 2024
1 parent 3883802 commit 5fa7ae1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
[![JSR][jsr-badge]][jsr-url]
[![Deno][deno-badge]][deno-url]
[![TypeScript][typescript-badge]][typescript-url]
[![CI][ci-badge]][ci-url]
[![License: MIT][license-badge]][license-url]

# BitSet
A general-purpose interface for a byte array to manipulate individual bits.

## Example
```ts
import { BitSet } from "./src/mod.mts";
import { BitSet } from "jsr:@iluha168/bitset";

const bits = new BitSet(new ArrayBuffer(2))

Expand All @@ -22,4 +28,22 @@ console.log(bits.get(1))

console.log(Array.from(bits).join(''))
// Expected output: 0011100000000000
```

console.log(Array.from(bits.entries().take(3)))
// Expected output: [ [ 0, 0 ], [ 1, 0 ], [ 2, 1 ] ]
```

[jsr-badge]: https://jsr.io/badges/@iluha168/bitset?style=flat-square
[jsr-url]: https://jsr.io/@iluha168/bitset

[deno-badge]: https://img.shields.io/badge/Deno-000000?logo=Deno&logoColor=FFF&style=flat-square
[deno-url]: https://deno.com/

[typescript-badge]: https://img.shields.io/badge/TypeScript-3178C6?logo=TypeScript&logoColor=FFF&style=flat-square
[typescript-url]: https://www.typescriptlang.org/

[ci-badge]: https://img.shields.io/github/actions/workflow/status/iluha168/jsr-bitset/publish.yml?logo=github&style=flat-square
[ci-url]: https://github.com/iluha168/jsr-bitset/actions/workflows/publish.yml

[license-badge]: https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square
[license-url]: https://wei.mit-license.org
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@iluha168/bitset",
"version": "1.0.0",
"version": "1.1.0",
"exports": "./src/mod.mts",

"publish": {
Expand Down
6 changes: 6 additions & 0 deletions src/BitSet.mts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ export class BitSet {
yield this.get(i)
}

/** Yields [key, value] pairs for every bit in the array. */
*entries(): Generator<[number, number]> {
for(let i = 0; i < this.length(); i++)
yield [i, this.get(i)]
}

toString(): string {
const bits = Array
.from(this.bytes.subarray(0, BitSet.TO_STRING_BYTE_LIMIT))
Expand Down
14 changes: 11 additions & 3 deletions tests/BitSet_test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,21 @@ Deno.test("BitSet", async t => {
}
})

await t.step("mutating bytes during iteration", () => {
await t.step("entries", () => {
let i = 0;
for(const bit of bits){
for(const [entriesI, bit] of bits.entries()){
assertEquals(bit, bits.get(entriesI))
assertEquals(entriesI, i)

i++
}
})

await t.step("mutating bytes during iteration", () => {
for(const [i, bit] of bits.entries()){
assertEquals(bit, bits.get(i))

bits.bytes = new Uint8Array([Math.random() * 255])
i++
}
})
})

0 comments on commit 5fa7ae1

Please sign in to comment.