2015-06-27 18:06:02 -04:00
|
|
|
import vector
|
|
|
|
|
import io
|
2015-08-26 03:45:34 -04:00
|
|
|
import serialize
|
|
|
|
|
import util
|
2015-06-27 18:06:02 -04:00
|
|
|
|
|
|
|
|
fun set<T>(): set<T> {
|
|
|
|
|
var toRet.construct() : set<T>
|
|
|
|
|
return toRet
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun set<T>(item: T): set<T> {
|
|
|
|
|
var toRet.construct() : set<T>
|
|
|
|
|
toRet.add(item)
|
|
|
|
|
return toRet
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-28 14:27:48 -04:00
|
|
|
fun from_vector<T>(items: vector::vector<T>): set<T> {
|
|
|
|
|
var toRet.construct() : set<T>
|
2015-06-28 20:25:27 -04:00
|
|
|
items.for_each( fun(item: T) toRet.add(item); )
|
2015-06-28 14:27:48 -04:00
|
|
|
return toRet
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-26 03:45:34 -04:00
|
|
|
obj set<T> (Object, Serializable) {
|
2015-06-27 18:06:02 -04:00
|
|
|
var data: vector::vector<T>
|
2015-08-26 03:45:34 -04:00
|
|
|
fun construct(): *set<T> {
|
2015-06-27 18:06:02 -04:00
|
|
|
data.construct()
|
2015-08-26 03:45:34 -04:00
|
|
|
return this
|
2015-06-27 18:06:02 -04:00
|
|
|
}
|
2016-01-02 01:43:41 -05:00
|
|
|
fun construct(ammt: int): *set<T> {
|
|
|
|
|
data.construct(ammt)
|
|
|
|
|
return this
|
|
|
|
|
}
|
2015-07-04 17:02:51 -04:00
|
|
|
fun copy_construct(old: *set<T>) {
|
2015-06-27 18:06:02 -04:00
|
|
|
data.copy_construct(&old->data)
|
|
|
|
|
}
|
2016-01-20 13:50:40 -05:00
|
|
|
fun operator=(rhs: ref set<T>) {
|
2015-06-27 18:06:02 -04:00
|
|
|
destruct()
|
|
|
|
|
copy_construct(&rhs)
|
|
|
|
|
}
|
2015-08-26 03:45:34 -04:00
|
|
|
fun serialize(): vector::vector<char> {
|
|
|
|
|
return serialize::serialize(data)
|
|
|
|
|
}
|
|
|
|
|
fun unserialize(it: ref vector::vector<char>, pos: int): int {
|
2015-08-29 21:45:55 -04:00
|
|
|
return data.unserialize(it, pos)
|
2015-08-26 03:45:34 -04:00
|
|
|
}
|
2015-06-27 18:06:02 -04:00
|
|
|
fun operator==(rhs: set<T>): bool {
|
|
|
|
|
if (size() != rhs.size())
|
|
|
|
|
return false
|
|
|
|
|
return !data.any_true( fun(item: T): bool return !rhs.contains(item); )
|
|
|
|
|
}
|
2015-06-28 14:27:48 -04:00
|
|
|
fun operator!=(rhs: set<T>): bool {
|
|
|
|
|
return ! (*this == rhs)
|
|
|
|
|
}
|
2015-06-27 18:06:02 -04:00
|
|
|
fun destruct() {
|
|
|
|
|
data.destruct()
|
|
|
|
|
}
|
|
|
|
|
fun size():int {
|
|
|
|
|
return data.size
|
|
|
|
|
}
|
2015-07-13 12:16:30 -04:00
|
|
|
fun contains(items: set<T>): bool {
|
|
|
|
|
return items.size() == 0 || !items.any_true( fun(item: T): bool return !contains(item); )
|
|
|
|
|
}
|
2015-06-27 18:06:02 -04:00
|
|
|
fun contains(item: T): bool {
|
|
|
|
|
return data.find(item) != -1
|
|
|
|
|
}
|
2016-02-22 16:18:55 -05:00
|
|
|
fun operator+=(item: ref T) {
|
2015-08-03 18:37:42 -04:00
|
|
|
add(item)
|
|
|
|
|
}
|
2016-02-22 16:18:55 -05:00
|
|
|
fun operator+=(items: ref set<T>) {
|
2015-08-03 18:37:42 -04:00
|
|
|
add(items)
|
|
|
|
|
}
|
2016-02-25 14:24:55 -05:00
|
|
|
fun operator+(items: ref set<T>): set<T> {
|
|
|
|
|
var to_ret.copy_construct(this): set<T>
|
|
|
|
|
to_ret.add(items)
|
|
|
|
|
return to_ret
|
|
|
|
|
}
|
2015-08-06 02:42:40 -04:00
|
|
|
fun add(item: ref T) {
|
2015-06-27 18:06:02 -04:00
|
|
|
if (!contains(item))
|
|
|
|
|
data.add(item)
|
|
|
|
|
}
|
2016-01-02 01:43:41 -05:00
|
|
|
fun add_all(items: ref set<T>) {
|
|
|
|
|
add(items)
|
|
|
|
|
}
|
2015-08-06 02:42:40 -04:00
|
|
|
fun add(items: ref set<T>) {
|
|
|
|
|
items.for_each( fun(item: ref T) add(item); )
|
2015-06-27 18:06:02 -04:00
|
|
|
}
|
|
|
|
|
fun remove(item: T) {
|
|
|
|
|
var idx = data.find(item)
|
|
|
|
|
if (idx == -1) {
|
2016-01-20 13:50:40 -05:00
|
|
|
/*io::println("CANNOT FIND ITEM TO REMOVE")*/
|
2015-06-27 18:06:02 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
data.remove(idx)
|
|
|
|
|
}
|
2015-08-05 03:43:34 -04:00
|
|
|
fun for_each(func: fun(ref T):void) {
|
|
|
|
|
data.for_each(func)
|
|
|
|
|
}
|
2015-06-27 18:06:02 -04:00
|
|
|
fun for_each(func: fun(T):void) {
|
2015-06-28 20:25:27 -04:00
|
|
|
data.for_each(func)
|
2015-06-27 18:06:02 -04:00
|
|
|
}
|
2015-07-13 12:16:30 -04:00
|
|
|
fun any_true(func: fun(T):bool):bool {
|
|
|
|
|
return data.any_true(func)
|
|
|
|
|
}
|
2015-12-28 03:34:40 -05:00
|
|
|
fun reduce<U>(func: fun(T,U): U, initial: U): U {
|
|
|
|
|
return data.reduce(func, initial)
|
|
|
|
|
}
|
2016-01-02 01:43:41 -05:00
|
|
|
fun flatten_map<U>(func: fun(T):set<U>):set<U> {
|
|
|
|
|
var newSet.construct(size()): 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):set<T> {
|
|
|
|
|
var newSet.construct(): set<T>
|
|
|
|
|
newSet.data = data.filter(func)
|
|
|
|
|
return newSet
|
|
|
|
|
}
|
2015-06-27 18:06:02 -04:00
|
|
|
}
|
|
|
|
|
|