Module agora.script.Stack

Contains a stack implementation for use with the script execution engine.

It uses a linked-list rather than a vector to avoid unnecessary copying due to stomping prevention as the same item may be popped and later pushed to the stack. In addition, this makes it very cheap to copy the stack as all internal items are immutable anyway.

The stack must be initialized with a set of size constraints, the maximum size of the stack, and the maximum size of any one item on the stack.

Example

import std.array;
const StackMaxTotalSize = 16_384;
const StackMaxItemSize = 512;
Stack stack = Stack(StackMaxTotalSize, StackMaxItemSize);
assert(stack.empty());
assert(stack.count() == 0);
assert(stack.used_bytes == 0);
stack.push([1, 2, 3]);
assert(stack.count() == 1);
assert(stack.used_bytes == 3);
stack.push([255]);
assert(stack.count() == 2);
assert(stack.used_bytes == 4);
assert(stack.peek() == [255]);
assert(stack.count() == 2);     // did not consume
assert(stack.peek() == [255]);  // ditto
assert(stack[].array == [[255], [1, 2, 3]]);
assert(!stack.empty());
// copies disabled: either use 'ref' or explicitly do a 'copy()'
static assert(!is(typeof( { Stack nogo = stack; } )));
Stack copy = stack.copy();
assert(copy.StackMaxTotalSize == stack.StackMaxTotalSize);
assert(copy.StackMaxItemSize == stack.StackMaxItemSize);
assert(copy.stack.empty() == stack.stack.empty());
assert(copy.num_items == stack.num_items);
assert(copy.used_bytes == stack.used_bytes);
assert(stack.pop() == [255]);
assert(stack.count() == 1);
assert(stack.used_bytes == 3);
assert(!stack.empty());
assert(stack.pop() == [1, 2, 3]);
assert(stack.count() == 0);
assert(stack.used_bytes == 0);
assert(stack.empty());
assert(copy.count() == 2);     // did not consume copy
assert(copy.used_bytes == 4);  // ditto
assert(!copy.empty());         // ditto
assert(stack.canPush(ubyte(42).repeat(100).array));
assert(!stack.canPush(ubyte(42).repeat(StackMaxItemSize + 1).array));

// overflow checks
Stack over = Stack(4, 2);
assert(!over.canPush([1, 2, 3]));  // item overflow
over.push([1, 2]);
over.push([1, 2]);
assert(!over.canPush([1, 2]));  // stack overflow

Structs

NameDescription
Stack