-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatientPriorityQueue.kt
More file actions
93 lines (76 loc) · 2.07 KB
/
PatientPriorityQueue.kt
File metadata and controls
93 lines (76 loc) · 2.07 KB
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
package dataStructures.priorityQueue
import dataStructures.heap.left
import dataStructures.heap.parent
import dataStructures.heap.right
import otherAlgorithms.exchange
data class Patient(val name: String, val patientId: Int, val priority: Int)
data class PatientPriorityQueue(
val heap: Array<Patient?>,
val positions: IntArray,
var size: Int,
val compare: (a: Patient, b: Patient) -> Int
)
/**
* Add and position a new element
* Time Complexity: O(log₂n)
*/
fun PatientPriorityQueue.offer(elem: Patient) {
heap[size] = elem
positions[elem.patientId] = size
decreaseKey(size)
size++
}
/**
* Time Complexity: O(log₂n)
*/
private fun PatientPriorityQueue.decreaseKey(index: Int) {
var i = index
while (i > 0 && compare(heap[i]!!, heap[parent(i)]!!) < 0) {
exchange(heap, i, parent(i))
exchange(positions, heap[i]!!.patientId, heap[parent(i)]!!.patientId)
i = parent(i)
}
}
/**
* Return the element with more priority, or null if the heap is empty
*/
fun PatientPriorityQueue.peek(): Patient? =
heap[0]
/**
* Returns and removes the element with more priority
*/
fun PatientPriorityQueue.poll(): Patient? {
val elem = peek()
if (elem != null) {
heap[0] = heap[--size]
positions[heap[0]!!.patientId] = 0
heap[size] = null
minHeapify(0)
}
return elem
}
/**
* Time Complexity: O(log₂n)
*/
private fun PatientPriorityQueue.minHeapify(i: Int) {
val left = left(i)
val right = right(i)
var smallest = i
if (left < size && compare(heap[left]!!, heap[smallest]!!) < 0) smallest = left
if (right < size && compare(heap[right]!!, heap[smallest]!!) < 0) smallest = right
if (smallest == i) return
exchange(heap, i, smallest)
exchange(positions, heap[i]!!.patientId, heap[smallest]!!.patientId)
minHeapify(smallest)
}
/**
* Updates the status of a User
*/
fun PatientPriorityQueue.update(newStatus: Patient) {
val oldStatus = heap[positions[newStatus.patientId]]!!
heap[positions[newStatus.patientId]] = newStatus
if (compare(oldStatus, newStatus) > 0) {
decreaseKey(positions[newStatus.patientId])
} else
minHeapify(positions[newStatus.patientId])
}