Replace visited list for pass_common with hash_set (which isn't complete, but has the basics) for a massive speedup (6xish)
This commit is contained in:
@@ -21,7 +21,7 @@ fun from_vector<T>(items: vector::vector<T>): hash_set<T> {
|
||||
}
|
||||
|
||||
obj hash_set<T> (Object, Serializable) {
|
||||
var data: hash_map::hash_map<T>
|
||||
var data: hash_map::hash_map<T,bool>
|
||||
fun construct(): *hash_set<T> {
|
||||
data.construct()
|
||||
return this
|
||||
@@ -42,10 +42,10 @@ obj hash_set<T> (Object, Serializable) {
|
||||
fun unserialize(it: ref vector::vector<char>, pos: int): int {
|
||||
return data.unserialize(it, pos)
|
||||
}
|
||||
fun operator==(rhs: ref hash_set<T>): bool {
|
||||
if (size() != rhs.size())
|
||||
return false
|
||||
return !data.any_true( fun(item: T): bool return !rhs.contains(item); )
|
||||
// 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_set<V>): bool {
|
||||
return data == other.data
|
||||
}
|
||||
fun operator!=(rhs: ref hash_set<T>): bool {
|
||||
return ! (*this == rhs)
|
||||
@@ -56,58 +56,58 @@ obj hash_set<T> (Object, Serializable) {
|
||||
fun size():int {
|
||||
return data.size
|
||||
}
|
||||
fun contains(items: ref hash_set<T>): bool {
|
||||
return items.size() == 0 || !items.any_true( fun(item: T): bool return !contains(item); )
|
||||
}
|
||||
/*fun contains(items: ref hash_set<T>): bool {*/
|
||||
/*return items.size() == 0 || !items.any_true( fun(item: T): bool return !contains(item); )*/
|
||||
/*}*/
|
||||
fun contains(item: ref T): bool {
|
||||
return data.contains_key(item)
|
||||
}
|
||||
fun operator+=(item: ref T) {
|
||||
add(item)
|
||||
}
|
||||
fun operator+=(items: ref hash_set<T>) {
|
||||
add(items)
|
||||
}
|
||||
fun operator+(items: ref hash_set<T>): hash_set<T> {
|
||||
var to_ret.copy_construct(this): hash_set<T>
|
||||
to_ret.add(items)
|
||||
return to_ret
|
||||
}
|
||||
/*fun operator+=(items: ref hash_set<T>) {*/
|
||||
/*add(items)*/
|
||||
/*}*/
|
||||
/*fun operator+(items: ref hash_set<T>): hash_set<T> {*/
|
||||
/*var to_ret.copy_construct(this): hash_set<T>*/
|
||||
/*to_ret.add(items)*/
|
||||
/*return to_ret*/
|
||||
/*}*/
|
||||
fun add(item: ref T) {
|
||||
if (!contains(item))
|
||||
data.set(item,true)
|
||||
}
|
||||
fun add_all(items: ref hash_set<T>) {
|
||||
add(items)
|
||||
}
|
||||
fun add(items: ref hash_set<T>) {
|
||||
items.for_each( fun(item: ref T) add(item); )
|
||||
}
|
||||
/*fun add_all(items: ref hash_set<T>) {*/
|
||||
/*add(items)*/
|
||||
/*}*/
|
||||
/*fun add(items: ref hash_set<T>) {*/
|
||||
/*items.for_each( fun(item: ref T) add(item); )*/
|
||||
/*}*/
|
||||
fun remove(item: ref T) {
|
||||
data.remove(item)
|
||||
}
|
||||
fun for_each(func: fun(ref T):void) {
|
||||
data.for_each(func)
|
||||
}
|
||||
fun for_each(func: fun(T):void) {
|
||||
data.for_each(func)
|
||||
}
|
||||
fun any_true(func: fun(T):bool):bool {
|
||||
return data.any_true(func)
|
||||
}
|
||||
fun reduce<U>(func: fun(T,U): U, initial: U): U {
|
||||
return data.reduce(func, initial)
|
||||
}
|
||||
fun flatten_map<U>(func: fun(T):hash_set<U>):hash_set<U> {
|
||||
var newSet.construct(size()): hash_set<U>
|
||||
for (var i = 0; i < size(); i++;)
|
||||
func(data[i]).for_each(fun(item: ref U) newSet.add(item);)
|
||||
return newSet
|
||||
}
|
||||
fun filter(func: fun(T):bool):hash_set<T> {
|
||||
var newSet.construct(): hash_set<T>
|
||||
newSet.data = data.filter(func)
|
||||
return newSet
|
||||
}
|
||||
/*fun for_each(func: fun(ref T):void) {*/
|
||||
/*data.for_each(func)*/
|
||||
/*}*/
|
||||
/*fun for_each(func: fun(T):void) {*/
|
||||
/*data.for_each(func)*/
|
||||
/*}*/
|
||||
/*fun any_true(func: fun(T):bool):bool {*/
|
||||
/*return data.any_true(func)*/
|
||||
/*}*/
|
||||
/*fun reduce<U>(func: fun(T,U): U, initial: U): U {*/
|
||||
/*return data.reduce(func, initial)*/
|
||||
/*}*/
|
||||
/*fun flatten_map<U>(func: fun(T):hash_set<U>):hash_set<U> {*/
|
||||
/*var newSet.construct(size()): hash_set<U>*/
|
||||
/*for (var i = 0; i < size(); i++;)*/
|
||||
/*func(data[i]).for_each(fun(item: ref U) newSet.add(item);)*/
|
||||
/*return newSet*/
|
||||
/*}*/
|
||||
/*fun filter(func: fun(T):bool):hash_set<T> {*/
|
||||
/*var newSet.construct(): hash_set<T>*/
|
||||
/*newSet.data = data.filter(func)*/
|
||||
/*return newSet*/
|
||||
/*}*/
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user