Files
kraken/stdlib/hash_map.krak

115 lines
3.4 KiB
Plaintext
Raw Normal View History

2018-05-22 19:43:54 -04:00
import vec
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> {
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>>
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
}
fun destruct() {
data.destruct()
}
2018-05-22 19:43:54 -04:00
fun serialize(): vec::vec<char> {
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 {
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) {
var key_hash = util::hash(key)
if (!data[(key_hash%data.size) cast int].contains_key(key)) {
size++
if (size > data.size) {
2018-05-22 19:43:54 -04:00
var new_data.construct(size*2): vec::vec<map::map<T,U>>
for (var i = 0; i < size*2; i++;)
new_data.addEnd(map::map<T,U>())
for_each(fun(key: T, value: U) {
new_data[(util::hash(key)%new_data.size) cast int].set(key, value)
})
data.swap(new_data)
}
}
data[(key_hash%data.size) cast int].set(key, value)
}
2016-06-11 00:45:18 -07:00
fun get(key: ref T): ref U {
return data[(util::hash(key)%data.size) cast int].get(key)
}
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 {
return data[(util::hash(key)%data.size) cast int].contains_key(key)
}
2016-06-11 00:45:18 -07:00
fun contains_value(value: ref U): bool {
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 {
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) {
data[(util::hash(key)%data.size) cast int].remove(key)
}
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 {
return get(key)
}
2016-06-11 00:45:18 -07:00
fun operator[]=(key: ref T, value: ref U) {
set(key,value)
}
2016-06-11 00:45:18 -07:00
fun get_with_default(key: ref T, default_val: ref U): ref U {
if (contains_key(key))
return get(key)
return default_val
}
fun clear() {
data.clear()
size = 0
data.add(map::map<T,U>())
}
}