Skip to content

Commit

Permalink
feat(aoc-core): add PriorityQueue (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
leohidalgo authored Jan 15, 2025
1 parent 28ec761 commit d0260f8
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Sources/AOCCore/PriorityQueue.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Foundation

public struct PriorityQueue<Element, Cost: Comparable> {

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()
}
}

0 comments on commit d0260f8

Please sign in to comment.