2015-06-27 10:04:09 -04:00
|
|
|
import vector
|
2016-06-11 00:45:18 -07:00
|
|
|
import mem
|
2015-06-27 18:06:02 -04:00
|
|
|
import io
|
2015-08-26 03:45:34 -04:00
|
|
|
import serialize
|
|
|
|
|
import util
|
2015-06-27 10:04:09 -04:00
|
|
|
|
|
|
|
|
fun map<T,U>(): map<T,U> {
|
|
|
|
|
var toRet.construct(): map<T,U>
|
|
|
|
|
return toRet
|
|
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun map<T,U>(key: ref T, value: ref U): map<T,U> {
|
2015-06-27 10:04:09 -04:00
|
|
|
var toRet.construct(): map<T,U>
|
|
|
|
|
toRet.set(key, value)
|
|
|
|
|
return toRet
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-26 03:45:34 -04:00
|
|
|
obj map<T,U> (Object, Serializable) {
|
2015-06-27 10:04:09 -04:00
|
|
|
var keys: vector::vector<T>
|
2015-06-27 12:03:55 -04:00
|
|
|
var values: vector::vector<U>
|
2015-06-27 10:04:09 -04:00
|
|
|
|
2015-08-26 03:45:34 -04:00
|
|
|
fun construct(): *map<T,U> {
|
2015-06-27 10:04:09 -04:00
|
|
|
keys.construct()
|
|
|
|
|
values.construct()
|
2015-08-26 03:45:34 -04:00
|
|
|
return this
|
2015-06-27 10:04:09 -04:00
|
|
|
}
|
2015-07-04 17:02:51 -04:00
|
|
|
fun copy_construct(old: *map<T,U>) {
|
2015-06-27 10:04:09 -04:00
|
|
|
keys.copy_construct(&old->keys)
|
|
|
|
|
values.copy_construct(&old->values)
|
|
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun operator=(rhs: ref map<T,U>) {
|
|
|
|
|
keys = rhs.keys
|
|
|
|
|
values = rhs.values
|
2015-06-27 18:06:02 -04:00
|
|
|
}
|
2015-06-27 10:04:09 -04:00
|
|
|
fun destruct() {
|
|
|
|
|
keys.destruct()
|
|
|
|
|
values.destruct()
|
|
|
|
|
}
|
2015-08-26 03:45:34 -04:00
|
|
|
fun serialize(): vector::vector<char> {
|
|
|
|
|
return serialize::serialize(keys) + serialize::serialize(values)
|
|
|
|
|
}
|
|
|
|
|
fun unserialize(it: ref vector::vector<char>, pos: int): int {
|
2015-08-29 21:45:55 -04:00
|
|
|
pos = keys.unserialize(it, pos)
|
|
|
|
|
pos = values.unserialize(it, pos)
|
2015-08-26 03:45:34 -04:00
|
|
|
return pos
|
|
|
|
|
}
|
2015-12-28 03:34:40 -05:00
|
|
|
// the old unnecessary template to prevent generation
|
|
|
|
|
// if not used trick (in this case, changing out U with V)
|
|
|
|
|
fun operator==<V>(other: ref map<T,V>): bool {
|
|
|
|
|
return keys == other.keys && values == other.values
|
|
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun operator[]=(key: ref T, value: ref U) {
|
2015-06-27 11:46:31 -04:00
|
|
|
set(key,value)
|
|
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun set(key: ref T, value: ref U) {
|
2015-06-27 18:06:02 -04:00
|
|
|
var keyIdx = keys.find(key)
|
2015-06-27 10:04:09 -04:00
|
|
|
if (keyIdx >= 0) {
|
|
|
|
|
values.set(keyIdx, value)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
keys.add(key)
|
|
|
|
|
values.add(value)
|
|
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun contains_key(key: ref T): bool {
|
2015-07-07 00:46:00 -04:00
|
|
|
return keys.contains(key)
|
|
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun contains_value(value: ref U): bool {
|
2016-02-17 13:37:48 -05:00
|
|
|
return values.contains(value)
|
|
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun get(key: ref T): ref U {
|
2015-08-12 23:15:41 -04:00
|
|
|
var key_loc = keys.find(key)
|
2016-05-22 14:10:19 -07:00
|
|
|
if (key_loc == -1)
|
|
|
|
|
util::error("trying to access nonexistant key-value!")
|
2015-08-12 23:15:41 -04:00
|
|
|
return values.get(key_loc)
|
2015-06-27 18:06:02 -04:00
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun get_ptr_or_null(key: ref T): *U {
|
|
|
|
|
var key_loc = keys.find(key)
|
|
|
|
|
if (key_loc == -1)
|
|
|
|
|
return mem::null<U>()
|
|
|
|
|
return &values.get(key_loc)
|
|
|
|
|
}
|
|
|
|
|
fun get_with_default(key: ref T, default_val: ref U): ref U {
|
2016-02-15 16:31:01 -05:00
|
|
|
if (contains_key(key))
|
|
|
|
|
return get(key)
|
|
|
|
|
return default_val
|
|
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun reverse_get(value: ref U): ref T {
|
2016-02-15 16:31:01 -05:00
|
|
|
/*return values.get(keys.find(key))*/
|
|
|
|
|
var value_loc = values.find(value)
|
2016-05-22 14:10:19 -07:00
|
|
|
if (value_loc == -1)
|
|
|
|
|
util::error("trying to access nonexistant value-key!")
|
2016-02-15 16:31:01 -05:00
|
|
|
return keys.get(value_loc)
|
|
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun remove(key: ref T) {
|
2015-06-27 18:06:02 -04:00
|
|
|
var idx = keys.find(key)
|
2016-05-22 14:10:19 -07:00
|
|
|
if (idx < 0)
|
|
|
|
|
util::error("trying to remove nonexistant key-value!")
|
2015-06-27 18:06:02 -04:00
|
|
|
keys.remove(idx)
|
|
|
|
|
values.remove(idx)
|
2015-06-27 10:04:09 -04:00
|
|
|
}
|
2015-08-06 02:42:40 -04:00
|
|
|
fun clear() {
|
|
|
|
|
keys.clear()
|
|
|
|
|
values.clear()
|
|
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun operator[](key: ref T): ref U {
|
2015-06-27 10:04:09 -04:00
|
|
|
return get(key)
|
|
|
|
|
}
|
2015-06-27 12:03:55 -04:00
|
|
|
fun for_each(func: fun(T, U):void) {
|
|
|
|
|
for (var i = 0; i < keys.size; i++;)
|
|
|
|
|
func(keys[i], values[i])
|
|
|
|
|
}
|
2015-06-27 10:04:09 -04:00
|
|
|
}
|
|
|
|
|
|