fixed worldgen and threadexecutor so that they will actually wait for the thread termination

This commit is contained in:
minjaesong
2019-11-16 02:41:25 +09:00
parent 7939ff3690
commit e71c56cf0d
9 changed files with 74 additions and 94 deletions

View File

@@ -16,7 +16,7 @@ import java.util.*
class CircularArray<T>(val size: Int, val overwriteOnOverflow: Boolean): Iterable<T> {
/**
* What to do when old element is being overridden by the new element (only makes sense when ```overwriteOnOverflow = true```)
* What to do RIGHT BEFORE old element is being overridden by the new element (only makes sense when ```overwriteOnOverflow = true```)
*
* This function will not be called when ```removeHead()``` or ```removeTail()``` is called.
*/
@@ -48,6 +48,12 @@ class CircularArray<T>(val size: Int, val overwriteOnOverflow: Boolean): Iterabl
private inline fun incTail() { tail = (tail + 1).wrap() }
private inline fun decTail() { tail = (tail - 1).wrap() }
fun clear() {
tail = 0
head = 0
overflow = false
}
/**
* When the overflowing is enabled, tail element (ultimate element) will be changed into the penultimate element.
*/
@@ -116,6 +122,11 @@ class CircularArray<T>(val size: Int, val overwriteOnOverflow: Boolean): Iterabl
/** Returns the oldest (first of the array) element */
fun getTailElem(): T = buffer[tail]
/**
* Relative-indexed get. Index of zero will return the head element.
*/
operator fun get(index: Int) = buffer[(head - 1 - index).wrap()]
private fun getAbsoluteRange() = 0 until when {
head == tail -> buffer.size
tail > head -> buffer.size - (((head - 1).wrap()) - tail)
@@ -171,11 +182,11 @@ class CircularArray<T>(val size: Int, val overwriteOnOverflow: Boolean): Iterabl
return accumulator
}
private fun Int.wrap() = this fmod size
private inline fun Int.wrap() = this fmod size
override fun toString(): String {
return "CircularArray(size=${buffer.size}, head=$head, tail=$tail, overflow=$overflow)"
}
private infix fun Int.fmod(other: Int) = Math.floorMod(this, other)
private inline infix fun Int.fmod(other: Int) = Math.floorMod(this, other)
}

View File

@@ -1,66 +0,0 @@
package net.torvald.util
import java.util.*
/**
* Simple ArrayList wrapper that acts as history keeper. You can append any data but cannot delete.
*
* Created by minjaesong on 2016-07-13.
*/
class HistoryArray<T>(val size: Int) {
val history = ArrayList<T?>(Math.min(size, 256)) // 256: arbitrary set upper bound
val lastIndex = size - 1
val elemCount: Int
get() = history.size
fun add(value: T) {
if (history.size == 0) {
history.add(value)
return
}
// push existing values to an index
else {
for (i in history.size - 1 downTo 0) {
// if history.size is smaller than 'size', make room by appending
if (i == history.size - 1 && i < size - 1)
history.add(history[i])
// actually move if we have some room
else if (i < size - 1)
history[i + 1] = history[i]
}
}
// add new value to the room
history[0] = value
}
/**
* Get certain index from history. NOTE: index 0 means latest!
*/
operator fun get(index: Int): T? =
if (index >= history.size) null
else history[index]
/**
* Iterate from latest to oldest
*/
fun iterator() = history.iterator()
/**
* Iterate from latest to oldest
*/
fun forEach(action: (T?) -> Unit) = history.forEach(action)
val latest: T?
get() = this[0]
val oldest: T?
get() = this[history.size - 1]
fun clear() {
history.clear()
}
}