Templated methods work now, even explicit instantiation (along with implicit)

This commit is contained in:
Nathan Braswell
2016-02-20 02:36:35 -05:00
parent c5dda4b7ec
commit b073d5806b
8 changed files with 112 additions and 212 deletions

View File

@@ -1,4 +1,6 @@
import mem
import set
import map
import vector
import serialize
@@ -129,3 +131,28 @@ obj range {
}
}
// a function that allows the safe deletion of recursive and complicated data structures
fun safe_recursive_delete<T>(first: *T, addingFunc: fun(*T): set::set<*T>) {
var toDelete = set::set<*T>()
var next = set::set(first)
while (toDelete != next) {
toDelete = next
toDelete.for_each( fun(it: *T) next.add(addingFunc(it)); )
}
toDelete.for_each( fun(it: *T) mem::delete(it); )
}
// a function that allows the safe cloning of recursive and complicated data structures
// cloneing func is the func that does the cloning, it takes in a recursive clone function and
// a register clone function
fun safe_recursive_clone<T>(first: *T, cloningFunc: fun(*T, fun(*T):*T, fun(*T):void): void): *T {
var rec_map = map::map<*T,*T>()
// can't do type infrence if you need the type inside the expression...
var rec_it: fun(*T):*T = fun(it: *T): *T {
if (!rec_map.contains_key(it))
cloningFunc(it, rec_it, fun(cloned: *T) { rec_map[it] = cloned; })
return rec_map[it]
}
return rec_it(first)
}