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
7 if (typeof define !== 'function') {
8 var define = require('amdefine')(module, require);
10 define(function (require, exports, module) {
12 var util = require('./util');
15 * Determine whether mappingB is after mappingA with respect to generated
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;
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.
33 function MappingList() {
37 this._last = {generatedLine: -1, generatedColumn: 0};
41 * Iterate through internal items. This method takes the same arguments that
42 * `Array.prototype.forEach` takes.
44 * NOTE: The order of the mappings is NOT guaranteed.
46 MappingList.prototype.unsortedForEach =
47 function MappingList_forEach(aCallback, aThisArg) {
48 this._array.forEach(aCallback, aThisArg);
52 * Add the given source mapping.
54 * @param Object aMapping
56 MappingList.prototype.add = function MappingList_add(aMapping) {
58 if (generatedPositionAfter(this._last, aMapping)) {
59 this._last = aMapping;
60 this._array.push(aMapping);
63 this._array.push(aMapping);
68 * Returns the flat, sorted array of mappings. The mappings are sorted by
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
76 MappingList.prototype.toArray = function MappingList_toArray() {
78 this._array.sort(util.compareByGeneratedPositions);
84 exports.MappingList = MappingList;