2018-05-22 19:43:54 -04:00
|
|
|
import vec
|
2016-04-20 16:09:26 -04:00
|
|
|
import map
|
|
|
|
|
import io
|
|
|
|
|
import serialize
|
|
|
|
|
import util
|
|
|
|
|
|
|
|
|
|
fun hash_map<T,U>(): hash_map<T,U> {
|
|
|
|
|
var toRet.construct(): hash_map<T,U>
|
|
|
|
|
return toRet
|
|
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun hash_map<T,U>(key: ref T, value: ref U): hash_map<T,U> {
|
2016-04-20 16:09:26 -04:00
|
|
|
var toRet.construct(): hash_map<T,U>
|
|
|
|
|
toRet.set(key, value)
|
|
|
|
|
return toRet
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
obj hash_map<T,U> (Object, Serializable) {
|
2018-05-22 19:43:54 -04:00
|
|
|
var data: vec::vec<map::map<T,U>>
|
2016-04-20 16:09:26 -04:00
|
|
|
var size: int
|
|
|
|
|
|
|
|
|
|
fun construct(): *hash_map<T,U> {
|
|
|
|
|
data.construct()
|
|
|
|
|
data.add(map::map<T,U>())
|
|
|
|
|
size = 0
|
|
|
|
|
return this
|
|
|
|
|
}
|
|
|
|
|
fun copy_construct(old: *hash_map<T,U>) {
|
|
|
|
|
data.copy_construct(&old->data)
|
|
|
|
|
size = old->size
|
|
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun operator=(rhs: ref hash_map<T,U>) {
|
|
|
|
|
data = rhs.data
|
|
|
|
|
size = rhs.size
|
2016-04-20 16:09:26 -04:00
|
|
|
}
|
|
|
|
|
fun destruct() {
|
|
|
|
|
data.destruct()
|
|
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun serialize(): vec::vec<char> {
|
2016-04-20 16:09:26 -04:00
|
|
|
return serialize::serialize(data) + serialize::serialize(size)
|
|
|
|
|
}
|
2018-05-22 19:43:54 -04:00
|
|
|
fun unserialize(it: ref vec::vec<char>, pos: int): int {
|
2016-04-20 16:09:26 -04:00
|
|
|
pos = data.unserialize(it, pos)
|
|
|
|
|
util::unpack(size, pos) = serialize::unserialize<int>(it, pos)
|
|
|
|
|
return pos
|
|
|
|
|
}
|
|
|
|
|
// the old unnecessary template to prevent generation
|
|
|
|
|
// if not used trick (in this case, changing out U with V)
|
|
|
|
|
fun operator==<V>(other: ref hash_map<T,V>): bool {
|
|
|
|
|
return data == other.data
|
|
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun set(key: ref T, value: ref U) {
|
2016-06-26 04:44:54 -07:00
|
|
|
var key_hash = util::hash(key)
|
|
|
|
|
if (!data[(key_hash%data.size) cast int].contains_key(key)) {
|
2016-04-20 16:09:26 -04:00
|
|
|
size++
|
2016-04-22 02:58:14 -04:00
|
|
|
if (size > data.size) {
|
2018-05-22 19:43:54 -04:00
|
|
|
var new_data.construct(size*2): vec::vec<map::map<T,U>>
|
2016-04-22 02:58:14 -04:00
|
|
|
for (var i = 0; i < size*2; i++;)
|
|
|
|
|
new_data.addEnd(map::map<T,U>())
|
|
|
|
|
for_each(fun(key: T, value: U) {
|
2016-06-26 04:44:54 -07:00
|
|
|
new_data[(util::hash(key)%new_data.size) cast int].set(key, value)
|
2016-04-22 02:58:14 -04:00
|
|
|
})
|
2017-01-22 16:36:04 -05:00
|
|
|
data.swap(new_data)
|
2016-04-22 02:58:14 -04:00
|
|
|
}
|
2016-04-20 16:09:26 -04:00
|
|
|
}
|
2016-06-26 04:44:54 -07:00
|
|
|
data[(key_hash%data.size) cast int].set(key, value)
|
2016-04-20 16:09:26 -04:00
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun get(key: ref T): ref U {
|
2016-06-26 04:44:54 -07:00
|
|
|
return data[(util::hash(key)%data.size) cast int].get(key)
|
2016-04-20 16:09:26 -04:00
|
|
|
}
|
2017-01-22 16:36:04 -05:00
|
|
|
fun get_ptr_or_null(key: ref T): *U {
|
|
|
|
|
return data[(util::hash(key)%data.size) cast int].get_ptr_or_null(key)
|
|
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun contains_key(key: ref T): bool {
|
2016-06-26 04:44:54 -07:00
|
|
|
return data[(util::hash(key)%data.size) cast int].contains_key(key)
|
2016-04-20 16:09:26 -04:00
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun contains_value(value: ref U): bool {
|
2016-04-20 16:09:26 -04:00
|
|
|
for (var i = 0; i < data.size; i++;) {
|
|
|
|
|
if (data[i].contains_value(value))
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun reverse_get(value: ref U): ref T {
|
2016-04-20 16:09:26 -04:00
|
|
|
for (var i = 0; i < data.size; i++;) {
|
|
|
|
|
if (data[i].contains_value(value))
|
|
|
|
|
return data[i].reverse_get(value)
|
|
|
|
|
}
|
|
|
|
|
io::println("trying to reverse get a value that is not in the hash_map")
|
|
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun remove(key: ref T) {
|
2016-06-26 04:44:54 -07:00
|
|
|
data[(util::hash(key)%data.size) cast int].remove(key)
|
2016-04-20 16:09:26 -04:00
|
|
|
}
|
|
|
|
|
fun for_each(func: fun(T, U):void) {
|
|
|
|
|
for (var i = 0; i < data.size; i++;)
|
|
|
|
|
data[i].for_each(func)
|
|
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun operator[](key: ref T): ref U {
|
2016-04-20 16:09:26 -04:00
|
|
|
return get(key)
|
|
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun operator[]=(key: ref T, value: ref U) {
|
2016-04-20 16:09:26 -04:00
|
|
|
set(key,value)
|
|
|
|
|
}
|
2016-06-11 00:45:18 -07:00
|
|
|
fun get_with_default(key: ref T, default_val: ref U): ref U {
|
2016-04-20 16:09:26 -04:00
|
|
|
if (contains_key(key))
|
|
|
|
|
return get(key)
|
|
|
|
|
return default_val
|
|
|
|
|
}
|
|
|
|
|
fun clear() {
|
|
|
|
|
data.clear()
|
2016-04-22 02:58:14 -04:00
|
|
|
size = 0
|
|
|
|
|
data.add(map::map<T,U>())
|
2016-04-20 16:09:26 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|