
use mem

struct Lifo {
    data: ptr
    size: uint
    index: uint (0)

    static fn new() Lifo {
        let size : uint = 100
        return Lifo {
            size: size
            data: mem:alloc(size * size_of(ptr))
        }
    }

    fn add(item: ptr) {
        let index = this.index
        let new_index = index + 1
        this.index = new_index

        @ptrv(this.data, ptr, index) = item

        let size = this.size
        if new_index == size {
            let new_size = size * 2
            this.data = mem:resize(this.data, size * size_of(ptr), new_size * size_of(ptr))
            this.size = new_size
        }
    }
    fn pop() ptr !empty {
        let index = this.index
        if index == 0 : throw empty
        let new_index = index - 1
        this.index = new_index
        return @ptrv(this.data, ptr, new_index)
    }

    fn clear() {
        this.index = 0
    }

    fn free() {
        mem:free(this.data)
        mem:free(this)
    }
}
