| 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 |
/** |
| 16 |
* @fileoverview Datastructure: Priority Queue. |
| 17 |
* |
| 18 |
* |
| 19 |
* This file provides the implementation of a Priority Queue. Smaller priorities |
| 20 |
* move to the front of the queue. If two values have the same priority, |
| 21 |
* it is arbitrary which value will come to the front of the queue first. |
| 22 |
*/ |
| 23 |
|
| 24 |
// TODO(user): Should this rely on natural ordering via some Comparable |
| 25 |
// interface? |
| 26 |
|
| 27 |
|
| 28 |
goog.provide('goog.structs.PriorityQueue'); |
| 29 |
|
| 30 |
goog.require('goog.structs.Heap'); |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
/** |
| 35 |
* Class for Priority Queue datastructure. |
| 36 |
* |
| 37 |
* @constructor |
| 38 |
* @extends {goog.structs.Heap<number, VALUE>} |
| 39 |
* @template VALUE |
| 40 |
* @final |
| 41 |
*/ |
| 42 |
goog.structs.PriorityQueue = function() { |
| 43 |
goog.structs.Heap.call(this); |
| 44 |
}; |
| 45 |
goog.inherits(goog.structs.PriorityQueue, goog.structs.Heap); |
| 46 |
|
| 47 |
|
| 48 |
/** |
| 49 |
* Puts the specified value in the queue. |
| 50 |
* @param {number} priority The priority of the value. A smaller value here |
| 51 |
* means a higher priority. |
| 52 |
* @param {VALUE} value The value. |
| 53 |
*/ |
| 54 |
goog.structs.PriorityQueue.prototype.enqueue = function(priority, value) { |
| 55 |
this.insert(priority, value); |
| 56 |
}; |
| 57 |
|
| 58 |
|
| 59 |
/** |
| 60 |
* Retrieves and removes the head of this queue. |
| 61 |
* @return {VALUE} The element at the head of this queue. Returns undefined if |
| 62 |
* the queue is empty. |
| 63 |
*/ |
| 64 |
goog.structs.PriorityQueue.prototype.dequeue = function() { |
| 65 |
return this.remove(); |
| 66 |
}; |