annotate core/tools/CStack.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 classdef CStack < handle
wolffd@0 2 % CStack define a stack data strcuture
wolffd@0 3 %
wolffd@0 4 % It likes java.util.Stack, however, it could use CStack.content() to
wolffd@0 5 % return all the data (in cells) of the Stack, and it is a litter faster
wolffd@0 6 % than java's Stack.
wolffd@0 7 %
wolffd@0 8 % s = CStack(c); c is a cells, and could be omitted
wolffd@0 9 % s.size() return the numble of element
wolffd@0 10 % s.isempty() return true when the stack is empty
wolffd@0 11 % s.empty() delete the content of the stack
wolffd@0 12 % s.push(el) push el to the top of stack
wolffd@0 13 % s.pop() pop out the top of the stack, and return the element
wolffd@0 14 % s.top() return the top element of the stack
wolffd@0 15 % s.remove() remove all the elements in the stack
wolffd@0 16 % s.content() return all the data of the stack (in the form of a
wolffd@0 17 % cells with size [s.size(), 1]
wolffd@0 18 %
wolffd@0 19 % See also CList, CQueue
wolffd@0 20 %
wolffd@0 21 % Copyright: zhang@zhiqiang.org, 2010.
wolffd@0 22 % url: http://zhiqiang.org/blog/it/matlab-data-structures.html
wolffd@0 23
wolffd@0 24 properties (Access = private)
wolffd@0 25 buffer % 一个cell数组,保存栈的数据
wolffd@0 26 cur % 当前元素位置, or the length of the stack
wolffd@0 27 capacity % 栈的容量,当容量不够时,容量扩充为2倍。
wolffd@0 28 end
wolffd@0 29
wolffd@0 30 methods
wolffd@0 31 function obj = CStack(c)
wolffd@0 32 if nargin >= 1 && iscell(c)
wolffd@0 33 obj.buffer = c(:);
wolffd@0 34 obj.cur = numel(c);
wolffd@0 35 obj.capacity = obj.cur;
wolffd@0 36 elseif nargin >= 1
wolffd@0 37 obj.buffer = cell(100, 1);
wolffd@0 38 obj.cur = 1;
wolffd@0 39 obj.capacity =100;
wolffd@0 40 obj.buffer{1} = c;
wolffd@0 41 else
wolffd@0 42 obj.buffer = cell(100, 1);
wolffd@0 43 obj.capacity = 100;
wolffd@0 44 obj.cur = 0;
wolffd@0 45 end
wolffd@0 46 end
wolffd@0 47
wolffd@0 48 function s = size(obj)
wolffd@0 49 s = obj.cur;
wolffd@0 50 end
wolffd@0 51
wolffd@0 52 function remove(obj)
wolffd@0 53 obj.cur = 0;
wolffd@0 54 end
wolffd@0 55
wolffd@0 56 function b = empty(obj)
wolffd@0 57 b = obj.cur;
wolffd@0 58 obj.cur = 0;
wolffd@0 59 end
wolffd@0 60
wolffd@0 61 function b = isempty(obj)
wolffd@0 62 b = ~logical(obj.cur);
wolffd@0 63 end
wolffd@0 64
wolffd@0 65 function push(obj, el)
wolffd@0 66 if obj.cur >= obj.capacity
wolffd@0 67 obj.buffer(obj.capacity+1:2*obj.capacity) = cell(obj.capacity, 1);
wolffd@0 68 obj.capacity = 2*obj.capacity;
wolffd@0 69 end
wolffd@0 70 obj.cur = obj.cur + 1;
wolffd@0 71 obj.buffer{obj.cur} = el;
wolffd@0 72 end
wolffd@0 73
wolffd@0 74 function el = top(obj)
wolffd@0 75 if obj.cur == 0
wolffd@0 76 el = [];
wolffd@0 77 warning('CStack:No_Data', 'trying to get top element of an emtpy stack');
wolffd@0 78 else
wolffd@0 79 el = obj.buffer{obj.cur};
wolffd@0 80 end
wolffd@0 81 end
wolffd@0 82
wolffd@0 83 function el = pop(obj)
wolffd@0 84 if obj.cur == 0
wolffd@0 85 el = [];
wolffd@0 86 warning('CStack:No_Data', 'trying to pop element of an emtpy stack');
wolffd@0 87 else
wolffd@0 88 el = obj.buffer{obj.cur};
wolffd@0 89 obj.cur = obj.cur - 1;
wolffd@0 90 end
wolffd@0 91 end
wolffd@0 92
wolffd@0 93 function display(obj)
wolffd@0 94 if obj.cur
wolffd@0 95 for i = 1:obj.cur
wolffd@0 96 disp([num2str(i) '-th element of the stack:']);
wolffd@0 97 disp(obj.buffer{i});
wolffd@0 98 end
wolffd@0 99 else
wolffd@0 100 disp('The stack is empty');
wolffd@0 101 end
wolffd@0 102 end
wolffd@0 103
wolffd@0 104 function c = content(obj)
wolffd@0 105 c = obj.buffer(1:obj.cur);
wolffd@0 106 end
wolffd@0 107 end
wolffd@0 108 end