Skip to content

Commit 85b05cf

Browse files
authored
Merge pull request kodecocodes#276 from JaapWijnen/hashsetswift3
Hashset migrate to swift3
2 parents c783351 + 8328491 commit 85b05cf

File tree

3 files changed

+130
-122
lines changed

3 files changed

+130
-122
lines changed

Hash Set/HashSet.playground/Contents.swift

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,5 @@
11
//: Playground - noun: a place where people can play
22

3-
public struct HashSet<T: Hashable> {
4-
private var dictionary = Dictionary<T, Bool>()
5-
6-
public mutating func insert(element: T) {
7-
dictionary[element] = true
8-
}
9-
10-
public mutating func remove(element: T) {
11-
dictionary[element] = nil
12-
}
13-
14-
public func contains(element: T) -> Bool {
15-
return dictionary[element] != nil
16-
}
17-
18-
public func allElements() -> [T] {
19-
return Array(dictionary.keys)
20-
}
21-
22-
public var count: Int {
23-
return dictionary.count
24-
}
25-
26-
public var isEmpty: Bool {
27-
return dictionary.isEmpty
28-
}
29-
}
30-
313
var set = HashSet<String>()
324

335
set.insert("one")
@@ -43,24 +15,8 @@ set.remove("one")
4315
set.allElements()
4416
set.contains("one")
4517

46-
47-
4818
/* Union */
4919

50-
extension HashSet {
51-
public func union(otherSet: HashSet<T>) -> HashSet<T> {
52-
var combined = HashSet<T>()
53-
for obj in dictionary.keys {
54-
combined.insert(obj)
55-
}
56-
for obj in otherSet.dictionary.keys {
57-
combined.insert(obj)
58-
}
59-
return combined
60-
}
61-
}
62-
63-
6420
var setA = HashSet<Int>()
6521
setA.insert(1)
6622
setA.insert(2)
@@ -76,41 +32,13 @@ setB.insert(6)
7632
let union = setA.union(setB)
7733
union.allElements() // [5, 6, 2, 3, 1, 4]
7834

79-
80-
8135
/* Intersection */
8236

83-
extension HashSet {
84-
public func intersect(otherSet: HashSet<T>) -> HashSet<T> {
85-
var common = HashSet<T>()
86-
for obj in dictionary.keys {
87-
if otherSet.contains(obj) {
88-
common.insert(obj)
89-
}
90-
}
91-
return common
92-
}
93-
}
94-
9537
let intersection = setA.intersect(setB)
9638
intersection.allElements() // [3, 4]
9739

98-
99-
10040
/* Difference */
10141

102-
extension HashSet {
103-
public func difference(otherSet: HashSet<T>) -> HashSet<T> {
104-
var diff = HashSet<T>()
105-
for obj in dictionary.keys {
106-
if !otherSet.contains(obj) {
107-
diff.insert(obj)
108-
}
109-
}
110-
return diff
111-
}
112-
}
113-
11442
let difference1 = setA.difference(setB)
11543
difference1.allElements() // [2, 1]
11644

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//: Playground - noun: a place where people can play
2+
3+
public struct HashSet<T: Hashable> {
4+
fileprivate var dictionary = Dictionary<T, Bool>()
5+
6+
public init() {
7+
8+
}
9+
10+
public mutating func insert(_ element: T) {
11+
dictionary[element] = true
12+
}
13+
14+
public mutating func remove(_ element: T) {
15+
dictionary[element] = nil
16+
}
17+
18+
public func contains(_ element: T) -> Bool {
19+
return dictionary[element] != nil
20+
}
21+
22+
public func allElements() -> [T] {
23+
return Array(dictionary.keys)
24+
}
25+
26+
public var count: Int {
27+
return dictionary.count
28+
}
29+
30+
public var isEmpty: Bool {
31+
return dictionary.isEmpty
32+
}
33+
}
34+
35+
/* Union */
36+
37+
extension HashSet {
38+
public func union(_ otherSet: HashSet<T>) -> HashSet<T> {
39+
var combined = HashSet<T>()
40+
for obj in self.dictionary.keys {
41+
combined.insert(obj)
42+
}
43+
for obj in otherSet.dictionary.keys {
44+
combined.insert(obj)
45+
}
46+
return combined
47+
}
48+
}
49+
50+
/* Intersection */
51+
52+
extension HashSet {
53+
public func intersect(_ otherSet: HashSet<T>) -> HashSet<T> {
54+
var common = HashSet<T>()
55+
for obj in dictionary.keys {
56+
if otherSet.contains(obj) {
57+
common.insert(obj)
58+
}
59+
}
60+
return common
61+
}
62+
}
63+
64+
/* Difference */
65+
66+
extension HashSet {
67+
public func difference(_ otherSet: HashSet<T>) -> HashSet<T> {
68+
var diff = HashSet<T>()
69+
for obj in dictionary.keys {
70+
if !otherSet.contains(obj) {
71+
diff.insert(obj)
72+
}
73+
}
74+
return diff
75+
}
76+
}

Hash Set/README.markdown

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,35 @@ Here are the beginnings of `HashSet` in Swift:
3838

3939
```swift
4040
public struct HashSet<T: Hashable> {
41-
private var dictionary = Dictionary<T, Bool>()
42-
43-
public mutating func insert(element: T) {
44-
dictionary[element] = true
45-
}
46-
47-
public mutating func remove(element: T) {
48-
dictionary[element] = nil
49-
}
50-
51-
public func contains(element: T) -> Bool {
52-
return dictionary[element] != nil
53-
}
54-
55-
public func allElements() -> [T] {
56-
return Array(dictionary.keys)
57-
}
58-
59-
public var count: Int {
60-
return dictionary.count
61-
}
62-
63-
public var isEmpty: Bool {
64-
return dictionary.isEmpty
65-
}
41+
fileprivate var dictionary = Dictionary<T, Bool>()
42+
43+
public init() {
44+
45+
}
46+
47+
public mutating func insert(_ element: T) {
48+
dictionary[element] = true
49+
}
50+
51+
public mutating func remove(_ element: T) {
52+
dictionary[element] = nil
53+
}
54+
55+
public func contains(_ element: T) -> Bool {
56+
return dictionary[element] != nil
57+
}
58+
59+
public func allElements() -> [T] {
60+
return Array(dictionary.keys)
61+
}
62+
63+
public var count: Int {
64+
return dictionary.count
65+
}
66+
67+
public var isEmpty: Bool {
68+
return dictionary.isEmpty
69+
}
6670
}
6771
```
6872

@@ -101,16 +105,16 @@ Here is the code for the union operation:
101105

102106
```swift
103107
extension HashSet {
104-
public func union(otherSet: HashSet<T>) -> HashSet<T> {
105-
var combined = HashSet<T>()
106-
for obj in dictionary.keys {
107-
combined.insert(obj)
108-
}
109-
for obj in otherSet.dictionary.keys {
110-
combined.insert(obj)
108+
public func union(_ otherSet: HashSet<T>) -> HashSet<T> {
109+
var combined = HashSet<T>()
110+
for obj in self.dictionary.keys {
111+
combined.insert(obj)
112+
}
113+
for obj in otherSet.dictionary.keys {
114+
combined.insert(obj)
115+
}
116+
return combined
111117
}
112-
return combined
113-
}
114118
}
115119
```
116120

@@ -141,15 +145,15 @@ The *intersection* of two sets contains only the elements that they have in comm
141145

142146
```swift
143147
extension HashSet {
144-
public func intersect(otherSet: HashSet<T>) -> HashSet<T> {
145-
var common = HashSet<T>()
146-
for obj in dictionary.keys {
147-
if otherSet.contains(obj) {
148-
common.insert(obj)
149-
}
148+
public func intersect(_ otherSet: HashSet<T>) -> HashSet<T> {
149+
var common = HashSet<T>()
150+
for obj in dictionary.keys {
151+
if otherSet.contains(obj) {
152+
common.insert(obj)
153+
}
154+
}
155+
return common
150156
}
151-
return common
152-
}
153157
}
154158
```
155159

@@ -166,15 +170,15 @@ Finally, the *difference* between two sets removes the elements they have in com
166170

167171
```swift
168172
extension HashSet {
169-
public func difference(otherSet: HashSet<T>) -> HashSet<T> {
170-
var diff = HashSet<T>()
171-
for obj in dictionary.keys {
172-
if !otherSet.contains(obj) {
173-
diff.insert(obj)
174-
}
173+
public func difference(_ otherSet: HashSet<T>) -> HashSet<T> {
174+
var diff = HashSet<T>()
175+
for obj in dictionary.keys {
176+
if !otherSet.contains(obj) {
177+
diff.insert(obj)
178+
}
179+
}
180+
return diff
175181
}
176-
return diff
177-
}
178182
}
179183
```
180184

0 commit comments

Comments
 (0)