Skip to content

Commit 6c7eb0c

Browse files
author
Simon Whitaker
committed
Address review comments
1 parent d5c14d3 commit 6c7eb0c

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

Multiset/Multiset.playground/Sources/Multiset.swift

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,14 @@
88
import Foundation
99

1010
public struct Multiset<Element: Hashable> {
11-
fileprivate var storage: [Element: UInt]
12-
private var _count: UInt = 0
11+
private var storage: [Element: UInt] = [:]
12+
public private(set) var count: UInt = 0
1313

14-
public init() {
15-
storage = [:]
16-
}
14+
public init() {}
1715

1816
public mutating func add (_ elem: Element) {
19-
if let currentCount = storage[elem] {
20-
storage[elem] = currentCount + 1;
21-
} else {
22-
storage[elem] = 1
23-
}
24-
_count += 1
17+
storage[elem, default: 0] += 1
18+
count += 1
2519
}
2620

2721
public mutating func remove (_ elem: Element) {
@@ -31,7 +25,7 @@ public struct Multiset<Element: Hashable> {
3125
} else {
3226
storage.removeValue(forKey: elem)
3327
}
34-
_count -= 1
28+
count -= 1
3529
}
3630
}
3731

@@ -45,10 +39,6 @@ public struct Multiset<Element: Hashable> {
4539
return true
4640
}
4741

48-
public var count: UInt {
49-
return _count
50-
}
51-
5242
public func count(for key: Element) -> UInt {
5343
return storage[key] ?? 0
5444
}

Multiset/README.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,5 @@ public func count(for key: Element) -> UInt {
8787
return storage[key] ?? 0
8888
}
8989
```
90+
91+
*Written for the Swift Algorithm Club by Simon Whitaker*

0 commit comments

Comments
 (0)