94 lines
2.4 KiB
Plaintext
94 lines
2.4 KiB
Plaintext
import vector
|
|
import io
|
|
import serialize
|
|
import util
|
|
|
|
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, Serializable) {
|
|
var keys: vector::vector<T>
|
|
var values: vector::vector<U>
|
|
|
|
fun construct(): *map<T,U> {
|
|
keys.construct()
|
|
values.construct()
|
|
return this
|
|
}
|
|
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 serialize(): vector::vector<char> {
|
|
return serialize::serialize(keys) + serialize::serialize(values)
|
|
}
|
|
fun unserialize(it: ref vector::vector<char>, pos: int): int {
|
|
/*construct()*/
|
|
/*util::unpack(keys, pos) = serialize::unserialize<vector::vector<T>>(it, pos)*/
|
|
/*util::unpack(values, pos) = serialize::unserialize<vector::vector<U>>(it, pos)*/
|
|
pos = keys.unserialize(it, pos)
|
|
pos = values.unserialize(it, pos)
|
|
return pos
|
|
}
|
|
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))*/
|
|
var key_loc = keys.find(key)
|
|
if (key_loc == -1) {
|
|
io::println("trying to access nonexistant key-value!")
|
|
/*while (true) {}*/
|
|
}
|
|
return values.get(key_loc)
|
|
}
|
|
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])
|
|
}
|
|
}
|
|
|