tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

mapping-list.js (2301B)


      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 
      8 const util = require("./util");
      9 
     10 /**
     11 * Determine whether mappingB is after mappingA with respect to generated
     12 * position.
     13 */
     14 function generatedPositionAfter(mappingA, mappingB) {
     15  // Optimized for most common case
     16  const lineA = mappingA.generatedLine;
     17  const lineB = mappingB.generatedLine;
     18  const columnA = mappingA.generatedColumn;
     19  const columnB = mappingB.generatedColumn;
     20  return (
     21    lineB > lineA ||
     22    (lineB == lineA && columnB >= columnA) ||
     23    util.compareByGeneratedPositionsInflated(mappingA, mappingB) <= 0
     24  );
     25 }
     26 
     27 /**
     28 * A data structure to provide a sorted view of accumulated mappings in a
     29 * performance conscious manner. It trades a negligible overhead in general
     30 * case for a large speedup in case of mappings being added in order.
     31 */
     32 class MappingList {
     33  constructor() {
     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  unsortedForEach(aCallback, aThisArg) {
     47    this._array.forEach(aCallback, aThisArg);
     48  }
     49 
     50  /**
     51   * Add the given source mapping.
     52   *
     53   * @param Object aMapping
     54   */
     55  add(aMapping) {
     56    if (generatedPositionAfter(this._last, aMapping)) {
     57      this._last = aMapping;
     58      this._array.push(aMapping);
     59    } else {
     60      this._sorted = false;
     61      this._array.push(aMapping);
     62    }
     63  }
     64 
     65  /**
     66   * Returns the flat, sorted array of mappings. The mappings are sorted by
     67   * generated position.
     68   *
     69   * WARNING: This method returns internal data without copying, for
     70   * performance. The return value must NOT be mutated, and should be treated as
     71   * an immutable borrow. If you want to take ownership, you must make your own
     72   * copy.
     73   */
     74  toArray() {
     75    if (!this._sorted) {
     76      this._array.sort(util.compareByGeneratedPositionsInflated);
     77      this._sorted = true;
     78    }
     79    return this._array;
     80  }
     81 }
     82 
     83 exports.MappingList = MappingList;