Added mem::safe_recursive_clone, and while it works for regex, it's actually slower then remaking it. Hmmmm, maybe because some of the stdlib is inefficent

This commit is contained in:
Nathan Braswell
2015-07-07 00:46:00 -04:00
parent 92f5c63c9a
commit f3cdea068e
9 changed files with 68 additions and 26 deletions

View File

@@ -1,4 +1,5 @@
import set
import map
__if_comp__ __C__ simple_passthrough """
#include <stdlib.h>
@@ -105,4 +106,18 @@ fun safe_recursive_delete<T>(first: *T, addingFunc: fun(*T): set::set<*T>) {
toDelete.for_each( fun(it: *T) 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)
}