/
experimental.js
98 lines (81 loc) · 3.22 KB
/
experimental.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
(function (root, undefined) {
var doesArrayContainValue = function (array, value) {
if (typeof array.indexOf === 'function') {
return array.indexOf(value) >= 0;
} else {
// Unfortunately, "for-in" loops in Javascript are not as useful as
// they are in other languages, and in this scenario should be avoided.
// See: http://stackoverflow.com/questions/500504/javascript-for-in-with-arrays
for (var i = 0; i < array.length; i++) {
if (array[i] === value) { return true; }
}
return false;
}
};
root.Structures = (function(Structures) {
Structures.Stack = function (array) {
this._stack = array || [];
};
Structures.Stack.prototype = {
push: function (obj) {
this._stack.push(obj);
return this;
},
pop: function () {
return this._stack.pop();
},
peek: function () {
// Will return 'undefined' if index is negative (empty array)
return this._stack[this._stack.length - 1];
},
clear: function () {
// See http://jsperf.com/emptying-arrays for performance
// Personal tests on Chrome 17 and FF 10.0.2/11 show this method to be fastest.
// IE9 is faster when setting length to 0, but I'm biased towards Chrome/FF,
// and this is the more intuitive approach when reading code, anyways.
this._stack = [];
return this;
},
size: function () {
return this._stack.length;
},
contains: function (obj) {
return doesArrayContainValue(this._stack, obj);
},
isEmpty: function () {
return this._stack.length === 0;
}
};
Structures.Queue = function (array) {
this._queue = array || [];
};
Structures.Queue.prototype = {
push: function (obj) {
this._queue.push(obj);
return this;
},
pop: function () {
var obj = this._queue[0]; // 'undefined' if empty array
this._queue = this._queue.slice(1); // 'slice' will return empty array when called on an empty array
return obj;
},
peek: function () {
return this._queue[0]; // 'undefined' if empty array
},
clear: function () {
this._queue = [];
return this;
},
size: function () {
return this._queue.length;
},
contains: function (obj) {
return doesArrayContainValue(this._queue, obj);
},
isEmpty: function () {
return this._queue.length === 0;
}
};
return Structures;
})(root.Structures || {});
})(this);