Skip to content

Commit 39f324f

Browse files
committed
DRY bits calc
1 parent cce77e5 commit 39f324f

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

lib/charSet.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var CharSet = (function() {
1010
throw new Error('EntropyString only supports CharSets with a power of 2 characters')
1111
}
1212
const bitsPerByte = 8
13-
13+
1414
const privProps = {
1515
chars: chars,
1616
bitsPerChar: bitsPerChar,
@@ -19,7 +19,7 @@ var CharSet = (function() {
1919
}
2020
propMap.set(this, privProps)
2121
}
22-
22+
2323
CharSet.prototype.getChars = function() {
2424
return propMap.get(this).chars
2525
}
@@ -55,14 +55,18 @@ var CharSet = (function() {
5555

5656
const ndxFn = (bitsPerChar) => {
5757
let bitsPerByte = 8
58-
58+
59+
// If bitsPerBytes is a multiple of bitsPerChar, we can slice off an integer number
60+
// of chars per byte.
5961
if (lcm(bitsPerChar, bitsPerByte) === bitsPerByte) {
6062
return function(chunk, slice, bytes) {
6163
let lShift = bitsPerChar
6264
let rShift = bitsPerByte - bitsPerChar
6365
return ((bytes[chunk]<<(lShift*slice))&0xff)>>rShift
6466
}
6567
}
68+
// Otherwise, while slicing off bits per char, we will possibly straddle a couple
69+
// of bytes, so a bit more work is involved
6670
else {
6771
return function(chunk, slice, bytes) {
6872
let slicesPerChunk = lcm(bitsPerChar, bitsPerByte) / bitsPerByte
@@ -80,7 +84,7 @@ var CharSet = (function() {
8084
ndx += bytes[bNum+rOffset]>>rShiftIt
8185
}
8286
return ndx
83-
}
87+
}
8488
}
8589
}
8690
return CharSet

lib/entropy.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,27 @@ const _endianByteNum = (() => {
1313
return (buf8[0] === 0xff) ? [2,3,4,5,6,7] : [0,1,2,3,6,7]
1414
})()
1515

16-
const bits = (total, risk) => {
17-
if (total == 0) { return 0 }
16+
const _totalOf = (numStrings, log2Risk) => {
17+
if (numStrings == 0) { return 0 }
1818

19-
let N = 0
20-
if (total < 10001) {
21-
N = _log2(total) + _log2(total-1) + (_log2_10 * _log10(risk)) - 1
19+
let N
20+
if (numStrings < 10001) {
21+
N = _log2(numStrings) + _log2(numStrings-1)
2222
}
2323
else {
24-
const n = 2 * _log10(total) + _log10(risk)
25-
N = n * _log2_10 - 1
24+
N = 2 * _log2(numStrings)
2625
}
27-
return N
26+
return N + log2Risk - 1
2827
}
2928

30-
const bitsWithRiskPower = (total, rPower) => {
29+
const bits = (total, risk) => {
3130
if (total == 0) { return 0 }
31+
return _totalOf(total, _log2(risk))
32+
}
3233

33-
let N = 0
34-
if (total < 10001) {
35-
N = _log2(total) + _log2(total-1) + (_log2_10 * rPower) - 1
36-
}
37-
else {
38-
const n = 2 * _log10(total) + rPower
39-
N = n * _log2_10 - 1
40-
}
41-
return N
34+
const bitsWithRiskPower = (total, rPower) => {
35+
let log2Risk = _log2_10 * rPower
36+
return _totalOf(total, log2Risk)
4237
}
4338

4439
const bitsWithPowers = (tPower, rPower) => {
@@ -47,10 +42,8 @@ const bitsWithPowers = (tPower, rPower) => {
4742
return bitsWithRiskPower(Math.pow(10, tPower), rPower)
4843
}
4944
else {
50-
const n = 2 * tPower + rPower
51-
N = n * _log2_10 - 1
45+
return (2 * tPower + rPower) * _log2_10 - 1
5246
}
53-
return N
5447
}
5548

5649
const string = (entropyBits, charSet) => {

0 commit comments

Comments
 (0)