-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.m
157 lines (140 loc) · 3.69 KB
/
Queue.m
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
classdef Queue < handle
% Queue - strongly-typed Queue collection
%
% Properties:
%
% Type (string)
%
% Methods:
%
% Queue(type)
% display
% size
% isempty
% clear
% contains(obj)
% offer(obj)
% remove(obj)
% peek - returns [] if queue is empty
% poll - returns [] if queue is empty
% values - returns contents in a cell array
%
% Notes:
%
% Compatible classes must overload eq() for object-to-object comparisons.
%
% Example:
%
% q = Queue('Widget')
%
% q.offer(RedWidget(1))
% q.offer(RedWidget(3))
% q.offer(RedWidget(2))
% q.offer(BlueWidget(2))
% q.offer(BlueWidget(2))
%
% q.size
% q.remove(RedWidget(2));
% q.size
% q.remove(BlueWidget(2));
% q.size
%
% q.peek
% q.poll
% q.size
%
% Author: [email protected], 3/15/09
properties (GetAccess = protected, SetAccess = protected, Hidden = true)
Elements
end
properties (SetAccess = protected)
Type
end
methods
function[obj] = Queue(type)
if ~ischar(type)
throw(MException('Queue:constructorInvalidType','??? ''type'' must be a valid class name.'))
end
obj.Elements = {};
obj.Type = type;
end
function disp(obj)
disp([class(obj) '<' obj.Type '> (head on top)'])
if ~obj.isempty
for i = 1:obj.size
disp(obj.Elements{i})
end
else
disp([])
end
end
function[out] = size(obj)
out = length(obj.Elements);
end
function[out] = values(obj)
out = obj.Elements;
end
function[out] = isempty(obj)
out = obj.size == 0;
end
function[obj] = clear(obj)
obj.Elements = {};
end
function[out] = contains(obj,e)
out = false;
for i = 1:obj.size
if e == obj.Elements{i}
out = true;
break
end
end
end
function[obj] = offer(obj,e)
if length(e) > 1
throw(MException('Queue:offerMultiple','??? Cannot offer multiple elements at once.'))
end
if ~isa(e,obj.Type)
throw(MException('Queue:offerInvalidType','??? Invalid type.'))
end
if isempty(obj.Elements)
obj.Elements = {e};
else
obj.Elements{end+1} = e;
end
end
function[obj] = remove(obj,e)
if length(e) > 1
throw(MException('Queue:removeMultiple','??? Cannot remove multiple elements at once.'))
end
if ~isa(e,obj.Type)
throw(MException('Queue:removeInvalidType','??? Invalid type.'))
end
if ~isempty(obj.Elements)
k = [];
for i = 1:obj.size
if e == obj.Elements{i}
k = [k i]; %#ok
end
end
if ~isempty(k)
obj.Elements(k) = [];
end
end
end
function[out] = peek(obj)
if ~obj.isempty
out = obj.Elements{1};
else
out = [];
end
end
function[out] = poll(obj)
if ~obj.isempty
out = obj.Elements{1};
obj.Elements(1) = [];
else
out = [];
end
end
end
end