Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80559 views
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
if (typeof define !== 'function') {
8
var define = require('amdefine')(module, require);
9
}
10
define(function (require, exports, module) {
11
12
var util = require('./util');
13
14
/**
15
* Determine whether mappingB is after mappingA with respect to generated
16
* position.
17
*/
18
function generatedPositionAfter(mappingA, mappingB) {
19
// Optimized for most common case
20
var lineA = mappingA.generatedLine;
21
var lineB = mappingB.generatedLine;
22
var columnA = mappingA.generatedColumn;
23
var columnB = mappingB.generatedColumn;
24
return lineB > lineA || lineB == lineA && columnB >= columnA ||
25
util.compareByGeneratedPositions(mappingA, mappingB) <= 0;
26
}
27
28
/**
29
* A data structure to provide a sorted view of accumulated mappings in a
30
* performance conscious manner. It trades a neglibable overhead in general
31
* case for a large speedup in case of mappings being added in order.
32
*/
33
function MappingList() {
34
this._array = [];
35
this._sorted = true;
36
// Serves as infimum
37
this._last = {generatedLine: -1, generatedColumn: 0};
38
}
39
40
/**
41
* Iterate through internal items. This method takes the same arguments that
42
* `Array.prototype.forEach` takes.
43
*
44
* NOTE: The order of the mappings is NOT guaranteed.
45
*/
46
MappingList.prototype.unsortedForEach =
47
function MappingList_forEach(aCallback, aThisArg) {
48
this._array.forEach(aCallback, aThisArg);
49
};
50
51
/**
52
* Add the given source mapping.
53
*
54
* @param Object aMapping
55
*/
56
MappingList.prototype.add = function MappingList_add(aMapping) {
57
var mapping;
58
if (generatedPositionAfter(this._last, aMapping)) {
59
this._last = aMapping;
60
this._array.push(aMapping);
61
} else {
62
this._sorted = false;
63
this._array.push(aMapping);
64
}
65
};
66
67
/**
68
* Returns the flat, sorted array of mappings. The mappings are sorted by
69
* generated position.
70
*
71
* WARNING: This method returns internal data without copying, for
72
* performance. The return value must NOT be mutated, and should be treated as
73
* an immutable borrow. If you want to take ownership, you must make your own
74
* copy.
75
*/
76
MappingList.prototype.toArray = function MappingList_toArray() {
77
if (!this._sorted) {
78
this._array.sort(util.compareByGeneratedPositions);
79
this._sorted = true;
80
}
81
return this._array;
82
};
83
84
exports.MappingList = MappingList;
85
86
});
87
88