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