diff options
author | Florian Dold <florian.dold@gmail.com> | 2020-05-11 18:03:25 +0530 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2020-05-11 18:03:25 +0530 |
commit | 5d6192b0cd356f7e56fa8d6193a2e74233a52f4b (patch) | |
tree | 0360ba1d39e6ff081e25045652f457faca8cb879 /src/util | |
parent | 7e947ca2cdd8e66ea49822acbad81e7d35289c0a (diff) | |
download | wallet-core-5d6192b0cd356f7e56fa8d6193a2e74233a52f4b.tar.xz |
make planchet management during withdrawal O(n) instead of O(n^2)
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/amounts.ts | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/util/amounts.ts b/src/util/amounts.ts index 5953f5130..d962b6cbd 100644 --- a/src/util/amounts.ts +++ b/src/util/amounts.ts @@ -332,6 +332,33 @@ function check(a: any): boolean { } } +function mult(a: AmountJson, n: number): Result { + if (!Number.isInteger(n)) { + throw Error("amount can only be multipied by an integer"); + } + if (n < 0) { + throw Error("amount can only be multiplied by a positive integer"); + } + if (n == 0) { + return { amount: getZero(a.currency), saturated: false }; + } + let acc = {... a}; + while (n > 1) { + let r: Result; + if (n % 2 == 0) { + n = n / 2; + r = add(acc, acc); + } else { + r = add(acc, a); + } + if (r.saturated) { + return r; + } + acc = r.amount; + } + return { amount: acc, saturated: false }; +} + // Export all amount-related functions here for better IDE experience. export const Amounts = { stringify: stringify, @@ -341,9 +368,11 @@ export const Amounts = { add: add, sum: sum, sub: sub, + mult: mult, check: check, getZero: getZero, isZero: isZero, maxAmountValue: maxAmountValue, fromFloat: fromFloat, + copy: copy, }; |