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:
@@ -43,6 +43,9 @@ obj map<T,U> (Object) {
|
||||
keys.add(key)
|
||||
values.add(value)
|
||||
}
|
||||
fun contains_key(key: T): bool {
|
||||
return keys.contains(key)
|
||||
}
|
||||
fun get(key: T): U {
|
||||
return values.get(keys.find(key))
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -56,14 +56,21 @@ obj regex (Object) {
|
||||
}
|
||||
|
||||
fun copy_construct(old:*regex):void {
|
||||
begin = old->begin
|
||||
regexString.copy_construct(&old->regexString)
|
||||
/*construct(old->regexString)*/
|
||||
construct(old->regexString)
|
||||
/*begin = old->begin*/
|
||||
/*regexString.copy_construct(&old->regexString)*/
|
||||
/*begin = mem::safe_recursive_clone(old->begin, fun(it: *regexState, cloner: fun(*regexState):*regexState, register: fun(*regexState):void): void {*/
|
||||
/*var newOne = mem::new<regexState>()->construct(it->character)*/
|
||||
/*register(newOne)*/
|
||||
/*it->next_states.for_each(fun(next_state: *regexState) {*/
|
||||
/*newOne->next_states.add(cloner(next_state))*/
|
||||
/*})*/
|
||||
/*})*/
|
||||
}
|
||||
|
||||
fun destruct():void {
|
||||
regexString.destruct()
|
||||
/*mem::safe_recursive_delete(begin, fun(it: *regexState): set::set<*regexState> { return set::from_vector(it->next_states); } )*/
|
||||
mem::safe_recursive_delete(begin, fun(it: *regexState): set::set<*regexState> { return set::from_vector(it->next_states); } )
|
||||
}
|
||||
|
||||
fun operator==(other: regex):bool {
|
||||
@@ -72,7 +79,7 @@ obj regex (Object) {
|
||||
|
||||
fun operator=(other: regex):void {
|
||||
destruct()
|
||||
construct(other.regexString)
|
||||
copy_construct(&other)
|
||||
}
|
||||
|
||||
fun compile(regex_string: string::string): util::pair<*regexState, vector::vector<*regexState>> {
|
||||
|
||||
@@ -25,13 +25,6 @@ obj vector<T> (Object) {
|
||||
return this;
|
||||
}
|
||||
|
||||
fun construct(newSize: int): *vector<T>{
|
||||
size = newSize;
|
||||
available = newSize;
|
||||
|
||||
data = new<T>(newSize);
|
||||
return this;
|
||||
}
|
||||
|
||||
fun copy_construct(old: *vector<T>): void {
|
||||
construct()
|
||||
|
||||
Reference in New Issue
Block a user