root/doc/html/stack_8h-source.html @ 353

Revision 353, 17.8 kB (checked in by smidl, 16 years ago)

doc

RevLine 
[353]1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
3<title>mixpp: stack.h Source File</title>
4<link href="tabs.css" rel="stylesheet" type="text/css">
5<link href="doxygen.css" rel="stylesheet" type="text/css">
6</head><body>
7<!-- Generated by Doxygen 1.5.8 -->
8<script type="text/javascript">
9<!--
10function changeDisplayState (e){
11  var num=this.id.replace(/[^[0-9]/g,'');
12  var button=this.firstChild;
13  var sectionDiv=document.getElementById('dynsection'+num);
14  if (sectionDiv.style.display=='none'||sectionDiv.style.display==''){
15    sectionDiv.style.display='block';
16    button.src='open.gif';
17  }else{
18    sectionDiv.style.display='none';
19    button.src='closed.gif';
20  }
21}
22function initDynSections(){
23  var divs=document.getElementsByTagName('div');
24  var sectionCounter=1;
25  for(var i=0;i<divs.length-1;i++){
26    if(divs[i].className=='dynheader'&&divs[i+1].className=='dynsection'){
27      var header=divs[i];
28      var section=divs[i+1];
29      var button=header.firstChild;
30      if (button!='IMG'){
31        divs[i].insertBefore(document.createTextNode(' '),divs[i].firstChild);
32        button=document.createElement('img');
33        divs[i].insertBefore(button,divs[i].firstChild);
34      }
35      header.style.cursor='pointer';
36      header.onclick=changeDisplayState;
37      header.id='dynheader'+sectionCounter;
38      button.src='closed.gif';
39      section.id='dynsection'+sectionCounter;
40      section.style.display='none';
41      section.style.marginLeft='14px';
42      sectionCounter++;
43    }
44  }
45}
46window.onload = initDynSections;
47-->
48</script>
49<div class="navigation" id="top">
50  <div class="tabs">
51    <ul>
52      <li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
53      <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
54      <li><a href="modules.html"><span>Modules</span></a></li>
55      <li><a href="annotated.html"><span>Classes</span></a></li>
56      <li class="current"><a href="files.html"><span>Files</span></a></li>
57    </ul>
58  </div>
59  <div class="tabs">
60    <ul>
61      <li><a href="files.html"><span>File&nbsp;List</span></a></li>
62      <li><a href="globals.html"><span>File&nbsp;Members</span></a></li>
63    </ul>
64  </div>
65<h1>stack.h</h1><a href="stack_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001
66<a name="l00035"></a>00035 <span class="preprocessor">#ifndef STACK_H</span>
67<a name="l00036"></a>00036 <span class="preprocessor"></span><span class="preprocessor">#define STACK_H</span>
68<a name="l00037"></a>00037 <span class="preprocessor"></span>
69<a name="l00038"></a>00038 <span class="preprocessor">#include &lt;<a class="code" href="itassert_8h.html" title="Error handling functions - header file.">itpp/base/itassert.h</a>&gt;</span>
70<a name="l00039"></a>00039
71<a name="l00040"></a>00040
72<a name="l00041"></a>00041 <span class="keyword">namespace </span>itpp
73<a name="l00042"></a>00042 {
74<a name="l00043"></a>00043
75<a name="l00065"></a>00065 <span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;
76<a name="l00066"></a><a class="code" href="classitpp_1_1Stack.html">00066</a> <span class="keyword">class </span><a class="code" href="classitpp_1_1Stack.html" title="General stack class.">Stack</a>
77<a name="l00067"></a>00067 {
78<a name="l00068"></a>00068 <span class="keyword">public</span>:
79<a name="l00070"></a>00070   <a class="code" href="classitpp_1_1Stack.html#4cb90649da75b936c62765154a3a83dd" title="Default constructor.">Stack</a>();
80<a name="l00072"></a>00072   <a class="code" href="classitpp_1_1Stack.html#4cb90649da75b936c62765154a3a83dd" title="Default constructor.">Stack</a>(<span class="keywordtype">int</span> n);
81<a name="l00074"></a>00074   <a class="code" href="classitpp_1_1Stack.html#4cb90649da75b936c62765154a3a83dd" title="Default constructor.">Stack</a>(<span class="keyword">const</span> <a class="code" href="classitpp_1_1Stack.html" title="General stack class.">Stack&lt;T&gt;</a> &amp;s);
82<a name="l00076"></a>00076   <span class="keyword">virtual</span> <a class="code" href="classitpp_1_1Stack.html#16ce049c48699f4704b91000cbe223a7" title="Default destructor.">~Stack</a>();
83<a name="l00077"></a>00077
84<a name="l00079"></a>00079   T <a class="code" href="classitpp_1_1Stack.html#b37580fdfc0834fdefce08ed71543cc0" title="Pop the topmost element of the stack.">pop</a>();
85<a name="l00081"></a>00081   T <a class="code" href="classitpp_1_1Stack.html#cd617bc3ea3aa848a8390688986926e4" title="Peek at the topmost element of the stack, without removing it.">peek</a>() <span class="keyword">const</span>;
86<a name="l00083"></a>00083   <span class="keywordtype">void</span> <a class="code" href="classitpp_1_1Stack.html#7d367256a03d6cb42c8e19321b4a4ac4" title="Push an element at top of stack.">push</a>(T v);
87<a name="l00085"></a>00085   <span class="keywordtype">void</span> <a class="code" href="classitpp_1_1Stack.html#eec8bd657e5636f02472e8610231e557" title="Empty the stack.">clear</a>();
88<a name="l00086"></a>00086
89<a name="l00088"></a>00088   <span class="keywordtype">void</span> <a class="code" href="classitpp_1_1Stack.html#a2f0b3362f0fe86685756fd5015d0fa5" title="Assignment operator.">operator=</a>(<span class="keyword">const</span> <a class="code" href="classitpp_1_1Stack.html" title="General stack class.">Stack&lt;T&gt;</a> &amp;s);
90<a name="l00089"></a>00089
91<a name="l00091"></a><a class="code" href="classitpp_1_1Stack.html#3c8a6f4c9309682884234371b040b131">00091</a>   <span class="keywordtype">int</span> <a class="code" href="classitpp_1_1Stack.html#3c8a6f4c9309682884234371b040b131" title="Returns the maximum number of data elements the stack can store.">size</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> ndata; }
92<a name="l00093"></a><a class="code" href="classitpp_1_1Stack.html#53f0e07e95fca870bed08e44efce0d97">00093</a>   <span class="keywordtype">int</span> <a class="code" href="classitpp_1_1Stack.html#53f0e07e95fca870bed08e44efce0d97" title="Returns the number of data elements currently in the stack.">no_elements</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> valptr; }
93<a name="l00095"></a>00095   <span class="keywordtype">void</span> <a class="code" href="classitpp_1_1Stack.html#55bb36306697de98aec9da9b41c50e70" title="Resizing a Stack&amp;lt;T&amp;gt;.">set_size</a>(<span class="keywordtype">int</span> n, <span class="keywordtype">bool</span> copy = <span class="keyword">false</span>);
94<a name="l00096"></a>00096
95<a name="l00097"></a>00097 <span class="keyword">private</span>:
96<a name="l00098"></a>00098   <span class="keywordtype">int</span> valptr;
97<a name="l00099"></a>00099   <span class="keywordtype">int</span> ndata;
98<a name="l00100"></a>00100   T *data;
99<a name="l00101"></a>00101
100<a name="l00102"></a>00102 <span class="keyword">private</span>:
101<a name="l00103"></a>00103   <span class="keywordtype">void</span> alloc(<span class="keywordtype">int</span> n);
102<a name="l00104"></a>00104   <span class="keywordtype">void</span> free();
103<a name="l00105"></a>00105 };
104<a name="l00106"></a>00106
105<a name="l00107"></a>00107 <span class="comment">// --------------------------- Implementation starts here ----------------------------------</span>
106<a name="l00108"></a>00108
107<a name="l00109"></a>00109 <span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;
108<a name="l00110"></a><a class="code" href="classitpp_1_1Stack.html#4cb90649da75b936c62765154a3a83dd">00110</a> <a class="code" href="classitpp_1_1Stack.html#4cb90649da75b936c62765154a3a83dd" title="Default constructor.">Stack&lt;T&gt;::Stack</a>()
109<a name="l00111"></a>00111 {
110<a name="l00112"></a>00112   data = 0;
111<a name="l00113"></a>00113   ndata = 0;
112<a name="l00114"></a>00114   valptr = 0;
113<a name="l00115"></a>00115 }
114<a name="l00116"></a>00116
115<a name="l00117"></a>00117 <span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;
116<a name="l00118"></a><a class="code" href="classitpp_1_1Stack.html#67b6b5cc1dbd2ff12f56885ecf5b749d">00118</a> <a class="code" href="classitpp_1_1Stack.html#4cb90649da75b936c62765154a3a83dd" title="Default constructor.">Stack&lt;T&gt;::Stack</a>(<span class="keywordtype">int</span> n)
117<a name="l00119"></a>00119 {
118<a name="l00120"></a>00120   alloc(n);
119<a name="l00121"></a>00121   valptr = 0;
120<a name="l00122"></a>00122 }
121<a name="l00123"></a>00123
122<a name="l00124"></a>00124 <span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;
123<a name="l00125"></a><a class="code" href="classitpp_1_1Stack.html#237b05e0f8e473dbd0310200def68708">00125</a> <a class="code" href="classitpp_1_1Stack.html#4cb90649da75b936c62765154a3a83dd" title="Default constructor.">Stack&lt;T&gt;::Stack</a>(<span class="keyword">const</span> <a class="code" href="classitpp_1_1Stack.html" title="General stack class.">Stack&lt;T&gt;</a> &amp;s)
124<a name="l00126"></a>00126 {
125<a name="l00127"></a>00127   data = NULL;
126<a name="l00128"></a>00128   ndata = 0;
127<a name="l00129"></a>00129   valptr = s.<a class="code" href="classitpp_1_1Stack.html#c4fa464802226cabf8783a55ca1ba723">valptr</a>;
128<a name="l00130"></a>00130   alloc(s.<a class="code" href="classitpp_1_1Stack.html#9ee4835f0aa64053be51384a8990b78c">ndata</a>);
129<a name="l00131"></a>00131   <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; s.<a class="code" href="classitpp_1_1Stack.html#9ee4835f0aa64053be51384a8990b78c">ndata</a>; i++)
130<a name="l00132"></a>00132     data[i] = s.<a class="code" href="classitpp_1_1Stack.html#2ff2a8efd5e33763b7f0b8476de87ac3">data</a>[i];
131<a name="l00133"></a>00133 }
132<a name="l00134"></a>00134
133<a name="l00135"></a>00135 <span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;
134<a name="l00136"></a><a class="code" href="classitpp_1_1Stack.html#16ce049c48699f4704b91000cbe223a7">00136</a> <a class="code" href="classitpp_1_1Stack.html#16ce049c48699f4704b91000cbe223a7" title="Default destructor.">Stack&lt;T&gt;::~Stack</a>()
135<a name="l00137"></a>00137 {
136<a name="l00138"></a>00138   free();
137<a name="l00139"></a>00139 }
138<a name="l00140"></a>00140
139<a name="l00141"></a>00141 <span class="keyword">template</span> &lt;<span class="keyword">class</span> T&gt;
140<a name="l00142"></a><a class="code" href="classitpp_1_1Stack.html#b37580fdfc0834fdefce08ed71543cc0">00142</a> T <a class="code" href="classitpp_1_1Stack.html#b37580fdfc0834fdefce08ed71543cc0" title="Pop the topmost element of the stack.">Stack&lt;T&gt;::pop</a>()
141<a name="l00143"></a>00143 {
142<a name="l00144"></a>00144   <a class="code" href="group__errorhandlingfunc.html#g0dfeed1c189885b05ebebe78abcc9785" title="Abort if t is true.">it_error_if</a>(valptr == 0, <span class="stringliteral">"Stack&lt;T&gt;::pop: Empty stack"</span>);
143<a name="l00145"></a>00145   valptr--;
144<a name="l00146"></a>00146   <span class="keywordflow">return</span> data[valptr];
145<a name="l00147"></a>00147 }
146<a name="l00148"></a>00148
147<a name="l00149"></a>00149 <span class="keyword">template</span> &lt;<span class="keyword">class</span> T&gt;
148<a name="l00150"></a><a class="code" href="classitpp_1_1Stack.html#cd617bc3ea3aa848a8390688986926e4">00150</a> T <a class="code" href="classitpp_1_1Stack.html#cd617bc3ea3aa848a8390688986926e4" title="Peek at the topmost element of the stack, without removing it.">Stack&lt;T&gt;::peek</a>()<span class="keyword"> const</span>
149<a name="l00151"></a>00151 <span class="keyword"></span>{
150<a name="l00152"></a>00152   <a class="code" href="group__errorhandlingfunc.html#g0dfeed1c189885b05ebebe78abcc9785" title="Abort if t is true.">it_error_if</a>(valptr == 0, <span class="stringliteral">"Stack&lt;T&gt;::peek: Empty stack"</span>);
151<a name="l00153"></a>00153   <span class="keywordflow">return</span> data[valptr-1];
152<a name="l00154"></a>00154 }
153<a name="l00155"></a>00155
154<a name="l00156"></a>00156 <span class="keyword">template</span> &lt;<span class="keyword">class</span> T&gt;
155<a name="l00157"></a><a class="code" href="classitpp_1_1Stack.html#7d367256a03d6cb42c8e19321b4a4ac4">00157</a> <span class="keywordtype">void</span> <a class="code" href="classitpp_1_1Stack.html#7d367256a03d6cb42c8e19321b4a4ac4" title="Push an element at top of stack.">Stack&lt;T&gt;::push</a>(T v)
156<a name="l00158"></a>00158 {
157<a name="l00159"></a>00159   <a class="code" href="group__errorhandlingfunc.html#g0dfeed1c189885b05ebebe78abcc9785" title="Abort if t is true.">it_error_if</a>(valptr &gt;= ndata, <span class="stringliteral">"Stack&lt;T&gt;::push: Full stack"</span>);
158<a name="l00160"></a>00160   data[valptr] = v;
159<a name="l00161"></a>00161   valptr++;
160<a name="l00162"></a>00162 }
161<a name="l00163"></a>00163
162<a name="l00164"></a>00164 <span class="keyword">template</span> &lt;<span class="keyword">class</span> T&gt;
163<a name="l00165"></a><a class="code" href="classitpp_1_1Stack.html#eec8bd657e5636f02472e8610231e557">00165</a> <span class="keywordtype">void</span> <a class="code" href="classitpp_1_1Stack.html#eec8bd657e5636f02472e8610231e557" title="Empty the stack.">Stack&lt;T&gt;::clear</a>()
164<a name="l00166"></a>00166 {
165<a name="l00167"></a>00167   valptr = 0;
166<a name="l00168"></a>00168 }
167<a name="l00169"></a>00169
168<a name="l00170"></a>00170 <span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;
169<a name="l00171"></a>00171 <span class="keywordtype">void</span> <a class="code" href="classitpp_1_1Stack.html" title="General stack class.">Stack&lt;T&gt;::alloc</a>(<span class="keywordtype">int</span> n)
170<a name="l00172"></a>00172 {
171<a name="l00173"></a>00173   <span class="keywordflow">if</span> (n == 0) {
172<a name="l00174"></a>00174     data = NULL;
173<a name="l00175"></a>00175     ndata = 0;
174<a name="l00176"></a>00176   }
175<a name="l00177"></a>00177   <span class="keywordflow">else</span> {
176<a name="l00178"></a>00178     data = <span class="keyword">new</span> T[n];
177<a name="l00179"></a>00179     <a class="code" href="group__errorhandlingfunc.html#gb319550e696ee9d824d23c2a176bc3a6" title="Abort if t is not true and NDEBUG is not defined.">it_assert_debug</a>(data != 0, <span class="stringliteral">"Out of memory in Stack::alloc"</span>);
178<a name="l00180"></a>00180   }
179<a name="l00181"></a>00181   ndata = n;
180<a name="l00182"></a>00182 }
181<a name="l00183"></a>00183
182<a name="l00184"></a>00184 <span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;
183<a name="l00185"></a>00185 <span class="keywordtype">void</span> Stack&lt;T&gt;::free()
184<a name="l00186"></a>00186 {
185<a name="l00187"></a>00187
186<a name="l00188"></a>00188   <span class="keyword">delete</span> [] data;
187<a name="l00189"></a>00189
188<a name="l00190"></a>00190   data = 0;
189<a name="l00191"></a>00191   ndata = 0;
190<a name="l00192"></a>00192 }
191<a name="l00193"></a>00193
192<a name="l00194"></a>00194 <span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;
193<a name="l00195"></a><a class="code" href="classitpp_1_1Stack.html#a2f0b3362f0fe86685756fd5015d0fa5">00195</a> <span class="keywordtype">void</span> <a class="code" href="classitpp_1_1Stack.html#a2f0b3362f0fe86685756fd5015d0fa5" title="Assignment operator.">Stack&lt;T&gt;::operator=</a>(<span class="keyword">const</span> <a class="code" href="classitpp_1_1Stack.html" title="General stack class.">Stack&lt;T&gt;</a> &amp;s)
194<a name="l00196"></a>00196 {
195<a name="l00197"></a>00197   <a class="code" href="classitpp_1_1Stack.html#55bb36306697de98aec9da9b41c50e70" title="Resizing a Stack&amp;lt;T&amp;gt;.">set_size</a>(s.<a class="code" href="classitpp_1_1Stack.html#9ee4835f0aa64053be51384a8990b78c">ndata</a>);
196<a name="l00198"></a>00198   <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; ndata; i++)
197<a name="l00199"></a>00199     data[i] = s.<a class="code" href="classitpp_1_1Stack.html#2ff2a8efd5e33763b7f0b8476de87ac3">data</a>[i];
198<a name="l00200"></a>00200   valptr = 0;
199<a name="l00201"></a>00201 }
200<a name="l00202"></a>00202
201<a name="l00203"></a>00203 <span class="keyword">template</span>&lt;<span class="keyword">class</span> T&gt;
202<a name="l00204"></a><a class="code" href="classitpp_1_1Stack.html#55bb36306697de98aec9da9b41c50e70">00204</a> <span class="keywordtype">void</span> <a class="code" href="classitpp_1_1Stack.html#55bb36306697de98aec9da9b41c50e70" title="Resizing a Stack&amp;lt;T&amp;gt;.">Stack&lt;T&gt;::set_size</a>(<span class="keywordtype">int</span> sz, <span class="keywordtype">bool</span> copy)
203<a name="l00205"></a>00205 {
204<a name="l00206"></a>00206   <span class="keywordtype">int</span> i, <a class="code" href="group__protocol.html#g27dde5be5aac902c0de838b4b872cd2f" title="ADD DOCUMENTATION HERE.">min</a>;
205<a name="l00207"></a>00207   T *tmp;
206<a name="l00208"></a>00208
207<a name="l00209"></a>00209   <span class="keywordflow">if</span> (ndata == sz)
208<a name="l00210"></a>00210     <span class="keywordflow">return</span>;
209<a name="l00211"></a>00211
210<a name="l00212"></a>00212   <span class="keywordflow">if</span> (copy) {
211<a name="l00213"></a>00213     tmp = data;
212<a name="l00214"></a>00214     min = ndata &lt; sz ? ndata : sz;
213<a name="l00215"></a>00215     alloc(sz);
214<a name="l00216"></a>00216     <span class="keywordflow">for</span> (i = 0; i &lt; min; i++)
215<a name="l00217"></a>00217       data[i] = tmp[i];
216<a name="l00218"></a>00218     <span class="keyword">delete</span> [] tmp;
217<a name="l00219"></a>00219   }
218<a name="l00220"></a>00220   <span class="keywordflow">else</span> {
219<a name="l00221"></a>00221     free();
220<a name="l00222"></a>00222     alloc(sz);
221<a name="l00223"></a>00223   }
222<a name="l00224"></a>00224   ndata = sz;
223<a name="l00225"></a>00225 }
224<a name="l00226"></a>00226
225<a name="l00227"></a>00227 } <span class="comment">// namespace itpp</span>
226<a name="l00228"></a>00228
227<a name="l00229"></a>00229 <span class="preprocessor">#endif // #ifndef STACK_H</span>
228</pre></div></div>
229<hr size="1"><address style="text-align: right;"><small>Generated on Tue Jun 2 10:02:13 2009 for mixpp by&nbsp;
230<a href="http://www.doxygen.org/index.html">
231<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.8 </small></address>
232</body>
233</html>
Note: See TracBrowser for help on using the browser.