Skip to content
Snippets Groups Projects
Commit 7c4c3829 authored by Henrik (Grubba) Grubbström's avatar Henrik (Grubba) Grubbström
Browse files

Added quick_pop().

The initial stack size can now be specified to create() and reset().

Rev: lib/modules/Stack.pmod:1.4
parent 016b4ba6
No related branches found
No related tags found
No related merge requests found
...@@ -2,11 +2,13 @@ ...@@ -2,11 +2,13 @@
class stack { class stack {
int ptr; int ptr;
mixed *arr=allocate(32); mixed *arr;
void push(mixed val) void push(mixed val)
{ {
if(ptr==sizeof(arr)) arr+=allocate(ptr); if(ptr == sizeof(arr)) {
arr += allocate(ptr);
}
arr[ptr++] = val; arr[ptr++] = val;
} }
...@@ -18,6 +20,21 @@ class stack { ...@@ -18,6 +20,21 @@ class stack {
error("Stack underflow\n"); 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 pop(void|int val)
{ {
mixed foo; mixed foo;
...@@ -46,10 +63,15 @@ class stack { ...@@ -46,10 +63,15 @@ class stack {
return foo; return foo;
} }
void reset() void reset(int|void initial_size)
{ {
arr=allocate(32); arr = allocate(initial_size || 32);
ptr = 0; ptr = 0;
} }
void create(int|void initial_size)
{
arr = allocate(initial_size || 32);
}
}; };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment