Skip to content

Commit

Permalink
Merge pull request #149 from ephemient/kt/gradle
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient authored Dec 23, 2024
2 parents b9c0c32 + 7a72bcb commit 17d013b
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ internal class CommonPriorityQueue<E : Any>(private val comparator: Comparator<i
return true
}

@Throws(NoSuchElementException::class)
override fun peek(): E = storage.first()

@Suppress("NestedBlockDepth")
@Throws(NoSuchElementException::class)
override fun remove(): E {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ interface PriorityQueue<E : Any> : Iterable<E> {

fun add(element: E): Boolean

@Throws(NoSuchElementException::class)
fun peek(): E

@Throws(NoSuchElementException::class)
fun remove(): E
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,4 @@
package com.github.ephemient.aoc2024

internal class PriorityQueueImpl<E : Any>(private val comparator: Comparator<in E>) : PriorityQueue<E> {
private val storage = mutableListOf<E>()

override fun isEmpty(): Boolean = storage.isEmpty()

override fun add(element: E): Boolean {
storage.add(element)
var i = storage.lastIndex
while (i > 0) {
val j = (i - 1) / 2
val a = storage[j]
val b = storage[i]
if (comparator.compare(a, b) <= 0) break
storage[i] = a
storage[j] = b
i = j
}
return true
}

@Suppress("NestedBlockDepth")
@Throws(NoSuchElementException::class)
override fun remove(): E {
val first = storage.first()
val last = storage.removeLast()
if (storage.isNotEmpty()) {
storage[0] = last
var i = 0
while (2 * i + 2 < storage.size) {
val j = if (comparator.compare(storage[2 * i + 1], storage[2 * i + 2]) < 0) 2 * i + 1 else 2 * i + 2
if (comparator.compare(storage[i], storage[j]) <= 0) break
storage[i] = storage[j].also { storage[j] = storage[i] }
i = j
}
if (2 * i + 1 == storage.lastIndex && comparator.compare(storage[i], storage.last()) > 0) {
storage[i] = storage.last().also { storage[storage.lastIndex] = storage[i] }
}
}
return first
}

override fun iterator(): Iterator<E> = storage.iterator()
}

actual fun <E : Any> PriorityQueue(comparator: Comparator<E>): PriorityQueue<E> =
PriorityQueueImpl(comparator)
CommonPriorityQueue(comparator)
2 changes: 2 additions & 0 deletions kt/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
kotlin.code.style=official
kotlin.native.enableKlibsCrossCompilation=true
org.gradle.caching=true
org.gradle.configuration-cache=true
org.gradle.configuration-cache.parallel=true
org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m -XX:+UseParallelGC -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.parallel=true
2 changes: 1 addition & 1 deletion kt/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
3 changes: 1 addition & 2 deletions kt/gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 17d013b

Please sign in to comment.