most of hash map - have to commit fix for unify type first
This commit is contained in:
@@ -1137,7 +1137,10 @@ fun unify_type(template_type: *tree<symbol>, param_type: *type, new_map: *map<st
|
||||
new_map->set(concat_symbol_tree(template_type), param_type)
|
||||
}
|
||||
} else if (get_node("\"\\*\"", template_type)) {
|
||||
unify_type(template_type->children[1], param_type->clone_with_decreased_indirection(), new_map, template_replacements)
|
||||
// only unify on decreased indirection if we're not already at 0
|
||||
if (param_type->indirection)
|
||||
unify_type(template_type->children[1], param_type->clone_with_decreased_indirection(), new_map, template_replacements)
|
||||
else return;
|
||||
} else if (get_node("template_inst", template_type)) {
|
||||
if (param_type->is_object()) {
|
||||
var enclosing_template = param_type->type_def->type_def.scope[string("~enclosing_scope")][0]
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,7 +117,6 @@ obj importer (Object) {
|
||||
remove_node(symbol("def_nonterm", false), parse_tree)
|
||||
remove_node(symbol("obj_nonterm", false), parse_tree)
|
||||
remove_node(symbol("adt_nonterm", false), parse_tree)
|
||||
/*remove_node(symbol("\"\\|\"", true), parse_tree)*/
|
||||
|
||||
collapse_node(symbol("case_statement_list", false), parse_tree)
|
||||
collapse_node(symbol("opt_param_assign_list", false), parse_tree)
|
||||
|
||||
@@ -5,6 +5,7 @@ import tree:*
|
||||
import vector:*
|
||||
import stack:*
|
||||
import map:*
|
||||
import hash_map:*
|
||||
import util:*
|
||||
import string:*
|
||||
import mem:*
|
||||
@@ -405,6 +406,7 @@ obj parser (Object) {
|
||||
obj gss (Object) {
|
||||
var data: vector<vector<*tree<int>>>
|
||||
var edges: map< pair<*tree<int>, *tree<int>>, *tree<symbol> >
|
||||
/*var edges: hash_map< pair<*tree<int>, *tree<int>>, *tree<symbol> >*/
|
||||
|
||||
fun construct(): *gss {
|
||||
data.construct()
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import mem
|
||||
import io
|
||||
import set
|
||||
import map
|
||||
import vector
|
||||
import serialize
|
||||
|
||||
|
||||
// maybe my favorite function
|
||||
fun do_nothing() {}
|
||||
|
||||
@@ -25,6 +27,16 @@ fun min<T>(a: T, b: T): T {
|
||||
return a;
|
||||
}
|
||||
|
||||
fun hash<T(Hashable)>(item: T): int return item.hash()
|
||||
fun hash<T>(item: *T): int return (item) cast int
|
||||
fun hash(item: int): int return item
|
||||
fun hash(item: char): int return item
|
||||
// default hash
|
||||
fun hash<T>(item: T): int {
|
||||
io::println("using empty hash - please do not do!")
|
||||
return 0
|
||||
}
|
||||
|
||||
fun make_pair<T,U>(first: T, second: U): pair<T,U> {
|
||||
var it.construct(first, second): pair<T,U>
|
||||
return it
|
||||
@@ -46,7 +58,7 @@ obj unpack_dummy<T,U> {
|
||||
}
|
||||
}
|
||||
|
||||
obj pair<T,U> (Object, Serializable) {
|
||||
obj pair<T,U> (Object, Serializable, Hashable) {
|
||||
var first: T
|
||||
var second: U
|
||||
|
||||
@@ -83,6 +95,9 @@ obj pair<T,U> (Object, Serializable) {
|
||||
return second_pair.second
|
||||
}
|
||||
|
||||
/*fun hash():int return hash(first) ^ hash(second)*/
|
||||
fun hash():int return 0
|
||||
|
||||
// the old unnecessary template to prevent generation
|
||||
// if not used trick (in this case, changing out U with V)
|
||||
fun operator==<V>(other: ref pair<T,V>): bool {
|
||||
|
||||
Reference in New Issue
Block a user