diff --git a/Sources/AOCCore/PriorityQueue.swift b/Sources/AOCCore/PriorityQueue.swift new file mode 100644 index 0000000..3e73ae5 --- /dev/null +++ b/Sources/AOCCore/PriorityQueue.swift @@ -0,0 +1,22 @@ +import Foundation + +public struct PriorityQueue { + + public var isEmpty: Bool { queue.isEmpty } + + private var queue: [(element: Element, cost: Cost)] = [] + + public init() { } + + mutating + public func append(_ element: Element, _ cost: Cost) { + queue.append((element, cost)) + + queue.sort { $0.cost < $1.cost } + } + + mutating + public func removeFirst() -> (element: Element, cost: Cost) { + queue.removeFirst() + } +}