-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathQueue.m
More file actions
52 lines (36 loc) · 1.09 KB
/
Queue.m
File metadata and controls
52 lines (36 loc) · 1.09 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
classdef Queue < handle
properties
queue
head
size
end
methods
function obj = Queue(data, capacity)
obj.queue = repmat({data}, capacity, 1);
obj.head = uint32(1);
obj.size = uint32(0);
end
function push(obj, data)
if obj.size < numel(obj.queue)
obj.queue{mod(obj.head + obj.size - 1, numel(obj.queue)) + 1} = data;
obj.size = obj.size + 1;
end
end
function data = pop(obj)
data = obj.queue{obj.head};
if obj.size > 0
obj.head = mod(obj.head, numel(obj.queue)) + 1;
obj.size = obj.size - 1;
end
end
function data = peek(obj)
data = obj.queue{obj.head};
end
function clear(obj)
obj.size = uint32(0);
end
function e = isempty(obj)
e = obj.size < 1;
end
end
end