-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Use arrayview for bit set #4549
Merged
ericvergnaud
merged 8 commits into
antlr:dev
from
ericvergnaud:use-arrayview-for-bit-set
Mar 10, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3ad27c1
add test spec
ericvergnaud c169c92
use array view
ericvergnaud 3b76fea
oops
ericvergnaud 25c5b9b
refactor BitSet API
ericvergnaud 3868900
document algorithm
ericvergnaud d725c6e
more tests
ericvergnaud e4a77df
fix formatting
ericvergnaud 9478da4
reuse slot
ericvergnaud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import BitSet from "../src/antlr4/misc/BitSet.js"; | ||
|
||
describe('test BitSet', () => { | ||
|
||
it("is empty", () => { | ||
const bs = new BitSet(); | ||
expect(bs.length).toEqual(0); | ||
}) | ||
|
||
it("sets 1 value", () => { | ||
const bs = new BitSet(); | ||
bs.set(67); | ||
expect(bs.length).toEqual(1); | ||
expect(bs.get(67)).toBeTrue(); | ||
}) | ||
|
||
it("clears 1 value", () => { | ||
const bs = new BitSet(); | ||
bs.set(67); | ||
bs.clear(67) | ||
expect(bs.length).toEqual(0); | ||
expect(bs.get(67)).toBeFalse(); | ||
}) | ||
|
||
it("sets 2 consecutive values", () => { | ||
const bs = new BitSet(); | ||
bs.set(67); | ||
bs.set(68); | ||
expect(bs.length).toEqual(2); | ||
expect(bs.get(67)).toBeTrue(); | ||
expect(bs.get(68)).toBeTrue(); | ||
}) | ||
|
||
it("sets 2 close values", () => { | ||
const bs = new BitSet(); | ||
bs.set(67); | ||
bs.set(70); | ||
expect(bs.length).toEqual(2); | ||
expect(bs.get(67)).toBeTrue(); | ||
expect(bs.get(70)).toBeTrue(); | ||
}) | ||
|
||
it("sets 2 distant values", () => { | ||
const bs = new BitSet(); | ||
bs.set(67); | ||
bs.set(241); | ||
expect(bs.length).toEqual(2); | ||
expect(bs.get(67)).toBeTrue(); | ||
expect(bs.get(241)).toBeTrue(); | ||
}) | ||
|
||
it("combines 2 identical sets", () => { | ||
const bs1 = new BitSet(); | ||
bs1.set(67); | ||
const bs2 = new BitSet(); | ||
bs2.set(67); | ||
bs1.or(bs2); | ||
expect(bs1.length).toEqual(1); | ||
expect(bs1.get(67)).toBeTrue(); | ||
}) | ||
|
||
it("combines 2 distinct sets", () => { | ||
const bs1 = new BitSet(); | ||
bs1.set(67); | ||
const bs2 = new BitSet(); | ||
bs2.set(69); | ||
bs1.or(bs2); | ||
expect(bs1.length).toEqual(2); | ||
expect(bs1.get(67)).toBeTrue(); | ||
expect(bs1.get(69)).toBeTrue(); | ||
}) | ||
|
||
it("combines 2 overlapping sets", () => { | ||
const bs1 = new BitSet(); | ||
bs1.set(67); | ||
bs1.set(69); | ||
const bs2 = new BitSet(); | ||
bs2.set(69); | ||
bs2.set(71); | ||
bs1.or(bs2); | ||
expect(bs1.length).toEqual(3); | ||
expect(bs1.get(67)).toBeTrue(); | ||
expect(bs1.get(69)).toBeTrue(); | ||
expect(bs1.get(71)).toBeTrue(); | ||
}) | ||
|
||
it("returns values", () => { | ||
const bs = new BitSet(); | ||
bs.set(67); | ||
bs.set(69); | ||
const values = bs.values(); | ||
expect(values).toEqual([67, 69]); | ||
}) | ||
|
||
it("counts bits", () => { | ||
for(let i= 0; i <= 0xFF; i++) { | ||
// count bits the slow but easy to understand way (Kernighan method) | ||
let count1 = 0; | ||
let value = i; | ||
while(value) { | ||
if(value & 1) | ||
count1++; | ||
value >>= 1; | ||
} | ||
// count bits the fast way | ||
const count2 = BitSet._bitCount(i); | ||
expect(count2).toEqual(count1); | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you constructing a new array when I think Uint32Array.resize() is available?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid your thinking is incorrect. You can resize an ArrayBuffer but not a TypedArray. I suspect the reason for this is that the underlying buffer is shared (a poor design decision if you ask me) so resizing a view may actually silently resize other views. Resizing the buffer would be a hack, not something I'm comfortable with