@btc-vision/transaction 1.7.2 → 1.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/browser/index.js +1 -1
- package/browser/src/deterministic/DeterministicMap.d.ts +3 -1
- package/browser/src/deterministic/DeterministicSet.d.ts +4 -2
- package/browser/src/deterministic/Map.d.ts +15 -0
- package/browser/src/opnet.d.ts +1 -0
- package/build/deterministic/AddressMap.js +6 -2
- package/build/deterministic/DeterministicMap.d.ts +3 -1
- package/build/deterministic/DeterministicMap.js +46 -23
- package/build/deterministic/DeterministicSet.d.ts +4 -2
- package/build/deterministic/DeterministicSet.js +38 -20
- package/build/deterministic/Map.d.ts +15 -0
- package/build/deterministic/Map.js +66 -0
- package/build/opnet.d.ts +1 -0
- package/build/opnet.js +1 -0
- package/package.json +1 -1
- package/src/deterministic/AddressMap.ts +7 -3
- package/src/deterministic/DeterministicMap.ts +50 -25
- package/src/deterministic/DeterministicSet.ts +43 -20
- package/src/deterministic/Map.ts +76 -0
- package/src/opnet.ts +2 -0
|
@@ -1,12 +1,14 @@
|
|
|
1
|
+
import { Map } from './Map.js';
|
|
1
2
|
export declare class DeterministicMap<K, V> {
|
|
2
3
|
#private;
|
|
4
|
+
private compareFn;
|
|
3
5
|
private map;
|
|
4
|
-
private readonly compareFn;
|
|
5
6
|
constructor(compareFn: (a: K, b: K) => number);
|
|
6
7
|
get size(): number;
|
|
7
8
|
static fromMap<K, V>(map: Map<K, V>, compareFn: (a: K, b: K) => number): DeterministicMap<K, V>;
|
|
8
9
|
set(key: K, value: V): void;
|
|
9
10
|
get(key: K): V | undefined;
|
|
11
|
+
entries(): IterableIterator<[K, V]>;
|
|
10
12
|
keys(): IterableIterator<K>;
|
|
11
13
|
values(): IterableIterator<V>;
|
|
12
14
|
has(key: K): boolean;
|
|
@@ -2,12 +2,14 @@ export declare class DeterministicSet<T> {
|
|
|
2
2
|
private compareFn;
|
|
3
3
|
private elements;
|
|
4
4
|
constructor(compareFn: (a: T, b: T) => number);
|
|
5
|
+
get size(): number;
|
|
6
|
+
static fromSet<T>(set: Set<T>, compareFn: (a: T, b: T) => number): DeterministicSet<T>;
|
|
5
7
|
add(value: T): void;
|
|
6
8
|
delete(value: T): boolean;
|
|
7
9
|
has(value: T): boolean;
|
|
8
10
|
clear(): void;
|
|
9
11
|
forEach(callback: (value: T, set: DeterministicSet<T>) => void): void;
|
|
10
|
-
|
|
11
|
-
get size(): number;
|
|
12
|
+
values(): IterableIterator<T>;
|
|
12
13
|
[Symbol.iterator](): IterableIterator<T>;
|
|
14
|
+
private binarySearch;
|
|
13
15
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare class Map<K, V> {
|
|
2
|
+
protected _keys: K[];
|
|
3
|
+
protected _values: V[];
|
|
4
|
+
get size(): number;
|
|
5
|
+
keys(): IterableIterator<K>;
|
|
6
|
+
values(): IterableIterator<V>;
|
|
7
|
+
entries(): IterableIterator<[K, V]>;
|
|
8
|
+
set(key: K, value: V): void;
|
|
9
|
+
indexOf(key: K): number;
|
|
10
|
+
get(key: K): V | undefined;
|
|
11
|
+
has(key: K): boolean;
|
|
12
|
+
delete(key: K): boolean;
|
|
13
|
+
clear(): void;
|
|
14
|
+
[Symbol.iterator](): IterableIterator<[K, V]>;
|
|
15
|
+
}
|
package/browser/src/opnet.d.ts
CHANGED
|
@@ -75,6 +75,7 @@ export * from './metadata/tokens.js';
|
|
|
75
75
|
export * from './transaction/browser/Web3Provider.js';
|
|
76
76
|
export * from './keypair/Secp256k1PointDeriver.js';
|
|
77
77
|
export * from './transaction/ContractAddress.js';
|
|
78
|
+
export * from './deterministic/Map.js';
|
|
78
79
|
declare global {
|
|
79
80
|
interface Window {
|
|
80
81
|
unisat?: Unisat;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Map } from './Map.js';
|
|
1
2
|
export class AddressMap {
|
|
2
3
|
constructor(iterable) {
|
|
3
4
|
this.items = new Map();
|
|
@@ -63,7 +64,10 @@ export class AddressMap {
|
|
|
63
64
|
callback.call(thisArg, this.items.get(key.toBigInt()), key, this);
|
|
64
65
|
}
|
|
65
66
|
}
|
|
66
|
-
[Symbol.iterator]() {
|
|
67
|
-
|
|
67
|
+
*[Symbol.iterator]() {
|
|
68
|
+
for (let i = 0; i < this.keyOrder.length; i++) {
|
|
69
|
+
const key = this.keyOrder[i];
|
|
70
|
+
yield [key, this.items.get(key.toBigInt())];
|
|
71
|
+
}
|
|
68
72
|
}
|
|
69
73
|
}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
+
import { Map } from './Map.js';
|
|
1
2
|
export declare class DeterministicMap<K, V> {
|
|
2
3
|
#private;
|
|
4
|
+
private compareFn;
|
|
3
5
|
private map;
|
|
4
|
-
private readonly compareFn;
|
|
5
6
|
constructor(compareFn: (a: K, b: K) => number);
|
|
6
7
|
get size(): number;
|
|
7
8
|
static fromMap<K, V>(map: Map<K, V>, compareFn: (a: K, b: K) => number): DeterministicMap<K, V>;
|
|
8
9
|
set(key: K, value: V): void;
|
|
9
10
|
get(key: K): V | undefined;
|
|
11
|
+
entries(): IterableIterator<[K, V]>;
|
|
10
12
|
keys(): IterableIterator<K>;
|
|
11
13
|
values(): IterableIterator<V>;
|
|
12
14
|
has(key: K): boolean;
|
|
@@ -9,13 +9,14 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var
|
|
12
|
+
var _DeterministicMap_keys;
|
|
13
|
+
import { Map } from './Map.js';
|
|
13
14
|
export class DeterministicMap {
|
|
14
15
|
constructor(compareFn) {
|
|
15
|
-
_DeterministicMap_keyOrder.set(this, void 0);
|
|
16
|
-
this.map = new Map();
|
|
17
|
-
__classPrivateFieldSet(this, _DeterministicMap_keyOrder, [], "f");
|
|
18
16
|
this.compareFn = compareFn;
|
|
17
|
+
_DeterministicMap_keys.set(this, void 0);
|
|
18
|
+
this.map = new Map();
|
|
19
|
+
__classPrivateFieldSet(this, _DeterministicMap_keys, [], "f");
|
|
19
20
|
}
|
|
20
21
|
get size() {
|
|
21
22
|
return this.map.size;
|
|
@@ -29,30 +30,39 @@ export class DeterministicMap {
|
|
|
29
30
|
}
|
|
30
31
|
set(key, value) {
|
|
31
32
|
if (!this.map.has(key)) {
|
|
32
|
-
__classPrivateFieldGet(this,
|
|
33
|
-
|
|
33
|
+
let left = 0, right = __classPrivateFieldGet(this, _DeterministicMap_keys, "f").length;
|
|
34
|
+
while (left < right) {
|
|
35
|
+
const mid = Math.floor((left + right) / 2);
|
|
36
|
+
if (this.compareFn(__classPrivateFieldGet(this, _DeterministicMap_keys, "f")[mid], key) < 0) {
|
|
37
|
+
left = mid + 1;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
right = mid;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
__classPrivateFieldGet(this, _DeterministicMap_keys, "f").splice(left, 0, key);
|
|
34
44
|
}
|
|
35
45
|
this.map.set(key, value);
|
|
36
46
|
}
|
|
37
47
|
get(key) {
|
|
38
48
|
return this.map.get(key);
|
|
39
49
|
}
|
|
40
|
-
|
|
41
|
-
|
|
50
|
+
*entries() {
|
|
51
|
+
for (const key of __classPrivateFieldGet(this, _DeterministicMap_keys, "f")) {
|
|
52
|
+
yield [key, this.map.get(key)];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
*keys() {
|
|
56
|
+
yield* __classPrivateFieldGet(this, _DeterministicMap_keys, "f");
|
|
42
57
|
}
|
|
43
|
-
values() {
|
|
44
|
-
const
|
|
45
|
-
for (let i = 0; i < __classPrivateFieldGet(this, _DeterministicMap_keyOrder, "f").length; i++) {
|
|
46
|
-
const key = __classPrivateFieldGet(this, _DeterministicMap_keyOrder, "f")[i];
|
|
58
|
+
*values() {
|
|
59
|
+
for (const key of __classPrivateFieldGet(this, _DeterministicMap_keys, "f")) {
|
|
47
60
|
const value = this.map.get(key);
|
|
48
|
-
if (value
|
|
49
|
-
values.push(value);
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
61
|
+
if (value === undefined && !this.map.has(key)) {
|
|
52
62
|
throw new Error('Value not found');
|
|
53
63
|
}
|
|
64
|
+
yield value;
|
|
54
65
|
}
|
|
55
|
-
return values.values();
|
|
56
66
|
}
|
|
57
67
|
has(key) {
|
|
58
68
|
return this.map.has(key);
|
|
@@ -60,23 +70,36 @@ export class DeterministicMap {
|
|
|
60
70
|
delete(key) {
|
|
61
71
|
if (this.map.has(key)) {
|
|
62
72
|
this.map.delete(key);
|
|
63
|
-
|
|
64
|
-
|
|
73
|
+
let left = 0, right = __classPrivateFieldGet(this, _DeterministicMap_keys, "f").length - 1;
|
|
74
|
+
while (left <= right) {
|
|
75
|
+
const mid = Math.floor((left + right) / 2);
|
|
76
|
+
const cmp = this.compareFn(__classPrivateFieldGet(this, _DeterministicMap_keys, "f")[mid], key);
|
|
77
|
+
if (cmp === 0) {
|
|
78
|
+
__classPrivateFieldGet(this, _DeterministicMap_keys, "f").splice(mid, 1);
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
else if (cmp < 0) {
|
|
82
|
+
left = mid + 1;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
right = mid - 1;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
65
88
|
}
|
|
66
89
|
return false;
|
|
67
90
|
}
|
|
68
91
|
clear() {
|
|
69
92
|
this.map.clear();
|
|
70
|
-
__classPrivateFieldSet(this,
|
|
93
|
+
__classPrivateFieldSet(this, _DeterministicMap_keys, [], "f");
|
|
71
94
|
}
|
|
72
95
|
forEach(callback) {
|
|
73
|
-
for (const key of __classPrivateFieldGet(this,
|
|
96
|
+
for (const key of __classPrivateFieldGet(this, _DeterministicMap_keys, "f")) {
|
|
74
97
|
const value = this.map.get(key);
|
|
75
98
|
callback(value, key, this);
|
|
76
99
|
}
|
|
77
100
|
}
|
|
78
|
-
*[(
|
|
79
|
-
for (const key of __classPrivateFieldGet(this,
|
|
101
|
+
*[(_DeterministicMap_keys = new WeakMap(), Symbol.iterator)]() {
|
|
102
|
+
for (const key of __classPrivateFieldGet(this, _DeterministicMap_keys, "f")) {
|
|
80
103
|
yield [key, this.map.get(key)];
|
|
81
104
|
}
|
|
82
105
|
}
|
|
@@ -2,12 +2,14 @@ export declare class DeterministicSet<T> {
|
|
|
2
2
|
private compareFn;
|
|
3
3
|
private elements;
|
|
4
4
|
constructor(compareFn: (a: T, b: T) => number);
|
|
5
|
+
get size(): number;
|
|
6
|
+
static fromSet<T>(set: Set<T>, compareFn: (a: T, b: T) => number): DeterministicSet<T>;
|
|
5
7
|
add(value: T): void;
|
|
6
8
|
delete(value: T): boolean;
|
|
7
9
|
has(value: T): boolean;
|
|
8
10
|
clear(): void;
|
|
9
11
|
forEach(callback: (value: T, set: DeterministicSet<T>) => void): void;
|
|
10
|
-
|
|
11
|
-
get size(): number;
|
|
12
|
+
values(): IterableIterator<T>;
|
|
12
13
|
[Symbol.iterator](): IterableIterator<T>;
|
|
14
|
+
private binarySearch;
|
|
13
15
|
}
|
|
@@ -3,22 +3,32 @@ export class DeterministicSet {
|
|
|
3
3
|
this.compareFn = compareFn;
|
|
4
4
|
this.elements = [];
|
|
5
5
|
}
|
|
6
|
+
get size() {
|
|
7
|
+
return this.elements.length;
|
|
8
|
+
}
|
|
9
|
+
static fromSet(set, compareFn) {
|
|
10
|
+
const deterministicSet = new DeterministicSet(compareFn);
|
|
11
|
+
for (const value of set) {
|
|
12
|
+
deterministicSet.add(value);
|
|
13
|
+
}
|
|
14
|
+
return deterministicSet;
|
|
15
|
+
}
|
|
6
16
|
add(value) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
this.elements.
|
|
17
|
+
const { found, index } = this.binarySearch(value);
|
|
18
|
+
if (!found) {
|
|
19
|
+
this.elements.splice(index, 0, value);
|
|
10
20
|
}
|
|
11
21
|
}
|
|
12
22
|
delete(value) {
|
|
13
|
-
const index = this.
|
|
14
|
-
if (
|
|
15
|
-
|
|
23
|
+
const { found, index } = this.binarySearch(value);
|
|
24
|
+
if (found) {
|
|
25
|
+
this.elements.splice(index, 1);
|
|
26
|
+
return true;
|
|
16
27
|
}
|
|
17
|
-
|
|
18
|
-
return true;
|
|
28
|
+
return false;
|
|
19
29
|
}
|
|
20
30
|
has(value) {
|
|
21
|
-
return this.
|
|
31
|
+
return this.binarySearch(value).found;
|
|
22
32
|
}
|
|
23
33
|
clear() {
|
|
24
34
|
this.elements = [];
|
|
@@ -28,19 +38,27 @@ export class DeterministicSet {
|
|
|
28
38
|
callback(value, this);
|
|
29
39
|
}
|
|
30
40
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
for (const value of set) {
|
|
34
|
-
deterministicSet.add(value);
|
|
35
|
-
}
|
|
36
|
-
return deterministicSet;
|
|
37
|
-
}
|
|
38
|
-
get size() {
|
|
39
|
-
return this.elements.length;
|
|
41
|
+
*values() {
|
|
42
|
+
yield* this.elements;
|
|
40
43
|
}
|
|
41
44
|
*[Symbol.iterator]() {
|
|
42
|
-
|
|
43
|
-
|
|
45
|
+
yield* this.elements;
|
|
46
|
+
}
|
|
47
|
+
binarySearch(value) {
|
|
48
|
+
let left = 0, right = this.elements.length;
|
|
49
|
+
while (left < right) {
|
|
50
|
+
const mid = Math.floor((left + right) / 2);
|
|
51
|
+
const cmp = this.compareFn(this.elements[mid], value);
|
|
52
|
+
if (cmp === 0) {
|
|
53
|
+
return { found: true, index: mid };
|
|
54
|
+
}
|
|
55
|
+
else if (cmp < 0) {
|
|
56
|
+
left = mid + 1;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
right = mid;
|
|
60
|
+
}
|
|
44
61
|
}
|
|
62
|
+
return { found: false, index: left };
|
|
45
63
|
}
|
|
46
64
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare class Map<K, V> {
|
|
2
|
+
protected _keys: K[];
|
|
3
|
+
protected _values: V[];
|
|
4
|
+
get size(): number;
|
|
5
|
+
keys(): IterableIterator<K>;
|
|
6
|
+
values(): IterableIterator<V>;
|
|
7
|
+
entries(): IterableIterator<[K, V]>;
|
|
8
|
+
set(key: K, value: V): void;
|
|
9
|
+
indexOf(key: K): number;
|
|
10
|
+
get(key: K): V | undefined;
|
|
11
|
+
has(key: K): boolean;
|
|
12
|
+
delete(key: K): boolean;
|
|
13
|
+
clear(): void;
|
|
14
|
+
[Symbol.iterator](): IterableIterator<[K, V]>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
export class Map {
|
|
2
|
+
constructor() {
|
|
3
|
+
this._keys = [];
|
|
4
|
+
this._values = [];
|
|
5
|
+
}
|
|
6
|
+
get size() {
|
|
7
|
+
return this._keys.length;
|
|
8
|
+
}
|
|
9
|
+
*keys() {
|
|
10
|
+
yield* this._keys;
|
|
11
|
+
}
|
|
12
|
+
*values() {
|
|
13
|
+
yield* this._values;
|
|
14
|
+
}
|
|
15
|
+
*entries() {
|
|
16
|
+
for (let i = 0; i < this._keys.length; i++) {
|
|
17
|
+
yield [this._keys[i], this._values[i]];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
set(key, value) {
|
|
21
|
+
const index = this.indexOf(key);
|
|
22
|
+
if (index == -1) {
|
|
23
|
+
this._keys.push(key);
|
|
24
|
+
this._values.push(value);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
this._values[index] = value;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
indexOf(key) {
|
|
31
|
+
for (let i = 0; i < this._keys.length; i++) {
|
|
32
|
+
if (this._keys[i] == key) {
|
|
33
|
+
return i;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return -1;
|
|
37
|
+
}
|
|
38
|
+
get(key) {
|
|
39
|
+
const index = this.indexOf(key);
|
|
40
|
+
if (index == -1) {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
return this._values[index];
|
|
44
|
+
}
|
|
45
|
+
has(key) {
|
|
46
|
+
return this.indexOf(key) != -1;
|
|
47
|
+
}
|
|
48
|
+
delete(key) {
|
|
49
|
+
const index = this.indexOf(key);
|
|
50
|
+
if (index == -1) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
this._keys.splice(index, 1);
|
|
54
|
+
this._values.splice(index, 1);
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
clear() {
|
|
58
|
+
this._keys = [];
|
|
59
|
+
this._values = [];
|
|
60
|
+
}
|
|
61
|
+
*[Symbol.iterator]() {
|
|
62
|
+
for (let i = 0; i < this._keys.length; i++) {
|
|
63
|
+
yield [this._keys[i], this._values[i]];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
package/build/opnet.d.ts
CHANGED
|
@@ -75,6 +75,7 @@ export * from './metadata/tokens.js';
|
|
|
75
75
|
export * from './transaction/browser/Web3Provider.js';
|
|
76
76
|
export * from './keypair/Secp256k1PointDeriver.js';
|
|
77
77
|
export * from './transaction/ContractAddress.js';
|
|
78
|
+
export * from './deterministic/Map.js';
|
|
78
79
|
declare global {
|
|
79
80
|
interface Window {
|
|
80
81
|
unisat?: Unisat;
|
package/build/opnet.js
CHANGED
package/package.json
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Address } from '../keypair/Address.js';
|
|
2
|
+
import { Map } from './Map.js';
|
|
2
3
|
|
|
3
4
|
export class AddressMap<V> {
|
|
4
5
|
private items: Map<bigint, V>;
|
|
@@ -15,7 +16,7 @@ export class AddressMap<V> {
|
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
18
|
|
|
18
|
-
get size(): number {
|
|
19
|
+
public get size(): number {
|
|
19
20
|
return this.keyOrder.length;
|
|
20
21
|
}
|
|
21
22
|
|
|
@@ -84,7 +85,10 @@ export class AddressMap<V> {
|
|
|
84
85
|
}
|
|
85
86
|
}
|
|
86
87
|
|
|
87
|
-
[Symbol.iterator](): IterableIterator<[Address, V]> {
|
|
88
|
-
|
|
88
|
+
*[Symbol.iterator](): IterableIterator<[Address, V]> {
|
|
89
|
+
for (let i = 0; i < this.keyOrder.length; i++) {
|
|
90
|
+
const key = this.keyOrder[i];
|
|
91
|
+
yield [key, this.items.get(key.toBigInt()) as V];
|
|
92
|
+
}
|
|
89
93
|
}
|
|
90
94
|
}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
+
import { Map } from './Map.js';
|
|
2
|
+
|
|
1
3
|
export class DeterministicMap<K, V> {
|
|
2
4
|
private map: Map<K, V>;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
#keyOrder: K[];
|
|
5
|
+
#keys: K[];
|
|
6
6
|
|
|
7
|
-
constructor(compareFn: (a: K, b: K) => number) {
|
|
7
|
+
constructor(private compareFn: (a: K, b: K) => number) {
|
|
8
8
|
this.map = new Map<K, V>();
|
|
9
|
-
this.#
|
|
10
|
-
this.compareFn = compareFn;
|
|
9
|
+
this.#keys = [];
|
|
11
10
|
}
|
|
12
11
|
|
|
13
12
|
public get size(): number {
|
|
@@ -27,8 +26,18 @@ export class DeterministicMap<K, V> {
|
|
|
27
26
|
|
|
28
27
|
public set(key: K, value: V): void {
|
|
29
28
|
if (!this.map.has(key)) {
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
// Binary search for insertion position
|
|
30
|
+
let left = 0,
|
|
31
|
+
right = this.#keys.length;
|
|
32
|
+
while (left < right) {
|
|
33
|
+
const mid = Math.floor((left + right) / 2);
|
|
34
|
+
if (this.compareFn(this.#keys[mid], key) < 0) {
|
|
35
|
+
left = mid + 1;
|
|
36
|
+
} else {
|
|
37
|
+
right = mid;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
this.#keys.splice(left, 0, key);
|
|
32
41
|
}
|
|
33
42
|
this.map.set(key, value);
|
|
34
43
|
}
|
|
@@ -37,25 +46,25 @@ export class DeterministicMap<K, V> {
|
|
|
37
46
|
return this.map.get(key);
|
|
38
47
|
}
|
|
39
48
|
|
|
40
|
-
public
|
|
41
|
-
|
|
49
|
+
public *entries(): IterableIterator<[K, V]> {
|
|
50
|
+
for (const key of this.#keys) {
|
|
51
|
+
yield [key, this.map.get(key) as V];
|
|
52
|
+
}
|
|
42
53
|
}
|
|
43
54
|
|
|
44
|
-
public
|
|
45
|
-
|
|
55
|
+
public *keys(): IterableIterator<K> {
|
|
56
|
+
yield* this.#keys;
|
|
57
|
+
}
|
|
46
58
|
|
|
47
|
-
|
|
48
|
-
|
|
59
|
+
public *values(): IterableIterator<V> {
|
|
60
|
+
for (const key of this.#keys) {
|
|
49
61
|
const value = this.map.get(key);
|
|
50
|
-
|
|
51
|
-
if (value !== undefined) {
|
|
52
|
-
values.push(value);
|
|
53
|
-
} else {
|
|
62
|
+
if (value === undefined && !this.map.has(key)) {
|
|
54
63
|
throw new Error('Value not found');
|
|
55
64
|
}
|
|
56
|
-
}
|
|
57
65
|
|
|
58
|
-
|
|
66
|
+
yield value as V;
|
|
67
|
+
}
|
|
59
68
|
}
|
|
60
69
|
|
|
61
70
|
public has(key: K): boolean {
|
|
@@ -65,26 +74,42 @@ export class DeterministicMap<K, V> {
|
|
|
65
74
|
public delete(key: K): boolean {
|
|
66
75
|
if (this.map.has(key)) {
|
|
67
76
|
this.map.delete(key);
|
|
68
|
-
|
|
69
|
-
|
|
77
|
+
|
|
78
|
+
// Binary search to find the key's index
|
|
79
|
+
let left = 0,
|
|
80
|
+
right = this.#keys.length - 1;
|
|
81
|
+
while (left <= right) {
|
|
82
|
+
const mid = Math.floor((left + right) / 2);
|
|
83
|
+
const cmp = this.compareFn(this.#keys[mid], key);
|
|
84
|
+
|
|
85
|
+
if (cmp === 0) {
|
|
86
|
+
// Found it, remove at this index
|
|
87
|
+
this.#keys.splice(mid, 1);
|
|
88
|
+
return true;
|
|
89
|
+
} else if (cmp < 0) {
|
|
90
|
+
left = mid + 1;
|
|
91
|
+
} else {
|
|
92
|
+
right = mid - 1;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
70
95
|
}
|
|
71
96
|
return false;
|
|
72
97
|
}
|
|
73
98
|
|
|
74
99
|
public clear(): void {
|
|
75
100
|
this.map.clear();
|
|
76
|
-
this.#
|
|
101
|
+
this.#keys = [];
|
|
77
102
|
}
|
|
78
103
|
|
|
79
104
|
public forEach(callback: (value: V, key: K, map: DeterministicMap<K, V>) => void): void {
|
|
80
|
-
for (const key of this.#
|
|
105
|
+
for (const key of this.#keys) {
|
|
81
106
|
const value = this.map.get(key) as V;
|
|
82
107
|
callback(value, key, this);
|
|
83
108
|
}
|
|
84
109
|
}
|
|
85
110
|
|
|
86
111
|
*[Symbol.iterator](): IterableIterator<[K, V]> {
|
|
87
|
-
for (const key of this.#
|
|
112
|
+
for (const key of this.#keys) {
|
|
88
113
|
yield [key, this.map.get(key) as V];
|
|
89
114
|
}
|
|
90
115
|
}
|
|
@@ -5,25 +5,39 @@ export class DeterministicSet<T> {
|
|
|
5
5
|
this.elements = [];
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
+
public get size(): number {
|
|
9
|
+
return this.elements.length;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public static fromSet<T>(set: Set<T>, compareFn: (a: T, b: T) => number): DeterministicSet<T> {
|
|
13
|
+
const deterministicSet = new DeterministicSet<T>(compareFn);
|
|
14
|
+
for (const value of set) {
|
|
15
|
+
deterministicSet.add(value);
|
|
16
|
+
}
|
|
17
|
+
return deterministicSet;
|
|
18
|
+
}
|
|
19
|
+
|
|
8
20
|
public add(value: T): void {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
21
|
+
const { found, index } = this.binarySearch(value);
|
|
22
|
+
|
|
23
|
+
if (!found) {
|
|
24
|
+
this.elements.splice(index, 0, value);
|
|
12
25
|
}
|
|
13
26
|
}
|
|
14
27
|
|
|
15
28
|
public delete(value: T): boolean {
|
|
16
|
-
const index = this.
|
|
17
|
-
|
|
18
|
-
|
|
29
|
+
const { found, index } = this.binarySearch(value);
|
|
30
|
+
|
|
31
|
+
if (found) {
|
|
32
|
+
this.elements.splice(index, 1);
|
|
33
|
+
return true;
|
|
19
34
|
}
|
|
20
35
|
|
|
21
|
-
|
|
22
|
-
return true;
|
|
36
|
+
return false;
|
|
23
37
|
}
|
|
24
38
|
|
|
25
39
|
public has(value: T): boolean {
|
|
26
|
-
return this.
|
|
40
|
+
return this.binarySearch(value).found;
|
|
27
41
|
}
|
|
28
42
|
|
|
29
43
|
public clear(): void {
|
|
@@ -36,21 +50,30 @@ export class DeterministicSet<T> {
|
|
|
36
50
|
}
|
|
37
51
|
}
|
|
38
52
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
for (const value of set) {
|
|
42
|
-
deterministicSet.add(value);
|
|
43
|
-
}
|
|
44
|
-
return deterministicSet;
|
|
53
|
+
*values(): IterableIterator<T> {
|
|
54
|
+
yield* this.elements;
|
|
45
55
|
}
|
|
46
56
|
|
|
47
|
-
|
|
48
|
-
|
|
57
|
+
*[Symbol.iterator](): IterableIterator<T> {
|
|
58
|
+
yield* this.elements;
|
|
49
59
|
}
|
|
50
60
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
61
|
+
private binarySearch(value: T): { found: boolean; index: number } {
|
|
62
|
+
let left = 0, right = this.elements.length;
|
|
63
|
+
|
|
64
|
+
while (left < right) {
|
|
65
|
+
const mid = Math.floor((left + right) / 2);
|
|
66
|
+
const cmp = this.compareFn(this.elements[mid], value);
|
|
67
|
+
|
|
68
|
+
if (cmp === 0) {
|
|
69
|
+
return { found: true, index: mid };
|
|
70
|
+
} else if (cmp < 0) {
|
|
71
|
+
left = mid + 1;
|
|
72
|
+
} else {
|
|
73
|
+
right = mid;
|
|
74
|
+
}
|
|
54
75
|
}
|
|
76
|
+
|
|
77
|
+
return { found: false, index: left };
|
|
55
78
|
}
|
|
56
79
|
}
|