arkanalyzer 1.0.20 → 1.0.21

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.
Files changed (279) hide show
  1. package/lib/Config.d.ts +70 -70
  2. package/lib/Config.js +198 -198
  3. package/lib/Scene.d.ts +316 -316
  4. package/lib/Scene.js +1316 -1316
  5. package/lib/callgraph/algorithm/AbstractAnalysis.d.ts +28 -28
  6. package/lib/callgraph/algorithm/AbstractAnalysis.js +144 -144
  7. package/lib/callgraph/algorithm/ClassHierarchyAnalysis.d.ts +10 -10
  8. package/lib/callgraph/algorithm/ClassHierarchyAnalysis.js +69 -69
  9. package/lib/callgraph/algorithm/RapidTypeAnalysis.d.ts +15 -15
  10. package/lib/callgraph/algorithm/RapidTypeAnalysis.js +146 -146
  11. package/lib/callgraph/common/Statistics.d.ts +57 -57
  12. package/lib/callgraph/common/Statistics.js +228 -228
  13. package/lib/callgraph/model/CallGraph.d.ts +96 -96
  14. package/lib/callgraph/model/CallGraph.js +354 -354
  15. package/lib/callgraph/model/builder/CallGraphBuilder.d.ts +15 -15
  16. package/lib/callgraph/model/builder/CallGraphBuilder.js +106 -106
  17. package/lib/callgraph/pointerAnalysis/Context.d.ts +37 -37
  18. package/lib/callgraph/pointerAnalysis/Context.js +155 -155
  19. package/lib/callgraph/pointerAnalysis/DummyCallCreator.d.ts +22 -22
  20. package/lib/callgraph/pointerAnalysis/DummyCallCreator.js +104 -104
  21. package/lib/callgraph/pointerAnalysis/PTAUtils.d.ts +5 -5
  22. package/lib/callgraph/pointerAnalysis/PTAUtils.js +46 -46
  23. package/lib/callgraph/pointerAnalysis/Pag.d.ts +261 -261
  24. package/lib/callgraph/pointerAnalysis/Pag.js +930 -930
  25. package/lib/callgraph/pointerAnalysis/PagBuilder.d.ts +157 -157
  26. package/lib/callgraph/pointerAnalysis/PagBuilder.js +1399 -1399
  27. package/lib/callgraph/pointerAnalysis/PointerAnalysis.d.ts +72 -72
  28. package/lib/callgraph/pointerAnalysis/PointerAnalysis.js +573 -573
  29. package/lib/callgraph/pointerAnalysis/PointerAnalysisConfig.d.ts +15 -15
  30. package/lib/callgraph/pointerAnalysis/PointerAnalysisConfig.js +81 -81
  31. package/lib/callgraph/pointerAnalysis/PtsDS.d.ts +80 -80
  32. package/lib/callgraph/pointerAnalysis/PtsDS.js +308 -308
  33. package/lib/core/base/Constant.d.ts +48 -48
  34. package/lib/core/base/Constant.js +103 -103
  35. package/lib/core/base/Decorator.d.ts +15 -15
  36. package/lib/core/base/Decorator.js +46 -46
  37. package/lib/core/base/DefUseChain.d.ts +8 -8
  38. package/lib/core/base/DefUseChain.js +25 -25
  39. package/lib/core/base/Expr.d.ts +340 -340
  40. package/lib/core/base/Expr.js +917 -917
  41. package/lib/core/base/Local.d.ts +104 -104
  42. package/lib/core/base/Local.js +176 -176
  43. package/lib/core/base/Position.d.ts +30 -30
  44. package/lib/core/base/Position.js +147 -147
  45. package/lib/core/base/Ref.d.ts +173 -173
  46. package/lib/core/base/Ref.js +374 -374
  47. package/lib/core/base/Stmt.d.ts +237 -237
  48. package/lib/core/base/Stmt.js +525 -525
  49. package/lib/core/base/Trap.d.ts +8 -8
  50. package/lib/core/base/Trap.js +30 -30
  51. package/lib/core/base/Type.d.ts +308 -308
  52. package/lib/core/base/Type.js +639 -639
  53. package/lib/core/base/TypeExpr.d.ts +71 -71
  54. package/lib/core/base/TypeExpr.js +155 -155
  55. package/lib/core/base/Value.d.ts +32 -32
  56. package/lib/core/base/Value.js +16 -16
  57. package/lib/core/common/ArkError.d.ts +14 -14
  58. package/lib/core/common/ArkError.js +28 -28
  59. package/lib/core/common/ArkIRTransformer.d.ts +67 -67
  60. package/lib/core/common/ArkIRTransformer.js +690 -690
  61. package/lib/core/common/ArkValueTransformer.d.ts +89 -89
  62. package/lib/core/common/ArkValueTransformer.js +1478 -1478
  63. package/lib/core/common/Builtin.d.ts +36 -36
  64. package/lib/core/common/Builtin.js +81 -81
  65. package/lib/core/common/Const.d.ts +21 -21
  66. package/lib/core/common/Const.js +43 -43
  67. package/lib/core/common/DummyMainCreater.d.ts +46 -46
  68. package/lib/core/common/DummyMainCreater.js +319 -319
  69. package/lib/core/common/EtsConst.d.ts +73 -73
  70. package/lib/core/common/EtsConst.js +1021 -1021
  71. package/lib/core/common/ExprUseReplacer.d.ts +21 -21
  72. package/lib/core/common/ExprUseReplacer.js +124 -124
  73. package/lib/core/common/IRInference.d.ts +36 -36
  74. package/lib/core/common/IRInference.d.ts.map +1 -1
  75. package/lib/core/common/IRInference.js +651 -648
  76. package/lib/core/common/IRUtils.d.ts +18 -18
  77. package/lib/core/common/IRUtils.js +141 -141
  78. package/lib/core/common/ModelUtils.d.ts +70 -70
  79. package/lib/core/common/ModelUtils.js +712 -712
  80. package/lib/core/common/RefUseReplacer.d.ts +13 -13
  81. package/lib/core/common/RefUseReplacer.js +52 -52
  82. package/lib/core/common/SdkUtils.d.ts +8 -8
  83. package/lib/core/common/SdkUtils.js +105 -105
  84. package/lib/core/common/StmtDefReplacer.d.ts +12 -12
  85. package/lib/core/common/StmtDefReplacer.js +41 -41
  86. package/lib/core/common/StmtUseReplacer.d.ts +16 -16
  87. package/lib/core/common/StmtUseReplacer.js +108 -108
  88. package/lib/core/common/TSConst.d.ts +18 -18
  89. package/lib/core/common/TSConst.js +36 -36
  90. package/lib/core/common/TypeInference.d.ts +102 -102
  91. package/lib/core/common/TypeInference.js +748 -748
  92. package/lib/core/common/ValueUtil.d.ts +12 -12
  93. package/lib/core/common/ValueUtil.js +54 -54
  94. package/lib/core/common/VisibleValue.d.ts +36 -36
  95. package/lib/core/common/VisibleValue.js +212 -212
  96. package/lib/core/dataflow/DataflowProblem.d.ts +20 -20
  97. package/lib/core/dataflow/DataflowProblem.js +20 -20
  98. package/lib/core/dataflow/DataflowResult.d.ts +7 -7
  99. package/lib/core/dataflow/DataflowResult.js +26 -26
  100. package/lib/core/dataflow/DataflowSolver.d.ts +43 -43
  101. package/lib/core/dataflow/DataflowSolver.js +307 -307
  102. package/lib/core/dataflow/Edge.d.ts +11 -11
  103. package/lib/core/dataflow/Edge.js +31 -31
  104. package/lib/core/dataflow/Fact.d.ts +6 -6
  105. package/lib/core/dataflow/Fact.js +24 -24
  106. package/lib/core/dataflow/GenericDataFlow.d.ts +142 -142
  107. package/lib/core/dataflow/GenericDataFlow.js +109 -109
  108. package/lib/core/dataflow/ReachingDef.d.ts +62 -62
  109. package/lib/core/dataflow/ReachingDef.js +168 -168
  110. package/lib/core/dataflow/UndefinedVariable.d.ts +45 -45
  111. package/lib/core/dataflow/UndefinedVariable.js +288 -288
  112. package/lib/core/dataflow/Util.d.ts +9 -9
  113. package/lib/core/dataflow/Util.js +64 -64
  114. package/lib/core/graph/BaseExplicitGraph.d.ts +58 -58
  115. package/lib/core/graph/BaseExplicitGraph.js +157 -157
  116. package/lib/core/graph/BaseImplicitGraph.d.ts +77 -77
  117. package/lib/core/graph/BaseImplicitGraph.js +78 -78
  118. package/lib/core/graph/BasicBlock.d.ts +104 -104
  119. package/lib/core/graph/BasicBlock.js +280 -280
  120. package/lib/core/graph/Cfg.d.ts +59 -59
  121. package/lib/core/graph/Cfg.js +282 -282
  122. package/lib/core/graph/DependsGraph.d.ts +35 -35
  123. package/lib/core/graph/DependsGraph.js +87 -87
  124. package/lib/core/graph/DominanceFinder.d.ts +15 -15
  125. package/lib/core/graph/DominanceFinder.js +121 -121
  126. package/lib/core/graph/DominanceTree.d.ts +12 -12
  127. package/lib/core/graph/DominanceTree.js +70 -70
  128. package/lib/core/graph/GraphTraits.d.ts +7 -7
  129. package/lib/core/graph/GraphTraits.js +16 -16
  130. package/lib/core/graph/Scc.d.ts +61 -61
  131. package/lib/core/graph/Scc.js +222 -222
  132. package/lib/core/graph/ViewTree.d.ts +114 -114
  133. package/lib/core/graph/ViewTree.js +16 -16
  134. package/lib/core/graph/builder/CfgBuilder.d.ts +176 -176
  135. package/lib/core/graph/builder/CfgBuilder.js +1139 -1139
  136. package/lib/core/graph/builder/ConditionBuilder.d.ts +15 -15
  137. package/lib/core/graph/builder/ConditionBuilder.js +252 -252
  138. package/lib/core/graph/builder/LoopBuilder.d.ts +20 -20
  139. package/lib/core/graph/builder/LoopBuilder.js +251 -251
  140. package/lib/core/graph/builder/SwitchBuilder.d.ts +11 -11
  141. package/lib/core/graph/builder/SwitchBuilder.js +152 -152
  142. package/lib/core/graph/builder/TrapBuilder.d.ts +16 -16
  143. package/lib/core/graph/builder/TrapBuilder.js +272 -272
  144. package/lib/core/graph/builder/ViewTreeBuilder.d.ts +203 -203
  145. package/lib/core/graph/builder/ViewTreeBuilder.js +1055 -1055
  146. package/lib/core/model/ArkBaseModel.d.ts +59 -59
  147. package/lib/core/model/ArkBaseModel.js +274 -274
  148. package/lib/core/model/ArkBody.d.ts +24 -24
  149. package/lib/core/model/ArkBody.js +65 -65
  150. package/lib/core/model/ArkClass.d.ts +206 -206
  151. package/lib/core/model/ArkClass.js +439 -439
  152. package/lib/core/model/ArkExport.d.ts +72 -72
  153. package/lib/core/model/ArkExport.js +147 -147
  154. package/lib/core/model/ArkField.d.ts +62 -62
  155. package/lib/core/model/ArkField.js +113 -113
  156. package/lib/core/model/ArkFile.d.ts +131 -131
  157. package/lib/core/model/ArkFile.js +267 -267
  158. package/lib/core/model/ArkImport.d.ts +44 -44
  159. package/lib/core/model/ArkImport.js +108 -108
  160. package/lib/core/model/ArkMetadata.d.ts +31 -31
  161. package/lib/core/model/ArkMetadata.js +55 -55
  162. package/lib/core/model/ArkMethod.d.ts +261 -261
  163. package/lib/core/model/ArkMethod.js +592 -592
  164. package/lib/core/model/ArkNamespace.d.ts +66 -66
  165. package/lib/core/model/ArkNamespace.js +209 -209
  166. package/lib/core/model/ArkSignature.d.ts +162 -162
  167. package/lib/core/model/ArkSignature.js +379 -379
  168. package/lib/core/model/builder/ArkClassBuilder.d.ts +12 -12
  169. package/lib/core/model/builder/ArkClassBuilder.js +454 -454
  170. package/lib/core/model/builder/ArkExportBuilder.d.ts +24 -24
  171. package/lib/core/model/builder/ArkExportBuilder.js +210 -210
  172. package/lib/core/model/builder/ArkFieldBuilder.d.ts +8 -8
  173. package/lib/core/model/builder/ArkFieldBuilder.js +193 -193
  174. package/lib/core/model/builder/ArkFileBuilder.d.ts +9 -9
  175. package/lib/core/model/builder/ArkFileBuilder.js +165 -165
  176. package/lib/core/model/builder/ArkImportBuilder.d.ts +4 -4
  177. package/lib/core/model/builder/ArkImportBuilder.js +128 -128
  178. package/lib/core/model/builder/ArkMethodBuilder.d.ts +63 -63
  179. package/lib/core/model/builder/ArkMethodBuilder.js +486 -486
  180. package/lib/core/model/builder/ArkNamespaceBuilder.d.ts +5 -5
  181. package/lib/core/model/builder/ArkNamespaceBuilder.js +205 -205
  182. package/lib/core/model/builder/ArkSignatureBuilder.d.ts +8 -8
  183. package/lib/core/model/builder/ArkSignatureBuilder.js +40 -40
  184. package/lib/core/model/builder/BodyBuilder.d.ts +55 -30
  185. package/lib/core/model/builder/BodyBuilder.d.ts.map +1 -1
  186. package/lib/core/model/builder/BodyBuilder.js +542 -371
  187. package/lib/core/model/builder/builderUtils.d.ts +18 -18
  188. package/lib/core/model/builder/builderUtils.js +558 -558
  189. package/lib/index.d.ts +82 -80
  190. package/lib/index.d.ts.map +1 -1
  191. package/lib/index.js +201 -196
  192. package/lib/save/ArkStream.d.ts +23 -23
  193. package/lib/save/ArkStream.js +83 -83
  194. package/lib/save/DotPrinter.d.ts +43 -43
  195. package/lib/save/DotPrinter.js +170 -170
  196. package/lib/save/GraphPrinter.d.ts +16 -16
  197. package/lib/save/GraphPrinter.js +134 -134
  198. package/lib/save/JsonPrinter.d.ts +30 -30
  199. package/lib/save/JsonPrinter.js +580 -580
  200. package/lib/save/Printer.d.ts +12 -12
  201. package/lib/save/Printer.js +27 -27
  202. package/lib/save/PrinterBuilder.d.ts +53 -53
  203. package/lib/save/PrinterBuilder.js +145 -145
  204. package/lib/save/ViewTreePrinter.d.ts +14 -14
  205. package/lib/save/ViewTreePrinter.js +123 -123
  206. package/lib/save/arkir/ArkIRClassPrinter.d.ts +13 -13
  207. package/lib/save/arkir/ArkIRClassPrinter.js +92 -92
  208. package/lib/save/arkir/ArkIRFieldPrinter.d.ts +11 -11
  209. package/lib/save/arkir/ArkIRFieldPrinter.js +64 -64
  210. package/lib/save/arkir/ArkIRFilePrinter.d.ts +12 -12
  211. package/lib/save/arkir/ArkIRFilePrinter.js +54 -54
  212. package/lib/save/arkir/ArkIRMethodPrinter.d.ts +16 -16
  213. package/lib/save/arkir/ArkIRMethodPrinter.js +159 -159
  214. package/lib/save/arkir/ArkIRNamespacePrinter.d.ts +11 -11
  215. package/lib/save/arkir/ArkIRNamespacePrinter.js +66 -66
  216. package/lib/save/base/BasePrinter.d.ts +23 -23
  217. package/lib/save/base/BasePrinter.js +70 -70
  218. package/lib/save/base/ExportPrinter.d.ts +8 -8
  219. package/lib/save/base/ExportPrinter.js +67 -67
  220. package/lib/save/base/ImportPrinter.d.ts +9 -9
  221. package/lib/save/base/ImportPrinter.js +92 -92
  222. package/lib/save/base/PrinterUtils.d.ts +24 -24
  223. package/lib/save/base/PrinterUtils.js +208 -208
  224. package/lib/save/serializeArkIR.d.ts +8 -8
  225. package/lib/save/serializeArkIR.js +294 -294
  226. package/lib/save/source/SourceBase.d.ts +22 -22
  227. package/lib/save/source/SourceBase.js +64 -64
  228. package/lib/save/source/SourceBody.d.ts +58 -58
  229. package/lib/save/source/SourceBody.js +296 -296
  230. package/lib/save/source/SourceClass.d.ts +25 -25
  231. package/lib/save/source/SourceClass.js +187 -187
  232. package/lib/save/source/SourceField.d.ts +13 -13
  233. package/lib/save/source/SourceField.js +73 -73
  234. package/lib/save/source/SourceFilePrinter.d.ts +12 -12
  235. package/lib/save/source/SourceFilePrinter.js +69 -69
  236. package/lib/save/source/SourceMethod.d.ts +22 -22
  237. package/lib/save/source/SourceMethod.d.ts.map +1 -1
  238. package/lib/save/source/SourceMethod.js +196 -193
  239. package/lib/save/source/SourceNamespace.d.ts +11 -11
  240. package/lib/save/source/SourceNamespace.js +83 -83
  241. package/lib/save/source/SourceStmt.d.ts +178 -178
  242. package/lib/save/source/SourceStmt.js +838 -838
  243. package/lib/save/source/SourceTransformer.d.ts +46 -46
  244. package/lib/save/source/SourceTransformer.js +446 -446
  245. package/lib/transformer/FunctionTransformer.d.ts +2 -2
  246. package/lib/transformer/FunctionTransformer.js +17 -17
  247. package/lib/transformer/SceneTransformer.d.ts +2 -2
  248. package/lib/transformer/SceneTransformer.js +17 -17
  249. package/lib/transformer/StaticSingleAssignmentFormer.d.ts +12 -12
  250. package/lib/transformer/StaticSingleAssignmentFormer.js +259 -259
  251. package/lib/transformer/Transformer.d.ts +6 -6
  252. package/lib/transformer/Transformer.js +22 -22
  253. package/lib/utils/AstTreeUtils.d.ts +4 -4
  254. package/lib/utils/AstTreeUtils.js +26 -26
  255. package/lib/utils/CfgStructualAnalysis.d.ts +110 -110
  256. package/lib/utils/CfgStructualAnalysis.js +1277 -1277
  257. package/lib/utils/FileUtils.d.ts +18 -18
  258. package/lib/utils/FileUtils.js +135 -135
  259. package/lib/utils/SparseBitVector.d.ts +100 -100
  260. package/lib/utils/SparseBitVector.js +445 -445
  261. package/lib/utils/callGraphUtils.d.ts +30 -30
  262. package/lib/utils/callGraphUtils.js +205 -205
  263. package/lib/utils/crypto_utils.d.ts +5 -5
  264. package/lib/utils/crypto_utils.js +57 -57
  265. package/lib/utils/entryMethodUtils.d.ts +13 -13
  266. package/lib/utils/entryMethodUtils.js +110 -110
  267. package/lib/utils/getAllFiles.d.ts +9 -9
  268. package/lib/utils/getAllFiles.js +90 -90
  269. package/lib/utils/json5parser.d.ts +6 -6
  270. package/lib/utils/json5parser.js +146 -146
  271. package/lib/utils/logger.d.ts +18 -18
  272. package/lib/utils/logger.d.ts.map +1 -1
  273. package/lib/utils/logger.js +97 -90
  274. package/lib/utils/pathTransfer.d.ts +1 -1
  275. package/lib/utils/pathTransfer.js +25 -25
  276. package/node_modules/json5/lib/cli.js +0 -0
  277. package/node_modules/ohos-typescript/bin/tsc +0 -0
  278. package/node_modules/ohos-typescript/bin/tsserver +0 -0
  279. package/package.json +3 -3
@@ -1,445 +1,445 @@
1
- "use strict";
2
- /*
3
- * Copyright (c) 2024 Huawei Device Co., Ltd.
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.SparseBitVector = void 0;
18
- const BITWORD_SIZE = 16; // bits of a Word
19
- const DEFAULT_SIZE = 64;
20
- class SparseBitVectorElement {
21
- constructor(elementSize = DEFAULT_SIZE) {
22
- this.ELEMENT_SIZE = elementSize;
23
- this.BITWORDS_NUM = Math.ceil(this.ELEMENT_SIZE / BITWORD_SIZE);
24
- this.bits = new Uint16Array(this.BITWORDS_NUM);
25
- }
26
- word(idx) {
27
- return this.bits[idx];
28
- }
29
- clone() {
30
- return new Uint16Array(this.bits);
31
- }
32
- get elementSize() {
33
- return this.ELEMENT_SIZE;
34
- }
35
- get bitWordNum() {
36
- return this.BITWORDS_NUM;
37
- }
38
- // Check if the element is empty (all bits are zero)
39
- isEmpty() {
40
- return this.isZero();
41
- }
42
- // Set a bit at the given index
43
- set(bitIdx) {
44
- const wordIndex = Math.floor(bitIdx / BITWORD_SIZE);
45
- const bitOffset = bitIdx % BITWORD_SIZE;
46
- this.bits[wordIndex] |= 1 << bitOffset;
47
- }
48
- setWord(word) {
49
- this.bits = word;
50
- }
51
- // Reset a bit at the given index
52
- reset(bitIdx) {
53
- const wordIndex = Math.floor(bitIdx / BITWORD_SIZE);
54
- const bitOffset = bitIdx % BITWORD_SIZE;
55
- this.bits[wordIndex] &= ~(1 << bitOffset);
56
- }
57
- // Test if a bit is set
58
- test(bitIdx) {
59
- const wordIndex = Math.floor(bitIdx / BITWORD_SIZE);
60
- const bitOffset = bitIdx % BITWORD_SIZE;
61
- return (this.bits[wordIndex] & (1 << bitOffset)) !== 0;
62
- }
63
- // Set if not existing, else return
64
- test_and_set(bitIdx) {
65
- let old = this.test(bitIdx);
66
- if (!old) {
67
- this.set(bitIdx);
68
- return true;
69
- }
70
- return false;
71
- }
72
- // Count the number of set bits in this element
73
- count() {
74
- let numBits = 0;
75
- this.bits.forEach((word) => {
76
- numBits += this.countBits(word);
77
- });
78
- return numBits;
79
- }
80
- // Find the index of the first set bit in this element
81
- findFirst() {
82
- for (let i = 0; i < this.bits.length; i++) {
83
- if (this.bits[i] !== 0) {
84
- return i * BITWORD_SIZE + this.countTrailingZeros(this.bits[i]);
85
- }
86
- }
87
- return -1; // No bits are set
88
- }
89
- // Find the next set bit after the given index
90
- findNext(bitIdx) {
91
- bitIdx++;
92
- let wordIndex = Math.floor(bitIdx / BITWORD_SIZE);
93
- let bitOffset = bitIdx % BITWORD_SIZE;
94
- // Check the current word
95
- // Mask off previous bits
96
- let word = this.bits[wordIndex] & (~0 << bitOffset);
97
- if (word !== 0) {
98
- return wordIndex * BITWORD_SIZE + this.countTrailingZeros(word);
99
- }
100
- // Check subsequent words
101
- for (let i = wordIndex + 1; i < this.bits.length; i++) {
102
- if (this.bits[i] !== 0) {
103
- return i * BITWORD_SIZE + this.countTrailingZeros(this.bits[i]);
104
- }
105
- }
106
- return -1; // No more bits are set
107
- }
108
- // Comparison
109
- equals(rhs) {
110
- for (let i = 0; i < this.BITWORDS_NUM; i++) {
111
- if (this.bits[i] !== rhs.word(i)) {
112
- return false;
113
- }
114
- }
115
- return true;
116
- }
117
- // Union this element with another element and return true if this one changed
118
- unionWith(other) {
119
- let changed = false;
120
- for (let i = 0; i < this.bits.length; i++) {
121
- const oldWord = changed ? 0 : this.bits[i];
122
- this.bits[i] |= other.bits[i];
123
- if (!changed && oldWord !== this.bits[i]) {
124
- changed = true;
125
- }
126
- }
127
- return changed;
128
- }
129
- // Intersect this element with another element and return true if this one changed.
130
- intersectWith(other) {
131
- let changed = false;
132
- for (let i = 0; i < this.bits.length; i++) {
133
- const oldWord = changed ? 0 : this.bits[i];
134
- this.bits[i] &= other.bits[i];
135
- if (!changed && oldWord !== this.bits[i]) {
136
- changed = true;
137
- }
138
- }
139
- return changed;
140
- }
141
- // Subtract another SparseBitVectorElement from this one.
142
- subtractWith(rhs) {
143
- let changed = false;
144
- // Perform subtraction: this = this & ~rhs
145
- for (let i = 0; i < this.bits.length; i++) {
146
- const oldWord = this.bits[i];
147
- this.bits[i] &= ~rhs.bits[i];
148
- // If any bit was changed, mark as changed
149
- if (this.bits[i] !== oldWord) {
150
- changed = true;
151
- }
152
- }
153
- return changed;
154
- }
155
- // Count the number of set bits in a word
156
- countBitsV2(word) {
157
- let count = 0;
158
- while (word !== 0) {
159
- word &= word - 1;
160
- count++;
161
- }
162
- return count;
163
- }
164
- // Count the number of set bits in a word
165
- countBits(word) {
166
- // assume the value is treated as a unsigned integer
167
- let v = word;
168
- // Step 1: Pairwise addition of bits
169
- v = v - ((v >> 1) & 0x55555555);
170
- // Step 2: Group bits into 4-bit chunks and add
171
- v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
172
- // Step 3: Group bits into 8-bit chunks and add
173
- v = (v + (v >> 4)) & 0xf0f0f0f;
174
- // Step 4: Multiply by a magic number to sum all 8-bit chunks into the highest byte
175
- v = (v * 0x1010101) >> 24;
176
- return v;
177
- }
178
- isZero() {
179
- for (let i = 0; i < this.BITWORDS_NUM; i++) {
180
- if (this.bits[i] !== 0) {
181
- return false;
182
- }
183
- }
184
- return true;
185
- }
186
- // Count trailing zeros in a word
187
- countTrailingZeros(word) {
188
- if (word === 0) {
189
- return BITWORD_SIZE;
190
- }
191
- if ((word & 1) !== 0) {
192
- return 0;
193
- }
194
- let zeroBits = 0;
195
- let shift = BITWORD_SIZE / 2; // Start with half the bit width
196
- let mask = (1 << shift) - 1; // Mask for the lower half
197
- while (shift > 0) {
198
- if ((word & mask) === 0) {
199
- word >>= shift;
200
- zeroBits |= Number(shift);
201
- }
202
- shift >>= 1;
203
- mask >>= shift;
204
- }
205
- return zeroBits;
206
- }
207
- }
208
- class SparseBitVector {
209
- constructor(elementsSize = DEFAULT_SIZE) {
210
- // Unordered storage of elements.
211
- // key is actually the element index (normally it is in element)
212
- this.elements = new Map();
213
- this.ELEMENT_SIZE = elementsSize;
214
- }
215
- get elementSize() {
216
- return this.ELEMENT_SIZE;
217
- }
218
- get elems() {
219
- return this.elements;
220
- }
221
- // Set a bit at the given index
222
- set(bitIdx) {
223
- const elementIndex = Math.floor(bitIdx / this.ELEMENT_SIZE);
224
- let element = this.elements.get(elementIndex);
225
- if (!element) {
226
- element = new SparseBitVectorElement(this.ELEMENT_SIZE);
227
- this.elements.set(elementIndex, element);
228
- }
229
- element.set(bitIdx % this.ELEMENT_SIZE);
230
- }
231
- // Test if a bit is set
232
- test(bitIdx) {
233
- const elementIndex = Math.floor(bitIdx / this.ELEMENT_SIZE);
234
- const element = this.elements.get(elementIndex);
235
- return element ? element.test(bitIdx % this.ELEMENT_SIZE) : false;
236
- }
237
- // Set a bit if not existing. Else return
238
- testAndSet(bitIdx) {
239
- let old = this.test(bitIdx);
240
- if (!old) {
241
- this.set(bitIdx);
242
- return true;
243
- }
244
- return false;
245
- }
246
- // Reset a bit at the given index
247
- reset(bitIdx) {
248
- const elementIndex = Math.floor(bitIdx / this.ELEMENT_SIZE);
249
- let element = this.elements.get(elementIndex);
250
- if (element) {
251
- element.reset(bitIdx % this.ELEMENT_SIZE);
252
- if (element.isEmpty()) {
253
- this.elements.delete(elementIndex);
254
- }
255
- }
256
- }
257
- // Clear all elements
258
- clear() {
259
- this.elements.clear();
260
- }
261
- // Clone, return a deep copied object
262
- clone() {
263
- const newVector = new SparseBitVector(this.elementSize);
264
- for (const [idx, element] of this.elements) {
265
- const newElement = new SparseBitVectorElement(this.elementSize);
266
- newElement.setWord(element.clone());
267
- newVector.elems.set(idx, newElement);
268
- }
269
- return newVector;
270
- }
271
- // Find the first set bit in the vector
272
- findFirst() {
273
- if (this.elements.size === 0)
274
- return -1;
275
- const firstElement = this.elements.entries().next().value;
276
- if (firstElement) {
277
- const firstBit = firstElement[1].findFirst();
278
- return firstElement[0] * this.ELEMENT_SIZE + firstBit;
279
- }
280
- else {
281
- return -1;
282
- }
283
- }
284
- // Count the number of set bits in the vector
285
- count() {
286
- let count = 0;
287
- this.elements.forEach((elem, _) => {
288
- count += elem.count();
289
- });
290
- return count;
291
- }
292
- // Check if the vector is empty
293
- isEmpty() {
294
- return this.elements.size === 0;
295
- }
296
- [Symbol.iterator]() {
297
- let iter = this.elements.entries();
298
- let next = iter.next();
299
- const elementSize = this.ELEMENT_SIZE;
300
- let element = next.value;
301
- if (!element) {
302
- return {
303
- next() {
304
- return { value: undefined, done: true };
305
- },
306
- [Symbol.iterator]() {
307
- return this; // Make the iterator itself iterable
308
- },
309
- };
310
- }
311
- let bitIndex = element[1].findFirst();
312
- return {
313
- next() {
314
- if (element) {
315
- let v = element[0] * elementSize + bitIndex;
316
- bitIndex = element[1].findNext(bitIndex);
317
- if (bitIndex === -1) {
318
- next = iter.next();
319
- element = next.value;
320
- if (element) {
321
- bitIndex = element[1].findFirst();
322
- }
323
- }
324
- return { value: v, done: false };
325
- }
326
- return { value: undefined, done: true };
327
- },
328
- [Symbol.iterator]() {
329
- return this; // Make the iterator itself iterable
330
- },
331
- };
332
- }
333
- /**
334
- * Check if this SparseBitVector is equal to another SparseBitVector.
335
- */
336
- equals(rhs) {
337
- if (this.ELEMENT_SIZE !== rhs.ELEMENT_SIZE || this.elems.size !== rhs.elems.size) {
338
- return false;
339
- }
340
- let rhsElems = rhs.elems;
341
- for (let p of this.elements) {
342
- let rhsElem = rhsElems.get(p[0]);
343
- if (!rhsElem) {
344
- return false;
345
- }
346
- if (!rhsElem.equals(p[1])) {
347
- return false;
348
- }
349
- }
350
- return true;
351
- }
352
- /**
353
- * Perform a union operation with another SparseBitVector.
354
- * Returns True if this vector was changed, false otherwise.
355
- */
356
- unionWith(rhs) {
357
- if (this.equals(rhs) || rhs.elems.size === 0) {
358
- return false;
359
- }
360
- let changed = false;
361
- let newElems = new Map();
362
- for (let p of rhs.elems) {
363
- let elem = this.elements.get(p[0]);
364
- if (elem) {
365
- changed = elem.unionWith(p[1]) || changed;
366
- }
367
- else {
368
- newElems.set(p[0], p[1]);
369
- }
370
- }
371
- if (newElems.size > 0) {
372
- newElems.forEach((v, k) => this.elements.set(k, v));
373
- changed = true;
374
- }
375
- return changed;
376
- }
377
- /**
378
- * Perform an intersection operation with another SparseBitVector.
379
- * Returns True if this vector was changed, false otherwise.
380
- */
381
- intersectWith(rhs) {
382
- if (this.equals(rhs) || rhs.elems.size === 0) {
383
- return false;
384
- }
385
- let changed = false;
386
- // If either vector is empty, the result is empty
387
- if (this.elements.size === 0 || rhs.elems.size === 0) {
388
- if (this.elements.size > 0) {
389
- this.elements = new Map();
390
- changed = true;
391
- }
392
- return changed;
393
- }
394
- let needDeleteIdx = new Set();
395
- for (let p of this.elems) {
396
- let elem = rhs.elems.get(p[0]);
397
- if (elem) {
398
- changed = p[1].intersectWith(elem) || changed;
399
- if (changed && p[1].isZero()) {
400
- needDeleteIdx.add(p[0]);
401
- }
402
- }
403
- else {
404
- needDeleteIdx.add(p[0]);
405
- }
406
- }
407
- if (needDeleteIdx.size > 0) {
408
- needDeleteIdx.forEach((idx) => this.elements.delete(idx));
409
- changed = true;
410
- }
411
- return changed;
412
- }
413
- /**
414
- * Subtract another SparseBitVector from this one.
415
- * This operation modifies the current SparseBitVector in place.
416
- * Return True if the current SparseBitVector was modified, false otherwise.
417
- */
418
- subtractWith(rhs) {
419
- if (this.elementSize !== rhs.elementSize || this.isEmpty() || rhs.isEmpty()) {
420
- return false;
421
- }
422
- let needDeleteIdx = new Set();
423
- let changed = false;
424
- for (const [elementIndex, element] of this.elements) {
425
- const rhsElement = rhs.elements.get(elementIndex);
426
- if (rhsElement) {
427
- changed = element.subtractWith(rhsElement) || changed;
428
- if (element.isEmpty()) {
429
- needDeleteIdx.add(elementIndex);
430
- }
431
- }
432
- }
433
- if (needDeleteIdx.size > 0) {
434
- needDeleteIdx.forEach((idx) => this.elements.delete(idx));
435
- changed = true;
436
- }
437
- return changed;
438
- }
439
- // Dump as string
440
- toString() {
441
- let ar = [...this];
442
- return ar.toString();
443
- }
444
- }
445
- exports.SparseBitVector = SparseBitVector;
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2024 Huawei Device Co., Ltd.
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.SparseBitVector = void 0;
18
+ const BITWORD_SIZE = 16; // bits of a Word
19
+ const DEFAULT_SIZE = 64;
20
+ class SparseBitVectorElement {
21
+ constructor(elementSize = DEFAULT_SIZE) {
22
+ this.ELEMENT_SIZE = elementSize;
23
+ this.BITWORDS_NUM = Math.ceil(this.ELEMENT_SIZE / BITWORD_SIZE);
24
+ this.bits = new Uint16Array(this.BITWORDS_NUM);
25
+ }
26
+ word(idx) {
27
+ return this.bits[idx];
28
+ }
29
+ clone() {
30
+ return new Uint16Array(this.bits);
31
+ }
32
+ get elementSize() {
33
+ return this.ELEMENT_SIZE;
34
+ }
35
+ get bitWordNum() {
36
+ return this.BITWORDS_NUM;
37
+ }
38
+ // Check if the element is empty (all bits are zero)
39
+ isEmpty() {
40
+ return this.isZero();
41
+ }
42
+ // Set a bit at the given index
43
+ set(bitIdx) {
44
+ const wordIndex = Math.floor(bitIdx / BITWORD_SIZE);
45
+ const bitOffset = bitIdx % BITWORD_SIZE;
46
+ this.bits[wordIndex] |= 1 << bitOffset;
47
+ }
48
+ setWord(word) {
49
+ this.bits = word;
50
+ }
51
+ // Reset a bit at the given index
52
+ reset(bitIdx) {
53
+ const wordIndex = Math.floor(bitIdx / BITWORD_SIZE);
54
+ const bitOffset = bitIdx % BITWORD_SIZE;
55
+ this.bits[wordIndex] &= ~(1 << bitOffset);
56
+ }
57
+ // Test if a bit is set
58
+ test(bitIdx) {
59
+ const wordIndex = Math.floor(bitIdx / BITWORD_SIZE);
60
+ const bitOffset = bitIdx % BITWORD_SIZE;
61
+ return (this.bits[wordIndex] & (1 << bitOffset)) !== 0;
62
+ }
63
+ // Set if not existing, else return
64
+ test_and_set(bitIdx) {
65
+ let old = this.test(bitIdx);
66
+ if (!old) {
67
+ this.set(bitIdx);
68
+ return true;
69
+ }
70
+ return false;
71
+ }
72
+ // Count the number of set bits in this element
73
+ count() {
74
+ let numBits = 0;
75
+ this.bits.forEach((word) => {
76
+ numBits += this.countBits(word);
77
+ });
78
+ return numBits;
79
+ }
80
+ // Find the index of the first set bit in this element
81
+ findFirst() {
82
+ for (let i = 0; i < this.bits.length; i++) {
83
+ if (this.bits[i] !== 0) {
84
+ return i * BITWORD_SIZE + this.countTrailingZeros(this.bits[i]);
85
+ }
86
+ }
87
+ return -1; // No bits are set
88
+ }
89
+ // Find the next set bit after the given index
90
+ findNext(bitIdx) {
91
+ bitIdx++;
92
+ let wordIndex = Math.floor(bitIdx / BITWORD_SIZE);
93
+ let bitOffset = bitIdx % BITWORD_SIZE;
94
+ // Check the current word
95
+ // Mask off previous bits
96
+ let word = this.bits[wordIndex] & (~0 << bitOffset);
97
+ if (word !== 0) {
98
+ return wordIndex * BITWORD_SIZE + this.countTrailingZeros(word);
99
+ }
100
+ // Check subsequent words
101
+ for (let i = wordIndex + 1; i < this.bits.length; i++) {
102
+ if (this.bits[i] !== 0) {
103
+ return i * BITWORD_SIZE + this.countTrailingZeros(this.bits[i]);
104
+ }
105
+ }
106
+ return -1; // No more bits are set
107
+ }
108
+ // Comparison
109
+ equals(rhs) {
110
+ for (let i = 0; i < this.BITWORDS_NUM; i++) {
111
+ if (this.bits[i] !== rhs.word(i)) {
112
+ return false;
113
+ }
114
+ }
115
+ return true;
116
+ }
117
+ // Union this element with another element and return true if this one changed
118
+ unionWith(other) {
119
+ let changed = false;
120
+ for (let i = 0; i < this.bits.length; i++) {
121
+ const oldWord = changed ? 0 : this.bits[i];
122
+ this.bits[i] |= other.bits[i];
123
+ if (!changed && oldWord !== this.bits[i]) {
124
+ changed = true;
125
+ }
126
+ }
127
+ return changed;
128
+ }
129
+ // Intersect this element with another element and return true if this one changed.
130
+ intersectWith(other) {
131
+ let changed = false;
132
+ for (let i = 0; i < this.bits.length; i++) {
133
+ const oldWord = changed ? 0 : this.bits[i];
134
+ this.bits[i] &= other.bits[i];
135
+ if (!changed && oldWord !== this.bits[i]) {
136
+ changed = true;
137
+ }
138
+ }
139
+ return changed;
140
+ }
141
+ // Subtract another SparseBitVectorElement from this one.
142
+ subtractWith(rhs) {
143
+ let changed = false;
144
+ // Perform subtraction: this = this & ~rhs
145
+ for (let i = 0; i < this.bits.length; i++) {
146
+ const oldWord = this.bits[i];
147
+ this.bits[i] &= ~rhs.bits[i];
148
+ // If any bit was changed, mark as changed
149
+ if (this.bits[i] !== oldWord) {
150
+ changed = true;
151
+ }
152
+ }
153
+ return changed;
154
+ }
155
+ // Count the number of set bits in a word
156
+ countBitsV2(word) {
157
+ let count = 0;
158
+ while (word !== 0) {
159
+ word &= word - 1;
160
+ count++;
161
+ }
162
+ return count;
163
+ }
164
+ // Count the number of set bits in a word
165
+ countBits(word) {
166
+ // assume the value is treated as a unsigned integer
167
+ let v = word;
168
+ // Step 1: Pairwise addition of bits
169
+ v = v - ((v >> 1) & 0x55555555);
170
+ // Step 2: Group bits into 4-bit chunks and add
171
+ v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
172
+ // Step 3: Group bits into 8-bit chunks and add
173
+ v = (v + (v >> 4)) & 0xf0f0f0f;
174
+ // Step 4: Multiply by a magic number to sum all 8-bit chunks into the highest byte
175
+ v = (v * 0x1010101) >> 24;
176
+ return v;
177
+ }
178
+ isZero() {
179
+ for (let i = 0; i < this.BITWORDS_NUM; i++) {
180
+ if (this.bits[i] !== 0) {
181
+ return false;
182
+ }
183
+ }
184
+ return true;
185
+ }
186
+ // Count trailing zeros in a word
187
+ countTrailingZeros(word) {
188
+ if (word === 0) {
189
+ return BITWORD_SIZE;
190
+ }
191
+ if ((word & 1) !== 0) {
192
+ return 0;
193
+ }
194
+ let zeroBits = 0;
195
+ let shift = BITWORD_SIZE / 2; // Start with half the bit width
196
+ let mask = (1 << shift) - 1; // Mask for the lower half
197
+ while (shift > 0) {
198
+ if ((word & mask) === 0) {
199
+ word >>= shift;
200
+ zeroBits |= Number(shift);
201
+ }
202
+ shift >>= 1;
203
+ mask >>= shift;
204
+ }
205
+ return zeroBits;
206
+ }
207
+ }
208
+ class SparseBitVector {
209
+ constructor(elementsSize = DEFAULT_SIZE) {
210
+ // Unordered storage of elements.
211
+ // key is actually the element index (normally it is in element)
212
+ this.elements = new Map();
213
+ this.ELEMENT_SIZE = elementsSize;
214
+ }
215
+ get elementSize() {
216
+ return this.ELEMENT_SIZE;
217
+ }
218
+ get elems() {
219
+ return this.elements;
220
+ }
221
+ // Set a bit at the given index
222
+ set(bitIdx) {
223
+ const elementIndex = Math.floor(bitIdx / this.ELEMENT_SIZE);
224
+ let element = this.elements.get(elementIndex);
225
+ if (!element) {
226
+ element = new SparseBitVectorElement(this.ELEMENT_SIZE);
227
+ this.elements.set(elementIndex, element);
228
+ }
229
+ element.set(bitIdx % this.ELEMENT_SIZE);
230
+ }
231
+ // Test if a bit is set
232
+ test(bitIdx) {
233
+ const elementIndex = Math.floor(bitIdx / this.ELEMENT_SIZE);
234
+ const element = this.elements.get(elementIndex);
235
+ return element ? element.test(bitIdx % this.ELEMENT_SIZE) : false;
236
+ }
237
+ // Set a bit if not existing. Else return
238
+ testAndSet(bitIdx) {
239
+ let old = this.test(bitIdx);
240
+ if (!old) {
241
+ this.set(bitIdx);
242
+ return true;
243
+ }
244
+ return false;
245
+ }
246
+ // Reset a bit at the given index
247
+ reset(bitIdx) {
248
+ const elementIndex = Math.floor(bitIdx / this.ELEMENT_SIZE);
249
+ let element = this.elements.get(elementIndex);
250
+ if (element) {
251
+ element.reset(bitIdx % this.ELEMENT_SIZE);
252
+ if (element.isEmpty()) {
253
+ this.elements.delete(elementIndex);
254
+ }
255
+ }
256
+ }
257
+ // Clear all elements
258
+ clear() {
259
+ this.elements.clear();
260
+ }
261
+ // Clone, return a deep copied object
262
+ clone() {
263
+ const newVector = new SparseBitVector(this.elementSize);
264
+ for (const [idx, element] of this.elements) {
265
+ const newElement = new SparseBitVectorElement(this.elementSize);
266
+ newElement.setWord(element.clone());
267
+ newVector.elems.set(idx, newElement);
268
+ }
269
+ return newVector;
270
+ }
271
+ // Find the first set bit in the vector
272
+ findFirst() {
273
+ if (this.elements.size === 0)
274
+ return -1;
275
+ const firstElement = this.elements.entries().next().value;
276
+ if (firstElement) {
277
+ const firstBit = firstElement[1].findFirst();
278
+ return firstElement[0] * this.ELEMENT_SIZE + firstBit;
279
+ }
280
+ else {
281
+ return -1;
282
+ }
283
+ }
284
+ // Count the number of set bits in the vector
285
+ count() {
286
+ let count = 0;
287
+ this.elements.forEach((elem, _) => {
288
+ count += elem.count();
289
+ });
290
+ return count;
291
+ }
292
+ // Check if the vector is empty
293
+ isEmpty() {
294
+ return this.elements.size === 0;
295
+ }
296
+ [Symbol.iterator]() {
297
+ let iter = this.elements.entries();
298
+ let next = iter.next();
299
+ const elementSize = this.ELEMENT_SIZE;
300
+ let element = next.value;
301
+ if (!element) {
302
+ return {
303
+ next() {
304
+ return { value: undefined, done: true };
305
+ },
306
+ [Symbol.iterator]() {
307
+ return this; // Make the iterator itself iterable
308
+ },
309
+ };
310
+ }
311
+ let bitIndex = element[1].findFirst();
312
+ return {
313
+ next() {
314
+ if (element) {
315
+ let v = element[0] * elementSize + bitIndex;
316
+ bitIndex = element[1].findNext(bitIndex);
317
+ if (bitIndex === -1) {
318
+ next = iter.next();
319
+ element = next.value;
320
+ if (element) {
321
+ bitIndex = element[1].findFirst();
322
+ }
323
+ }
324
+ return { value: v, done: false };
325
+ }
326
+ return { value: undefined, done: true };
327
+ },
328
+ [Symbol.iterator]() {
329
+ return this; // Make the iterator itself iterable
330
+ },
331
+ };
332
+ }
333
+ /**
334
+ * Check if this SparseBitVector is equal to another SparseBitVector.
335
+ */
336
+ equals(rhs) {
337
+ if (this.ELEMENT_SIZE !== rhs.ELEMENT_SIZE || this.elems.size !== rhs.elems.size) {
338
+ return false;
339
+ }
340
+ let rhsElems = rhs.elems;
341
+ for (let p of this.elements) {
342
+ let rhsElem = rhsElems.get(p[0]);
343
+ if (!rhsElem) {
344
+ return false;
345
+ }
346
+ if (!rhsElem.equals(p[1])) {
347
+ return false;
348
+ }
349
+ }
350
+ return true;
351
+ }
352
+ /**
353
+ * Perform a union operation with another SparseBitVector.
354
+ * Returns True if this vector was changed, false otherwise.
355
+ */
356
+ unionWith(rhs) {
357
+ if (this.equals(rhs) || rhs.elems.size === 0) {
358
+ return false;
359
+ }
360
+ let changed = false;
361
+ let newElems = new Map();
362
+ for (let p of rhs.elems) {
363
+ let elem = this.elements.get(p[0]);
364
+ if (elem) {
365
+ changed = elem.unionWith(p[1]) || changed;
366
+ }
367
+ else {
368
+ newElems.set(p[0], p[1]);
369
+ }
370
+ }
371
+ if (newElems.size > 0) {
372
+ newElems.forEach((v, k) => this.elements.set(k, v));
373
+ changed = true;
374
+ }
375
+ return changed;
376
+ }
377
+ /**
378
+ * Perform an intersection operation with another SparseBitVector.
379
+ * Returns True if this vector was changed, false otherwise.
380
+ */
381
+ intersectWith(rhs) {
382
+ if (this.equals(rhs) || rhs.elems.size === 0) {
383
+ return false;
384
+ }
385
+ let changed = false;
386
+ // If either vector is empty, the result is empty
387
+ if (this.elements.size === 0 || rhs.elems.size === 0) {
388
+ if (this.elements.size > 0) {
389
+ this.elements = new Map();
390
+ changed = true;
391
+ }
392
+ return changed;
393
+ }
394
+ let needDeleteIdx = new Set();
395
+ for (let p of this.elems) {
396
+ let elem = rhs.elems.get(p[0]);
397
+ if (elem) {
398
+ changed = p[1].intersectWith(elem) || changed;
399
+ if (changed && p[1].isZero()) {
400
+ needDeleteIdx.add(p[0]);
401
+ }
402
+ }
403
+ else {
404
+ needDeleteIdx.add(p[0]);
405
+ }
406
+ }
407
+ if (needDeleteIdx.size > 0) {
408
+ needDeleteIdx.forEach((idx) => this.elements.delete(idx));
409
+ changed = true;
410
+ }
411
+ return changed;
412
+ }
413
+ /**
414
+ * Subtract another SparseBitVector from this one.
415
+ * This operation modifies the current SparseBitVector in place.
416
+ * Return True if the current SparseBitVector was modified, false otherwise.
417
+ */
418
+ subtractWith(rhs) {
419
+ if (this.elementSize !== rhs.elementSize || this.isEmpty() || rhs.isEmpty()) {
420
+ return false;
421
+ }
422
+ let needDeleteIdx = new Set();
423
+ let changed = false;
424
+ for (const [elementIndex, element] of this.elements) {
425
+ const rhsElement = rhs.elements.get(elementIndex);
426
+ if (rhsElement) {
427
+ changed = element.subtractWith(rhsElement) || changed;
428
+ if (element.isEmpty()) {
429
+ needDeleteIdx.add(elementIndex);
430
+ }
431
+ }
432
+ }
433
+ if (needDeleteIdx.size > 0) {
434
+ needDeleteIdx.forEach((idx) => this.elements.delete(idx));
435
+ changed = true;
436
+ }
437
+ return changed;
438
+ }
439
+ // Dump as string
440
+ toString() {
441
+ let ar = [...this];
442
+ return ar.toString();
443
+ }
444
+ }
445
+ exports.SparseBitVector = SparseBitVector;