annotate core/tools/CQueue.m @ 0:cc4b1211e677 tip

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