-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPriorityQueue.swift
More file actions
47 lines (37 loc) ยท 876 Bytes
/
PriorityQueue.swift
File metadata and controls
47 lines (37 loc) ยท 876 Bytes
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
//
// PriorityQueue.swift
// Testor
//
// Created by woong on 2020/10/18.
// Copyright ยฉ 2020 woong. All rights reserved.
//
import Foundation
struct PriorityQueue<T> {
var heap: Heap<T>
init(sort: @escaping (T, T) -> Bool) {
heap = Heap<T>(sort: sort)
}
var isEmpty: Bool {
return heap.isEmpty
}
var count: Int {
return heap.count
}
func peek() -> T? {
return heap.peek()
}
mutating func enqueue(element: T) {
heap.insert(element)
}
mutating func dequeue() -> T? {
return heap.remove()
}
mutating func changePriority(index i: Int, value: T) {
heap.replace(index: i, value: value)
}
}
extension PriorityQueue where T: Equatable {
func index(of element: T) -> Int? {
return heap.index(of: element)
}
}