Deque
public struct Deque<T>
A double-ended queue (deque) is a collection that generalizes a queue, for which elements can be added to or removed from either the front or back.
The enqueueFirst
, enqueueLast
, dequeueFirst
and dequeueLast
operations run in amortized constant time.
Conforms to Sequence
, ExpressibleByArrayLiteral
,
CustomStringConvertible
.
-
Constructs an empty deque.
Declaration
Swift
public init()
-
Constructs a deque from a sequence, such as an array. The elements will be enqueued from first to last.
Declaration
Swift
public init<S: Sequence>(_ elements: S) where S.Iterator.Element == T
-
Number of elements stored in the deque.
Declaration
Swift
public var count : Int
-
Returns
true
if and only ifcount == 0
.Declaration
Swift
public var isEmpty: Bool
-
The front element in the deque, or
nil
if the deque is empty.Declaration
Swift
public var first: T?
-
The back element in the deque, or
nil
if the deque is empty.Declaration
Swift
public var last: T?
-
Inserts an element into the back of the deque.
Declaration
Swift
public mutating func enqueueLast(_ element: T)
-
Inserts an element into the front of the deque.
Declaration
Swift
public mutating func enqueueFirst(_ element: T)
-
Retrieves and removes the front element of the deque.
Declaration
Swift
public mutating func dequeueFirst() -> T
Return Value
The front element, or
nil
if the deque is empty. -
Retrieves and removes the back element of the deque.
Declaration
Swift
public mutating func dequeueLast() -> T
Return Value
The back element, or
nil
if the deque is empty. -
Removes all the elements from the deque, and by default clears the underlying storage buffer.
Declaration
Swift
public mutating func removeAll(keepingCapacity keep: Bool = false)
-
Provides for-in loop functionality. Generates elements in FIFO order.
Declaration
Swift
public func makeIterator() -> AnyIterator<T>
Return Value
A generator over the elements.
-
Constructs a deque using an array literal. The elements will be enqueued from first to last.
let deque: Deque<Int> = [1,2,3]
Declaration
Swift
public init(arrayLiteral elements: T...)
-
A string containing a suitable textual representation of the deque.
Declaration
Swift
public var description: String