1 | /* -*- Mode: js; js-indent-level: 2; -*- */
|
---|
2 | /*
|
---|
3 | * Copyright 2014 Mozilla Foundation and contributors
|
---|
4 | * Licensed under the New BSD license. See LICENSE or:
|
---|
5 | * http://opensource.org/licenses/BSD-3-Clause
|
---|
6 | */
|
---|
7 |
|
---|
8 | const util = require("./util");
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Determine whether mappingB is after mappingA with respect to generated
|
---|
12 | * position.
|
---|
13 | */
|
---|
14 | function generatedPositionAfter(mappingA, mappingB) {
|
---|
15 | // Optimized for most common case
|
---|
16 | const lineA = mappingA.generatedLine;
|
---|
17 | const lineB = mappingB.generatedLine;
|
---|
18 | const columnA = mappingA.generatedColumn;
|
---|
19 | const columnB = mappingB.generatedColumn;
|
---|
20 | return lineB > lineA || lineB == lineA && columnB >= columnA ||
|
---|
21 | util.compareByGeneratedPositionsInflated(mappingA, mappingB) <= 0;
|
---|
22 | }
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * A data structure to provide a sorted view of accumulated mappings in a
|
---|
26 | * performance conscious manner. It trades a negligible overhead in general
|
---|
27 | * case for a large speedup in case of mappings being added in order.
|
---|
28 | */
|
---|
29 | class MappingList {
|
---|
30 | constructor() {
|
---|
31 | this._array = [];
|
---|
32 | this._sorted = true;
|
---|
33 | // Serves as infimum
|
---|
34 | this._last = {generatedLine: -1, generatedColumn: 0};
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Iterate through internal items. This method takes the same arguments that
|
---|
39 | * `Array.prototype.forEach` takes.
|
---|
40 | *
|
---|
41 | * NOTE: The order of the mappings is NOT guaranteed.
|
---|
42 | */
|
---|
43 | unsortedForEach(aCallback, aThisArg) {
|
---|
44 | this._array.forEach(aCallback, aThisArg);
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Add the given source mapping.
|
---|
49 | *
|
---|
50 | * @param Object aMapping
|
---|
51 | */
|
---|
52 | add(aMapping) {
|
---|
53 | if (generatedPositionAfter(this._last, aMapping)) {
|
---|
54 | this._last = aMapping;
|
---|
55 | this._array.push(aMapping);
|
---|
56 | } else {
|
---|
57 | this._sorted = false;
|
---|
58 | this._array.push(aMapping);
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Returns the flat, sorted array of mappings. The mappings are sorted by
|
---|
64 | * generated position.
|
---|
65 | *
|
---|
66 | * WARNING: This method returns internal data without copying, for
|
---|
67 | * performance. The return value must NOT be mutated, and should be treated as
|
---|
68 | * an immutable borrow. If you want to take ownership, you must make your own
|
---|
69 | * copy.
|
---|
70 | */
|
---|
71 | toArray() {
|
---|
72 | if (!this._sorted) {
|
---|
73 | this._array.sort(util.compareByGeneratedPositionsInflated);
|
---|
74 | this._sorted = true;
|
---|
75 | }
|
---|
76 | return this._array;
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | exports.MappingList = MappingList;
|
---|