165 lines
4.4 KiB
Plaintext
165 lines
4.4 KiB
Plaintext
import mem:*;
|
|
import util:*;
|
|
import io:*;
|
|
|
|
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 = 0
|
|
}
|
|
|
|
fun operator=(other:vector<T>):void {
|
|
resize(other.size)
|
|
for (var i = 0; i < other.size; i++;)
|
|
set(i, other.get(i))
|
|
}
|
|
|
|
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: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 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; }
|
|
|
|
fun set(index: int, dataIn: T): void {
|
|
if (index < 0 || index >= size)
|
|
return;
|
|
data[index] = dataIn;
|
|
}
|
|
fun add(dataIn: T): void { addEnd(dataIn); }
|
|
fun addEnd(dataIn: T): void {
|
|
size++;
|
|
if (size >= available)
|
|
resize(size*2);
|
|
maybe_copy_construct(&data[size-1], &dataIn);
|
|
}
|
|
fun do(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 flatten_map<U,V>(func: fun(T,V):vector<U>, extraParam:V):vector<U> {
|
|
var newVec.construct(): vector<U>
|
|
for (var i = 0; i < size; i++;) {
|
|
var to_add = func(data[i], extraParam)
|
|
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 filter<U>(func: fun(T,U):bool, extraParam: U):vector<T> {
|
|
var newVec.construct(): vector<T>
|
|
for (var i = 0; i < size; i++;)
|
|
if (func(data[i], extraParam))
|
|
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
|
|
}
|
|
};
|
|
|