-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStack.swift
42 lines (35 loc) · 1.03 KB
/
Stack.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
//
// Stack.swift
//
// Advent of Code Tools
//
/// A simple FIFO collection of elements
public struct Stack<Element> {
public var storage: [Element]
public init() {
storage = []
}
/// Return the number of element in this stack
public var count: Int { storage.count }
/// A Boolean value indication whether the stack is empty
@inlinable public var isEmpty: Bool { storage.isEmpty }
/// Add an element at the top of stack.
@inlinable public mutating func push(_ element: Element) {
storage.append(element)
}
/// Remove the element at the top of the stack and return it.
/// For an empty stack, return `nil`.
@discardableResult
@inlinable
public mutating func pop() -> Element? {
if storage.isEmpty {
return nil
}
return storage.removeLast()
}
/// Return the top element of the stack without removing it.
/// For an empty stack, return `nil`.
@inlinable public func peek() -> Element? {
storage.last
}
}