fix bitmask handling 🐛

This commit is contained in:
Jannis R 2019-03-29 19:06:16 +01:00
parent d0f7ca1b6c
commit 0ce5669899
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
2 changed files with 8 additions and 18 deletions

View file

@ -21,7 +21,7 @@ const createFormatProductsFilter = (profile) => {
if (!hasProp(filter, product) || filter[product] !== true) continue if (!hasProp(filter, product) || filter[product] !== true) continue
if (!byProduct[product]) throw new TypeError('unknown product ' + product) if (!byProduct[product]) throw new TypeError('unknown product ' + product)
products++ products++
for (let bitmask of byProduct[product].bitmasks) res += bitmask for (let bitmask of byProduct[product].bitmasks) res = res ^ bitmask
} }
if (products === 0) throw new Error('no products used') if (products === 0) throw new Error('no products used')

View file

@ -2,28 +2,18 @@
const createParseBitmask = (profile) => { const createParseBitmask = (profile) => {
const defaultProducts = {} const defaultProducts = {}
let withBitmask = [] for (let product of profile.products) defaultProducts[product.id] = false
for (let product of profile.products) {
defaultProducts[product.id] = false
for (let bitmask of product.bitmasks) {
withBitmask.push([bitmask, product])
}
}
withBitmask.sort((a, b) => b[0] - a[0]) // descending
const parseBitmask = (bitmask) => { const parseBitmask = (bitmask) => {
const res = Object.assign({}, defaultProducts) const res = Object.assign({}, defaultProducts)
for (let [pBitmask, product] of withBitmask) { const bits = bitmask.toString(2).split('').map(i => parseInt(i)).reverse()
if ((pBitmask & bitmask) > 0) { for (let i = 0; i < bits.length; i++) {
res[product.id] = true if (!bits[i]) continue // ignore `0`
bitmask -= pBitmask
}
else{
res[product.id] = false
}
}
const product = profile.products.find(p => p.bitmasks.includes(Math.pow(2, i)))
if (product) res[product.id] = true
}
return res return res
} }
return parseBitmask return parseBitmask