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 | var 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 | var lineA = mappingA.generatedLine;
|
---|
17 | var lineB = mappingB.generatedLine;
|
---|
18 | var columnA = mappingA.generatedColumn;
|
---|
19 | var 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 neglibable overhead in general
|
---|
27 | * case for a large speedup in case of mappings being added in order.
|
---|
28 | */
|
---|
29 | function MappingList() {
|
---|
30 | this._array = [];
|
---|
31 | this._sorted = true;
|
---|
32 | // Serves as infimum
|
---|
33 | this._last = {generatedLine: -1, generatedColumn: 0};
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Iterate through internal items. This method takes the same arguments that
|
---|
38 | * `Array.prototype.forEach` takes.
|
---|
39 | *
|
---|
40 | * NOTE: The order of the mappings is NOT guaranteed.
|
---|
41 | */
|
---|
42 | MappingList.prototype.unsortedForEach =
|
---|
43 | function MappingList_forEach(aCallback, aThisArg) {
|
---|
44 | this._array.forEach(aCallback, aThisArg);
|
---|
45 | };
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Add the given source mapping.
|
---|
49 | *
|
---|
50 | * @param Object aMapping
|
---|
51 | */
|
---|
52 | MappingList.prototype.add = function MappingList_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 | MappingList.prototype.toArray = function MappingList_toArray() {
|
---|
72 | if (!this._sorted) {
|
---|
73 | this._array.sort(util.compareByGeneratedPositionsInflated);
|
---|
74 | this._sorted = true;
|
---|
75 | }
|
---|
76 | return this._array;
|
---|
77 | };
|
---|
78 |
|
---|
79 | exports.MappingList = MappingList;
|
---|