mapping-list.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. if (typeof define !== 'function') {
  8. var define = require('amdefine')(module, require);
  9. }
  10. define(function (require, exports, module) {
  11. var util = require('./util');
  12. /**
  13. * Determine whether mappingB is after mappingA with respect to generated
  14. * position.
  15. */
  16. function generatedPositionAfter(mappingA, mappingB) {
  17. // Optimized for most common case
  18. var lineA = mappingA.generatedLine;
  19. var lineB = mappingB.generatedLine;
  20. var columnA = mappingA.generatedColumn;
  21. var columnB = mappingB.generatedColumn;
  22. return lineB > lineA || lineB == lineA && columnB >= columnA ||
  23. util.compareByGeneratedPositionsInflated(mappingA, mappingB) <= 0;
  24. }
  25. /**
  26. * A data structure to provide a sorted view of accumulated mappings in a
  27. * performance conscious manner. It trades a neglibable overhead in general
  28. * case for a large speedup in case of mappings being added in order.
  29. */
  30. function MappingList() {
  31. this._array = [];
  32. this._sorted = true;
  33. // Serves as infimum
  34. this._last = {generatedLine: -1, generatedColumn: 0};
  35. }
  36. /**
  37. * Iterate through internal items. This method takes the same arguments that
  38. * `Array.prototype.forEach` takes.
  39. *
  40. * NOTE: The order of the mappings is NOT guaranteed.
  41. */
  42. MappingList.prototype.unsortedForEach =
  43. function MappingList_forEach(aCallback, aThisArg) {
  44. this._array.forEach(aCallback, aThisArg);
  45. };
  46. /**
  47. * Add the given source mapping.
  48. *
  49. * @param Object aMapping
  50. */
  51. MappingList.prototype.add = function MappingList_add(aMapping) {
  52. var mapping;
  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. * Returns the flat, sorted array of mappings. The mappings are sorted by
  63. * generated position.
  64. *
  65. * WARNING: This method returns internal data without copying, for
  66. * performance. The return value must NOT be mutated, and should be treated as
  67. * an immutable borrow. If you want to take ownership, you must make your own
  68. * copy.
  69. */
  70. MappingList.prototype.toArray = function MappingList_toArray() {
  71. if (!this._sorted) {
  72. this._array.sort(util.compareByGeneratedPositionsInflated);
  73. this._sorted = true;
  74. }
  75. return this._array;
  76. };
  77. exports.MappingList = MappingList;
  78. });