-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathContents.swift
322 lines (241 loc) · 8.65 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//: [Previous](@previous)
import Foundation
public struct Queue<T> {
// MARK: - Private properties
private var data: [T]
// MARK: - Public properties
public var count: Int {
return data.count
}
public var capacity: Int {
get {
return data.capacity
}
set {
data.reserveCapacity(capacity)
}
}
// MARK: - Initializers
public init() {
data = [T]()
}
public init<S: Sequence>(_ elements: S) where S.Iterator.Element == T {
self.init()
data.append(contentsOf: elements)
}
// MARK: - API methods
public mutating func dequeue() -> T? {
return data.removeFirst()
}
public func peek() -> T? {
return data.first
}
public mutating func enqueue(element: T) {
data.append(element)
}
public mutating func clear() {
data.removeAll()
}
public func isFull() -> Bool {
return count == data.capacity
}
public func isEmpty() -> Bool {
return data.isEmpty
}
// MARK: - Private methods
private func checkIndex(index: Int) throws {
if index < 0 || index > count {
throw QueueError.indexOutOfRange
}
}
}
// MARK: - Custom error enum type declaration
enum QueueError: Error {
case indexOutOfRange
}
// MARK: - CustomStringConvertable and CustomStirngDebugConvertable protocols conformance
extension Queue: CustomStringConvertible, CustomDebugStringConvertible {
public var description: String {
return data.description
}
public var debugDescription: String {
return data.description
}
}
// MARK: - ExpressibleByArrayLiteral protocol conformance
extension Queue: ExpressibleByArrayLiteral {
public init(arrayLiteral elements: T...) {
self.init(elements)
}
}
// MARK: - Sequnce protocol conformance
extension Queue: Sequence {
public func makeIterator() -> AnyIterator<T> {
let indexedIterator = IndexingIterator(_elements: data.lazy)
return AnyIterator(indexedIterator)
}
public func generate() -> AnyIterator<T> {
let indexingIteratoer = IndexingIterator(_elements: data.lazy)
return AnyIterator(indexingIteratoer)
}
}
// MARK: - MutableCollection protocol conformance
extension Queue: MutableCollection {
// MARK: - Core protocol conformance
public var startIndex: Int {
return 0
}
public var endIndex: Int {
return count - 1
}
public func index(after i: Int) -> Int {
return data.index(after: i)
}
// MARK: - Subscript implementation
public subscript(index: Int) -> T {
get {
checkHandledIndex(index: index)
return data[index]
}
set {
checkHandledIndex(index: index)
data[index] = newValue
}
}
// MARK: - Utility method
private func checkHandledIndex(index: Int) {
do {
try checkIndex(index: index)
} catch {
fatalError(error.localizedDescription)
}
}
}
//: The next step is to implement interactive scene for the data structure:
import SpriteKit
import PlaygroundSupport
class QueueScene: SKScene {
var queueElements: Queue<QueueElementNode>! {
didSet {
if queueElements != nil {
numberOfElements.text = "\(queueElements.count) animals"
}
}
}
lazy var numberOfElements: SKLabelNode = {
let label = SKLabelNode(text: "0 animals")
label.position = CGPoint(x: 500, y: 300)
label.color = .gray
label.fontSize = 28
label.verticalAlignmentMode = .center
return label
}()
override func didMove(to view: SKView) {
queueElements = createRandomQeueueElementNodeArray(for: 4)
appearenceAnimation()
dequeueButton()
enqueueButton()
label()
description()
addChild(numberOfElements)
}
func label() {
let nameLabel = SKLabelNode(text: "Queue")
nameLabel.position = CGPoint(x: 300, y: 760)
nameLabel.color = .gray
nameLabel.fontSize = 64
nameLabel.verticalAlignmentMode = .center
addChild(nameLabel)
}
func description() {
let nameLabel = SKLabelNode(text: "Limit is 7 animals")
nameLabel.position = CGPoint(x: 300, y: 700)
nameLabel.color = .darkGray
nameLabel.fontSize = 24
nameLabel.verticalAlignmentMode = .center
addChild(nameLabel)
}
func dequeueButton() {
let popButton = SKSpriteNode(color: .white, size: CGSize(width: 120, height: 50))
popButton.position = CGPoint(x: 100, y: 700)
popButton.name = "pop"
let popLabel = SKLabelNode(text: "Dequeue")
popLabel.fontColor = SKColor.darkGray
popLabel.fontSize = 24
popLabel.verticalAlignmentMode = SKLabelVerticalAlignmentMode.center
popButton.addChild(popLabel)
addChild(popButton)
}
func enqueueButton() {
let pushButton = SKSpriteNode(color: .white, size: CGSize(width: 120, height: 50))
pushButton.position = CGPoint(x: 500, y: 700)
pushButton.name = "push"
let pushLabel = SKLabelNode(text: "Enqueue")
pushLabel.fontColor = SKColor.darkGray
pushLabel.fontSize = 24
pushLabel.verticalAlignmentMode = SKLabelVerticalAlignmentMode.center
pushButton.addChild(pushLabel)
addChild(pushButton)
}
func appearenceAnimation() {
// Animate creation of the stack of books
let appearAction = SKAction.unhide()
for (index, book) in queueElements.enumerated() {
book.position = CGPoint.init(x: 300, y: 500)
book.isHidden = true
addChild(book)
let moveDown = SKAction.move(to: CGPoint(x: 300, y: CGFloat(80 * CGFloat(index + 1))), duration: 1.5)
let waitAction = SKAction.wait(forDuration: TimeInterval(index * 2))
let sequence = SKAction.sequence([waitAction, appearAction, moveDown])
book.run(sequence)
}
}
func createRandomQeueueElementNodeArray(for numberOfElements: Int) -> Queue<QueueElementNode> {
var nodes: Queue<QueueElementNode> = Queue()
for _ in 0..<numberOfElements {
let node = QueueElementNode()
nodes.enqueue(element: node)
}
return nodes
}
// MARK: - Overrides
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let selfLocation = touches.first?.location(in: self) else {
return
}
let nodes = self.nodes(at: selfLocation)
for node in nodes {
if node.name == "pop" {
// pop action
let element = queueElements.dequeue()
let moveUpAction = SKAction.move(by: CGVector(dx: 0, dy: -200), duration: 0.5)
let removeAction = SKAction.removeFromParent()
let moveQueueDown = SKAction.run {
let moveDownAction = SKAction.move(by: CGVector(dx: 0, dy: -80), duration: 0.5)
self.queueElements.forEach({ (node) in
node.run(moveDownAction)
})
}
let actionSequence = SKAction.sequence([moveUpAction, removeAction, moveQueueDown])
element?.run(actionSequence)
} else if node.name == "push", queueElements.count < 7 {
let node = QueueElementNode()
node.position = CGPoint.init(x: 300, y: 600)
node.isHidden = true
addChild(node)
queueElements.enqueue(element: node)
let appearAction = SKAction.unhide()
let moveDown = SKAction.move(to: CGPoint(x: 300, y: CGFloat(80 * CGFloat(queueElements.count))), duration: 1.5)
let waitAction = SKAction.wait(forDuration: TimeInterval(1))
let sequence = SKAction.sequence([waitAction, appearAction, moveDown])
node.run(sequence)
}
}
}
}
let skScene = QueueScene(size: CGSize(width: 600, height: 800))
skScene.backgroundColor = .black
let skView = SKView(frame: CGRect(x: 0, y: 0, width: 600, height: 800))
skView.presentScene(skScene)
PlaygroundPage.current.liveView = skView
//: [Next](@next)