Skip to content

Commit fd2a699

Browse files
committed
[Basic] Remove OrderedSet MutableCollection conformance
The semantics for MutableCollection and RandomAccessCollection are proving to be undefined in edge cases and right now we don't really need them as cycle detection could be implemented without it. This basically reverts: 04781a2 and 79cc5ed
1 parent 10aa409 commit fd2a699

File tree

2 files changed

+9
-74
lines changed

2 files changed

+9
-74
lines changed

Sources/Basic/OrderedSet.swift

Lines changed: 9 additions & 40 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, MutableCollection, RandomAccessCollection {
13+
public struct OrderedSet<E: Hashable>: Equatable, Collection {
1414
public typealias Element = E
1515
public typealias Index = Int
1616
public typealias Indices = CountableRange<Int>
@@ -77,45 +77,6 @@ public struct OrderedSet<E: Hashable>: Equatable, MutableCollection, RandomAcces
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-
}
113-
}
114-
115-
extension OrderedSet: RangeReplaceableCollection {
116-
mutating public func replaceSubrange<C>(_ subrange: Range<Int>, with newElements: C) where C : Collection, C.Iterator.Element == Element {
117-
self[subrange] = ArraySlice(newElements)
118-
}
11980
}
12081

12182
extension OrderedSet: ExpressibleByArrayLiteral {
@@ -128,6 +89,14 @@ extension OrderedSet: ExpressibleByArrayLiteral {
12889
}
12990
}
13091

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+
131100
public func ==<T>(lhs: OrderedSet<T>, rhs: OrderedSet<T>) -> Bool {
132101
return lhs.contents == rhs.contents
133102
}

Tests/BasicTests/OrderedSetTests.swift

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -51,41 +51,7 @@ 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-
set.replaceSubrange(0..<2, with: [1, 2])
80-
XCTAssertEqual(set.contents, [1, 2, 9])
81-
XCTAssertFalse(set.contains(6))
82-
XCTAssert(set.contains(1))
83-
XCTAssertFalse(set.contains(7))
84-
XCTAssert(set.contains(2))
85-
}
86-
8754
static var allTests = [
8855
("testBasics", testBasics),
89-
("testMutation", testMutation),
9056
]
9157
}

0 commit comments

Comments
 (0)