@btc-vision/transaction 1.7.3 → 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.
@@ -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
- static fromSet<T>(set: Set<T>, compareFn: (a: T, b: T) => number): DeterministicSet<T>;
11
- get size(): number;
12
+ values(): IterableIterator<T>;
12
13
  [Symbol.iterator](): IterableIterator<T>;
14
+ private binarySearch;
13
15
  }
@@ -1,13 +1,12 @@
1
- import { i32 } from '../utils/types.js';
2
1
  export declare class Map<K, V> {
3
2
  protected _keys: K[];
4
3
  protected _values: V[];
5
- get size(): i32;
4
+ get size(): number;
6
5
  keys(): IterableIterator<K>;
7
6
  values(): IterableIterator<V>;
8
7
  entries(): IterableIterator<[K, V]>;
9
8
  set(key: K, value: V): void;
10
- indexOf(key: K): i32;
9
+ indexOf(key: K): number;
11
10
  get(key: K): V | undefined;
12
11
  has(key: K): boolean;
13
12
  delete(key: K): boolean;
@@ -30,8 +30,17 @@ export class DeterministicMap {
30
30
  }
31
31
  set(key, value) {
32
32
  if (!this.map.has(key)) {
33
- __classPrivateFieldGet(this, _DeterministicMap_keys, "f").push(key);
34
- __classPrivateFieldGet(this, _DeterministicMap_keys, "f").sort(this.compareFn);
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);
35
44
  }
36
45
  this.map.set(key, value);
37
46
  }
@@ -61,8 +70,21 @@ export class DeterministicMap {
61
70
  delete(key) {
62
71
  if (this.map.has(key)) {
63
72
  this.map.delete(key);
64
- __classPrivateFieldSet(this, _DeterministicMap_keys, __classPrivateFieldGet(this, _DeterministicMap_keys, "f").filter((k) => k !== key), "f");
65
- return true;
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
+ }
66
88
  }
67
89
  return false;
68
90
  }
@@ -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
- static fromSet<T>(set: Set<T>, compareFn: (a: T, b: T) => number): DeterministicSet<T>;
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
- if (!this.elements.includes(value)) {
8
- this.elements.push(value);
9
- this.elements.sort(this.compareFn);
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.elements.indexOf(value);
14
- if (index === -1) {
15
- return false;
23
+ const { found, index } = this.binarySearch(value);
24
+ if (found) {
25
+ this.elements.splice(index, 1);
26
+ return true;
16
27
  }
17
- this.elements.splice(index, 1);
18
- return true;
28
+ return false;
19
29
  }
20
30
  has(value) {
21
- return this.elements.includes(value);
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
- static fromSet(set, compareFn) {
32
- const deterministicSet = new DeterministicSet(compareFn);
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
- for (const value of this.elements) {
43
- yield value;
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
  }
@@ -1,13 +1,12 @@
1
- import { i32 } from '../utils/types.js';
2
1
  export declare class Map<K, V> {
3
2
  protected _keys: K[];
4
3
  protected _values: V[];
5
- get size(): i32;
4
+ get size(): number;
6
5
  keys(): IterableIterator<K>;
7
6
  values(): IterableIterator<V>;
8
7
  entries(): IterableIterator<[K, V]>;
9
8
  set(key: K, value: V): void;
10
- indexOf(key: K): i32;
9
+ indexOf(key: K): number;
11
10
  get(key: K): V | undefined;
12
11
  has(key: K): boolean;
13
12
  delete(key: K): boolean;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@btc-vision/transaction",
3
3
  "type": "module",
4
- "version": "1.7.3",
4
+ "version": "1.7.4",
5
5
  "author": "BlobMaster41",
6
6
  "description": "OPNet transaction library allows you to create and sign transactions for the OPNet network.",
7
7
  "engines": {
@@ -26,8 +26,18 @@ export class DeterministicMap<K, V> {
26
26
 
27
27
  public set(key: K, value: V): void {
28
28
  if (!this.map.has(key)) {
29
- this.#keys.push(key);
30
- this.#keys.sort(this.compareFn);
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);
31
41
  }
32
42
  this.map.set(key, value);
33
43
  }
@@ -64,8 +74,24 @@ export class DeterministicMap<K, V> {
64
74
  public delete(key: K): boolean {
65
75
  if (this.map.has(key)) {
66
76
  this.map.delete(key);
67
- this.#keys = this.#keys.filter((k) => k !== key);
68
- return true;
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
+ }
69
95
  }
70
96
  return false;
71
97
  }
@@ -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
- if (!this.elements.includes(value)) {
10
- this.elements.push(value);
11
- this.elements.sort(this.compareFn);
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.elements.indexOf(value);
17
- if (index === -1) {
18
- return false;
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
- this.elements.splice(index, 1);
22
- return true;
36
+ return false;
23
37
  }
24
38
 
25
39
  public has(value: T): boolean {
26
- return this.elements.includes(value);
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
- public static fromSet<T>(set: Set<T>, compareFn: (a: T, b: T) => number): DeterministicSet<T> {
40
- const deterministicSet = new DeterministicSet<T>(compareFn);
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
- public get size(): number {
48
- return this.elements.length;
57
+ *[Symbol.iterator](): IterableIterator<T> {
58
+ yield* this.elements;
49
59
  }
50
60
 
51
- *[Symbol.iterator](): IterableIterator<T> {
52
- for (const value of this.elements) {
53
- yield value;
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
  }
@@ -1,10 +1,8 @@
1
- import { i32 } from '../utils/types.js';
2
-
3
1
  export class Map<K, V> {
4
2
  protected _keys: K[] = [];
5
3
  protected _values: V[] = [];
6
4
 
7
- public get size(): i32 {
5
+ public get size(): number {
8
6
  return this._keys.length;
9
7
  }
10
8
 
@@ -17,13 +15,13 @@ export class Map<K, V> {
17
15
  }
18
16
 
19
17
  public *entries(): IterableIterator<[K, V]> {
20
- for (let i: i32 = 0; i < this._keys.length; i++) {
18
+ for (let i: number = 0; i < this._keys.length; i++) {
21
19
  yield [this._keys[i], this._values[i]];
22
20
  }
23
21
  }
24
22
 
25
23
  public set(key: K, value: V): void {
26
- const index: i32 = this.indexOf(key);
24
+ const index: number = this.indexOf(key);
27
25
  if (index == -1) {
28
26
  this._keys.push(key);
29
27
  this._values.push(value);
@@ -32,8 +30,8 @@ export class Map<K, V> {
32
30
  }
33
31
  }
34
32
 
35
- public indexOf(key: K): i32 {
36
- for (let i: i32 = 0; i < this._keys.length; i++) {
33
+ public indexOf(key: K): number {
34
+ for (let i: number = 0; i < this._keys.length; i++) {
37
35
  if (this._keys[i] == key) {
38
36
  return i;
39
37
  }
@@ -43,7 +41,7 @@ export class Map<K, V> {
43
41
  }
44
42
 
45
43
  public get(key: K): V | undefined {
46
- const index: i32 = this.indexOf(key);
44
+ const index: number = this.indexOf(key);
47
45
  if (index == -1) {
48
46
  return undefined;
49
47
  }
@@ -55,7 +53,7 @@ export class Map<K, V> {
55
53
  }
56
54
 
57
55
  public delete(key: K): boolean {
58
- const index: i32 = this.indexOf(key);
56
+ const index: number = this.indexOf(key);
59
57
  if (index == -1) {
60
58
  return false;
61
59
  }
@@ -71,7 +69,7 @@ export class Map<K, V> {
71
69
  }
72
70
 
73
71
  *[Symbol.iterator](): IterableIterator<[K, V]> {
74
- for (let i: i32 = 0; i < this._keys.length; i++) {
72
+ for (let i: number = 0; i < this._keys.length; i++) {
75
73
  yield [this._keys[i], this._values[i]];
76
74
  }
77
75
  }