tscommons-core
Advanced tools
Comparing version 1.27.0 to 1.28.0
@@ -26,2 +26,3 @@ export declare abstract class CommonsNumber { | ||
static sampleIndices(length: number, samples: number, blockSize: number, allowUnderflow?: boolean): number[]; | ||
static roughFactor(items: number, factor: number): number[]; | ||
} |
@@ -252,4 +252,51 @@ "use strict"; | ||
} | ||
static roughFactor(items, factor) { | ||
if (factor === 0) | ||
throw new Error('Cannot factor into zero'); | ||
if (items < factor) | ||
return [items]; | ||
const div = Math.round(items / factor); | ||
const center = div * factor; | ||
const results = []; | ||
for (let i = div; i-- > 0;) | ||
results.push(factor); | ||
if (items === center) | ||
return results; | ||
let ttl = items; // just to protect against infinite loops | ||
if (items < center) { | ||
// subtract from the rightmost back | ||
let index = results.length - 1; | ||
while (ttl-- > 0) { | ||
let sum = 0; | ||
for (const r of results) | ||
sum += r; | ||
if (sum === items) | ||
break; | ||
results[index]--; | ||
index--; | ||
if (index < 0) | ||
index = results.length - 1; | ||
} | ||
} | ||
else { | ||
// add from the leftmost forward | ||
let index = 0; | ||
while (ttl-- > 0) { | ||
let sum = 0; | ||
for (const r of results) | ||
sum += r; | ||
if (sum === items) | ||
break; | ||
results[index]++; | ||
index++; | ||
if (index === results.length) | ||
index = 0; | ||
} | ||
} | ||
if (ttl < 0) | ||
throw new Error('TTL expired during attempt roughtFactor'); | ||
return results; | ||
} | ||
} | ||
exports.CommonsNumber = CommonsNumber; | ||
//# sourceMappingURL=commons-number.js.map |
{ | ||
"name": "tscommons-core", | ||
"version": "1.27.0", | ||
"version": "1.28.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
276403
3661