forked from YoYoGames/GameMaker-HTML5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyyQueue.js
More file actions
101 lines (90 loc) · 3.64 KB
/
Copy pathyyQueue.js
File metadata and controls
101 lines (90 loc) · 3.64 KB
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
// **********************************************************************************************************************
//
// Copyright (c)2011, YoYo Games Ltd. All Rights reserved.
//
// File: yyQueue.js
// Created: 21/10/2011
// Author: Mike
// Project: HTML5
// Description: Simple "queue"
//
// Date Version BY Comment
// ----------------------------------------------------------------------------------------------------------------------
// 21/10/2011 V1.0 MJD 1st version.
//
// **********************************************************************************************************************
// #############################################################################################
/// Function:<summary>
/// Create a new QUEUE
/// </summary>
// #############################################################################################
/**@constructor*/
function yyQueue()
{
this.queue = [];
this.offset = 0;
this.Pop = yyQueue.prototype.Dequeue;
this.Push = yyQueue.prototype.Enqueue;
};
// #############################################################################################
/// Function: <summary>
/// Get the length of the queue
/// </summary>
// #############################################################################################
yyQueue.prototype.Length = function () {
return (this.queue.length - this.offset);
};
// #############################################################################################
/// Function: <summary>
/// Length at LEAST have "_count" elements in it?
/// </summary>
// #############################################################################################
yyQueue.prototype.AtLeast = function (_count) {
return ((this.queue.length - this.offset) >= _count);
};
// #############################################################################################
/// Function: <summary>
/// Is the queue empty?
/// </summary>
// #############################################################################################
yyQueue.prototype.IsEmpty = function () {
return (this.queue.length==0);
};
// #############################################################################################
/// Function: <summary>
/// Enqueue an item
/// </summary>
// #############################################################################################
yyQueue.prototype.Enqueue = function (_item) {
this.queue.push(_item);
};
// #############################################################################################
/// Function: <summary>
/// Dequeue an item
/// </summary>
// #############################################################################################
yyQueue.prototype.Dequeue = function ()
{
if( this.IsEmpty() ) return undefined;
// get item from start of queue
var item = this.queue[this.offset];
// Only cut down on space "now and then", and base it on the size of the queue.
if ((++this.offset * 2) >= this.queue.length)
{
this.queue = this.queue.slice(this.offset);
this.offset = 0;
}
return item;
};
// #############################################################################################
/// Function: <summary>
/// Look at the start of the queue
/// </summary>
// #############################################################################################
yyQueue.prototype.peek = function () {
if (this.queue.length > 0){
return this.queue[this.offset];
} else{
return undefined;
}
};