ArraySlice

struct ArraySlice<T>

The Array-like type that represents a sub-sequence of any Array, ContiguousArray, or other ArraySlice.

ArraySlice always uses contiguous storage and does not bridge to Objective-C.

Warning: Long-term storage of ArraySlice instances is discouraged

Because a ArraySlice presents a view onto the storage of some larger array even after the original array's lifetime ends, storing the slice may prolong the lifetime of elements that are no longer accessible, which can manifest as apparent memory and object leakage. To prevent this effect, use ArraySlice only for transient computation.

Inheritance ArrayLiteralConvertible, CollectionType, DebugPrintable, ExtensibleCollectionType, MutableCollectionType, MutableSliceable, Printable, RangeReplaceableCollectionType, Reflectable, SequenceType, Sliceable, _ArrayType, _CollectionType, _DestructorSafeContainer, _ExtensibleCollectionType, _SequenceType, _Sequence_Type, _Sliceable, __ArrayType View Protocol Hierarchy →
Associated Types
Element = T

The type of element stored by this ArraySlice

SubSlice = ArraySlice<T>

A type that can represent a sub-range of an ArraySlice

Generator = IndexingGenerator<ArraySlice<T>>

Type alias inferred.

Index = Int

Type alias inferred.

SubSequence = ArraySlice<T>

Type alias inferred.

Import import Swift

Initializers

init()

Construct an empty ArraySlice

Declaration

init()
init(_: _SliceBuffer<T>)

Initialization from an existing buffer does not have "array.init" semantics because the caller may retain an alias to buffer.

Declaration

init(_ buffer: _SliceBuffer<T>)
init<S : SequenceType where T == T>(_: S)

Construct from an arbitrary sequence with elements of type T

Declaration

init<S : SequenceType where T == T>(_ s: S)
init(arrayLiteral:)

Create an instance containing elements.

Declaration

init(arrayLiteral elements: T...)
init(count:repeatedValue:)

Construct a ArraySlice of count elements, each initialized to repeatedValue.

Declaration

init(count: Int, repeatedValue: T)

Instance Variables

var capacity: Int

How many elements the ArraySlice can store without reallocation

Declaration

var capacity: Int { get }
var count: Int

How many elements the ArraySlice stores

Declaration

var count: Int { get }
var debugDescription: String

A textual representation of self, suitable for debugging.

Declaration

var debugDescription: String { get }
var description: String

A textual representation of self.

Declaration

var description: String { get }
var endIndex: Int

A "past-the-end" element index; the successor of the last valid subscript argument.

Declaration

var endIndex: Int { get }
var first: T?

The first element, or nil if the array is empty

Declaration

var first: T? { get }
var isEmpty: Bool

true if and only if the ArraySlice is empty

Declaration

var isEmpty: Bool { get }
var last: T?

The last element, or nil if the array is empty

Declaration

var last: T? { get }
var startIndex: Int

Always zero, which is the index of the first element when non-empty.

Declaration

var startIndex: Int { get }

Subscripts

subscript(_: Int)

Declaration

subscript(index: Int) -> T
subscript(_: Range<Int>)

Declaration

subscript(subRange: Range<Int>) -> ArraySlice<T>

Instance Methods

mutating func append(_:)

Append newElement to the ArraySlice

Complexity: amortized O(1) unless self's storage is shared with another live array; O(count) otherwise.

Declaration

mutating func append(newElement: T)
mutating func extend(_:)

Append the elements of newElements to self.

Complexity: O(length of result)

Declaration

mutating func extend<S : SequenceType where T == T>(newElements: S)
func filter(_:)

Return an ArraySlice containing the elements x of self for which includeElement(x) is true

Declaration

func filter(includeElement: (T) -> Bool) -> ArraySlice<T>
func flatMap(_:)

Return an ArraySlice containing the results of calling transform(x) on each element x of self and flattening the result.

Declaration

func flatMap<U>(transform: @noescape (T) -> ArraySlice<U>) -> ArraySlice<U>
func generate()

Return a generator over the elements.

Complexity: O(1)

Declaration

func generate() -> IndexingGenerator<ArraySlice<T>>
func getMirror()

Returns a mirror that reflects self.

Declaration

func getMirror() -> MirrorType
mutating func insert(_:atIndex:)

Insert newElement at index i.

Requires: i <= count

Complexity: O(count).

Declaration

mutating func insert(newElement: T, atIndex i: Int)
func join(_:)

Interpose self between each consecutive pair of elements, and concatenate the elements of the resulting sequence. For example, [-1, -2].join([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) yields [1, 2, 3, -1, -2, 4, 5, 6, -1, -2, 7, 8, 9]

Declaration

func join<S : SequenceType where ArraySlice<T> == ArraySlice<T>>(elements: S) -> ArraySlice<T>
func map(_:)

Return an ArraySlice containing the results of calling transform(x) on each element x of self

Declaration

func map<U>(transform: (T) -> U) -> ArraySlice<U>
func reduce(_:combine:)

Return the result of repeatedly calling combine with an accumulated value initialized to initial and each element of self, in turn, i.e. return combine(combine(...combine(combine(initial, self[0]), self[1]),...self[count-2]), self[count-1]).

Declaration

func reduce<U>(initial: U, combine: @noescape (U, T) -> U) -> U
mutating func removeAll(_:)

Remove all elements.

Postcondition: capacity == 0 iff keepCapacity is false.

Complexity: O(count(self)).

Declaration

mutating func removeAll(keepCapacity: Bool = default)
mutating func removeAtIndex(_:)

Remove and return the element at index i

Invalidates all indices with respect to self.

Complexity: O(count).

Declaration

mutating func removeAtIndex(index: Int) -> T
mutating func removeLast()

Remove an element from the end of the ArraySlice in O(1). Requires: count > 0

Declaration

mutating func removeLast() -> T
mutating func removeRange(_:)

Remove the indicated subRange of elements

Complexity: O(count).

Declaration

mutating func removeRange(subRange: Range<Int>)
mutating func replaceRange(_:with:)

Replace the given subRange of elements with newElements.

Complexity: O(count(subRange)) if subRange.endIndex == self.endIndex and isEmpty(newElements), O(N) otherwise.

Declaration

mutating func replaceRange<C : CollectionType where T == T>(subRange: Range<Int>, with newElements: C)
mutating func reserveCapacity(_:)

Reserve enough space to store minimumCapacity elements.

PostCondition: capacity >= minimumCapacity and the array has mutable contiguous storage.

Complexity: O(count)

Declaration

mutating func reserveCapacity(minimumCapacity: Int)
func reverse()

A ArraySlice containing the elements of self in reverse order

Declaration

func reverse() -> ArraySlice<T>
mutating func sort(_:)

Sort self in-place according to isOrderedBefore. Requires: isOrderedBefore induces a strict weak ordering over the elements.

Declaration

mutating func sort(isOrderedBefore: (T, T) -> Bool)
func sorted(_:)

Return a copy of self that has been sorted according to isOrderedBefore. Requires: isOrderedBefore induces a strict weak ordering over the elements.

Declaration

func sorted(isOrderedBefore: (T, T) -> Bool) -> ArraySlice<T>
mutating func splice(_:atIndex:)

Insert newElements at index i

Invalidates all indices with respect to self.

Complexity: O(count + count(newElements)).

Declaration

mutating func splice<S : CollectionType where T == T>(newElements: S, atIndex i: Int)
func withUnsafeBufferPointer(_:)

Call body(p), where p is a pointer to the ArraySlice's contiguous storage.

Often, the optimizer can eliminate bounds checks within an array algorithm, but when that fails, invoking the same algorithm on body's argument lets you trade safety for speed.

Declaration

func withUnsafeBufferPointer<R>(body: @noescape (UnsafeBufferPointer<T>) -> R) -> R
mutating func withUnsafeMutableBufferPointer(_:)

Call body(p), where p is a pointer to the ArraySlice's mutable contiguous storage.

Often, the optimizer can eliminate bounds- and uniqueness-checks within an array algorithm, but when that fails, invoking the same algorithm on body's argument lets you trade safety for speed.

Declaration

mutating func withUnsafeMutableBufferPointer<R>(body: @noescape (inout UnsafeMutableBufferPointer<T>) -> R) -> R