|
| 1 | +//: [Previous](@previous) |
| 2 | + |
| 3 | +import Foundation |
| 4 | + |
| 5 | +struct Dequeue<T> { |
| 6 | + // MARK: - Private properties |
| 7 | + |
| 8 | + private var data: [T] |
| 9 | + |
| 10 | + // MARK: - Public properties |
| 11 | + |
| 12 | + public var count: Int { |
| 13 | + return data.count |
| 14 | + } |
| 15 | + |
| 16 | + public var capacity: Int { |
| 17 | + get { |
| 18 | + return data.capacity |
| 19 | + } |
| 20 | + set { |
| 21 | + data.reserveCapacity(capacity) |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + // MARK: - Initializers |
| 26 | + |
| 27 | + public init() { |
| 28 | + data = [] |
| 29 | + } |
| 30 | + |
| 31 | + public init<S: Sequence>(_ elements: S) where S.Iterator.Element == T { |
| 32 | + self.init() |
| 33 | + data.append(contentsOf: elements) |
| 34 | + } |
| 35 | + |
| 36 | + // MARK: - Methods |
| 37 | + |
| 38 | + public mutating func dequeueFront() -> T? { |
| 39 | + return data.removeFirst() |
| 40 | + } |
| 41 | + |
| 42 | + public mutating func dequeueBack() -> T? { |
| 43 | + return data.removeLast() |
| 44 | + } |
| 45 | + |
| 46 | + public mutating func enqueue(front element: T) { |
| 47 | + data.insert(element, at: 0) |
| 48 | + } |
| 49 | + |
| 50 | + public mutating func enqueue(back element: T) { |
| 51 | + data.append(element) |
| 52 | + } |
| 53 | + |
| 54 | + public mutating func clear() { |
| 55 | + data.removeAll() |
| 56 | + } |
| 57 | + |
| 58 | + public mutating func isFull() -> Bool { |
| 59 | + return count == data.capacity |
| 60 | + } |
| 61 | + |
| 62 | + public func isEmpty() -> Bool { |
| 63 | + return data.isEmpty |
| 64 | + } |
| 65 | + |
| 66 | + public func peekFirst() -> T? { |
| 67 | + return data.first |
| 68 | + } |
| 69 | + |
| 70 | + public func peekLast() -> T? { |
| 71 | + return data.last |
| 72 | + } |
| 73 | + |
| 74 | + // MARK: - Private methods |
| 75 | + |
| 76 | + private func checkIndex(index: Int) throws { |
| 77 | + if index < 0 || index > count { |
| 78 | + throw DequeueError.indexOutOfRange |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +enum DequeueError: Error { |
| 84 | + case indexOutOfRange |
| 85 | +} |
| 86 | + |
| 87 | +extension Dequeue: CustomStringConvertible, CustomDebugStringConvertible { |
| 88 | + |
| 89 | + public var description: String { |
| 90 | + return data.description |
| 91 | + } |
| 92 | + |
| 93 | + public var debugDescription: String { |
| 94 | + return data.description |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +extension Dequeue: ExpressibleByArrayLiteral { |
| 99 | + |
| 100 | + public init(arrayLiteral elements: T...) { |
| 101 | + self.init(elements) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +extension Dequeue: Sequence { |
| 106 | + |
| 107 | + public func makeIterator() -> AnyIterator<T> { |
| 108 | + let indexedIterator = IndexingIterator(_elements: data.lazy) |
| 109 | + return AnyIterator(indexedIterator) |
| 110 | + } |
| 111 | + |
| 112 | + public func generate() -> AnyIterator<T> { |
| 113 | + let indexingIteratoer = IndexingIterator(_elements: data.lazy) |
| 114 | + return AnyIterator(indexingIteratoer) |
| 115 | + |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +extension Dequeue: MutableCollection { |
| 121 | + |
| 122 | + // MARK: - Core protocol conformance |
| 123 | + |
| 124 | + public var startIndex: Int { |
| 125 | + return 0 |
| 126 | + } |
| 127 | + |
| 128 | + public var endIndex: Int { |
| 129 | + return count - 1 |
| 130 | + } |
| 131 | + |
| 132 | + public func index(after i: Int) -> Int { |
| 133 | + return data.index(after: i) |
| 134 | + } |
| 135 | + |
| 136 | + // MARK: - Subscript implementation |
| 137 | + |
| 138 | + public subscript(index: Int) -> T { |
| 139 | + get { |
| 140 | + checkHandledIndex(index: index) |
| 141 | + return data[index] |
| 142 | + } |
| 143 | + set { |
| 144 | + checkHandledIndex(index: index) |
| 145 | + data[index] = newValue |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // MARK: - Utility method |
| 150 | + |
| 151 | + private func checkHandledIndex(index: Int) { |
| 152 | + do { |
| 153 | + try checkIndex(index: index) |
| 154 | + } catch { |
| 155 | + fatalError(error.localizedDescription) |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +//: Usage: |
| 161 | + |
| 162 | +var dequeue = Dequeue([1,2,34,5,6,7]) |
| 163 | +let back = dequeue.dequeueBack() |
| 164 | +let front = dequeue.dequeueFront() |
| 165 | + |
| 166 | +dequeue.enqueue(front: 99) |
| 167 | +dequeue.enqueue(back: 99) |
| 168 | + |
| 169 | +//: [Next](@next) |
0 commit comments