Faster version of Container.balance

This commit is contained in:
DBotThePony 2023-08-03 18:36:17 +07:00
parent 20d478cbfb
commit 06e6168a73
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -263,7 +263,7 @@ fun Container.balance(slots: IntRange) {
balance(IntArraySet().also { it.addAll(slots) })
}
fun Container.balance(startSlot: Int = 0, endSlot: Int = containerSize - 1) {
fun Container.balance(startSlot: Int, endSlot: Int) {
require(startSlot <= endSlot) { "Invalid slot range: $startSlot .. $endSlot" }
var any = false
@ -277,3 +277,10 @@ fun Container.balance(startSlot: Int = 0, endSlot: Int = containerSize - 1) {
if (!any) return
balance(IntArraySet(endSlot - startSlot + 1).also { for (i in startSlot .. endSlot) it.add(i) }, false)
}
fun Container.balance() {
if (isEmpty)
return
balance(IntArraySet(containerSize).also { for (i in 0 until containerSize) it.add(i) }, false)
}