1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/* struct::stack - critcl - layer 3 definitions.
*
* -> Method functions.
* Implementations for all stack methods.
*/
#include "util.h"
#include "m.h"
#include "s.h"
#include "ms.h"
/* .................................................. */
/*
*---------------------------------------------------------------------------
*
* stm_CLEAR --
*
* Removes all elements currently on the stack. I.e empties the stack.
*
* Results:
* A standard Tcl result code.
*
* Side effects:
* Only internal, memory allocation changes ...
*
*---------------------------------------------------------------------------
*/
int
stm_CLEAR (S* s, Tcl_Interp* interp, Tcl_Size objc, Tcl_Obj* CONST* objv)
{
/* Syntax: stack clear
* [0] [1]
*/
if (objc != 2) {
Tcl_WrongNumArgs (interp, 2, objv, NULL); /* OK tcl9 */
return TCL_ERROR;
}
/*
* Delete and recreate the stack memory. A combination of delete/new,
* except the main structure is left unchanged
*/
Tcl_DecrRefCount (s->stack);
s->max = 0;
s->stack = Tcl_NewListObj (0,NULL); /* OK tcl9 */
Tcl_IncrRefCount (s->stack);
return TCL_OK;
}
/*
*---------------------------------------------------------------------------
*
* stm_DESTROY --
*
* Destroys the whole stack object.
*
* Results:
* A standard Tcl result code.
*
* Side effects:
* Releases memory.
*
*---------------------------------------------------------------------------
*/
int
stm_DESTROY (S* s, Tcl_Interp* interp, Tcl_Size objc, Tcl_Obj* CONST* objv)
{
/* Syntax: stack destroy
* [0] [1]
*/
if (objc != 2) {
Tcl_WrongNumArgs (interp, 2, objv, NULL); /* OK tcl9 */
return TCL_ERROR;
}
Tcl_DeleteCommandFromToken(interp, s->cmd);
return TCL_OK;
}
/*
*---------------------------------------------------------------------------
*
* stm_GET --
*
* Non-destructively retrieves all elements of the stack.
*
* Results:
* A standard Tcl result code.
*
* Side effects:
* Only internal, memory allocation changes ...
*
*---------------------------------------------------------------------------
*/
int
stm_GET (S* s, Tcl_Interp* interp, Tcl_Size objc, Tcl_Obj* CONST* objv, int revers)
{
/* Syntax: stack get
* [0] [1]
*/
Tcl_Size n;
if (objc != 2) {
Tcl_WrongNumArgs (interp, 2, objv, NULL); /* OK tcl9 */
return TCL_ERROR;
}
Tcl_ListObjLength (interp, s->stack, &n); /* OK tcl9 */
if (n) {
return st_peek (s, interp, n, 0, 1, revers, 1
/* no pop, list all, <revers>, return result */);
} else {
Tcl_SetObjResult (interp, Tcl_NewListObj (0,NULL)); /* OK tcl9 */
return TCL_OK;
}
}
/*
*---------------------------------------------------------------------------
*
* stm_TRIM --
*
* Destructively retrieves one or more elements from the top of the
* stack, trims the stack to a new size.
*
* Results:
* A standard Tcl result code.
*
* Side effects:
* Only internal, memory allocation changes ...
*
*---------------------------------------------------------------------------
*/
int
stm_TRIM (S* s, Tcl_Interp* interp, Tcl_Size objc, Tcl_Obj* CONST* objv, int ret)
{
/* Syntax: stack trim N
* [0] [1] [2]
*/
Tcl_Size n, len;
if (objc != 3) {
Tcl_WrongNumArgs (interp, 2, objv, "newsize"); /* OK tcl9 */
return TCL_ERROR;
}
if (Tcl_GetSizeIntFromObj(interp, objv[2], &n) != TCL_OK) { /* OK tcl9 */
return TCL_ERROR;
} else if (n < 0) {
Tcl_AppendResult (interp, "invalid size ",
Tcl_GetString (objv[2]),
NULL);
return TCL_ERROR;
}
Tcl_ListObjLength (interp, s->stack, &len); /* OK tcl9 */
if (len > n) {
return st_peek (s, interp, len-n, 1, 1, 0, ret
/* pop, list all, normal order, <ret> */);
} else {
Tcl_SetObjResult (interp, Tcl_NewListObj (0,NULL)); /* OK tcl9 */
return TCL_OK;
}
}
/*
*---------------------------------------------------------------------------
*
* stm_PEEK/POP --
*
* (Non-)destructively retrieves one or more elements from the top of the
* stack.
*
* Results:
* A standard Tcl result code.
*
* Side effects:
* Only internal, memory allocation changes ...
*
*---------------------------------------------------------------------------
*/
int
stm_PEEK (S* s, Tcl_Interp* interp, Tcl_Size objc, Tcl_Obj* CONST* objv, int pop, int revers)
{
/* Syntax: stack peek|pop ?n?
* [0] [1] [2]
*/
Tcl_Size listc = 0;
Tcl_Obj** listv;
Tcl_Obj* r;
Tcl_Size n = 1;
if ((objc != 2) && (objc != 3)) {
Tcl_WrongNumArgs (interp, 2, objv, "?n?"); /* OK tcl9 */
return TCL_ERROR;
}
if (objc == 3) {
if (Tcl_GetSizeIntFromObj(interp, objv[2], &n) != TCL_OK) { /* OK tcl9 */
return TCL_ERROR;
} else if (n < 1) {
Tcl_AppendResult (interp, "invalid item count ",
Tcl_GetString (objv[2]),
NULL);
return TCL_ERROR;
}
}
return st_peek (s, interp, n, pop, 0, revers, 1
/* <pop>, single, <revers>, return result */);
}
/*
*---------------------------------------------------------------------------
*
* stm_PUSH --
*
* Adds one or more elements to the stack.
*
* Results:
* A standard Tcl result code.
*
* Side effects:
* May release and allocate memory.
*
*---------------------------------------------------------------------------
*/
int
stm_PUSH (S* s, Tcl_Interp* interp, Tcl_Size objc, Tcl_Obj* CONST* objv)
{
/* Syntax: stack push item...
* [0] [1] [2]
*/
Tcl_Size i;
if (objc < 3) {
Tcl_WrongNumArgs (interp, 2, objv, "item ?item ...?"); /* OK tcl9 */
return TCL_ERROR;
}
for (i = 2; i < objc; i++) {
Tcl_ListObjAppendElement (interp, s->stack, objv[i]);
}
return TCL_OK;
}
/*
*---------------------------------------------------------------------------
*
* stm_ROTATE --
*
* Rotates the N top elements of the stack by K steps.
*
* Results:
* A standard Tcl result code.
*
* Side effects:
* May release and allocate memory.
*
*---------------------------------------------------------------------------
*/
int
stm_ROTATE (S* s, Tcl_Interp* interp, Tcl_Size objc, Tcl_Obj* CONST* objv)
{
/* Syntax: stack rotate count steps
* [0] [1] [2] [3]
*/
Tcl_Size listc = 0, n, steps, start, i, j;
Tcl_Obj** listv = NULL;
Tcl_Obj** tmp = NULL;
if (objc != 4) {
Tcl_WrongNumArgs (interp, 2, objv, "count steps"); /* OK tcl9 */
return TCL_ERROR;
}
if (Tcl_GetSizeIntFromObj(interp, objv[2], &n) != TCL_OK) { /* OK tcl9 */
return TCL_ERROR;
}
if (Tcl_GetSizeIntFromObj(interp, objv[3], &steps) != TCL_OK) { /* OK tcl9 */
return TCL_ERROR;
}
Tcl_ListObjGetElements (interp, s->stack, &listc, &listv); /* OK tcl9 */
if (n > listc) {
Tcl_AppendResult (interp, "insufficient items on stack to fill request",
NULL);
return TCL_ERROR;
}
/* Begin rotation */
start = listc - n;
steps = steps % n;
while (steps < 0) steps += n;
steps = n - steps;
listv += start;
tmp = NALLOC(n,Tcl_Obj*);
for (i = 0; i < n; i++) {
j = (i + steps) % n;
ASSERT_BOUNDS (i,n);
ASSERT_BOUNDS (j,n);
tmp[i] = listv[j];
}
for (i = 0; i < n; i++) {
ASSERT_BOUNDS (i,n);
listv[i] = tmp [i];
}
ckfree ((char*) tmp);
return TCL_OK;
}
/*
*---------------------------------------------------------------------------
*
* stm_SIZE --
*
* Returns the number of elements currently held by the stack.
*
* Results:
* A standard Tcl result code.
*
* Side effects:
* None.
*
*---------------------------------------------------------------------------
*/
int
stm_SIZE (S* s, Tcl_Interp* interp, Tcl_Size objc, Tcl_Obj* CONST* objv)
{
/* Syntax: stack size
* [0] [1]
*/
Tcl_Size listc = 0;
if ((objc != 2)) {
Tcl_WrongNumArgs (interp, 2, objv, NULL); /* OK tcl9 */
return TCL_ERROR;
}
Tcl_ListObjLength (interp, s->stack, &listc); /* OK tcl9 */
Tcl_SetObjResult (interp, Tcl_NewSizeIntObj (listc));
return TCL_OK;
}
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|