tinyqueue.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.TinyQueue = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
  2. 'use strict';
  3. module.exports = TinyQueue;
  4. module.exports.default = TinyQueue;
  5. function TinyQueue(data, compare) {
  6. if (!(this instanceof TinyQueue)) return new TinyQueue(data, compare);
  7. this.data = data || [];
  8. this.length = this.data.length;
  9. this.compare = compare || defaultCompare;
  10. if (this.length > 0) {
  11. for (var i = (this.length >> 1) - 1; i >= 0; i--) this._down(i);
  12. }
  13. }
  14. function defaultCompare(a, b) {
  15. return a < b ? -1 : a > b ? 1 : 0;
  16. }
  17. TinyQueue.prototype = {
  18. push: function (item) {
  19. this.data.push(item);
  20. this.length++;
  21. this._up(this.length - 1);
  22. },
  23. pop: function () {
  24. if (this.length === 0) return undefined;
  25. var top = this.data[0];
  26. this.length--;
  27. if (this.length > 0) {
  28. this.data[0] = this.data[this.length];
  29. this._down(0);
  30. }
  31. this.data.pop();
  32. return top;
  33. },
  34. peek: function () {
  35. return this.data[0];
  36. },
  37. _up: function (pos) {
  38. var data = this.data;
  39. var compare = this.compare;
  40. var item = data[pos];
  41. while (pos > 0) {
  42. var parent = (pos - 1) >> 1;
  43. var current = data[parent];
  44. if (compare(item, current) >= 0) break;
  45. data[pos] = current;
  46. pos = parent;
  47. }
  48. data[pos] = item;
  49. },
  50. _down: function (pos) {
  51. var data = this.data;
  52. var compare = this.compare;
  53. var halfLength = this.length >> 1;
  54. var item = data[pos];
  55. while (pos < halfLength) {
  56. var left = (pos << 1) + 1;
  57. var right = left + 1;
  58. var best = data[left];
  59. if (right < this.length && compare(data[right], best) < 0) {
  60. left = right;
  61. best = data[right];
  62. }
  63. if (compare(best, item) >= 0) break;
  64. data[pos] = best;
  65. pos = left;
  66. }
  67. data[pos] = item;
  68. }
  69. };
  70. },{}]},{},[1])(1)
  71. });