forked from apple/swift-collections
/
MinimalMutableRandomAccessCollection.swift
182 lines (156 loc) · 5.84 KB
/
MinimalMutableRandomAccessCollection.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// Loosely adapted from https://github.com/apple/swift/tree/main/stdlib/private/StdlibCollectionUnittest
/// A minimal implementation of `RandomAccessCollection & MutableCollection` with extra checks.
public struct MinimalMutableRandomAccessCollection<Element> {
internal var _core: _MinimalCollectionCore<Element>
public let timesMakeIteratorCalled = ResettableValue(0)
public let timesUnderestimatedCountCalled = ResettableValue(0)
public let timesRangeChecksCalled = ResettableValue(0)
public let timesIndexNavigationCalled = ResettableValue(0)
public let timesSubscriptGetterCalled = ResettableValue(0)
public let timesSubscriptSetterCalled = ResettableValue(0)
public let timesRangeSubscriptGetterCalled = ResettableValue(0)
public let timesRangeSubscriptSetterCalled = ResettableValue(0)
public let timesSwapCalled = ResettableValue(0)
public let timesPartitionCalled = ResettableValue(0)
public init<S: Sequence>(
_ elements: S,
context: TestContext = TestContext.current,
underestimatedCount: UnderestimatedCountBehavior = .value(0)
) where S.Element == Element {
self._core = _MinimalCollectionCore(context: context, elements: elements, underestimatedCount: underestimatedCount)
}
var _context: TestContext {
_core.context
}
}
extension MinimalMutableRandomAccessCollection: Sequence {
public typealias Iterator = MinimalIterator<Element>
public func makeIterator() -> MinimalIterator<Element> {
timesMakeIteratorCalled.increment()
return MinimalIterator(_core.elements)
}
public var underestimatedCount: Int {
timesUnderestimatedCountCalled.increment()
return _core.underestimatedCount
}
}
extension MinimalMutableRandomAccessCollection: RandomAccessCollection {
public typealias Index = MinimalIndex
public typealias SubSequence = Slice<Self>
public typealias Indices = DefaultIndices<Self>
public var startIndex: MinimalIndex {
timesIndexNavigationCalled.increment()
return _core.startIndex
}
public var endIndex: MinimalIndex {
timesIndexNavigationCalled.increment()
return _core.endIndex
}
public var isEmpty: Bool {
timesIndexNavigationCalled.increment()
return _core.isEmpty
}
public var count: Int {
timesIndexNavigationCalled.increment()
return _core.count
}
public func _failEarlyRangeCheck(
_ index: MinimalIndex,
bounds: Range<MinimalIndex>
) {
timesRangeChecksCalled.increment()
_core._failEarlyRangeCheck(index, bounds: bounds)
}
public func _failEarlyRangeCheck(
_ range: Range<MinimalIndex>,
bounds: Range<MinimalIndex>
) {
timesRangeChecksCalled.increment()
_core._failEarlyRangeCheck(range, bounds: bounds)
}
public func index(after i: MinimalIndex) -> MinimalIndex {
timesIndexNavigationCalled.increment()
return _core.index(after: i)
}
public func index(before i: MinimalIndex) -> MinimalIndex {
timesIndexNavigationCalled.increment()
return _core.index(before: i)
}
public func distance(from start: MinimalIndex, to end: MinimalIndex)
-> Int {
timesIndexNavigationCalled.increment()
return _core.distance(from: start, to: end)
}
public func index(_ i: Index, offsetBy n: Int) -> Index {
timesIndexNavigationCalled.increment()
return _core.index(i, offsetBy: n)
}
public subscript(i: MinimalIndex) -> Element {
get {
timesSubscriptGetterCalled.increment()
return _core[i]
}
set {
timesSubscriptSetterCalled.increment()
_core[i] = newValue
}
}
public subscript(bounds: Range<MinimalIndex>) -> SubSequence {
get {
timesRangeSubscriptGetterCalled.increment()
_core.assertValid(bounds)
return Slice(base: self, bounds: bounds)
}
set {
timesRangeSubscriptSetterCalled.increment()
_core.assertValid(bounds)
expectEqual(bounds.upperBound._offset - bounds.lowerBound._offset,
newValue.endIndex._offset - newValue.startIndex._offset,
"Range assignment cannot change number of elements in MutableCollection")
_core.elements[bounds.lowerBound._offset ..< bounds.upperBound._offset] =
Self._coreSlice(from: newValue)
// Don't invalidate indices.
}
}
static func _coreSlice(from slice: SubSequence) -> ArraySlice<Element> {
slice.base._core.assertValid(slice.startIndex ..< slice.endIndex)
return slice.base._core.elements[slice.startIndex._offset ..< slice.endIndex._offset]
}
}
extension MinimalMutableRandomAccessCollection: MutableCollection {
public mutating func partition(
by belongsInSecondPartition: (Element) throws -> Bool
) rethrows -> Index {
timesPartitionCalled.increment()
let pivot = try _core.elements.partition(by: belongsInSecondPartition)
return _core.index(at: pivot)
}
public mutating func swapAt(_ i: Index, _ j: Index) {
_core.assertValidIndexBeforeEnd(i)
_core.assertValidIndexBeforeEnd(j)
timesSwapCalled.increment()
_core.elements.swapAt(i._offset, j._offset)
// Don't invalidate indices.
}
public mutating func _withUnsafeMutableBufferPointerIfSupported<R>(
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
) rethrows -> R? {
return nil
}
public mutating func withContiguousMutableStorageIfAvailable<R>(
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
) rethrows -> R? {
return nil
}
}