Files
kraken/stdlib/set.krak

141 lines
3.6 KiB
Plaintext
Raw Normal View History

2018-05-22 19:43:54 -04:00
import vec
import io
import serialize
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
}
2018-05-22 19:43:54 -04:00
fun from_vector<T>(items: vec::vec<T>): set<T> {
var toRet.construct() : set<T>
items.for_each( fun(item: T) toRet.add(item); )
return toRet
}
obj set<T> (Object, Serializable) {
2018-05-22 19:43:54 -04:00
var data: vec::vec<T>
fun construct(): *set<T> {
data.construct()
return this
}
fun construct(ammt: int): *set<T> {
data.construct(ammt)
return this
}
fun copy_construct(old: *set<T>) {
data.copy_construct(&old->data)
}
fun operator=(rhs: ref set<T>) {
2016-05-05 04:51:10 -04:00
data = rhs.data
}
2018-05-22 19:43:54 -04:00
fun serialize(): vec::vec<char> {
return serialize::serialize(data)
}
2018-05-22 19:43:54 -04:00
fun unserialize(it: ref vec::vec<char>, pos: int): int {
return data.unserialize(it, pos)
}
2016-05-05 04:51:10 -04:00
fun operator==(rhs: ref set<T>): bool {
if (size() != rhs.size())
return false
return !data.any_true( fun(item: T): bool return !rhs.contains(item); )
}
2016-05-05 04:51:10 -04:00
fun operator!=(rhs: ref set<T>): bool {
return ! (*this == rhs)
}
fun destruct() {
data.destruct()
}
fun size():int {
return data.size
}
2016-05-05 04:51:10 -04:00
fun contains(items: ref set<T>): bool {
2015-07-13 12:16:30 -04:00
return items.size() == 0 || !items.any_true( fun(item: T): bool return !contains(item); )
}
2016-05-05 04:51:10 -04:00
fun contains(item: ref T): bool {
return data.find(item) != -1
}
fun operator+=(item: ref T) {
add(item)
}
fun operator+=(items: ref set<T>) {
add(items)
}
fun operator+(item: ref T): set<T> {
var to_ret.copy_construct(this): set<T>
to_ret.add(item)
return to_ret
}
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) {
if (!contains(item))
data.add(item)
}
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); )
}
2018-05-22 19:43:54 -04:00
fun add(items: ref vec::vec<T>) {
2017-02-02 00:46:36 -05:00
items.for_each( fun(item: ref T) add(item); )
}
2016-05-05 04:51:10 -04:00
fun remove(item: ref 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)
}
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)
}
2017-02-02 00:46:36 -05:00
fun map<U>(func: fun(T):U):set<U> {
var newSet.construct(size()): set<U>
for (var i = 0; i < size(); i++;)
newSet.add(func(data[i]))
return newSet
}
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
}
fun chaotic_closure(func: fun(T): set<T>) {
var prev_size = 0
while (prev_size != data.size) {
prev_size = data.size
for (var i = 0; i < data.size; i++;)
add(func(data[i]))
}
}
}