8000 Fix: Wrong `get` logic in `Bitmap` class. by ElPrudi · Pull Request #15 · Torathion/bitbob · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix: Wrong get logic in Bitmap class. #15

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

Merged
merged 5 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import shiny from 'eslint-config-shiny'

export default await shiny()
export default [
...await shiny({ configs: ['base', 'format', 'vitest']}),
{
rules: {
'no-underscore-dangle': 0,
'unicorn/prefer-at': 0
}
}
]
21 changes: 14 additions & 7 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ declare module 'bitbob' {
*
* It is recommended to use an `enum` for easier handling of this class.
*/
export class Bitmap {
export class Bitmap extends BitHandler {
constructor(initialState?: number)
/**
* Applies a state as a mask to the bitmap.
Expand Down Expand Up @@ -81,14 +81,21 @@ declare module 'bitbob' {
* @param end - ending bit position.
*/
flipRange(start: number, end: number): void
/**
* Extracts the bit value of the given bit position.
*
* @param bit - target bit position.
* @returns Either `1` for a set state, otherwise `0`.
*/
override get(bit: number): boolean
/**
* Checks if a specific flag (bit) is set.
* This also has the ability to check for multiple states at once, acting as an OR operation.
*
* @param bit - the flag(s) to check.
* @returns `true`, if the condition is met, otherwise `false`.
*/
has(mask: number): boolean
override has(mask: number): boolean
/**
* Checks if a specific subset of a state is met.
*
Expand All @@ -101,7 +108,7 @@ declare module 'bitbob' {
*
* @param 10000 bit - the flag to set.
*/
set(bit: number): void
override set(bit: number): void
/**
* Sets a field of flags at once (0 counting).
*
Expand Down Expand Up @@ -140,7 +147,7 @@ declare module 'bitbob' {
* Numbers are added from right to left, meaning the more numbers you store, the bigger the state. Each number is assigned to a static
* field, separated by the pointer start indices. There is the possibility to address more space than currently used.
*/
export class ComposedNumber {
export class ComposedNumber extends BitHandler {
constructor(initialState?: number, reserve?: number)
/**
* Copies the current `ComposedNumber` into a new one.
Expand All @@ -154,14 +161,14 @@ declare module 'bitbob' {
* @param value - Number to store.
* @param reserve - Bit length to reserve.
*/
set(value: number, reserve?: number): void
override set(value: number, reserve?: number): void
/**
* Returns the nth number stored inside the composed number.
*
* @param id - The nth number.
* @returns The stored number.
*/
get(id: number): number
override get(id: number): number
/**
* Entirely overwrites the state and pointers of the number.
*
Expand All @@ -188,7 +195,7 @@ declare module 'bitbob' {
* @param value - Target value.
* @returns `true` if the store has this value stored, otherwise `false`.
*/
has(value: number): boolean
override has(value: number): boolean
/**
* Finds the index of a stored number.
*
Expand Down
3 changes: 2 additions & 1 deletion src/classes/Bitmap.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ILog2Sequence } from 'src/constants'
import BitHandler from './BitHandler'

/**
Expand Down Expand Up @@ -50,7 +51,7 @@ export default class Bitmap extends BitHandler {
* @returns Either `1` for a set state, otherwise `0`.
*/
override get(bit: number): boolean {
return !!((this._state >> (bit >> 1)) & 1)
return !!((this._state >> ILog2Sequence[(bit * 0x04ad19df) >>> 27]) & 1)
}

/**
Expand Down
20 changes: 20 additions & 0 deletions test/classes/Bitmap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ describe('Bitmap', () => {
expect(bitmap.get(Test.B)).toBe(true)
expect(bitmap.get(Test.C)).toBe(true)
})

it('works with large powers of 2', () => {
const bitmap = new Bitmap()
const Test = createBitmapStates(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'])

bitmap.set(Test.L)
bitmap.set(Test.M)

expect(bitmap.get(Test.A)).toBe(false)
expect(bitmap.get(Test.L)).toBe(true)
expect(bitmap.get(Test.M)).toBe(true)

bitmap.toggle(Test.L)
bitmap.toggle(Test.M)
bitmap.toggle(Test.I)

expect(bitmap.get(Test.L)).toBe(false)
expect(bitmap.get(Test.M)).toBe(false)
expect(bitmap.get(Test.I)).toBe(true)
})
})

it('can unset a flag', () => {
Expand Down
Loading
0