-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
638 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export declare class SortedList<T> { | ||
private array; | ||
private compare; | ||
constructor(array: Array<T>, compare: (a: T, b: T) => number); | ||
pop(): T | undefined; | ||
insert(element: T): number; | ||
length(): number; | ||
toArray(): T[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
exports.SortedList = void 0; | ||
class SortedList { | ||
constructor(array, compare) { | ||
this.array = array; | ||
this.compare = compare; | ||
this.array.sort(compare); | ||
} | ||
pop() { | ||
return this.array.shift(); | ||
} | ||
insert(element) { | ||
let high = this.array.length - 1; | ||
let low = 0; | ||
let mid; | ||
let highElement, lowElement, midElement; | ||
let compareHigh, compareLow, compareMid; | ||
let targetIndex; | ||
while (targetIndex === undefined) { | ||
if (high < low) { | ||
targetIndex = low; | ||
continue; | ||
} | ||
mid = Math.floor((low + high) / 2); | ||
highElement = this.array[high]; | ||
lowElement = this.array[low]; | ||
midElement = this.array[mid]; | ||
compareHigh = this.compare(element, highElement); | ||
compareLow = this.compare(element, lowElement); | ||
compareMid = this.compare(element, midElement); | ||
if (low === high) { | ||
// Target index is either to the left or right of element at low | ||
if (compareLow <= 0) targetIndex = low; | ||
else targetIndex = low + 1; | ||
continue; | ||
} | ||
if (compareHigh >= 0) { | ||
// Target index is to the right of high | ||
low = high; | ||
continue; | ||
} | ||
if (compareLow <= 0) { | ||
// Target index is to the left of low | ||
high = low; | ||
continue; | ||
} | ||
if (compareMid <= 0) { | ||
// Target index is to the left of mid | ||
high = mid; | ||
continue; | ||
} | ||
// Target index is to the right of mid | ||
low = mid + 1; | ||
} | ||
this.array.splice(targetIndex, 0, element); | ||
return targetIndex; | ||
} | ||
length() { | ||
return this.array.length; | ||
} | ||
toArray() { | ||
return this.array; | ||
} | ||
} | ||
exports.SortedList = SortedList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
import * as assert from 'assert'; | ||
import { describe, it } from 'mocha'; | ||
import { HuffmanTapTreeNode, Taptree } from '../src/types'; | ||
import { tapTreeUsingHuffmanConstructor } from '../src/psbt/bip371'; | ||
|
||
describe('Taptree using Huffman Constructor', () => { | ||
const scriptBuff = Buffer.from(''); | ||
|
||
it('test empty array', () => { | ||
assert.throws( | ||
() => tapTreeUsingHuffmanConstructor([]), | ||
'Cannot create taptree from empty list.', | ||
); | ||
}); | ||
|
||
it( | ||
'should return only one node for a single leaf', | ||
testLeafDistances([{ weight: 1, node: { output: scriptBuff } }], [0]), | ||
); | ||
|
||
it( | ||
'should return a balanced tree for a list of scripts with equal weights', | ||
testLeafDistances( | ||
[ | ||
{ | ||
weight: 1, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
{ | ||
weight: 1, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
{ | ||
weight: 1, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
{ | ||
weight: 1, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
], | ||
[2, 2, 2, 2], | ||
), | ||
); | ||
|
||
it( | ||
'should return an optimal binary tree for a list of scripts with weights [1, 2, 3, 4, 5]', | ||
testLeafDistances( | ||
[ | ||
{ | ||
weight: 1, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
{ | ||
weight: 2, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
{ | ||
weight: 3, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
{ | ||
weight: 4, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
{ | ||
weight: 5, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
], | ||
[3, 3, 2, 2, 2], | ||
), | ||
); | ||
|
||
it( | ||
'should return an optimal binary tree for a list of scripts with weights [1, 2, 3, 3]', | ||
testLeafDistances( | ||
[ | ||
{ | ||
weight: 1, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
{ | ||
weight: 2, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
{ | ||
weight: 3, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
{ | ||
weight: 3, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
], | ||
[3, 3, 2, 1], | ||
), | ||
); | ||
|
||
it( | ||
'should return an optimal binary tree for a list of scripts with some negative weights: [1, 2, 3, -3]', | ||
testLeafDistances( | ||
[ | ||
{ | ||
weight: 1, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
{ | ||
weight: 2, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
{ | ||
weight: 3, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
{ | ||
weight: -3, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
], | ||
[3, 2, 1, 3], | ||
), | ||
); | ||
|
||
it( | ||
'should return an optimal binary tree for a list of scripts with some weights specified as infinity', | ||
testLeafDistances( | ||
[ | ||
{ | ||
weight: 1, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
{ | ||
weight: Number.POSITIVE_INFINITY, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
{ | ||
weight: 3, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
{ | ||
weight: Number.NEGATIVE_INFINITY, | ||
node: { | ||
output: scriptBuff, | ||
}, | ||
}, | ||
], | ||
[3, 1, 2, 3], | ||
), | ||
); | ||
}); | ||
|
||
function testLeafDistances( | ||
input: HuffmanTapTreeNode[], | ||
expectedDistances: number[], | ||
) { | ||
return () => { | ||
const tree = tapTreeUsingHuffmanConstructor(input); | ||
|
||
if (!Array.isArray(tree)) { | ||
// tree is just one node | ||
assert.deepEqual([0], expectedDistances); | ||
return; | ||
} | ||
|
||
const leaves = input.map(value => value.node); | ||
|
||
const map = new Map<Taptree, number>(); // Map of leaf to actual distance | ||
let currentDistance = 1; | ||
let currentArray: Array<Taptree[] | Taptree> = tree as any; | ||
let nextArray: Array<Taptree[] | Taptree> = []; | ||
while (currentArray.length > 0) { | ||
currentArray.forEach(value => { | ||
if (Array.isArray(value)) { | ||
nextArray = nextArray.concat(value); | ||
return; | ||
} | ||
map.set(value, currentDistance); | ||
}); | ||
|
||
currentDistance += 1; // New level | ||
currentArray = nextArray; | ||
nextArray = []; | ||
} | ||
|
||
const actualDistances = leaves.map(value => map.get(value)); | ||
assert.deepEqual(actualDistances, expectedDistances); | ||
}; | ||
} |
Oops, something went wrong.