-
Notifications
You must be signed in to change notification settings - Fork 150
/
PriorityQueue.kt
124 lines (108 loc) · 3.89 KB
/
PriorityQueue.kt
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Copyright (c) 2017 Kotlin Algorithm Club
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.uuddlrlrba.ktalgs.datastructures
import io.uuddlrlrba.ktalgs.sorts.exch
import java.util.*
import kotlin.Comparator
class PriorityQueue<T>(size: Int, val comparator: Comparator<T>? = null) : Collection<T> {
public override var size: Int = 0
private set
private var arr: Array<T?> = Array<Comparable<T>?>(size, { null }) as Array<T?>
public fun add(element: T) {
if (size + 1 == arr.size) {
resize()
}
arr[++size] = element
swim(size)
}
public fun peek(): T {
if (size == 0) throw NoSuchElementException()
return arr[1]!!
}
public fun poll(): T {
if (size == 0) throw NoSuchElementException()
val res = peek()
arr.exch(1, size--)
sink(1)
arr[size + 1] = null
if ((size > 0) && (size == (arr.size - 1) / 4)) {
resize()
}
return res
}
private fun swim(n: Int) {
Companion.swim(arr, n, comparator)
}
private fun sink(n: Int) {
Companion.sink(arr, n, size, comparator)
}
private fun resize() {
val old = arr
arr = Array<Comparable<T>?>(size * 2, { null }) as Array<T?>
System.arraycopy(old, 0, arr, 0, size + 1)
}
public override fun isEmpty(): Boolean {
return size == 0
}
override fun contains(element: T): Boolean {
for (obj in this) {
if (obj == element) return true
}
return false
}
override fun containsAll(elements: Collection<T>): Boolean {
for (element in elements) {
if (!contains(element)) return false
}
return true
}
override fun iterator(): Iterator<T> {
return arr.copyOfRange(1, size + 1).map { it!! }.iterator()
}
companion object {
private fun<T> greater(arr: Array<T?>, i: Int, j: Int, comparator: Comparator<T>? = null): Boolean {
if (comparator != null) {
return comparator.compare(arr[i], arr[j]) > 0
} else {
val left = arr[i]!! as Comparable<T>
return left > arr[j]!!
}
}
public fun<T> sink(arr: Array<T?>, a: Int, size: Int, comparator: Comparator<T>? = null) {
var k = a
while (2 * k <= size) {
var j = 2 * k
if (j < size && greater(arr, j, j + 1, comparator)) j++
if (!greater(arr, k, j, comparator)) break
arr.exch(k, j)
k = j
}
}
public fun<T> swim(arr: Array<T?>, size: Int, comparator: Comparator<T>? = null) {
var n = size
while (n > 1 && greater(arr, n / 2, n, comparator)) {
arr.exch(n, n / 2)
n /= 2
}
}
}
}