import hash_map import vec import io import serialize import set 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: vec::vec): 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(): vec::vec { return serialize::serialize(data) } fun unserialize(it: ref vec::vec, 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==(other: ref hash_set): bool { return data == other.data } 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(fun(key: T, value: bool) { func(key); }) } fun map(func: fun(T):U): set::set { var newSet.construct(size()): set::set 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(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*/ /*}*/ fun chaotic_closure(func: fun(T): set::set) { var prev_size = 0 while (prev_size != size()) { prev_size = size() var to_add.construct(size()): vec::vec 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); }) } } fun pop(): T { return data.pop().first } fun union(other: hash_set): hash_set { for_each(fun(i: T) { other.add(i) }) return other } fun operator-(items: ref hash_set): hash_set { var to_ret.copy_construct(this): hash_set items.for_each(fun(i: T) { to_ret.remove(i) }) return to_ret } }