Pinsave/counterstrike
1
1/* -*- Mode: js; js-indent-level: 2; -*- */2/*3 * Copyright 2014 Mozilla Foundation and contributors4 * Licensed under the New BSD license. See LICENSE or:5 * http://opensource.org/licenses/BSD-3-Clause6 */7 8var util = require('./util');9 10/**11 * Determine whether mappingB is after mappingA with respect to generated12 * position.13 */14function generatedPositionAfter(mappingA, mappingB) {15 // Optimized for most common case16 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 a26 * performance conscious manner. It trades a neglibable overhead in general27 * case for a large speedup in case of mappings being added in order.28 */29function MappingList() {30 this._array = [];31 this._sorted = true;32 // Serves as infimum33 this._last = {generatedLine: -1, generatedColumn: 0};34}35 36/**37 * Iterate through internal items. This method takes the same arguments that38 * `Array.prototype.forEach` takes.39 *40 * NOTE: The order of the mappings is NOT guaranteed.41 */42MappingList.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 aMapping51 */52MappingList.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 by64 * generated position.65 *66 * WARNING: This method returns internal data without copying, for67 * performance. The return value must NOT be mutated, and should be treated as68 * an immutable borrow. If you want to take ownership, you must make your own69 * copy.70 */71MappingList.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 79exports.MappingList = MappingList;80 