/
SliceBuffer.swift
332 lines (291 loc) · 9.74 KB
/
SliceBuffer.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//===--- SliceBuffer.swift - Backing storage for ArraySlice<T> ------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
/// Buffer type for ArraySlice<T>
public
struct _SliceBuffer<T> : _ArrayBufferType {
typealias Element = T
typealias NativeStorage = _ContiguousArrayStorage<T>
typealias NativeBuffer = _ContiguousArrayBuffer<T>
init(owner: AnyObject, start: UnsafeMutablePointer<T>, count: Int,
hasNativeBuffer: Bool) {
self.owner = owner
self.start = start
self._countAndFlags = (UInt(count) << 1) | (hasNativeBuffer ? 1 : 0)
}
public
init() {
let empty = _ContiguousArrayBuffer<T>()
owner = empty.owner
start = empty.baseAddress
_countAndFlags = 1
_invariantCheck()
}
public
init(_ buffer: NativeBuffer) {
owner = buffer.owner
start = buffer.baseAddress
_countAndFlags = (UInt(buffer.count) << 1) | 1
_invariantCheck()
}
func _invariantCheck() {
let isNative = _hasNativeBuffer
let isNativeStorage: Bool = (owner as? _ContiguousArrayStorageBase) != nil
_sanityCheck(isNativeStorage == isNative)
if isNative {
_sanityCheck(count <= nativeBuffer.count)
}
}
var _hasNativeBuffer: Bool {
return (_countAndFlags & 1) != 0
}
var nativeBuffer: NativeBuffer {
_sanityCheck(_hasNativeBuffer)
return NativeBuffer(
owner as? _ContiguousArrayStorageBase ?? _emptyArrayStorage)
}
/// Replace the given subRange with the first newCount elements of
/// the given collection.
///
/// Requires: this buffer is backed by a uniquely-referenced
/// _ContiguousArrayBuffer,
///
/// Requires: insertCount <= numericCast(count(newValues))
///
public
mutating func replace<C: CollectionType where C.Generator.Element == T>(
#subRange: Range<Int>, with insertCount: Int, elementsOf newValues: C
) {
_invariantCheck()
// FIXME: <rdar://problem/17464946> with
// -DSWIFT_STDLIB_ASSERTIONS=FALSE, enabling this sanityCheck
// actually causes leaks in the stdlib/NewArray.swift.gyb test
/* _sanityCheck(insertCount <= numericCast(count(newValues))) */
_sanityCheck(_hasNativeBuffer && isUniquelyReferenced())
var native = nativeBuffer
let offset = start - native.baseAddress
let eraseCount = Swift.count(subRange)
let growth = insertCount - eraseCount
let oldCount = count
_sanityCheck(native.count + growth <= native.capacity)
native.replace(
subRange: (subRange.startIndex+offset)..<(subRange.endIndex + offset),
with: insertCount,
elementsOf: newValues)
setLocalCount(oldCount + growth)
_invariantCheck()
}
/// A value that identifies the storage used by the buffer. Two
/// buffers address the same elements when they have the same
/// identity and count.
public var identity: UnsafePointer<Void> {
return UnsafePointer(start)
}
/// An object that keeps the elements stored in this buffer alive
public
var owner: AnyObject
var start: UnsafeMutablePointer<T>
var _countAndFlags: UInt
//===--- Non-essential bits ---------------------------------------------===//
public
mutating func requestUniqueMutableBackingBuffer(minimumCapacity: Int)
-> NativeBuffer?
{
_invariantCheck()
if _fastPath(_hasNativeBuffer && isUniquelyReferenced()) {
if capacity >= minimumCapacity {
// Since we have the last reference, drop any inaccessible
// trailing elements in the underlying storage. That will
// tend to reduce shuffling of later elements. Since this
// function isn't called for subscripting, this won't slow
// down that case.
var native = nativeBuffer
let offset = self.baseAddress - native.baseAddress
let backingCount = native.count
let myCount = count
if _slowPath(backingCount > myCount + offset) {
native.replace(
subRange: (myCount+offset)..<backingCount,
with: 0,
elementsOf: EmptyCollection())
}
_invariantCheck()
return native
}
}
return nil
}
public
mutating func isMutableAndUniquelyReferenced() -> Bool {
return _hasNativeBuffer && isUniquelyReferenced()
}
public
mutating func isMutableAndUniquelyReferencedOrPinned() -> Bool {
return _hasNativeBuffer && isUniquelyReferencedOrPinned()
}
/// If this buffer is backed by a `_ContiguousArrayBuffer`
/// containing the same number of elements as `self`, return it.
/// Otherwise, return `nil`.
public
func requestNativeBuffer() -> _ContiguousArrayBuffer<Element>? {
_invariantCheck()
if _fastPath(_hasNativeBuffer && nativeBuffer.count == count) {
return nativeBuffer
}
return nil
}
public
func _uninitializedCopy(
subRange: Range<Int>, var target: UnsafeMutablePointer<T>
) -> UnsafeMutablePointer<T> {
_invariantCheck()
_sanityCheck(subRange.startIndex >= 0)
_sanityCheck(subRange.endIndex >= subRange.startIndex)
_sanityCheck(subRange.endIndex <= count)
let c = subRange.endIndex - subRange.startIndex
target.initializeFrom(start + subRange.startIndex, count: c)
return target + c
}
internal func _getBaseAddress() -> UnsafeMutablePointer<T> {
return baseAddress
}
public
var baseAddress: UnsafeMutablePointer<T> {
return start
}
var arrayPropertyIsNative : Bool {
return _hasNativeBuffer
}
var arrayPropertyNeedsElementTypeCheck : Bool {
return false
}
public
var count: Int {
get {
return Int(_countAndFlags >> 1)
}
set {
let growth = newValue - count
if growth != 0 {
nativeBuffer.count += growth
setLocalCount(newValue)
}
_invariantCheck()
}
}
/// Return whether the given `index` is valid for subscripting, i.e. `0
/// ≤ index < count`
internal func _isValidSubscript(index : Int,
hoistedIsNativeBuffer: Bool) -> Bool {
return index >= 0 && index < count
}
/// Modify the count in this buffer without a corresponding change
/// in the underlying nativeBuffer. The implementation of replace()
/// uses this, because it does a wholesale replace in the underlying
/// buffer.
mutating func setLocalCount(newValue: Int) {
_countAndFlags = (UInt(newValue) << 1) | (_countAndFlags & 1)
}
public
var capacity: Int {
let count = self.count
if _slowPath(!_hasNativeBuffer) {
return count
}
let n = nativeBuffer
if (count + start) == (n.count + n.baseAddress) {
return count + (n.capacity - n.count)
}
return count
}
mutating func isUniquelyReferenced() -> Bool {
return isUniquelyReferencedNonObjC(&owner)
}
mutating func isUniquelyReferencedOrPinned() -> Bool {
return isUniquelyReferencedOrPinnedNonObjC(&owner)
}
func getElement(i: Int, hoistedIsNativeBuffer: Bool,
hoistedNeedsElementTypeCheck: Bool) -> T {
_sanityCheck(i >= 0, "negative slice index is out of range")
_sanityCheck(i < count, "slice index out of range")
return start[i]
}
/// Access the element at `position`.
///
/// Requires: `position` is a valid position in `self` and
/// `position != endIndex`.
public subscript(position: Int) -> T {
get {
return getElement(position, hoistedIsNativeBuffer: true,
hoistedNeedsElementTypeCheck: false)
}
nonmutating set {
_sanityCheck(position >= 0, "negative slice index is out of range")
_sanityCheck(position < count, "slice index out of range")
start[position] = newValue
}
}
public
subscript (subRange: Range<Int>) -> _SliceBuffer {
_sanityCheck(subRange.startIndex >= 0)
_sanityCheck(subRange.endIndex >= subRange.startIndex)
_sanityCheck(subRange.endIndex <= count)
return _SliceBuffer(
owner: owner, start: start + subRange.startIndex,
count: subRange.endIndex - subRange.startIndex,
hasNativeBuffer: _hasNativeBuffer)
}
//===--- CollectionType conformance -------------------------------------===//
/// The position of the first element in a non-empty collection.
///
/// Identical to `endIndex` in an empty collection.
public
var startIndex: Int {
return 0
}
/// The collection's "past the end" position.
///
/// `endIndex` is not a valid argument to `subscript`, and is always
/// reachable from `startIndex` by zero or more applications of
/// `successor()`.
public
var endIndex: Int {
return count
}
/// Return a *generator* over the elements of this *sequence*.
///
/// Complexity: O(1)
public func generate() -> IndexingGenerator<_SliceBuffer> {
return IndexingGenerator(self)
}
//===--- misc -----------------------------------------------------------===//
/// Call `body(p)`, where `p` is an `UnsafeBufferPointer` over the
/// underlying contiguous storage.
public
func withUnsafeBufferPointer<R>(
@noescape body: (UnsafeBufferPointer<Element>) -> R
) -> R {
let ret = body(UnsafeBufferPointer(start: self.baseAddress, count: count))
_fixLifetime(self)
return ret
}
/// Call `body(p)`, where `p` is an `UnsafeMutableBufferPointer`
/// over the underlying contiguous storage.
public
mutating func withUnsafeMutableBufferPointer<R>(
@noescape body: (UnsafeMutableBufferPointer<T>) -> R
) -> R {
let ret = body(
UnsafeMutableBufferPointer(start: baseAddress, count: count))
_fixLifetime(self)
return ret
}
}