A data structure which yelds its stored elements with a predetermined sequential order.
Queues works upon two basilar operations: enqueue and dequeue.
For example a queue using the FIFO order (first in, first out), will dequeue elements starting from the first enqueued element, proceding sequentially to the last enqueued one. On the contrary a queue using the LIFO order (last in, first out) —a.k.a stack— will dequeue elements starting from the last enqeueued one, proceding sequentially to the first equeued one.
Despite not being stricktly required, a type conforming to Queue
protocol might as well benefit from also conforming to Sequence
or Collection
protocol in order to retain some common functionalities and properties.
The count
and the isEmpty
properties are an example of properties which could be mutated from the Collection
conformance, or easily implemented from a finite Sequence
.
To add Queue
conformance to a type, it must match these requirements:
count
and isEmpty
propertiescapacity
and isFull
propertiespeek()
and dequeue()
methodsclear(keepingCapacity:)
methodenqueue(_:)
and enqueue(contentsOf:)
methodsreserveCapacity(_:)
methodConforming types are expected to provide the count
, isEmpty
, capacity
, isFull
and peek()
properties and method as O(1) operations.
The dequeue()
and enqueue(_:)
methods are expected to be at least O(log n) operations, where n is the count of stored elements, thus enqueue(contentsOf:)
is expected to be an O(logn + k) where k is the count of elements stored in the sequence specified as contentsOf
parameter.
Reasonably the clear(keepingCapacity:)
and reserveCapacity(_:)
are expected to be at worst O(n) operations.
link |
Stars: 0 |
Last commit: 3 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics