2019-09-12 10:33:56 -07:00
|
|
|
|
class MySet(*set) {
|
2016-12-05 23:44:36 +01:00
|
|
|
|
|
|
|
|
|
|
method init {
|
2019-09-12 10:33:56 -07:00
|
|
|
|
var elems = set
|
|
|
|
|
|
set = Hash()
|
2016-12-05 23:44:36 +01:00
|
|
|
|
elems.each { |e| self += e }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
method +(elem) {
|
2019-09-12 10:33:56 -07:00
|
|
|
|
set{elem} = elem
|
|
|
|
|
|
self
|
2016-12-05 23:44:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
method del(elem) {
|
2019-09-12 10:33:56 -07:00
|
|
|
|
set.delete(elem)
|
2016-12-05 23:44:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
method has(elem) {
|
2019-09-12 10:33:56 -07:00
|
|
|
|
set.has_key(elem)
|
2016-12-05 23:44:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-09-12 10:33:56 -07:00
|
|
|
|
method ∪(MySet that) {
|
|
|
|
|
|
MySet(set.values..., that.values...)
|
2016-12-05 23:44:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-09-12 10:33:56 -07:00
|
|
|
|
method ∩(MySet that) {
|
|
|
|
|
|
MySet(set.keys.grep{ |k| k ∈ that } \
|
|
|
|
|
|
.map { |k| set{k} }...)
|
2016-12-05 23:44:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-09-12 10:33:56 -07:00
|
|
|
|
method ∖(MySet that) {
|
|
|
|
|
|
MySet(set.keys.grep{|k| !(k ∈ that) } \
|
|
|
|
|
|
.map {|k| set{k} }...)
|
2016-12-05 23:44:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-09-12 10:33:56 -07:00
|
|
|
|
method ^(MySet that) {
|
|
|
|
|
|
var d = ((self ∖ that) ∪ (that ∖ self))
|
|
|
|
|
|
MySet(d.values...)
|
2016-12-05 23:44:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
method count { set.len }
|
|
|
|
|
|
|
2019-09-12 10:33:56 -07:00
|
|
|
|
method ≡(MySet that) {
|
|
|
|
|
|
(self ∖ that -> count.is_zero) && (that ∖ self -> count.is_zero)
|
2016-12-05 23:44:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
method values { set.values }
|
|
|
|
|
|
|
2019-09-12 10:33:56 -07:00
|
|
|
|
method ⊆(MySet that) {
|
2016-12-05 23:44:36 +01:00
|
|
|
|
that.set.keys.each { |k|
|
2019-09-12 10:33:56 -07:00
|
|
|
|
k ∈ self || return false
|
2016-12-05 23:44:36 +01:00
|
|
|
|
}
|
2019-09-12 10:33:56 -07:00
|
|
|
|
return true
|
2016-12-05 23:44:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
method to_s {
|
|
|
|
|
|
"Set{" + set.values.map{|e| "#{e}"}.sort.join(', ') + "}"
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class Object {
|
2019-09-12 10:33:56 -07:00
|
|
|
|
method ∈(MySet set) {
|
|
|
|
|
|
set.has(self)
|
2016-12-05 23:44:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|