react / wstein / node_modules / browserify / node_modules / browser-pack / node_modules / combine-source-map / node_modules / source-map / lib / source-map / mapping-list.js
80559 views/* -*- Mode: js; js-indent-level: 2; -*- */1/*2* Copyright 2014 Mozilla Foundation and contributors3* Licensed under the New BSD license. See LICENSE or:4* http://opensource.org/licenses/BSD-3-Clause5*/6if (typeof define !== 'function') {7var define = require('amdefine')(module, require);8}9define(function (require, exports, module) {1011var util = require('./util');1213/**14* Determine whether mappingB is after mappingA with respect to generated15* position.16*/17function generatedPositionAfter(mappingA, mappingB) {18// Optimized for most common case19var lineA = mappingA.generatedLine;20var lineB = mappingB.generatedLine;21var columnA = mappingA.generatedColumn;22var columnB = mappingB.generatedColumn;23return lineB > lineA || lineB == lineA && columnB >= columnA ||24util.compareByGeneratedPositions(mappingA, mappingB) <= 0;25}2627/**28* A data structure to provide a sorted view of accumulated mappings in a29* performance conscious manner. It trades a neglibable overhead in general30* case for a large speedup in case of mappings being added in order.31*/32function MappingList() {33this._array = [];34this._sorted = true;35// Serves as infimum36this._last = {generatedLine: -1, generatedColumn: 0};37}3839/**40* Iterate through internal items. This method takes the same arguments that41* `Array.prototype.forEach` takes.42*43* NOTE: The order of the mappings is NOT guaranteed.44*/45MappingList.prototype.unsortedForEach =46function MappingList_forEach(aCallback, aThisArg) {47this._array.forEach(aCallback, aThisArg);48};4950/**51* Add the given source mapping.52*53* @param Object aMapping54*/55MappingList.prototype.add = function MappingList_add(aMapping) {56var mapping;57if (generatedPositionAfter(this._last, aMapping)) {58this._last = aMapping;59this._array.push(aMapping);60} else {61this._sorted = false;62this._array.push(aMapping);63}64};6566/**67* Returns the flat, sorted array of mappings. The mappings are sorted by68* generated position.69*70* WARNING: This method returns internal data without copying, for71* performance. The return value must NOT be mutated, and should be treated as72* an immutable borrow. If you want to take ownership, you must make your own73* copy.74*/75MappingList.prototype.toArray = function MappingList_toArray() {76if (!this._sorted) {77this._array.sort(util.compareByGeneratedPositions);78this._sorted = true;79}80return this._array;81};8283exports.MappingList = MappingList;8485});868788