import vector import io import serialize import util fun set(): set { var toRet.construct() : set return toRet } fun set(item: T): set { var toRet.construct() : set toRet.add(item) return toRet } fun from_vector(items: vector::vector): set { var toRet.construct() : set items.for_each( fun(item: T) toRet.add(item); ) return toRet } obj set (Object, Serializable) { var data: vector::vector fun construct(): *set { data.construct() return this } fun construct(ammt: int): *set { data.construct(ammt) return this } fun copy_construct(old: *set) { data.copy_construct(&old->data) } fun operator=(rhs: ref set) { destruct() copy_construct(&rhs) } 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: set): bool { if (size() != rhs.size()) return false return !data.any_true( fun(item: T): bool return !rhs.contains(item); ) } fun operator!=(rhs: set): bool { return ! (*this == rhs) } fun destruct() { data.destruct() } fun size():int { return data.size } fun contains(items: set): bool { return items.size() == 0 || !items.any_true( fun(item: T): bool return !contains(item); ) } fun contains(item: T): bool { return data.find(item) != -1 } fun operator+=(item: T) { add(item) } fun operator+=(items: set) { add(items) } fun add(item: ref T) { if (!contains(item)) data.add(item) } fun add_all(items: ref set) { add(items) } fun add(items: ref set) { items.for_each( fun(item: ref T) add(item); ) } fun remove(item: T) { var idx = data.find(item) if (idx == -1) { /*io::println("CANNOT FIND ITEM TO REMOVE")*/ return } data.remove(idx) } 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):set):set { var newSet.construct(size()): 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):set { var newSet.construct(): set newSet.data = data.filter(func) return newSet } }