110 lines
2.8 KiB
Plaintext
110 lines
2.8 KiB
Plaintext
import vec
|
|
import serialize
|
|
import util
|
|
|
|
fun stack_from_vector<T>(i: vec::vec<T>): stack<T> {
|
|
var to_ret.construct(): stack<T>
|
|
to_ret.data = i
|
|
return to_ret
|
|
}
|
|
|
|
fun stack<T>():stack<T> {
|
|
var out.construct():stack<T>
|
|
return out
|
|
}
|
|
fun stack<T>(in:T):stack<T> {
|
|
var out.construct():stack<T>
|
|
out.push(in)
|
|
return out
|
|
}
|
|
|
|
obj stack<T> (Object, Serializable) {
|
|
var data: vec::vec<T>
|
|
fun construct(): *stack<T> {
|
|
data.construct()
|
|
return this
|
|
}
|
|
fun construct(ammt: int): *stack<T> {
|
|
data.construct(ammt)
|
|
return this
|
|
}
|
|
fun copy_construct(other: *stack<T>) {
|
|
data.copy_construct(&other->data)
|
|
}
|
|
fun destruct() {
|
|
data.destruct()
|
|
}
|
|
fun operator=(other: ref stack<T>) {
|
|
data = other.data
|
|
}
|
|
fun serialize(): vec::vec<char> {
|
|
return serialize::serialize(data)
|
|
}
|
|
fun unserialize(it: ref vec::vec<char>, pos: int): int {
|
|
return data.unserialize(it, pos)
|
|
}
|
|
fun push(it: ref T) {
|
|
data.addEnd(it)
|
|
}
|
|
fun pop(): T {
|
|
var toRet = data[data.size-1]
|
|
data.remove(data.size-1)
|
|
return toRet
|
|
}
|
|
fun clear() {
|
|
data.clear()
|
|
}
|
|
fun top(): ref T {
|
|
return data[data.size-1]
|
|
}
|
|
fun from_top(num: int): ref T {
|
|
return data[(data.size-1)-num]
|
|
}
|
|
fun size(): int {
|
|
return data.size
|
|
}
|
|
fun available(): int {
|
|
return data.available
|
|
}
|
|
fun empty():bool {
|
|
return data.size == 0
|
|
}
|
|
fun for_each(func: fun(ref T):void) {
|
|
data.for_each(func)
|
|
}
|
|
fun for_each(func: fun(T):void) {
|
|
data.for_each(func)
|
|
}
|
|
fun for_each_reverse(func: fun(ref T):void) {
|
|
data.for_each_reverse(func)
|
|
}
|
|
fun for_each_reverse(func: fun(T):void) {
|
|
data.for_each_reverse(func)
|
|
}
|
|
fun reverse_vector(): vec::vec<T>
|
|
return data.reverse()
|
|
fun index_from_top_satisfying(func_raw: run(T):bool): int {
|
|
var temp_lambda = fun(i: T):bool { return func_raw(i); }
|
|
return index_from_top_satisfying(temp_lambda);
|
|
}
|
|
fun index_from_top_satisfying(func: fun(T):bool): int {
|
|
for (var i = 0; i < data.size; i++;)
|
|
if (func(from_top(i)))
|
|
return i
|
|
return -1
|
|
}
|
|
fun item_from_top_satisfying(func_raw: run(T):bool): T {
|
|
var temp_lambda = fun(i: T):bool { return func_raw(i); }
|
|
return item_from_top_satisfying(temp_lambda);
|
|
}
|
|
fun item_from_top_satisfying(func: fun(T):bool): T {
|
|
return from_top(index_from_top_satisfying(func))
|
|
}
|
|
fun item_from_top_satisfying_or(func: fun(T):bool, other: T): T {
|
|
var idx = index_from_top_satisfying(func)
|
|
if (idx != -1)
|
|
return from_top(idx)
|
|
return other
|
|
}
|
|
}
|