-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflset.rb
88 lines (88 loc) · 1.63 KB
/
flset.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
class FLSet
attr_accessor :arr
def initialize()
@arr = Array.new()
end
def isEmpty()
if (@arr.length == 0) then
return true
else
return false
end
end
def contains(value)
return @arr.include?(value)
end
def insert(value)
if (not self.contains(value)) then
@arr.push(value)
end
end
def remove(value)
if (@arr.include?(value)) then
@arr.delete(value)
end
end
def subtract(anotherSet)
raise "Invalid param" if (not(anotherSet.is_a? FLSet))
ret = FLSet.new()
if (anotherSet.isEmpty() or self.isEmpty()) then
return self
else
for elem in @arr do
if (not anotherSet.contains(elem)) then
ret.insert(elem)
end
end
end
return ret
end
def intersect(anotherSet)
raise "Invalid param" if (not(anotherSet.is_a? FLSet))
ret = FLSet.new()
if (anotherSet.isEmpty() or self.isEmpty()) then
return ret
else
for elem in @arr do
if (anotherSet.contains(elem)) then
ret.insert(elem)
end
end
end
return ret
end
def union(anotherSet)
raise "Invalid param" if (not(anotherSet.is_a? FLSet))
ret = FLSet.new()
for elem in @arr do
ret.insert(elem)
end
for elem in anotherSet.to_a() do
ret.insert(elem)
end
return ret
end
def exclusiveOr(anotherSet)
raise "Invalid param" if (not(anotherSet.is_a? FLSet))
ret = FLSet.new()
for elem in @arr do
if (not anotherSet.contains(elem)) then
ret.insert(elem)
end
end
for elem in anotherSet.to_a() do
if (not self.contains(elem)) then
ret.insert(elem)
end
end
return ret
end
def to_a()
ret = Array.new()
for e in @arr do
ret.push(e)
end
return ret
end
private :arr, :arr=
end