1 /* -*- Mode: js; js-indent-level: 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
8 var util = require('./util');
11 * Determine whether mappingB is after mappingA with respect to generated
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;
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.
29 function MappingList() {
33 this._last = {generatedLine: -1, generatedColumn: 0};
37 * Iterate through internal items. This method takes the same arguments that
38 * `Array.prototype.forEach` takes.
40 * NOTE: The order of the mappings is NOT guaranteed.
42 MappingList.prototype.unsortedForEach =
43 function MappingList_forEach(aCallback, aThisArg) {
44 this._array.forEach(aCallback, aThisArg);
48 * Add the given source mapping.
50 * @param Object aMapping
52 MappingList.prototype.add = function MappingList_add(aMapping) {
53 if (generatedPositionAfter(this._last, aMapping)) {
54 this._last = aMapping;
55 this._array.push(aMapping);
58 this._array.push(aMapping);
63 * Returns the flat, sorted array of mappings. The mappings are sorted by
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
71 MappingList.prototype.toArray = function MappingList_toArray() {
73 this._array.sort(util.compareByGeneratedPositionsInflated);
79 exports.MappingList = MappingList;