Skip to content

Commit 4fbe62b

Browse files
author
Simon Whitaker
committed
Adopt Ray Wenderlich Swift coding style
https://github.com/raywenderlich/swift-style-guide
1 parent 8fcb7e4 commit 4fbe62b

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

Multiset/Multiset.playground/Sources/Multiset.swift

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,36 @@
77

88
import Foundation
99

10-
public struct Multiset<T: Hashable> {
11-
fileprivate var storage: Dictionary<T, Int>
12-
private var _count: Int = 0
10+
public struct Multiset<Element: Hashable> {
11+
fileprivate var storage: [Element: Int]
12+
private var _count = 0
1313

1414
public init() {
15-
storage = Dictionary<T, Int>()
15+
storage = [:]
1616
}
1717

18-
public mutating func add (_ elem: T) {
19-
if let currentCount = self.storage[elem] {
20-
self.storage[elem] = currentCount + 1;
18+
public mutating func add (_ elem: Element) {
19+
if let currentCount = storage[elem] {
20+
storage[elem] = currentCount + 1;
2121
} else {
22-
self.storage[elem] = 1
22+
storage[elem] = 1
2323
}
2424
_count += 1
2525
}
2626

27-
public mutating func remove (_ elem: T) {
28-
if let currentCount = self.storage[elem] {
27+
public mutating func remove (_ elem: Element) {
28+
if let currentCount = storage[elem] {
2929
if currentCount > 1 {
30-
self.storage[elem] = currentCount - 1
30+
storage[elem] = currentCount - 1
3131
} else {
32-
self.storage.removeValue(forKey: elem)
32+
storage.removeValue(forKey: elem)
3333
}
3434
_count -= 1
3535
}
3636
}
3737

38-
public func isSubSet (of superset: Multiset<T>) -> Bool {
39-
for (key, count) in self.storage {
38+
public func isSubSet (of superset: Multiset<Element>) -> Bool {
39+
for (key, count) in storage {
4040
let supersetcount = superset.storage[key] ?? 0
4141
if count > supersetcount {
4242
return false
@@ -46,30 +46,27 @@ public struct Multiset<T: Hashable> {
4646
}
4747

4848
public var count: Int {
49-
get {
50-
return _count
51-
}
49+
return _count
5250
}
5351

54-
public func count(for key: T) -> Int {
55-
return self.storage[key] ?? 0
52+
public func count(for key: Element) -> Int {
53+
return storage[key] ?? 0
5654
}
5755

58-
public var allItems: [T] {
59-
get {
60-
var result = Array<T>()
61-
for (key, count) in self.storage {
62-
for _ in 0 ..< count {
63-
result.append(key)
64-
}
56+
public var allItems: [Element] {
57+
var result = Array<Element>()
58+
for (key, count) in storage {
59+
for _ in 0 ..< count {
60+
result.append(key)
6561
}
66-
return result
6762
}
63+
return result
6864
}
6965
}
7066

67+
// MARK: - Equatable
7168
extension Multiset: Equatable {
72-
public static func == (lhs: Multiset<T>, rhs: Multiset<T>) -> Bool {
69+
public static func == (lhs: Multiset<Element>, rhs: Multiset<Element>) -> Bool {
7370
if lhs.storage.count != rhs.storage.count {
7471
return false
7572
}

0 commit comments

Comments
 (0)