Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

merge.js 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const normalize_1 = require("./normalize");
  4. const range_tree_1 = require("./range-tree");
  5. /**
  6. * Merges a list of process coverages.
  7. *
  8. * The result is normalized.
  9. * The input values may be mutated, it is not safe to use them after passing
  10. * them to this function.
  11. * The computation is synchronous.
  12. *
  13. * @param processCovs Process coverages to merge.
  14. * @return Merged process coverage.
  15. */
  16. function mergeProcessCovs(processCovs) {
  17. if (processCovs.length === 0) {
  18. return { result: [] };
  19. }
  20. const urlToScripts = new Map();
  21. for (const processCov of processCovs) {
  22. for (const scriptCov of processCov.result) {
  23. let scriptCovs = urlToScripts.get(scriptCov.url);
  24. if (scriptCovs === undefined) {
  25. scriptCovs = [];
  26. urlToScripts.set(scriptCov.url, scriptCovs);
  27. }
  28. scriptCovs.push(scriptCov);
  29. }
  30. }
  31. const result = [];
  32. for (const scripts of urlToScripts.values()) {
  33. // assert: `scripts.length > 0`
  34. result.push(mergeScriptCovs(scripts));
  35. }
  36. const merged = { result };
  37. normalize_1.normalizeProcessCov(merged);
  38. return merged;
  39. }
  40. exports.mergeProcessCovs = mergeProcessCovs;
  41. /**
  42. * Merges a list of matching script coverages.
  43. *
  44. * Scripts are matching if they have the same `url`.
  45. * The result is normalized.
  46. * The input values may be mutated, it is not safe to use them after passing
  47. * them to this function.
  48. * The computation is synchronous.
  49. *
  50. * @param scriptCovs Process coverages to merge.
  51. * @return Merged script coverage, or `undefined` if the input list was empty.
  52. */
  53. function mergeScriptCovs(scriptCovs) {
  54. if (scriptCovs.length === 0) {
  55. return undefined;
  56. }
  57. else if (scriptCovs.length === 1) {
  58. const merged = scriptCovs[0];
  59. normalize_1.deepNormalizeScriptCov(merged);
  60. return merged;
  61. }
  62. const first = scriptCovs[0];
  63. const scriptId = first.scriptId;
  64. const url = first.url;
  65. const rangeToFuncs = new Map();
  66. for (const scriptCov of scriptCovs) {
  67. for (const funcCov of scriptCov.functions) {
  68. const rootRange = stringifyFunctionRootRange(funcCov);
  69. let funcCovs = rangeToFuncs.get(rootRange);
  70. if (funcCovs === undefined ||
  71. // if the entry in rangeToFuncs is function-level granularity and
  72. // the new coverage is block-level, prefer block-level.
  73. (!funcCovs[0].isBlockCoverage && funcCov.isBlockCoverage)) {
  74. funcCovs = [];
  75. rangeToFuncs.set(rootRange, funcCovs);
  76. }
  77. else if (funcCovs[0].isBlockCoverage && !funcCov.isBlockCoverage) {
  78. // if the entry in rangeToFuncs is block-level granularity, we should
  79. // not append function level granularity.
  80. continue;
  81. }
  82. funcCovs.push(funcCov);
  83. }
  84. }
  85. const functions = [];
  86. for (const funcCovs of rangeToFuncs.values()) {
  87. // assert: `funcCovs.length > 0`
  88. functions.push(mergeFunctionCovs(funcCovs));
  89. }
  90. const merged = { scriptId, url, functions };
  91. normalize_1.normalizeScriptCov(merged);
  92. return merged;
  93. }
  94. exports.mergeScriptCovs = mergeScriptCovs;
  95. /**
  96. * Returns a string representation of the root range of the function.
  97. *
  98. * This string can be used to match function with same root range.
  99. * The string is derived from the start and end offsets of the root range of
  100. * the function.
  101. * This assumes that `ranges` is non-empty (true for valid function coverages).
  102. *
  103. * @param funcCov Function coverage with the range to stringify
  104. * @internal
  105. */
  106. function stringifyFunctionRootRange(funcCov) {
  107. const rootRange = funcCov.ranges[0];
  108. return `${rootRange.startOffset.toString(10)};${rootRange.endOffset.toString(10)}`;
  109. }
  110. /**
  111. * Merges a list of matching function coverages.
  112. *
  113. * Functions are matching if their root ranges have the same span.
  114. * The result is normalized.
  115. * The input values may be mutated, it is not safe to use them after passing
  116. * them to this function.
  117. * The computation is synchronous.
  118. *
  119. * @param funcCovs Function coverages to merge.
  120. * @return Merged function coverage, or `undefined` if the input list was empty.
  121. */
  122. function mergeFunctionCovs(funcCovs) {
  123. if (funcCovs.length === 0) {
  124. return undefined;
  125. }
  126. else if (funcCovs.length === 1) {
  127. const merged = funcCovs[0];
  128. normalize_1.normalizeFunctionCov(merged);
  129. return merged;
  130. }
  131. const functionName = funcCovs[0].functionName;
  132. const trees = [];
  133. for (const funcCov of funcCovs) {
  134. // assert: `fn.ranges.length > 0`
  135. // assert: `fn.ranges` is sorted
  136. trees.push(range_tree_1.RangeTree.fromSortedRanges(funcCov.ranges));
  137. }
  138. // assert: `trees.length > 0`
  139. const mergedTree = mergeRangeTrees(trees);
  140. normalize_1.normalizeRangeTree(mergedTree);
  141. const ranges = mergedTree.toRanges();
  142. const isBlockCoverage = !(ranges.length === 1 && ranges[0].count === 0);
  143. const merged = { functionName, ranges, isBlockCoverage };
  144. // assert: `merged` is normalized
  145. return merged;
  146. }
  147. exports.mergeFunctionCovs = mergeFunctionCovs;
  148. /**
  149. * @precondition Same `start` and `end` for all the trees
  150. */
  151. function mergeRangeTrees(trees) {
  152. if (trees.length <= 1) {
  153. return trees[0];
  154. }
  155. const first = trees[0];
  156. let delta = 0;
  157. for (const tree of trees) {
  158. delta += tree.delta;
  159. }
  160. const children = mergeRangeTreeChildren(trees);
  161. return new range_tree_1.RangeTree(first.start, first.end, delta, children);
  162. }
  163. class RangeTreeWithParent {
  164. constructor(parentIndex, tree) {
  165. this.parentIndex = parentIndex;
  166. this.tree = tree;
  167. }
  168. }
  169. class StartEvent {
  170. constructor(offset, trees) {
  171. this.offset = offset;
  172. this.trees = trees;
  173. }
  174. static compare(a, b) {
  175. return a.offset - b.offset;
  176. }
  177. }
  178. class StartEventQueue {
  179. constructor(queue) {
  180. this.queue = queue;
  181. this.nextIndex = 0;
  182. this.pendingOffset = 0;
  183. this.pendingTrees = undefined;
  184. }
  185. static fromParentTrees(parentTrees) {
  186. const startToTrees = new Map();
  187. for (const [parentIndex, parentTree] of parentTrees.entries()) {
  188. for (const child of parentTree.children) {
  189. let trees = startToTrees.get(child.start);
  190. if (trees === undefined) {
  191. trees = [];
  192. startToTrees.set(child.start, trees);
  193. }
  194. trees.push(new RangeTreeWithParent(parentIndex, child));
  195. }
  196. }
  197. const queue = [];
  198. for (const [startOffset, trees] of startToTrees) {
  199. queue.push(new StartEvent(startOffset, trees));
  200. }
  201. queue.sort(StartEvent.compare);
  202. return new StartEventQueue(queue);
  203. }
  204. setPendingOffset(offset) {
  205. this.pendingOffset = offset;
  206. }
  207. pushPendingTree(tree) {
  208. if (this.pendingTrees === undefined) {
  209. this.pendingTrees = [];
  210. }
  211. this.pendingTrees.push(tree);
  212. }
  213. next() {
  214. const pendingTrees = this.pendingTrees;
  215. const nextEvent = this.queue[this.nextIndex];
  216. if (pendingTrees === undefined) {
  217. this.nextIndex++;
  218. return nextEvent;
  219. }
  220. else if (nextEvent === undefined) {
  221. this.pendingTrees = undefined;
  222. return new StartEvent(this.pendingOffset, pendingTrees);
  223. }
  224. else {
  225. if (this.pendingOffset < nextEvent.offset) {
  226. this.pendingTrees = undefined;
  227. return new StartEvent(this.pendingOffset, pendingTrees);
  228. }
  229. else {
  230. if (this.pendingOffset === nextEvent.offset) {
  231. this.pendingTrees = undefined;
  232. for (const tree of pendingTrees) {
  233. nextEvent.trees.push(tree);
  234. }
  235. }
  236. this.nextIndex++;
  237. return nextEvent;
  238. }
  239. }
  240. }
  241. }
  242. function mergeRangeTreeChildren(parentTrees) {
  243. const result = [];
  244. const startEventQueue = StartEventQueue.fromParentTrees(parentTrees);
  245. const parentToNested = new Map();
  246. let openRange;
  247. while (true) {
  248. const event = startEventQueue.next();
  249. if (event === undefined) {
  250. break;
  251. }
  252. if (openRange !== undefined && openRange.end <= event.offset) {
  253. result.push(nextChild(openRange, parentToNested));
  254. openRange = undefined;
  255. }
  256. if (openRange === undefined) {
  257. let openRangeEnd = event.offset + 1;
  258. for (const { parentIndex, tree } of event.trees) {
  259. openRangeEnd = Math.max(openRangeEnd, tree.end);
  260. insertChild(parentToNested, parentIndex, tree);
  261. }
  262. startEventQueue.setPendingOffset(openRangeEnd);
  263. openRange = { start: event.offset, end: openRangeEnd };
  264. }
  265. else {
  266. for (const { parentIndex, tree } of event.trees) {
  267. if (tree.end > openRange.end) {
  268. const right = tree.split(openRange.end);
  269. startEventQueue.pushPendingTree(new RangeTreeWithParent(parentIndex, right));
  270. }
  271. insertChild(parentToNested, parentIndex, tree);
  272. }
  273. }
  274. }
  275. if (openRange !== undefined) {
  276. result.push(nextChild(openRange, parentToNested));
  277. }
  278. return result;
  279. }
  280. function insertChild(parentToNested, parentIndex, tree) {
  281. let nested = parentToNested.get(parentIndex);
  282. if (nested === undefined) {
  283. nested = [];
  284. parentToNested.set(parentIndex, nested);
  285. }
  286. nested.push(tree);
  287. }
  288. function nextChild(openRange, parentToNested) {
  289. const matchingTrees = [];
  290. for (const nested of parentToNested.values()) {
  291. if (nested.length === 1 && nested[0].start === openRange.start && nested[0].end === openRange.end) {
  292. matchingTrees.push(nested[0]);
  293. }
  294. else {
  295. matchingTrees.push(new range_tree_1.RangeTree(openRange.start, openRange.end, 0, nested));
  296. }
  297. }
  298. parentToNested.clear();
  299. return mergeRangeTrees(matchingTrees);
  300. }
  301. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIl9zcmMvbWVyZ2UudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSwyQ0FNcUI7QUFDckIsNkNBQXlDO0FBR3pDOzs7Ozs7Ozs7O0dBVUc7QUFDSC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