First run at a dead-simple map library. Writing this has reminded me of the need for a []= operator as well as automatic generation of functions for objects, which really should also include ==

This commit is contained in:
Nathan Braswell
2015-06-27 10:04:09 -04:00
parent f3c0c8a705
commit b18c18ec30
6 changed files with 112 additions and 1 deletions

View File

@@ -2,17 +2,33 @@ 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
}
fun vector<T>():vector<T> {
fun vector<T>(first:T, second:T):vector<T> {
var out.construct():vector<T>
out.add(first)
out.add(second)
return out
}
fun vector<T>(first:T, second:T, third:T):vector<T> {
var out.construct():vector<T>
out.add(first)
out.add(second)
out.add(third)
return out
}
obj vector<T> (Object) {
var data: T*;
var size: int;
@@ -112,6 +128,18 @@ obj vector<T> (Object) {
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_index<U>(value: U): int {
for (var i = 0; i < size; i++;)
if (data[i] == value)
return i;
return -1;
}
fun set(index: int, dataIn: T): void {
if (index < 0 || index >= size)
return;