From c1bb504ccb111701902c17d0e9a9888ef87eb582 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Mon, 9 Aug 2021 13:26:17 +0900 Subject: [PATCH] more conventional sortedarraylist --- .../terrarum/gameworld/WorldSimulator.kt | 3 ++ src/net/torvald/util/SortedArrayList.kt | 33 ++++++++++++------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/net/torvald/terrarum/gameworld/WorldSimulator.kt b/src/net/torvald/terrarum/gameworld/WorldSimulator.kt index 2adbb7491..bb82d2958 100644 --- a/src/net/torvald/terrarum/gameworld/WorldSimulator.kt +++ b/src/net/torvald/terrarum/gameworld/WorldSimulator.kt @@ -10,6 +10,7 @@ import net.torvald.terrarum.gameactors.ActorWithBody import net.torvald.terrarum.gamecontroller.KeyToggler import net.torvald.terrarum.gameitem.ItemID import net.torvald.terrarum.modulebasegame.gameactors.ActorHumanoid +import net.torvald.terrarum.modulebasegame.gameactors.FixtureBase import net.torvald.terrarum.worlddrawer.BlocksDrawer import net.torvald.terrarum.worlddrawer.WorldCamera import org.khelekore.prtree.* @@ -460,6 +461,8 @@ object WorldSimulator { val for_x_start = (WorldCamera.x.toFloat() / TILE_SIZE).floorInt() val for_x_end = for_x_start + BlocksDrawer.tilesInHorizontal - 1 + val fixtures = Terrarum.ingame!!.actorContainer.filterIsInstance() + for (y in for_y_start - wiresimOverscan..for_y_end + wiresimOverscan) { for (x in for_x_start - wiresimOverscan..for_x_end + wiresimOverscan) { diff --git a/src/net/torvald/util/SortedArrayList.kt b/src/net/torvald/util/SortedArrayList.kt index fbaa6014d..26b63a7ab 100644 --- a/src/net/torvald/util/SortedArrayList.kt +++ b/src/net/torvald/util/SortedArrayList.kt @@ -2,19 +2,20 @@ package net.torvald.util import net.torvald.terrarum.lock import java.util.concurrent.locks.ReentrantLock +import java.util.function.Consumer /** * The modification of the arraylist that its element is always sorted. * * Created by minjaesong on 2019-03-12. */ -class SortedArrayList(initialSize: Int = 10) : Iterable { +class SortedArrayList>(initialSize: Int = 10) : List { val arrayList = ArrayList(initialSize) /** */ - fun add(elem: T) { + fun add(elem: Comparable) { // don't append-at-tail-and-sort; just insert at right index // this is a modified binary search to search the right "spot" where the insert elem fits ReentrantLock().lock { @@ -24,29 +25,34 @@ class SortedArrayList(initialSize: Int = 10) : Iterable { while (low < high) { val mid = (low + high).ushr(1) - if ((arrayList[mid] as Comparable).compareTo(elem) > 0) + if ((arrayList[mid] as Comparable).compareTo(elem as T) > 0) high = mid else low = mid + 1 } - arrayList.add(low, elem) + arrayList.add(low, elem as T) } } - val size: Int + override val size: Int get() = arrayList.size + override inline fun isEmpty() = arrayList.isEmpty() + override inline fun lastIndexOf(element: T) = arrayList.lastIndexOf(element) inline fun removeAt(index: Int) = arrayList.removeAt(index) - inline fun remove(element: T) = arrayList.remove(element) + inline fun remove(element: T) = indexOf(element).let { if (it != -1) removeAt(it) } inline fun removeLast() = arrayList.removeAt(arrayList.size - 1) - operator fun get(index: Int) = arrayList[index] + override operator inline fun get(index: Int) = arrayList[index] fun getOrNull(index: Int?) = if (index == null) null else get(index) + + override fun indexOf(element: T): Int = searchForIndex(element.hashCode()) { element.hashCode() } ?: -1 + /** * Searches for the element. Null if the element was not found */ - fun contains(element: T): Boolean { + override fun contains(element: T): Boolean { // code from collections/Collections.kt var low = 0 var high = this.size - 1 @@ -56,7 +62,7 @@ class SortedArrayList(initialSize: Int = 10) : Iterable { val midVal = get(mid) - if ((element as Comparable).compareTo(midVal) > 0) + if ((element) > midVal) low = mid + 1 else if (element < midVal) high = mid - 1 @@ -66,6 +72,8 @@ class SortedArrayList(initialSize: Int = 10) : Iterable { return false // key not found } + override fun containsAll(elements: Collection) = arrayList.containsAll(elements) + /** Searches the element using given predicate instead of the element itself. Returns index in the array where desired, null when there is no such element. * element is stored. * (e.g. search the Actor by its ID rather than the actor instance) @@ -100,8 +108,11 @@ class SortedArrayList(initialSize: Int = 10) : Iterable { */ fun > searchFor(searchQuery: R, searchHow: (T) -> R = { it as R }): T? = getOrNull(searchForIndex(searchQuery, searchHow)) - override fun iterator() = arrayList.iterator() - inline fun forEach(action: (T) -> Unit) = arrayList.forEach(action) + override inline fun iterator() = arrayList.iterator() + override inline fun listIterator() = arrayList.listIterator() + override inline fun listIterator(index: Int) = arrayList.listIterator(index) + override inline fun subList(fromIndex: Int, toIndex: Int) = arrayList.subList(fromIndex, toIndex) + override inline fun forEach(action: Consumer?) = arrayList.forEach(action) inline fun forEachIndexed(action: (Int, T) -> Unit) = arrayList.forEachIndexed(action)