mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
154 lines
4.7 KiB
Swift
154 lines
4.7 KiB
Swift
//===--- UnsafeBufferPointer.swift.gyb ------------------------*- swift -*-===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2016 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// An iterator for the elements in the buffer referenced by
|
|
/// `UnsafeBufferPointer` or `UnsafeMutableBufferPointer`.
|
|
public struct UnsafeBufferPointerIterator<Element>
|
|
: IteratorProtocol, Sequence {
|
|
|
|
/// Advances to the next element and returns it, or `nil` if no next element
|
|
/// exists. Once `nil` has been returned, all subsequent calls return `nil`.
|
|
public mutating func next() -> Element? {
|
|
if _position == _end { return nil }
|
|
|
|
let result = _position!.pointee
|
|
_position! += 1
|
|
return result
|
|
}
|
|
|
|
internal var _position, _end: UnsafePointer<Element>?
|
|
}
|
|
|
|
%for Mutable in ('Mutable', ''):
|
|
/// A non-owning pointer to buffer of ${Mutable.lower()} `Element`s stored
|
|
/// contiguously in memory, presenting a `Collection` interface to the
|
|
/// underlying elements.
|
|
///
|
|
/// The pointer should be aligned to `alignof(Element.self)`.
|
|
public struct Unsafe${Mutable}BufferPointer<Element>
|
|
: ${Mutable}Indexable, ${Mutable}Collection, RandomAccessCollection {
|
|
|
|
public typealias Index = Int
|
|
public typealias IndexDistance = Int
|
|
public typealias Iterator =
|
|
IndexingIterator<Unsafe${Mutable}BufferPointer<Element>>
|
|
|
|
/// Always zero, which is the index of the first element in a
|
|
/// non-empty buffer.
|
|
public var startIndex: Int {
|
|
return 0
|
|
}
|
|
|
|
/// The "past the end" position; always identical to `count`.
|
|
///
|
|
/// `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 {
|
|
return count
|
|
}
|
|
|
|
public typealias Indices = CountableRange<Int>
|
|
|
|
public var indices: Indices {
|
|
return startIndex..<endIndex
|
|
}
|
|
|
|
/// Access the `i`th element in the buffer.
|
|
public subscript(i: Int) -> Element {
|
|
get {
|
|
_debugPrecondition(i >= 0)
|
|
_debugPrecondition(i < endIndex)
|
|
return _position![i]
|
|
}
|
|
%if Mutable:
|
|
nonmutating set {
|
|
_debugPrecondition(i >= 0)
|
|
_debugPrecondition(i < endIndex)
|
|
_position![i] = newValue
|
|
}
|
|
%end
|
|
}
|
|
|
|
public subscript(bounds: Range<Int>)
|
|
-> ${Mutable}RandomAccessSlice<Unsafe${Mutable}BufferPointer<Element>>
|
|
{
|
|
get {
|
|
// FIXME: swift-3-indexing-model: range check.
|
|
// FIXME: swift-3-indexing-model: tests.
|
|
return ${Mutable}RandomAccessSlice(
|
|
base: self, bounds: bounds)
|
|
}
|
|
% if Mutable:
|
|
set {
|
|
// FIXME: swift-3-indexing-model: range check.
|
|
// FIXME: swift-3-indexing-model: tests.
|
|
_writeBackMutableSlice(&self, bounds: bounds, slice: newValue)
|
|
}
|
|
% end
|
|
}
|
|
|
|
/// Construct an Unsafe${Mutable}Pointer over the `count` contiguous
|
|
/// `Element` instances beginning at `start`.
|
|
///
|
|
/// If `start` is nil, `count` must be 0. However, `count` may be 0 even for
|
|
/// a nonzero `start`.
|
|
public init(start: Unsafe${Mutable}Pointer<Element>?, count: Int) {
|
|
_precondition(
|
|
count >= 0, "Unsafe${Mutable}BufferPointer with negative count")
|
|
_precondition(
|
|
count == 0 || start != nil,
|
|
"Unsafe${Mutable}BufferPointer has a nil start and nonzero count")
|
|
_position = start
|
|
_end = start.map { $0 + count }
|
|
}
|
|
|
|
/// Returns an iterator over the elements of this sequence.
|
|
///
|
|
/// - Complexity: O(1).
|
|
public func makeIterator() -> UnsafeBufferPointerIterator<Element> {
|
|
return UnsafeBufferPointerIterator(_position: _position, _end: _end)
|
|
}
|
|
|
|
/// A pointer to the first element of the buffer.
|
|
public var baseAddress: Unsafe${Mutable}Pointer<Element>? {
|
|
return _position
|
|
}
|
|
|
|
/// The number of elements in the buffer.
|
|
public var count: Int {
|
|
if _position == nil {
|
|
_sanityCheck(_end == nil)
|
|
return 0
|
|
}
|
|
return _end! - _position!
|
|
}
|
|
|
|
let _position, _end: Unsafe${Mutable}Pointer<Element>?
|
|
}
|
|
|
|
extension Unsafe${Mutable}BufferPointer : CustomDebugStringConvertible {
|
|
/// A textual representation of `self`, suitable for debugging.
|
|
public var debugDescription: String {
|
|
return "Unsafe${Mutable}BufferPointer"
|
|
+ "(start: \(_position.map(String.init(_:)) ?? "nil"), count: \(count))"
|
|
}
|
|
}
|
|
%end
|
|
|
|
@available(*, unavailable, renamed: "UnsafeBufferPointerIterator")
|
|
public struct UnsafeBufferPointerGenerator<Element> {}
|
|
|
|
// ${'Local Variables'}:
|
|
// eval: (read-only-mode 1)
|
|
// End:
|