Skip to content

stdlib: make Array implementation internal, part 2 #4085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 40 additions & 40 deletions stdlib/public/core/ArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ internal typealias _ArrayBridgeStorage
= _BridgeStorage<_ContiguousArrayStorageBase, _NSArrayCore>

@_fixed_layout
public struct _ArrayBuffer<Element> : _ArrayBufferProtocol {
internal struct _ArrayBuffer<Element> : _ArrayBufferProtocol {

/// Create an empty buffer.
public init() {
internal init() {
_storage = _ArrayBridgeStorage(native: _emptyArrayStorage)
}

public init(nsArray: _NSArrayCore) {
internal init(nsArray: _NSArrayCore) {
_sanityCheck(_isClassOrObjCExistential(Element.self))
_storage = _ArrayBridgeStorage(objC: nsArray)
}
Expand All @@ -46,7 +46,7 @@ public struct _ArrayBuffer<Element> : _ArrayBufferProtocol {

/// The spare bits that are set when a native array needs deferred
/// element type checking.
var deferredTypeCheckMask: Int { return 1 }
internal var deferredTypeCheckMask: Int { return 1 }

/// Returns an `_ArrayBuffer<U>` containing the same elements,
/// deferring checking each element's `U`-ness until it is accessed.
Expand All @@ -68,7 +68,7 @@ public struct _ArrayBuffer<Element> : _ArrayBufferProtocol {
native: _native._storage, bits: deferredTypeCheckMask))
}

var needsElementTypeCheck: Bool {
internal var needsElementTypeCheck: Bool {
// NSArray's need an element typecheck when the element type isn't AnyObject
return !_isNativeTypeChecked && !(AnyObject.self is Element.Type)
}
Expand All @@ -83,18 +83,18 @@ public struct _ArrayBuffer<Element> : _ArrayBufferProtocol {

extension _ArrayBuffer {
/// Adopt the storage of `source`.
public init(_buffer source: NativeBuffer, shiftedToStartIndex: Int) {
internal init(_buffer source: NativeBuffer, shiftedToStartIndex: Int) {
_sanityCheck(shiftedToStartIndex == 0, "shiftedToStartIndex must be 0")
_storage = _ArrayBridgeStorage(native: source._storage)
}

/// `true`, if the array is native and does not need a deferred type check.
var arrayPropertyIsNativeTypeChecked: Bool {
internal var arrayPropertyIsNativeTypeChecked: Bool {
return _isNativeTypeChecked
}

/// Returns `true` iff this buffer's storage is uniquely-referenced.
mutating func isUniquelyReferenced() -> Bool {
internal mutating func isUniquelyReferenced() -> Bool {
if !_isClassOrObjCExistential(Element.self) {
return _storage.isUniquelyReferenced_native_noSpareBits()
}
Expand All @@ -103,7 +103,7 @@ extension _ArrayBuffer {

/// Returns `true` iff this buffer's storage is either
/// uniquely-referenced or pinned.
mutating func isUniquelyReferencedOrPinned() -> Bool {
internal mutating func isUniquelyReferencedOrPinned() -> Bool {
if !_isClassOrObjCExistential(Element.self) {
return _storage.isUniquelyReferencedOrPinned_native_noSpareBits()
}
Expand All @@ -113,15 +113,15 @@ extension _ArrayBuffer {
/// Convert to an NSArray.
///
/// O(1) if the element type is bridged verbatim, O(N) otherwise.
public func _asCocoaArray() -> _NSArrayCore {
internal func _asCocoaArray() -> _NSArrayCore {
return _fastPath(_isNative) ? _native._asCocoaArray() : _nonNative
}

/// If this buffer is backed by a uniquely-referenced mutable
/// `_ContiguousArrayBuffer` that can be grown in-place to allow the self
/// buffer store minimumCapacity elements, returns that buffer.
/// Otherwise, returns `nil`.
public mutating func requestUniqueMutableBackingBuffer(minimumCapacity: Int)
internal mutating func requestUniqueMutableBackingBuffer(minimumCapacity: Int)
-> NativeBuffer? {
if _fastPath(isUniquelyReferenced()) {
let b = _native
Expand All @@ -132,18 +132,18 @@ extension _ArrayBuffer {
return nil
}

public mutating func isMutableAndUniquelyReferenced() -> Bool {
internal mutating func isMutableAndUniquelyReferenced() -> Bool {
return isUniquelyReferenced()
}

public mutating func isMutableAndUniquelyReferencedOrPinned() -> Bool {
internal mutating func isMutableAndUniquelyReferencedOrPinned() -> Bool {
return 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() -> NativeBuffer? {
internal func requestNativeBuffer() -> NativeBuffer? {
if !_isClassOrObjCExistential(Element.self) {
return _native
}
Expand All @@ -170,7 +170,7 @@ extension _ArrayBuffer {
}
}

func _typeCheck(_ subRange: Range<Int>) {
internal func _typeCheck(_ subRange: Range<Int>) {
if !_isClassOrObjCExistential(Element.self) {
return
}
Expand All @@ -189,7 +189,7 @@ extension _ArrayBuffer {
/// memory starting at `target`. Return a pointer "past the end" of the
/// just-initialized memory.
@discardableResult
public func _copyContents(
internal func _copyContents(
subRange bounds: Range<Int>,
initializing target: UnsafeMutablePointer<Element>
) -> UnsafeMutablePointer<Element> {
Expand Down Expand Up @@ -221,7 +221,7 @@ extension _ArrayBuffer {

/// Returns a `_SliceBuffer` containing the given sub-range of elements in
/// `bounds` from this buffer.
public subscript(bounds: Range<Int>) -> _SliceBuffer<Element> {
internal subscript(bounds: Range<Int>) -> _SliceBuffer<Element> {
get {
_typeCheck(bounds)

Expand Down Expand Up @@ -254,7 +254,7 @@ extension _ArrayBuffer {

// No contiguous storage found; we must allocate
let result = _ContiguousArrayBuffer<Element>(
uninitializedCount: boundsCount, minimumCapacity: 0)
_uninitializedCount: boundsCount, minimumCapacity: 0)

// Tell Cocoa to copy the objects into our storage
cocoa.buffer.getObjects(
Expand All @@ -273,17 +273,17 @@ extension _ArrayBuffer {
/// A pointer to the first element.
///
/// - Precondition: The elements are known to be stored contiguously.
public var firstElementAddress: UnsafeMutablePointer<Element> {
internal var firstElementAddress: UnsafeMutablePointer<Element> {
_sanityCheck(_isNative, "must be a native buffer")
return _native.firstElementAddress
}

public var firstElementAddressIfContiguous: UnsafeMutablePointer<Element>? {
internal var firstElementAddressIfContiguous: UnsafeMutablePointer<Element>? {
return _fastPath(_isNative) ? firstElementAddress : nil
}

/// The number of elements the buffer stores.
public var count: Int {
internal var count: Int {
@inline(__always)
get {
return _fastPath(_isNative) ? _native.count : _nonNative.count
Expand Down Expand Up @@ -331,13 +331,13 @@ extension _ArrayBuffer {
}

/// The number of elements the buffer can store without reallocation.
public var capacity: Int {
internal var capacity: Int {
return _fastPath(_isNative) ? _native.capacity : _nonNative.count
}

@_versioned
@inline(__always)
func getElement(_ i: Int, wasNativeTypeChecked: Bool) -> Element {
internal func getElement(_ i: Int, wasNativeTypeChecked: Bool) -> Element {
if _fastPath(wasNativeTypeChecked) {
return _nativeTypeChecked[i]
}
Expand All @@ -346,7 +346,7 @@ extension _ArrayBuffer {

@_versioned
@inline(never)
func _getElementSlowPath(_ i: Int) -> AnyObject {
internal func _getElementSlowPath(_ i: Int) -> AnyObject {
_sanityCheck(
_isClassOrObjCExistential(Element.self),
"Only single reference elements can be indexed here.")
Expand All @@ -372,7 +372,7 @@ extension _ArrayBuffer {
}

/// Get or set the value of the ith element.
public subscript(i: Int) -> Element {
internal subscript(i: Int) -> Element {
get {
return getElement(i, wasNativeTypeChecked: _isNativeTypeChecked)
}
Expand All @@ -394,7 +394,7 @@ extension _ArrayBuffer {
/// Call `body(p)`, where `p` is an `UnsafeBufferPointer` over the
/// underlying contiguous storage. If no such storage exists, it is
/// created on-demand.
public func withUnsafeBufferPointer<R>(
internal func withUnsafeBufferPointer<R>(
_ body: (UnsafeBufferPointer<Element>) throws -> R
) rethrows -> R {
if _fastPath(_isNative) {
Expand All @@ -409,7 +409,7 @@ extension _ArrayBuffer {
/// over the underlying contiguous storage.
///
/// - Precondition: Such contiguous storage exists or the buffer is empty.
public mutating func withUnsafeMutableBufferPointer<R>(
internal mutating func withUnsafeMutableBufferPointer<R>(
_ body: (UnsafeMutableBufferPointer<Element>) throws -> R
) rethrows -> R {
_sanityCheck(
Expand All @@ -422,22 +422,22 @@ extension _ArrayBuffer {
}

/// An object that keeps the elements stored in this buffer alive.
public var owner: AnyObject {
internal var owner: AnyObject {
return _fastPath(_isNative) ? _native._storage : _nonNative
}

/// An object that keeps the elements stored in this buffer alive.
///
/// - Precondition: This buffer is backed by a `_ContiguousArrayBuffer`.
public var nativeOwner: AnyObject {
internal var nativeOwner: AnyObject {
_sanityCheck(_isNative, "Expect a native array")
return _native._storage
}

/// 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: UnsafeRawPointer {
internal var identity: UnsafeRawPointer {
if _isNative {
return _native.identity
}
Expand All @@ -450,7 +450,7 @@ extension _ArrayBuffer {
/// The position of the first element in a non-empty collection.
///
/// In an empty collection, `startIndex == endIndex`.
public var startIndex: Int {
internal var startIndex: Int {
return 0
}

Expand All @@ -459,18 +459,18 @@ extension _ArrayBuffer {
/// `endIndex` is not a valid argument to `subscript`, and is always
/// reachable from `startIndex` by zero or more applications of
/// `index(after:)`.
public var endIndex: Int {
internal var endIndex: Int {
return count
}

public typealias Indices = CountableRange<Int>
internal typealias Indices = CountableRange<Int>

//===--- private --------------------------------------------------------===//
typealias Storage = _ContiguousArrayStorage<Element>
public typealias NativeBuffer = _ContiguousArrayBuffer<Element>
internal typealias Storage = _ContiguousArrayStorage<Element>
internal typealias NativeBuffer = _ContiguousArrayBuffer<Element>

@_versioned
var _isNative: Bool {
internal var _isNative: Bool {
if !_isClassOrObjCExistential(Element.self) {
return true
} else {
Expand All @@ -479,7 +479,7 @@ extension _ArrayBuffer {
}

/// `true`, if the array is native and does not need a deferred type check.
var _isNativeTypeChecked: Bool {
internal var _isNativeTypeChecked: Bool {
if !_isClassOrObjCExistential(Element.self) {
return true
} else {
Expand All @@ -491,7 +491,7 @@ extension _ArrayBuffer {
///
/// - Precondition: `_isNative`.
@_versioned
var _native: NativeBuffer {
internal var _native: NativeBuffer {
return NativeBuffer(
_isClassOrObjCExistential(Element.self)
? _storage.nativeInstance : _storage.nativeInstance_noSpareBits)
Expand All @@ -501,12 +501,12 @@ extension _ArrayBuffer {
///
/// - Precondition: `_isNativeTypeChecked`.
@_versioned
var _nativeTypeChecked: NativeBuffer {
internal var _nativeTypeChecked: NativeBuffer {
return NativeBuffer(_storage.nativeInstance_noSpareBits)
}

@_versioned
var _nonNative: _NSArrayCore {
internal var _nonNative: _NSArrayCore {
@inline(__always)
get {
_sanityCheck(_isClassOrObjCExistential(Element.self))
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/core/ArrayBufferProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

/// The underlying buffer for an ArrayType conforms to
/// `_ArrayBufferProtocol`. This buffer does not provide value semantics.
public protocol _ArrayBufferProtocol
internal protocol _ArrayBufferProtocol
: MutableCollection, RandomAccessCollection {

associatedtype Indices : RandomAccessCollection = CountableRange<Int>
Expand Down Expand Up @@ -127,11 +127,11 @@ public protocol _ArrayBufferProtocol

extension _ArrayBufferProtocol where Index == Int {

public var subscriptBaseAddress: UnsafeMutablePointer<Element> {
internal var subscriptBaseAddress: UnsafeMutablePointer<Element> {
return firstElementAddress
}

public mutating func replace<C>(
internal mutating func replace<C>(
subRange: Range<Int>,
with newCount: Int,
elementsOf newValues: C
Expand Down
3 changes: 1 addition & 2 deletions stdlib/public/core/ArrayType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
//
//===----------------------------------------------------------------------===//

public // @testable
protocol _ArrayProtocol
internal protocol _ArrayProtocol
: RangeReplaceableCollection,
ExpressibleByArrayLiteral
{
Expand Down
Loading