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)
|
new_map->set(concat_symbol_tree(template_type), param_type)
|
||||||
}
|
}
|
||||||
} else if (get_node("\"\\*\"", template_type)) {
|
} else if (get_node("\"\\*\"", template_type)) {
|
||||||
|
// 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)
|
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)) {
|
} else if (get_node("template_inst", template_type)) {
|
||||||
if (param_type->is_object()) {
|
if (param_type->is_object()) {
|
||||||
var enclosing_template = param_type->type_def->type_def.scope[string("~enclosing_scope")][0]
|
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("def_nonterm", false), parse_tree)
|
||||||
remove_node(symbol("obj_nonterm", false), parse_tree)
|
remove_node(symbol("obj_nonterm", false), parse_tree)
|
||||||
remove_node(symbol("adt_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("case_statement_list", false), parse_tree)
|
||||||
collapse_node(symbol("opt_param_assign_list", false), parse_tree)
|
collapse_node(symbol("opt_param_assign_list", false), parse_tree)
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import tree:*
|
|||||||
import vector:*
|
import vector:*
|
||||||
import stack:*
|
import stack:*
|
||||||
import map:*
|
import map:*
|
||||||
|
import hash_map:*
|
||||||
import util:*
|
import util:*
|
||||||
import string:*
|
import string:*
|
||||||
import mem:*
|
import mem:*
|
||||||
@@ -405,6 +406,7 @@ obj parser (Object) {
|
|||||||
obj gss (Object) {
|
obj gss (Object) {
|
||||||
var data: vector<vector<*tree<int>>>
|
var data: vector<vector<*tree<int>>>
|
||||||
var edges: map< pair<*tree<int>, *tree<int>>, *tree<symbol> >
|
var edges: map< pair<*tree<int>, *tree<int>>, *tree<symbol> >
|
||||||
|
/*var edges: hash_map< pair<*tree<int>, *tree<int>>, *tree<symbol> >*/
|
||||||
|
|
||||||
fun construct(): *gss {
|
fun construct(): *gss {
|
||||||
data.construct()
|
data.construct()
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
import mem
|
import mem
|
||||||
|
import io
|
||||||
import set
|
import set
|
||||||
import map
|
import map
|
||||||
import vector
|
import vector
|
||||||
import serialize
|
import serialize
|
||||||
|
|
||||||
|
|
||||||
// maybe my favorite function
|
// maybe my favorite function
|
||||||
fun do_nothing() {}
|
fun do_nothing() {}
|
||||||
|
|
||||||
@@ -25,6 +27,16 @@ fun min<T>(a: T, b: T): T {
|
|||||||
return a;
|
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> {
|
fun make_pair<T,U>(first: T, second: U): pair<T,U> {
|
||||||
var it.construct(first, second): pair<T,U>
|
var it.construct(first, second): pair<T,U>
|
||||||
return it
|
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 first: T
|
||||||
var second: U
|
var second: U
|
||||||
|
|
||||||
@@ -83,6 +95,9 @@ obj pair<T,U> (Object, Serializable) {
|
|||||||
return second_pair.second
|
return second_pair.second
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*fun hash():int return hash(first) ^ hash(second)*/
|
||||||
|
fun hash():int return 0
|
||||||
|
|
||||||
// the old unnecessary template to prevent generation
|
// the old unnecessary template to prevent generation
|
||||||
// if not used trick (in this case, changing out U with V)
|
// if not used trick (in this case, changing out U with V)
|
||||||
fun operator==<V>(other: ref pair<T,V>): bool {
|
fun operator==<V>(other: ref pair<T,V>): bool {
|
||||||
|
|||||||
@@ -10,3 +10,15 @@ Lookie, a map!
|
|||||||
What I get for not testing different types
|
What I get for not testing different types
|
||||||
key: 7, value: Lookie, a map!
|
key: 7, value: Lookie, a map!
|
||||||
key: 30, value: we'll look for for_each too
|
key: 30, value: we'll look for for_each too
|
||||||
|
3
|
||||||
|
2
|
||||||
|
1
|
||||||
|
3
|
||||||
|
20
|
||||||
|
2
|
||||||
|
30
|
||||||
|
4
|
||||||
|
Lookie, a map!
|
||||||
|
What I get for not testing different types
|
||||||
|
key: 30, value: we'll look for for_each too
|
||||||
|
key: 7, value: Lookie, a map!
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import io:*
|
import io:*
|
||||||
import map:*
|
import map:*
|
||||||
|
import hash_map:*
|
||||||
|
|
||||||
fun main(): int {
|
fun test_map<T,V>(m: ref T, mapEx: ref V) {
|
||||||
var m = map(3,1)
|
|
||||||
m.set(2,2)
|
m.set(2,2)
|
||||||
m.set(1,3)
|
m.set(1,3)
|
||||||
println(m[1])
|
println(m[1])
|
||||||
@@ -16,12 +16,22 @@ fun main(): int {
|
|||||||
println(m[2])
|
println(m[2])
|
||||||
println(m[6])
|
println(m[6])
|
||||||
println(m[3])
|
println(m[3])
|
||||||
var mapEx = map(7, "Lookie, a map!")
|
|
||||||
mapEx[20] = "What I get for not testing different types"
|
mapEx[20] = "What I get for not testing different types"
|
||||||
mapEx[30] = "we'll look for for_each too"
|
mapEx[30] = "we'll look for for_each too"
|
||||||
println(mapEx[7])
|
println(mapEx[7])
|
||||||
println(mapEx[20])
|
println(mapEx[20])
|
||||||
mapEx.remove(20)
|
mapEx.remove(20)
|
||||||
mapEx.for_each(fun(key:int, value:*char) { print("key: "); print(key); print(", value: "); println(value); })
|
mapEx.for_each(fun(key:int, value:*char) { print("key: "); print(key); print(", value: "); println(value); })
|
||||||
|
}
|
||||||
|
fun main(): int {
|
||||||
|
var m1 = map(3,1)
|
||||||
|
var mapEx1 = map(7, "Lookie, a map!")
|
||||||
|
test_map(m1, mapEx1)
|
||||||
|
var m2 = hash_map(3,1)
|
||||||
|
var mapEx2 = hash_map(7, "Lookie, a map!")
|
||||||
|
test_map(m2, mapEx2)
|
||||||
|
/*println(m2.data.size)*/
|
||||||
|
/*println(m2.data[1].keys.size)*/
|
||||||
|
/*println(mapEx2.data.size)*/
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user