most of hash map - have to commit fix for unify type first
This commit is contained in:
108
stdlib/hash_map.krak
Normal file
108
stdlib/hash_map.krak
Normal file
@@ -0,0 +1,108 @@
|
||||
import vector
|
||||
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
|
||||
}
|
||||
fun hash_map<T,U>(key:T, value: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) {
|
||||
var data: vector::vector<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
|
||||
}
|
||||
fun operator=(rhs: hash_map<T,U>) {
|
||||
destruct()
|
||||
copy_construct(&rhs)
|
||||
}
|
||||
fun destruct() {
|
||||
data.destruct()
|
||||
}
|
||||
fun serialize(): vector::vector<char> {
|
||||
return serialize::serialize(data) + serialize::serialize(size)
|
||||
}
|
||||
fun unserialize(it: ref vector::vector<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
|
||||
}
|
||||
fun set(key: T, value: U) {
|
||||
var key_hash = util::hash(key)
|
||||
if (!data[key_hash%data.size].contains_key(key))
|
||||
size++
|
||||
data[key_hash%data.size].set(key, value)
|
||||
if (size > data.size) {
|
||||
var new_data = vector::vector<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].set(key, value)
|
||||
})
|
||||
data = new_data
|
||||
}
|
||||
}
|
||||
fun get(key: T): ref U {
|
||||
return data[util::hash(key)%data.size].get(key)
|
||||
}
|
||||
fun contains_key(key: T): bool {
|
||||
return data[util::hash(key)%data.size].contains_key(key)
|
||||
}
|
||||
fun contains_value(value: U): bool {
|
||||
for (var i = 0; i < data.size; i++;) {
|
||||
if (data[i].contains_value(value))
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
fun reverse_get(value: 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")
|
||||
}
|
||||
fun remove(key: T) {
|
||||
data[util::hash(key)%data.size].remove(key)
|
||||
}
|
||||
fun for_each(func: fun(T, U):void) {
|
||||
for (var i = 0; i < data.size; i++;)
|
||||
data[i].for_each(func)
|
||||
}
|
||||
fun operator[](key: T): ref U {
|
||||
return get(key)
|
||||
}
|
||||
fun operator[]=(key: T, value: U) {
|
||||
set(key,value)
|
||||
}
|
||||
fun get_with_default(key: T, default_val: ref U): ref U {
|
||||
if (contains_key(key))
|
||||
return get(key)
|
||||
return default_val
|
||||
}
|
||||
fun clear() {
|
||||
data.clear()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user