-
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
Changes from 6 commits
3ad27c1
c169c92
3b76fea
25c5b9b
3868900
d725c6e
e4a77df
9478da4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,31 +8,74 @@ import equalArrays from "../utils/equalArrays.js"; | |
export default class BitSet { | ||
|
||
constructor() { | ||
this.data = []; | ||
this.data = new Uint32Array(1); | ||
} | ||
|
||
add(value) { | ||
this.data[value] = true; | ||
set(index) { | ||
BitSet._checkIndex(index) | ||
this._resize(index); | ||
this.data[index >>> 5] |= 1 << index % 32; | ||
} | ||
|
||
or(set) { | ||
Object.keys(set.data).map(alt => this.add(alt), this); | ||
get(index) { | ||
BitSet._checkIndex(index) | ||
const slot = index >>> 5; | ||
if (slot >= this.data.length) { | ||
return false; | ||
} | ||
return (this.data[slot] & 1 << index % 32) !== 0; | ||
} | ||
|
||
remove(value) { | ||
delete this.data[value]; | ||
clear(index) { | ||
BitSet._checkIndex(index) | ||
const slot = index >>> 5; | ||
if (slot < this.data.length) { | ||
this.data[index >>> 5] &= ~(1 << index); | ||
} | ||
} | ||
|
||
has(value) { | ||
return this.data[value] === true; | ||
or(set) { | ||
const minCount = Math.min(this.data.length, set.data.length); | ||
for (let k = 0; k < minCount; ++k) { | ||
this.data[k] |= set.data[k]; | ||
} | ||
if (this.data.length < set.data.length) { | ||
this._resize((set.data.length << 5) - 1); | ||
const c = set.data.length; | ||
for (let k = minCount; k < c; ++k) { | ||
this.data[k] = set.data[k]; | ||
} | ||
} | ||
} | ||
|
||
values() { | ||
return Object.keys(this.data); | ||
const result = new Array(this.length); | ||
let pos = 0; | ||
const length = this.data.length; | ||
for (let k = 0; k < length; ++k) { | ||
let l = this.data[k]; | ||
while (l !== 0) { | ||
const t = l & -l; | ||
result[pos++] = (k << 5) + BitSet._bitCount(t - 1); | ||
l ^= t; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
minValue() { | ||
return Math.min.apply(null, this.values()); | ||
for (let k = 0; k < this.data.length; ++k) { | ||
let l = this.data[k]; | ||
if (l !== 0) { | ||
let result = 0; | ||
while ((l & 1) === 0) { | ||
result++; | ||
l >>= 1; | ||
} | ||
return result + (32 * k); | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
hashCode() { | ||
|
@@ -48,6 +91,33 @@ export default class BitSet { | |
} | ||
|
||
get length(){ | ||
return this.values().length; | ||
return this.data.map(l => BitSet._bitCount(l)).reduce((s,v) => s + v, 0); | ||
} | ||
|
||
_resize(index) { | ||
const count = index + 32 >>> 5; | ||
if (count <= this.data.length) { | ||
return; | ||
} | ||
const data = new Uint32Array(count); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 |
||
data.set(this.data); | ||
data.fill(0, this.data.length); | ||
this.data = data; | ||
} | ||
|
||
static _checkIndex(index) { | ||
if(index < 0) | ||
throw new RangeError("index cannot be negative"); | ||
} | ||
|
||
static _bitCount(l) { | ||
// see https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel | ||
let count = 0; | ||
l = l - ((l >> 1) & 0x55555555); | ||
l = (l & 0x33333333) + ((l >> 2) & 0x33333333); | ||
l = (l + (l >> 4)) & 0x0f0f0f0f; | ||
l = l + (l >> 8); | ||
l = l + (l >> 16); | ||
return count + l & 0x3f; | ||
} | ||
} |
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.
Indent off.
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.
Fixed