comparison core/tools/CQueue.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 classdef CQueue < handle
2 % CQueue define a queue data strcuture
3 %
4 % It likes java.util.Queue, however, it could use CQueue.content() to
5 % return all the data (in cells) of the Queue, and it is a litter faster
6 % than java's Queue.
7 %
8 % q = CQueue(c); c is a cells, and could be omitted
9 % s.size() return the numble of element
10 % s.isempty() return true when the queue is empty
11 % s.empty() delete all the elements in the queue.
12 % s.push(el) push el to the top of qeueu
13 % s.pop() pop out the the beginning of queue, and return the element
14 % s.front() return the the the beginning element of the qeueu
15 % s.back() return the the the rear element of the qeueu
16 % s.remove() remove all data from the queue
17 % s.content() return all the data of the queue (in the form of a
18 % cells with size [s.size(), 1]
19 %
20 % See also CList, CStack
21 % Copyright: zhang@zhiqiang.org, 2010.
22 % url: http://zhiqiang.org/blog/it/matlab-data-structures.html
23
24 properties (Access = private)
25 buffer % a cell, to maintain the data
26 beg % the start position of the queue
27 rear % the end position of the queue
28 % the actually data is buffer(beg:rear-1)
29 end
30
31 properties (Access = public)
32 capacity % 栈的容量,当容量不够时,容量扩充为2倍。
33 end
34
35 methods
36 function obj = CQueue(c) % 初始化
37 if nargin >= 1 && iscell(c)
38 obj.buffer = [c(:); cell(numel(c), 1)];
39 obj.beg = 1;
40 obj.rear = numel(c) + 1;
41 obj.capacity = 2*numel(c);
42 elseif nargin >= 1
43 obj.buffer = cell(100, 1);
44 obj.buffer{1} = c;
45 obj.beg = 1;
46 obj.rear = 2;
47 obj.capacity = 100;
48 else
49 obj.buffer = cell(100, 1);
50 obj.capacity = 100;
51 obj.beg = 1;
52 obj.rear = 1;
53 end
54 end
55
56 function s = size(obj) % 队列长度
57 if obj.rear >= obj.beg
58 s = obj.rear - obj.beg;
59 else
60 s = obj.rear - obj.beg + obj.capacity;
61 end
62 end
63
64 function b = isempty(obj) % return true when the queue is empty
65 b = ~logical(obj.size());
66 end
67
68 function s = empty(obj) % clear all the data in the queue
69 s = obj.size();
70 obj.beg = 1;
71 obj.rear = 1;
72 end
73
74 function push(obj, el) % 压入新元素到队尾
75 if obj.size >= obj.capacity - 1
76 sz = obj.size();
77 if obj.rear >= obj.front
78 obj.buffer(1:sz) = obj.buffer(obj.beg:obj.rear-1);
79 else
80 obj.buffer(1:sz) = obj.buffer([obj.beg:obj.capacity 1:obj.rear-1]);
81 end
82 obj.buffer(sz+1:obj.capacity*2) = cell(obj.capacity*2-sz, 1);
83 obj.capacity = numel(obj.buffer);
84 obj.beg = 1;
85 obj.rear = sz+1;
86 end
87 obj.buffer{obj.rear} = el;
88 obj.rear = mod(obj.rear, obj.capacity) + 1;
89 end
90
91 function el = front(obj) % 返回队首元素
92 if obj.rear ~= obj.beg
93 el = obj.buffer{obj.beg};
94 else
95 el = [];
96 warning('CQueue:NO_DATA', 'try to get data from an empty queue');
97 end
98 end
99
100 function el = back(obj) % 返回队尾元素
101
102 if obj.rear == obj.beg
103 el = [];
104 warning('CQueue:NO_DATA', 'try to get data from an empty queue');
105 else
106 if obj.rear == 1
107 el = obj.buffer{obj.capacity};
108 else
109 el = obj.buffer{obj.rear - 1};
110 end
111 end
112
113 end
114
115 function el = pop(obj) % 弹出队首元素
116 if obj.rear == obj.beg
117 error('CQueue:NO_Data', 'Trying to pop an empty queue');
118 else
119 el = obj.buffer{obj.beg};
120 obj.beg = obj.beg + 1;
121 if obj.beg > obj.capacity, obj.beg = 1; end
122 end
123 end
124
125 function remove(obj) % 清空队列
126 obj.beg = 1;
127 obj.rear = 1;
128 end
129
130 function display(obj) % 显示队列
131 if obj.size()
132 if obj.beg <= obj.rear
133 for i = obj.beg : obj.rear-1
134 disp([num2str(i - obj.beg + 1) '-th element of the stack:']);
135 disp(obj.buffer{i});
136 end
137 else
138 for i = obj.beg : obj.capacity
139 disp([num2str(i - obj.beg + 1) '-th element of the stack:']);
140 disp(obj.buffer{i});
141 end
142 for i = 1 : obj.rear-1
143 disp([num2str(i + obj.capacity - obj.beg + 1) '-th element of the stack:']);
144 disp(obj.buffer{i});
145 end
146 end
147 else
148 disp('The queue is empty');
149 end
150 end
151
152 function c = content(obj) % 取出队列元素
153 if obj.rear >= obj.beg
154 c = obj.buffer(obj.beg:obj.rear-1);
155 else
156 c = obj.buffer([obj.beg:obj.capacity 1:obj.rear-1]);
157 end
158 end
159 end
160 end