mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
36 lines
721 B
C
36 lines
721 B
C
module stack <Type>;
|
|
// Above: the parameterized type is applied to the entire module.
|
|
import std::mem;
|
|
|
|
struct Stack
|
|
{
|
|
usize capacity;
|
|
usize size;
|
|
Type* elems;
|
|
}
|
|
|
|
// The type methods offers dot syntax calls,
|
|
// so this function can either be called
|
|
// Stack.push(&my_stack, ...) or
|
|
// my_stack.push(...)
|
|
fn void Stack.push(Stack* this, Type element)
|
|
{
|
|
if (this.capacity == this.size)
|
|
{
|
|
this.capacity *= 2;
|
|
this.elems = mem::realloc(this.elems, $sizeof(Type) * this.capacity);
|
|
}
|
|
this.elems[this.size++] = element;
|
|
}
|
|
|
|
fn Type Stack.pop(Stack* this)
|
|
{
|
|
assert(this.size > 0);
|
|
return this.elems[--this.size];
|
|
}
|
|
|
|
fn bool Stack.empty(Stack* this)
|
|
{
|
|
return !this.size;
|
|
}
|