205 lines
5.4 KiB
Plaintext
205 lines
5.4 KiB
Plaintext
import mem:*;
|
|
import util:*;
|
|
import io:*;
|
|
|
|
fun vector<T>():vector<T> {
|
|
var out.construct():vector<T>
|
|
return out
|
|
}
|
|
|
|
fun vector<T>(in:T):vector<T> {
|
|
var out.construct():vector<T>
|
|
out.add(in)
|
|
return out
|
|
}
|
|
|
|
obj vector<T> (Object) {
|
|
var data: *T;
|
|
var size: int;
|
|
var available: int;
|
|
|
|
fun construct(): *vector<T> {
|
|
size = 0;
|
|
available = 8;
|
|
data = new<T>(8);
|
|
return this;
|
|
}
|
|
|
|
fun construct(newSize: int): *vector<T>{
|
|
size = newSize;
|
|
available = newSize;
|
|
|
|
data = new<T>(newSize);
|
|
return this;
|
|
}
|
|
|
|
fun copy_construct(old: *vector<T>): void {
|
|
construct()
|
|
for (var i = 0; i < old->size; i++;)
|
|
addEnd(old->get(i))
|
|
}
|
|
|
|
fun destruct(): void {
|
|
if (data)
|
|
delete(data, size);
|
|
//data = 1337
|
|
data = 0
|
|
}
|
|
|
|
fun operator=(other:vector<T>):void {
|
|
destruct()
|
|
copy_construct(&other)
|
|
}
|
|
|
|
fun operator+(other:vector<T>):vector<T> {
|
|
var newVec.construct():vector<T>
|
|
for (var i = 0; i < size; i++;)
|
|
newVec.addEnd(get(i))
|
|
for (var i = 0; i < other.size; i++;)
|
|
newVec.addEnd(other.get(i))
|
|
return newVec
|
|
}
|
|
|
|
fun operator+=(other: T):void {
|
|
addEnd(other)
|
|
}
|
|
|
|
fun operator+=(other:vector<T>):void {
|
|
for (var i = 0; i < other.size; i++;)
|
|
addEnd(other.get(i))
|
|
}
|
|
|
|
fun clone(): vector<T> {
|
|
var newVec.construct(): vector<T>
|
|
for (var i = 0; i < size; i++;)
|
|
newVec.addEnd(data[i])
|
|
return newVec
|
|
}
|
|
|
|
fun resize(newSize: int): bool {
|
|
var newData: *T = new<T>(newSize);
|
|
if (!newData)
|
|
return false;
|
|
for (var i: int = 0; i < lesser<int>(size, newSize); i++;)
|
|
maybe_copy_construct(&newData[i], &data[i]);
|
|
delete(data, size);
|
|
data = newData;
|
|
available = newSize;
|
|
size = lesser(size, newSize)
|
|
return true;
|
|
}
|
|
|
|
fun slice(start: int, end: int): vector<T> {
|
|
var new.construct(): vector<T>
|
|
if (start < 0)
|
|
start += size + 1
|
|
if (end < 0)
|
|
end += size + 1
|
|
for (var i = start; i < end; i++;)
|
|
new.add(data[i])
|
|
return new
|
|
}
|
|
|
|
fun at(index: int): T { return get(index); }
|
|
fun operator[](index: int): T { return get(index); }
|
|
fun get(index: int): T {
|
|
if (index < 0 || index >= size) {
|
|
println("Vector access out of bounds! Retuning 0th element as sanest option");
|
|
print("Vector tried to access element: ");
|
|
println(index);
|
|
print("Max Index of vector: ");
|
|
println(size-1);
|
|
return data[0];
|
|
}
|
|
return data[index];
|
|
}
|
|
|
|
fun getBackingMemory(): *T { return data; }
|
|
|
|
// This is a template for the interesting reason that structs
|
|
// can not be compared for equality in C, and maybe we haven't defined equality
|
|
// on an object that we want to put in a vector. In this way we avoid the problem
|
|
// by not generating this function unless it's called - we also get the ability to
|
|
// do a find index using a different type, which could be fun.
|
|
fun find<U>(value: U): int {
|
|
for (var i = 0; i < size; i++;)
|
|
if (data[i] == value)
|
|
return i;
|
|
return -1;
|
|
}
|
|
|
|
fun operator[]=(index: int, dataIn: T) {
|
|
set(index, dataIn)
|
|
}
|
|
fun set(index: int, dataIn: T): void {
|
|
if (index < 0 || index >= size)
|
|
return;
|
|
data[index] = dataIn;
|
|
}
|
|
fun add_all(dataIn: vector<T>): void {
|
|
for (var i = 0; i < dataIn.size; i++;)
|
|
addEnd(dataIn[i]);
|
|
}
|
|
fun add(dataIn: T): void { addEnd(dataIn); }
|
|
fun addEnd(dataIn: T): void {
|
|
if (size+1 >= available)
|
|
resize((size+1)*2);
|
|
maybe_copy_construct(&data[size], &dataIn);
|
|
size++;
|
|
}
|
|
|
|
fun remove(index: int) {
|
|
maybe_destruct(&data[index])
|
|
for (var i = index+1; i < size; i++;) {
|
|
maybe_copy_construct(&data[i-1], &data[i])
|
|
maybe_destruct(&data[i])
|
|
}
|
|
size--
|
|
}
|
|
|
|
fun for_each(func: fun(T):void):void {
|
|
for (var i = 0; i < size; i++;)
|
|
func(data[i])
|
|
}
|
|
fun in_place(func: fun(T):T):void {
|
|
for (var i = 0; i < size; i++;)
|
|
data[i] = func(data[i])
|
|
}
|
|
fun map<U>(func: fun(T):U):vector<U> {
|
|
var newVec.construct(): vector<U>
|
|
for (var i = 0; i < size; i++;)
|
|
newVec.addEnd(func(data[i]))
|
|
return newVec
|
|
}
|
|
fun flatten_map<U>(func: fun(T):vector<U>):vector<U> {
|
|
var newVec.construct(): vector<U>
|
|
for (var i = 0; i < size; i++;) {
|
|
var to_add = func(data[i])
|
|
for (var j = 0; j < to_add.size; j++;)
|
|
newVec.addEnd(to_add.get(j))
|
|
}
|
|
return newVec
|
|
}
|
|
fun filter(func: fun(T):bool):vector<T> {
|
|
var newVec.construct(): vector<T>
|
|
for (var i = 0; i < size; i++;)
|
|
if (func(data[i]))
|
|
newVec.addEnd(data[i])
|
|
return newVec
|
|
}
|
|
fun any_true(func: fun(T):bool):bool {
|
|
for (var i = 0; i < size; i++;)
|
|
if (func(data[i]))
|
|
return true
|
|
return false
|
|
}
|
|
fun max(func: fun(T,T):bool): T {
|
|
var maxIdx = 0
|
|
for (var i = 1; i < size; i++;)
|
|
if (func(data[maxIdx], data[i]))
|
|
maxIdx = i
|
|
return data[maxIdx]
|
|
}
|
|
};
|
|
|