Swift Double-ended queue implementation
Double-ended queue is a special kind of container which provides appending and prepending methods of amortized O(1) complexity. It is built on top of RingBuffer and provides Array-like interface.
Creating a deque:
let emptyDeque = Deque<String>()
let dequeFromArray: Deque<Int> = [1, 2, 3, 4]
Appending values:
var deque = Deque<String>()
deque.append("first element")
print(deque[0])
// Prints "first element"
Deque conforms to protocol RangeReplaceableCollection
as well:
var nums: Deque<Int> = [10, 20, 30, 40, 50]
nums.replaceSubrange(1...3, with: repeatElement(1, count: 5))
print(nums)
// Prints "[10, 1, 1, 1, 1, 1, 50]"
To install, specify Deque in your Podfile:
source 'https://github.com/interfere/Specs.git'
platform :ios, '8.0'
use_frameworks!
target '<Your Target Name>' do
pod 'Deque'
end
Deque is available under the MIT license. See the LICENSE file for more info.