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
|
#define error(X) throw( ({ (X), backtrace()[0..sizeof(backtrace())-2] }) )
class stack {
int ptr;
array arr;
void push(mixed val)
{
if(ptr == sizeof(arr)) {
arr += allocate(ptr);
}
arr[ptr++] = val;
}
mixed top()
{
if (ptr) {
return(arr[ptr-1]);
}
error("Stack underflow\n");
}
void quick_pop(void|int val)
{
if (val) {
if (ptr < val) {
ptr = 0;
} else {
ptr -= val;
}
} else {
if (ptr > 0) {
ptr--;
}
}
}
mixed pop(void|int val)
{
mixed foo;
if (val) {
if (ptr <= 0) {
error("Stack underflow\n");
}
if (ptr < val) {
val = ptr;
}
ptr -= val;
foo = arr[ptr..ptr + val - 1];
for (int i=0; i < val; i++) {
arr[ptr + i] = 0; /* Don't waste references */
}
} else {
if(--ptr < 0)
error("Stack underflow\n");
foo=arr[ptr];
arr[ptr]=0; /* Don't waste references */
}
return foo;
}
void reset(int|void initial_size)
{
arr = allocate(initial_size || 32);
ptr = 0;
}
void create(int|void initial_size)
{
arr = allocate(initial_size || 32);
}
};
|