54 lines
1.0 KiB
Plaintext
54 lines
1.0 KiB
Plaintext
Set = new map
|
|
Set["set"] = {}
|
|
|
|
Set.init = function(items)
|
|
set = new Set
|
|
set.set = {}
|
|
for item in items
|
|
set.add(item)
|
|
end for
|
|
return set
|
|
end function
|
|
|
|
Set.contains = function(item)
|
|
return self.set.hasIndex(item)
|
|
end function
|
|
|
|
Set.items = function
|
|
return self.set.indexes
|
|
end function
|
|
|
|
Set.add = function(item)
|
|
self.set[item] = true
|
|
end function
|
|
|
|
Set.union = function(other)
|
|
result = Set.init
|
|
result.set = self.set + other.set
|
|
return result
|
|
end function
|
|
|
|
Set.difference = function(other)
|
|
result = Set.init
|
|
for item in self.items
|
|
if not other.contains(item) then result.add(item)
|
|
end for
|
|
return result
|
|
end function
|
|
|
|
Set.symmetricDifference = function(other)
|
|
diff1 = self.difference(other)
|
|
diff2 = other.difference(self)
|
|
return diff1.union(diff2)
|
|
end function
|
|
|
|
a = ["John", "Serena", "Bob", "Mary", "Serena"]
|
|
b = ["Jim", "Mary", "John", "Jim", "Bob"]
|
|
|
|
A1 = Set.init(a)
|
|
B1 = Set.init(b)
|
|
|
|
print "A XOR B " + A1.symmetricDifference(B1).items
|
|
print "A - B " + A1.difference(B1).items
|
|
print "B - A " + B1.difference(A1).items
|