Get rid of regular array spliterator
This commit is contained in:
parent
ee21636529
commit
b19307ffdf
@ -1,45 +0,0 @@
|
||||
package ru.dbotthepony.kstarbound.util
|
||||
|
||||
import java.util.Spliterator
|
||||
import java.util.function.Consumer
|
||||
|
||||
class ArraySpliterator<T>(private val source: Array<T>, private val offset: Int = 0, private val size: Int = source.size) : Spliterator<T> {
|
||||
init {
|
||||
require(offset + size <= source.size) { "Invalid dimensions for spliterator: offset = $offset, size = $size, source has size of ${source.size}" }
|
||||
}
|
||||
|
||||
private var index = 0
|
||||
|
||||
override fun tryAdvance(action: Consumer<in T>): Boolean {
|
||||
if (index < size) {
|
||||
action.accept(source[offset + index++])
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
override fun trySplit(): Spliterator<T>? {
|
||||
if (index + 1 >= size) {
|
||||
return null
|
||||
}
|
||||
|
||||
val splitSize = size / 2
|
||||
|
||||
if (splitSize == 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
val pointer = index + offset
|
||||
index += splitSize
|
||||
return ArraySpliterator(source, pointer, splitSize)
|
||||
}
|
||||
|
||||
override fun estimateSize(): Long {
|
||||
return (size - index).toLong()
|
||||
}
|
||||
|
||||
override fun characteristics(): Int {
|
||||
return Spliterator.ORDERED or Spliterator.SIZED
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package ru.dbotthepony.kstarbound.util
|
||||
|
||||
import it.unimi.dsi.fastutil.objects.ObjectSpliterators
|
||||
import java.util.stream.Stream
|
||||
import java.util.stream.StreamSupport
|
||||
import kotlin.reflect.KClass
|
||||
@ -52,7 +53,7 @@ class NotNullTwoDimensionalArray<T : Any>(clazz: KClass<T>, private val width: I
|
||||
}
|
||||
|
||||
fun stream(): Stream<out T> {
|
||||
return StreamSupport.stream(ArraySpliterator(memory), false)
|
||||
return StreamSupport.stream(ObjectSpliterators.wrap(memory), false)
|
||||
}
|
||||
|
||||
fun indexedStream(): Stream<out Entry<T>> {
|
||||
|
@ -1,5 +1,6 @@
|
||||
package ru.dbotthepony.kstarbound.util
|
||||
|
||||
import it.unimi.dsi.fastutil.objects.ObjectSpliterators
|
||||
import java.util.stream.Stream
|
||||
import java.util.stream.StreamSupport
|
||||
import kotlin.reflect.KClass
|
||||
@ -44,7 +45,7 @@ class TwoDimensionalArray<T : Any>(clazz: KClass<T>, private val width: Int, pri
|
||||
}
|
||||
|
||||
fun stream(): Stream<out T?> {
|
||||
return StreamSupport.stream(ArraySpliterator(memory), false)
|
||||
return StreamSupport.stream(ObjectSpliterators.wrap(memory), false)
|
||||
}
|
||||
|
||||
fun indexedStream(): Stream<out Entry<T?>> {
|
||||
|
@ -3,36 +3,8 @@ package ru.dbotthepony.kstarbound.test
|
||||
import org.junit.jupiter.api.Assertions.*
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
import org.junit.jupiter.api.Test
|
||||
import ru.dbotthepony.kstarbound.util.ArraySpliterator
|
||||
import java.rmi.UnexpectedException
|
||||
|
||||
object MiscTests {
|
||||
@Test
|
||||
@DisplayName("Array Spliterator test")
|
||||
fun arraySpliterator() {
|
||||
val base = arrayOf(1, 2, 3, 4)
|
||||
val spliterator = ArraySpliterator(base)
|
||||
|
||||
assertTrue(spliterator.tryAdvance { assertEquals(1, it) })
|
||||
assertTrue(spliterator.tryAdvance { assertEquals(2, it) })
|
||||
assertTrue(spliterator.tryAdvance { assertEquals(3, it) })
|
||||
assertTrue(spliterator.tryAdvance { assertEquals(4, it) })
|
||||
assertFalse(spliterator.tryAdvance { throw UnexpectedException("Unreachable code") })
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Array Spliterator split test")
|
||||
fun arraySpliteratorSplit() {
|
||||
val base = arrayOf(1, 2, 3, 4, 5, 6)
|
||||
val spliterator = ArraySpliterator(base)
|
||||
|
||||
var i = 0
|
||||
|
||||
val splitted = spliterator.trySplit()!!
|
||||
|
||||
splitted.forEachRemaining { assertEquals(++i, it) }
|
||||
spliterator.forEachRemaining { assertEquals(++i, it) }
|
||||
|
||||
assertEquals(6, i)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user