import hash_map import vector import io import serialize fun hash_set(): hash_set { var toRet.construct() : hash_set return toRet } fun hash_set(item: T): hash_set { var toRet.construct() : hash_set toRet.add(item) return toRet } fun from_vector(items: vector::vector): hash_set { var toRet.construct() : hash_set items.for_each( fun(item: T) toRet.add(item); ) return toRet } obj hash_set (Object, Serializable) { var data: hash_map::hash_map fun construct(): *hash_set { data.construct() return this } /*fun construct(ammt: int): *hash_set {*/ /*data.construct(ammt)*/ /*return this*/ /*}*/ fun copy_construct(old: *hash_set) { data.copy_construct(&old->data) } fun operator=(rhs: ref hash_set) { data = rhs.data } fun serialize(): vector::vector { return serialize::serialize(data) } fun unserialize(it: ref vector::vector, pos: int): int { return data.unserialize(it, pos) } fun operator==(rhs: ref hash_set): bool { if (size() != rhs.size()) return false return !data.any_true( fun(item: T): bool return !rhs.contains(item); ) } fun operator!=(rhs: ref hash_set): bool { return ! (*this == rhs) } fun destruct() { data.destruct() } fun size():int { return data.size } fun contains(items: ref hash_set): 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) { add(items) } fun operator+(items: ref hash_set): hash_set { var to_ret.copy_construct(this): hash_set 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) { add(items) } fun add(items: ref hash_set) { 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(func: fun(T,U): U, initial: U): U { return data.reduce(func, initial) } fun flatten_map(func: fun(T):hash_set):hash_set { var newSet.construct(size()): hash_set 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 { var newSet.construct(): hash_set newSet.data = data.filter(func) return newSet } }