132 lines
3.7 KiB
Plaintext
132 lines
3.7 KiB
Plaintext
import hash_map
|
|
import vec
|
|
import io
|
|
import serialize
|
|
import set
|
|
|
|
fun hash_set<T>(): hash_set<T> {
|
|
var toRet.construct() : hash_set<T>
|
|
return toRet
|
|
}
|
|
|
|
fun hash_set<T>(item: T): hash_set<T> {
|
|
var toRet.construct() : hash_set<T>
|
|
toRet.add(item)
|
|
return toRet
|
|
}
|
|
|
|
fun from_vector<T>(items: vec::vec<T>): hash_set<T> {
|
|
var toRet.construct() : hash_set<T>
|
|
items.for_each( fun(item: T) toRet.add(item); )
|
|
return toRet
|
|
}
|
|
|
|
obj hash_set<T> (Object, Serializable) {
|
|
var data: hash_map::hash_map<T,bool>
|
|
fun construct(): *hash_set<T> {
|
|
data.construct()
|
|
return this
|
|
}
|
|
/*fun construct(ammt: int): *hash_set<T> {*/
|
|
/*data.construct(ammt)*/
|
|
/*return this*/
|
|
/*}*/
|
|
fun copy_construct(old: *hash_set<T>) {
|
|
data.copy_construct(&old->data)
|
|
}
|
|
fun operator=(rhs: ref hash_set<T>) {
|
|
data = rhs.data
|
|
}
|
|
fun serialize(): vec::vec<char> {
|
|
return serialize::serialize(data)
|
|
}
|
|
fun unserialize(it: ref vec::vec<char>, pos: int): int {
|
|
return data.unserialize(it, 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_set<V>): bool {
|
|
return data == other.data
|
|
}
|
|
fun operator!=(rhs: ref hash_set<T>): bool {
|
|
return ! (*this == rhs)
|
|
}
|
|
fun destruct() {
|
|
data.destruct()
|
|
}
|
|
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(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 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 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(fun(key: T, value: bool) { func(key); })
|
|
}
|
|
fun map<U>(func: fun(T):U): set::set<U> {
|
|
var newSet.construct(size()): set::set<U>
|
|
for_each(fun(i: T) { newSet.add(func(i)); })
|
|
return newSet
|
|
}
|
|
/*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 chaotic_closure(func: fun(T): set::set<T>) {
|
|
var prev_size = 0
|
|
while (prev_size != size()) {
|
|
prev_size = size()
|
|
var to_add.construct(size()): vec::vec<T>
|
|
for_each(fun(i: T) {
|
|
func(i).for_each(fun(j: T) { to_add.add(j); })
|
|
})
|
|
to_add.for_each(fun(i:T) { add(i); })
|
|
}
|
|
|
|
}
|
|
}
|
|
|