Files
kraken/stdlib/hash_set.krak

114 lines
3.2 KiB
Plaintext
Raw Normal View History

import hash_map
import vector
import io
import serialize
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: vector::vector<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(): vector::vector<char> {
return serialize::serialize(data)
}
fun unserialize(it: ref vector::vector<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(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*/
/*}*/
}