-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFiniteQueue.m
More file actions
137 lines (125 loc) · 3.8 KB
/
Copy pathFiniteQueue.m
File metadata and controls
137 lines (125 loc) · 3.8 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
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
%% FiniteQueue class
classdef FiniteQueue < handle
properties (SetAccess = 'private', GetAccess = 'private')
storage;
capacity;
ini;
fin;
end
methods
%% constructor
function obj = FiniteQueue(capacity_,storage_)
if nargin == 1 && capacity_ > 0
obj.capacity = capacity_;
obj.storage = zeros(1,capacity_);
obj.reset();
elseif nargin == 2 && capacity_ > 0
storage_size = size(storage_);
if storage_size(1) == 1
obj.capacity = capacity_;
obj.ini = 1;
if storage_size(2) >= capacity_
obj.fin = capacity_;
obj.storage = storage_(1:capacity_);
else
obj.fin = length(storage);
obj.storage = [storage_ ...
zeros(1,capacity_-obj.fin)];
end
end
else
error('Invalid input arguments.')
end
end
%% makes the queue empty
function [] = reset(obj)
obj.ini = 1;
obj.fin = 0;
end
%% destructor
function [] = del(obj)
obj.reset();
delete(obj);
end
%% returns the queue's capacity
function value = getCapacity(obj)
value = obj.capacity;
end
%% tells the current length of the queue
function L = length(obj)
if obj.isEmpty()
L = 0;
return
end
L = obj.fin-obj.ini+1;
if obj.fin < obj.ini
L = obj.capacity-obj.ini+1+obj.fin;
end
end
%% returns the full queue as a row vector
function [queue,L] = q2vec(obj)
L = obj.length();
if L == 0
queue = [];
return
end
queue = zeros(1,L);
i = obj.ini-1;
for i_ = 1:L
i = i + 1;
if i > obj.capacity
i = 1;
end
queue(i_) = obj.storage(i);
end
end
%% doubles the queue size
function [] = resize(obj)
[queue,L] = obj.q2vec();
obj.capacity = obj.capacity * 2;
obj.storage = [queue zeros(1,obj.capacity-L)];
obj.ini = 1;
obj.fin = L;
end
%% tells whether the queue is empty
function bool_val = isEmpty(obj)
bool_val = (obj.fin == 0 && obj.ini == 1);
end
%% enqueues a new element new_el
function [] = enqueue(obj,new_el)
if obj.length() == obj.capacity
obj.resize();
end
if obj.fin >= obj.ini && obj.fin < obj.capacity || ...
obj.fin < obj.ini-1 || obj.isEmpty
obj.fin = obj.fin + 1;
obj.storage(obj.fin) = new_el;
elseif obj.ini > 1 && obj.fin == obj.capacity
obj.fin = 1;
obj.storage(obj.fin) = new_el;
end
end
%% dequeues the first element enqueued
function [] = dequeue(obj)
if obj.isEmpty
return
end
if obj.ini == obj.fin
obj.reset();
return
end
obj.ini = obj.ini + 1;
if obj.ini > obj.capacity
obj.ini = 1;
end
end
%% returns the first element enqueued
function el = front(obj)
if obj.isEmpty
el = [];
return
end
el = obj.storage(obj.ini);
end
end
end