ContiguousArray

A fast, contiguously-stored array of T.

Efficiency is equivalent to that of Array, unless T is a class or @objc protocol type, in which case using ContiguousArray may be more efficient. Note, however, that ContiguousArray does not bridge to Objective-C. See Array, with which ContiguousArray shares most properties, for more detail.

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 ContiguousArray

A type that can represent a sub-range of a ContiguousArray

Type alias inferred.

Type alias inferred.

Type alias inferred.

Import
  • import Swift

Initializers

init()

Construct an empty ContiguousArray

Declaration

  • init()
init(_: _ContiguousArrayBuffer<T>)

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

Declaration

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

Construct from an arbitrary sequence with elements of type T

Declaration

init(arrayLiteral:)

Create an instance containing elements.

Declaration

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

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

Declaration

  • init(count: Int, repeatedValue: T)

Instance Variables

var capacity: Int

How many elements the ContiguousArray can store without reallocation

Declaration

  • var capacity: Int { get }
var count: Int

How many elements the ContiguousArray 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 ContiguousArray 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

Instance Methods

mutating func append(_:)

Append newElement to the ContiguousArray

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 a ContiguousArray containing the elements x of self for which includeElement(x) is true

Declaration

func flatMap(_:)

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

Declaration

func generate()

Return a generator over the elements.

Complexity: O(1)

Declaration

func getMirror()

Returns a mirror that reflects self.

Declaration

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 map(_:)

Return a ContiguousArray containing the results of calling transform(x) on each element x of self

Declaration

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 ContiguousArray 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 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 ContiguousArray containing the elements of self in reverse order

Declaration

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

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 ContiguousArray'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

mutating func withUnsafeMutableBufferPointer(_:)

Call body(p), where p is a pointer to the ContiguousArray'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