priorityqueue.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2006 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Datastructure: Priority Queue.
  16. *
  17. *
  18. * This file provides the implementation of a Priority Queue. Smaller priorities
  19. * move to the front of the queue. If two values have the same priority,
  20. * it is arbitrary which value will come to the front of the queue first.
  21. */
  22. // TODO(user): Should this rely on natural ordering via some Comparable
  23. // interface?
  24. goog.provide('goog.structs.PriorityQueue');
  25. goog.require('goog.structs.Heap');
  26. /**
  27. * Class for Priority Queue datastructure.
  28. *
  29. * @constructor
  30. * @extends {goog.structs.Heap<number, VALUE>}
  31. * @template VALUE
  32. * @final
  33. */
  34. goog.structs.PriorityQueue = function() {
  35. goog.structs.Heap.call(this);
  36. };
  37. goog.inherits(goog.structs.PriorityQueue, goog.structs.Heap);
  38. /**
  39. * Puts the specified value in the queue.
  40. * @param {number} priority The priority of the value. A smaller value here
  41. * means a higher priority.
  42. * @param {VALUE} value The value.
  43. */
  44. goog.structs.PriorityQueue.prototype.enqueue = function(priority, value) {
  45. this.insert(priority, value);
  46. };
  47. /**
  48. * Retrieves and removes the head of this queue.
  49. * @return {VALUE} The element at the head of this queue. Returns undefined if
  50. * the queue is empty.
  51. */
  52. goog.structs.PriorityQueue.prototype.dequeue = function() {
  53. return this.remove();
  54. };