/
Queue.js
105 lines (91 loc) · 2.55 KB
/
Queue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*global define*/
define(function() {
"use strict";
/**
* A queue that can enqueue items at the end, and dequeue items from the front.
*
* @alias Queue
* @constructor
*/
var Queue = function() {
this._array = [];
this._offset = 0;
/**
* The length of the queue.
*/
this.length = 0;
};
/**
* Enqueues the specified item.
*
* @param {Object} item The item to enqueue.
*/
Queue.prototype.enqueue = function(item) {
this._array.push(item);
this.length++;
};
/**
* Dequeues an item. Returns undefined if the queue is empty.
*/
Queue.prototype.dequeue = function() {
if (this.length === 0) {
return undefined;
}
var array = this._array;
var offset = this._offset;
var item = array[offset];
array[offset] = undefined;
offset++;
if (offset > 10 && offset * 2 > array.length) {
//compact array
this._array = array.slice(offset);
offset = 0;
}
this._offset = offset;
this.length--;
return item;
};
/**
* Check whether this queue contains the specified item.
*
* @param {Object} item the item to search for.
*/
Queue.prototype.contains = function(item) {
return this._array.indexOf(item) !== -1;
};
/**
* Remove all items from the queue.
*/
Queue.prototype.clear = function() {
this._array.length = this._offset = this.length = 0;
};
/**
* Sort the items in the queue in-place.
*
* @param {Queue~Comparator} compareFunction A function that defines the sort order.
*/
Queue.prototype.sort = function(compareFunction) {
if (this._offset > 0) {
//compact array
this._array = this._array.slice(this._offset);
this._offset = 0;
}
this._array.sort(compareFunction);
};
/**
* A function used to compare two items while sorting a queue.
* @callback Queue~Comparator
*
* @param {Object} a An item in the array.
* @param {Object} b An item in the array.
* @returns {Number} Returns a negative value if <code>a</code> is less than <code>b</code>,
* a positive value if <code>a</code> is greater than <code>b</code>, or
* 0 if <code>a</code> is equal to <code>b</code>.
*
* @example
* function compareNumbers(a, b) {
* return a - b;
* }
*/
return Queue;
});