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 copy_construct(old: *set) { data.copy_construct(&old->data) } fun operator=(rhs: set) { destruct() copy_construct(&rhs) } fun serialize(): vector::vector { return serialize::serialize(data) } fun unserialize(it: ref vector::vector, pos: int): int { /*construct()*/ /*util::unpack(data, pos) = serialize::unserialize>(it, pos)*/ 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(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) } }