1 /******************************************************************************
2 *
3 * Copyright (C) 2004-2008, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * Author: Alexey Krivonogov ( gentee )
11 *
12 ******************************************************************************/
13
14 /*-----------------------------------------------------------------------------
15 * Id: stack L "Stack"
16 *
17 * Summary: Stack. You can use variables of the #b(stack) type for working with
18 stacks. The #b(stack) type is inherited from the #b(arr) type. So,
19 you can also use #a(array, methods of the arr type).
20 *
21 * List: *Methods,stack_pop,stack_popval,stack_push,stack_top,
22 *Type,tstack
23 *
24 -----------------------------------------------------------------------------*/
25
26 /*-----------------------------------------------------------------------------
27 * Id: tstack T stack
28 *
29 * Summary: The main structure of the stack.
30 *
31 -----------------------------------------------------------------------------*/
32
33 type stack <inherit = arr> {
34 }
35
36 /*-----------------------------------------------------------------------------
37 * Id: stack_push F3
38 *
39 * Summary: Add an item to a stack.
40 *
41 * Return: The pointer to the added item.
42 *
43 -----------------------------------------------------------------------------*/
44
45 method uint stack.push
46 {
47 this.expand( 1 )
48 return this.index( *this - 1 )
49 }
50
51 /*-----------------------------------------------------------------------------
52 * Id: stack_push_1 FA
53 *
54 * Summary: The method adds a number to a stack.
55 *
56 * Params: val - Pushing uint or int number.
57 *
58 * Return: The added value is returned.
59 *
60 -----------------------------------------------------------------------------*/
61
62 method uint stack.push( uint val )
63 {
64 uint ind
65
66 ind = this.expand( 1 )
67 this.index( ind )->uint = val
68
69 return val
70 }
71
72 /*-----------------------------------------------------------------------------
73 * Id: stack_push_2 FA
74 *
75 * Summary: The method adds a string to a stack. The stack must be described as
76 #b( stack of str ).
77 *
78 * Params: val - Pushing string.
79 *
80 * Return: The added string is returned.
81 *
82 -----------------------------------------------------------------------------*/
83
84 method str stack.push( str val )
85 {
86 uint ptr = this.push()
87
88 ptr->str = val
89
90 return ptr->str
91 }
92
93 /*-----------------------------------------------------------------------------
94 * Id: stack_pop F3
95 *
96 * Summary: Extracting an item. The method deletes the top item from a stack.
97 *
98 * Return: The pointer to the next new top item.
99 *
100 -----------------------------------------------------------------------------*/
101
102 method uint stack.pop
103 {
104 uint count = *this
105
106 if count
107 {
108 this.del( --count )
109 }
110 return ?( count, this.index( count - 1 ), 0 )
111 }
112
113 /*-----------------------------------------------------------------------------
114 * Id: stack_top F3
115 *
116 * Summary: Get the top item in a stack.
117 *
118 * Return: The pointer to the top item.
119 *
120 -----------------------------------------------------------------------------*/
121
122 method uint stack.top
123 {
124 return ?( *this, this.index( *this - 1 ), 0 )
125 }
126
127 /*-----------------------------------------------------------------------------
128 * Id: stack_popval F3
129 *
130 * Summary: Extracting an number. The method extracts a number from a stack.
131 *
132 * Return: The number extracted from the stack is returned.
133 *
134 -----------------------------------------------------------------------------*/
135
136 method uint stack.popval
137 {
138 uint count = *this
139
140 if !count : return 0
141
142 uint ret = this.index( count - 1 )->uint
143 this.pop()
144
145 return ret
146 }
147
148 /*-----------------------------------------------------------------------------
149 ** Id: stack_pop_1 FA
150 *
151 * Summary: The method extracts a string from a stack. The stack must be
152 described as #b( stack of str ).
153 *
154 * Params: val - Result string.
155 *
156 * Return: #lng/retpar( val )
157 *
158 -----------------------------------------------------------------------------*/
159
160 method str stack.pop( str val )
161 {
162 uint count = *this
163
164 if !count : return val
165
166 val = this.index( count - 1 )->str
167 this.pop()
168
169 return val
170 }
171
172 /*
173 type stack <> {
174 arr sarr
175 }
176
177 operator uint *( stack left )
178 {
179 return *left.sarr
180 }
181
182 method uint stack.index( uint i )
183 {
184 return this.sarr.index( i )
185 }
186
187 method uint stack.push
188 {
189 this.sarr.expand( 1 )
190 return this.index( *this - 1 )
191 }
192
193 method uint stack.push( uint val )
194 {
195 uint ind
196
197 ind = this.sarr.expand( 1 )
198 this.index( ind )->uint = val
199
200 return val
201 }
202
203 method str stack.push( str val )
204 {
205 uint ptr = this.push()
206
207 ptr->str = val
208
209 return ptr->str
210 }
211
212 method uint stack.pop
213 {
214 uint count = *this
215
216 if count
217 {
218 count--
219 if type_hasdelete( this.sarr.itype )
220 {
221 type_delete( this.index( count ), this.sarr.itype )
222 }
223 this.sarr->buf.use -= this.sarr.isize
224 }
225 return ?( count, this.index( count - 1 ), 0 )
226 }
227
228 method uint stack.top
229 {
230 return ?( *this, this.index( *this - 1 ), 0 )
231 }
232
233 method uint stack.popval
234 {
235 uint count = *this
236
237 if !count : return 0
238
239 uint ret = this.index( count - 1 )->uint
240 this.pop()
241
242 return ret
243 }
244
245 method str stack.pop( str val )
246 {
247 uint count = *this
248
249 if !count : return val
250
251 val = this.index( count - 1 )->str
252 this.pop()
253
254 return val
255 }
256
257 // Метод для разрешения указания подтипа при описании переменной
258 method stack.oftype( uint itype )
259 {
260 this.sarr.oftype( itype )
261 }
262
263 method stack.clear
264 {
265 this.sarr.delete()
266 this.sarr->buf.use = 0
267 }
268 method stack.oftype( uint itype )
269 {
270 this->arr.oftype( itype )
271 }
272 */