Skip to content

Commit 79cc5ed

Browse files
committed
[Basic] Conform OrderedSet to MutableCollection
1 parent 1c16b69 commit 79cc5ed

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

Sources/Basic/OrderedSet.swift

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
/// An ordered set is an ordered collection of instances of `Element` in which
1212
/// uniqueness of the objects is guaranteed.
13-
public struct OrderedSet<E: Hashable>: Equatable, Collection {
13+
public struct OrderedSet<E: Hashable>: Equatable, MutableCollection, RandomAccessCollection {
1414
public typealias Element = E
1515
public typealias Index = Int
1616
public typealias Indices = CountableRange<Int>
@@ -77,6 +77,39 @@ public struct OrderedSet<E: Hashable>: Equatable, Collection {
7777
array.removeAll(keepingCapacity: keepCapacity)
7878
set.removeAll(keepingCapacity: keepCapacity)
7979
}
80+
81+
// MARK:- MutableCollection, RandomAccessCollection conformance
82+
83+
public var startIndex: Int { return contents.startIndex }
84+
public var endIndex: Int { return contents.endIndex }
85+
86+
public subscript(position: Int) -> Element {
87+
get {
88+
return array[position]
89+
}
90+
set(newValue) {
91+
let oldValue = array[position]
92+
// Remove the old value from set.
93+
set.remove(oldValue)
94+
// Add the new value.
95+
array[position] = newValue
96+
set.insert(newValue)
97+
}
98+
}
99+
100+
public subscript(bounds: Range<Int>) -> ArraySlice<Element> {
101+
get {
102+
return array[bounds]
103+
}
104+
set(newValues) {
105+
let oldValues = array[bounds]
106+
// Remove the old values from set.
107+
oldValues.forEach{ set.remove($0) }
108+
// Add the new values.
109+
array[bounds] = newValues
110+
newValues.forEach{ set.insert($0) }
111+
}
112+
}
80113
}
81114

82115
extension OrderedSet: ExpressibleByArrayLiteral {
@@ -89,14 +122,6 @@ extension OrderedSet: ExpressibleByArrayLiteral {
89122
}
90123
}
91124

92-
extension OrderedSet: RandomAccessCollection {
93-
public var startIndex: Int { return contents.startIndex }
94-
public var endIndex: Int { return contents.endIndex }
95-
public subscript(i: Int) -> Element {
96-
return contents[i]
97-
}
98-
}
99-
100125
public func ==<T>(lhs: OrderedSet<T>, rhs: OrderedSet<T>) -> Bool {
101126
return lhs.contents == rhs.contents
102127
}

Tests/BasicTests/OrderedSetTests.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,34 @@ class OrderedSetTests: XCTestCase {
5151
XCTAssertEqual(set.contents, [])
5252
}
5353

54+
func testMutation() {
55+
var set = OrderedSet<Int>()
56+
set.append(1)
57+
set.append(2)
58+
set.append(3)
59+
XCTAssertEqual(set.contents, [1, 2, 3])
60+
61+
set[0] = 4
62+
XCTAssertEqual(set.contents, [4, 2, 3])
63+
XCTAssertFalse(set.contains(1))
64+
XCTAssert(set.contains(4))
65+
66+
set[2] = 9
67+
XCTAssertEqual(set.contents, [4, 2, 9])
68+
XCTAssertFalse(set.contains(3))
69+
XCTAssert(set.contains(9))
70+
71+
XCTAssertEqual(set[0..<2], [4, 2])
72+
set[0..<2] = [6, 7]
73+
XCTAssertEqual(set, [6, 7, 9])
74+
XCTAssertFalse(set.contains(4))
75+
XCTAssert(set.contains(6))
76+
XCTAssertFalse(set.contains(2))
77+
XCTAssert(set.contains(7))
78+
}
79+
5480
static var allTests = [
5581
("testBasics", testBasics),
82+
("testMutation", testMutation),
5683
]
5784
}

0 commit comments

Comments
 (0)