-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.m
61 lines (52 loc) · 1.54 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
classdef Queue < handle
properties (Constant = true)
INITIAL_SIZE = 10; % Initial size of (resizable) list; this is for efficiency
end
properties
Data, % Must be {} vector
Empty, % Must be <T>
Length % Must be uint32
end
methods
function obj = Queue(Type)
obj.Data = repmat({Type}, 1, Queue.INITIAL_SIZE);
obj.Empty = {Type};
obj.Length = 0;
end
% Enqueue 1 item (must be single)
function enq(obj, item)
obj.Length = obj.Length + 1;
obj.Data = horzcat({item}, obj.Data);
end
% Enqueue several items (must be vector)
function enqa(obj, items)
for item = items
if (iscell(item))
obj.enq(item{1});
else
obj.enq(item);
end
end
end
% Dequeue 1 item
function [item] = deq(obj)
cell = obj.Data(obj.Length);
item = cell(1);
obj.Data(obj.Length) = obj.Empty;
obj.Length = obj.Length - 1;
end
% Report if empty
function result = isEmpty(obj)
result = (obj.Length == 0);
end
% Clear queue
function clear(obj)
obj.Data = repmat(obj.Empty, 1, Queue.INITIAL_SIZE);
obj.Length = 0;
end
% Convert to vector (empty space trimmed).
function data = vec(obj)
data = obj.Data(1:obj.Length);
end
end
end