@btc-vision/transaction
Version:
OPNet transaction library allows you to create and sign transactions for the OPNet network.
70 lines • 1.8 kB
JavaScript
export class DeterministicSet {
compareFn;
elements;
constructor(compareFn) {
this.compareFn = compareFn;
this.elements = [];
}
get size() {
return this.elements.length;
}
static fromSet(set, compareFn) {
const deterministicSet = new DeterministicSet(compareFn);
for (const value of set) {
deterministicSet.add(value);
}
return deterministicSet;
}
add(value) {
const { found, index } = this.binarySearch(value);
if (!found) {
this.elements.splice(index, 0, value);
}
}
delete(value) {
const { found, index } = this.binarySearch(value);
if (found) {
this.elements.splice(index, 1);
return true;
}
return false;
}
has(value) {
return this.binarySearch(value).found;
}
clear() {
this.elements = [];
}
[Symbol.dispose]() {
this.clear();
}
forEach(callback) {
for (const value of this.elements) {
callback(value, this);
}
}
*values() {
yield* this.elements;
}
*[Symbol.iterator]() {
yield* this.elements;
}
binarySearch(value) {
let left = 0, right = this.elements.length;
while (left < right) {
const mid = Math.floor((left + right) / 2);
const cmp = this.compareFn(this.elements[mid], value);
if (cmp === 0) {
return { found: true, index: mid };
}
else if (cmp < 0) {
left = mid + 1;
}
else {
right = mid;
}
}
return { found: false, index: left };
}
}
//# sourceMappingURL=DeterministicSet.js.map