74 lines
1.6 KiB
Plaintext
74 lines
1.6 KiB
Plaintext
import vector
|
|
import io
|
|
|
|
fun map<T,U>(): map<T,U> {
|
|
var toRet.construct(): map<T,U>
|
|
return toRet
|
|
}
|
|
fun map<T,U>(key:T, value:U): map<T,U> {
|
|
var toRet.construct(): map<T,U>
|
|
toRet.set(key, value)
|
|
return toRet
|
|
}
|
|
|
|
obj map<T,U> (Object) {
|
|
var keys: vector::vector<T>
|
|
var values: vector::vector<U>
|
|
|
|
fun construct() {
|
|
keys.construct()
|
|
values.construct()
|
|
}
|
|
fun copy_construct(old: *map<T,U>) {
|
|
keys.copy_construct(&old->keys)
|
|
values.copy_construct(&old->values)
|
|
}
|
|
fun operator=(rhs: map<T,U>) {
|
|
destruct()
|
|
copy_construct(&rhs)
|
|
}
|
|
fun destruct() {
|
|
keys.destruct()
|
|
values.destruct()
|
|
}
|
|
fun operator[]=(key: T, value: U) {
|
|
set(key,value)
|
|
}
|
|
fun set(key: T, value: U) {
|
|
var keyIdx = keys.find(key)
|
|
if (keyIdx >= 0) {
|
|
values.set(keyIdx, value)
|
|
return;
|
|
}
|
|
keys.add(key)
|
|
values.add(value)
|
|
}
|
|
fun contains_key(key: T): bool {
|
|
return keys.contains(key)
|
|
}
|
|
fun get(key: T): ref U {
|
|
return values.get(keys.find(key))
|
|
}
|
|
fun remove(key: T) {
|
|
var idx = keys.find(key)
|
|
if (idx < 0) {
|
|
io::println("trying to remove nonexistant key-value!")
|
|
return;
|
|
}
|
|
keys.remove(idx)
|
|
values.remove(idx)
|
|
}
|
|
fun clear() {
|
|
keys.clear()
|
|
values.clear()
|
|
}
|
|
fun operator[](key: T): ref U {
|
|
return get(key)
|
|
}
|
|
fun for_each(func: fun(T, U):void) {
|
|
for (var i = 0; i < keys.size; i++;)
|
|
func(keys[i], values[i])
|
|
}
|
|
}
|
|
|