-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSet.swift
45 lines (38 loc) · 1.04 KB
/
Set.swift
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
//
// Set.swift
//
// Advent of Code Tools
//
extension Set {
public static func + (lhs: Set, rhs: Set) -> Set {
lhs.union(rhs)
}
public static func += (lhs: inout Set, rhs: Set) {
lhs = lhs.union(rhs)
}
public static func + (lhs: Set, rhs: Set.Element) -> Set {
lhs.union([rhs])
}
public static func += (lhs: inout Set, rhs: Set.Element) {
lhs = lhs.union([rhs])
}
public static func - (lhs: Set, rhs: Set) -> Set {
lhs.subtracting(rhs)
}
public static func - (lhs: Set, rhs: Set.Element) -> Set {
lhs.subtracting([rhs])
}
}
extension Set {
public func combinations() -> [Set<Element>] {
var allCombinations = [Set<Element>]()
for element in self {
let oneElementCombo = Set([element])
for i in 0..<allCombinations.count {
allCombinations.append(allCombinations[i] + oneElementCombo)
}
allCombinations.append(oneElementCombo)
}
return allCombinations
}
}