Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement array element swapping & rotation #1227

Merged
merged 1 commit into from
Jan 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions kds/src/commonMain/kotlin/com/soywiz/kds/ArrayExt.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,89 @@
package com.soywiz.kds

import com.soywiz.kds.internal.*

@Deprecated("", ReplaceWith("Array<R>(size) { func(this[it]) }"))
inline fun <reified T, reified R> Array<T>.mapArray(func: (T) -> R): Array<R> = Array<R>(size) { func(this[it]) }

public fun <T> MutableList<T>.reverse(fromIndex: Int, toIndex: Int): Unit {
if (fromIndex < 0 || toIndex > size) {
throw IndexOutOfBoundsException("fromIndex: $fromIndex, toIndex: $toIndex, size: $size")
}
if (fromIndex > toIndex) {
throw IllegalArgumentException("fromIndex: $fromIndex > toIndex: $toIndex")
}
val midPoint = (fromIndex + toIndex) / 2
if (fromIndex == midPoint) return
var reverseIndex = toIndex - 1
for (index in fromIndex until midPoint) {
val tmp = this[index]
this[index] = this[reverseIndex]
this[reverseIndex] = tmp
reverseIndex--
}
}


fun BooleanArray.swap(lIndex: Int, rIndex: Int) { val temp = this[lIndex]; this[lIndex] = this[rIndex]; this[rIndex] = temp }
fun ByteArray.swap(lIndex: Int, rIndex: Int) { val temp = this[lIndex]; this[lIndex] = this[rIndex]; this[rIndex] = temp }
fun CharArray.swap(lIndex: Int, rIndex: Int) { val temp = this[lIndex]; this[lIndex] = this[rIndex]; this[rIndex] = temp }
fun ShortArray.swap(lIndex: Int, rIndex: Int) { val temp = this[lIndex]; this[lIndex] = this[rIndex]; this[rIndex] = temp }
fun IntArray.swap(lIndex: Int, rIndex: Int) { val temp = this[lIndex]; this[lIndex] = this[rIndex]; this[rIndex] = temp }
fun LongArray.swap(lIndex: Int, rIndex: Int) { val temp = this[lIndex]; this[lIndex] = this[rIndex]; this[rIndex] = temp }
fun FloatArray.swap(lIndex: Int, rIndex: Int) { val temp = this[lIndex]; this[lIndex] = this[rIndex]; this[rIndex] = temp }
fun DoubleArray.swap(lIndex: Int, rIndex: Int) { val temp = this[lIndex]; this[lIndex] = this[rIndex]; this[rIndex] = temp }
fun <T> Array<T>.swap(lIndex: Int, rIndex: Int) { val temp = this[lIndex]; this[lIndex] = this[rIndex]; this[rIndex] = temp }
fun <T> MutableList<T>.swap(lIndex: Int, rIndex: Int) { val temp = this[lIndex]; this[lIndex] = this[rIndex]; this[rIndex] = temp }

fun BooleanArray.rotateLeft(offset: Int = +1) = rotateRight(-offset)
fun ByteArray.rotateLeft(offset: Int = +1) = rotateRight(-offset)
fun CharArray.rotateLeft(offset: Int = +1) = rotateRight(-offset)
fun ShortArray.rotateLeft(offset: Int = +1) = rotateRight(-offset)
fun IntArray.rotateLeft(offset: Int = +1) = rotateRight(-offset)
fun LongArray.rotateLeft(offset: Int = +1) = rotateRight(-offset)
fun FloatArray.rotateLeft(offset: Int = +1) = rotateRight(-offset)
fun DoubleArray.rotateLeft(offset: Int = +1) = rotateRight(-offset)
fun <T> Array<T>.rotateLeft(offset: Int = +1) = rotateRight(-offset)
fun <T> MutableList<T>.rotateLeft(offset: Int = +1) = rotateRight(-offset)

fun BooleanArray.rotateRight(offset: Int = +1) = _rotateRight(size, offset) { start, end -> reverse(start, end) }
fun ByteArray.rotateRight(offset: Int = +1) = _rotateRight(size, offset) { start, end -> reverse(start, end) }
fun CharArray.rotateRight(offset: Int = +1) = _rotateRight(size, offset) { start, end -> reverse(start, end) }
fun ShortArray.rotateRight(offset: Int = +1) = _rotateRight(size, offset) { start, end -> reverse(start, end) }
fun IntArray.rotateRight(offset: Int = +1) = _rotateRight(size, offset) { start, end -> reverse(start, end) }
fun LongArray.rotateRight(offset: Int = +1) = _rotateRight(size, offset) { start, end -> reverse(start, end) }
fun FloatArray.rotateRight(offset: Int = +1) = _rotateRight(size, offset) { start, end -> reverse(start, end) }
fun DoubleArray.rotateRight(offset: Int = +1) = _rotateRight(size, offset) { start, end -> reverse(start, end) }
fun <T> Array<T>.rotateRight(offset: Int = +1) = _rotateRight(size, offset) { start, end -> reverse(start, end) }
fun <T> MutableList<T>.rotateRight(offset: Int = +1) = _rotateRight(size, offset) { start, end -> reverse(start, end) }

fun BooleanArray.rotatedLeft(offset: Int = +1): BooleanArray = copyOf().also { it.rotateLeft(offset) }
fun ByteArray.rotatedLeft(offset: Int = +1): ByteArray = copyOf().also { it.rotateLeft(offset) }
fun CharArray.rotatedLeft(offset: Int = +1): CharArray = copyOf().also { it.rotateLeft(offset) }
fun ShortArray.rotatedLeft(offset: Int = +1): ShortArray = copyOf().also { it.rotateLeft(offset) }
fun IntArray.rotatedLeft(offset: Int = +1): IntArray = copyOf().also { it.rotateLeft(offset) }
fun LongArray.rotatedLeft(offset: Int = +1): LongArray = copyOf().also { it.rotateLeft(offset) }
fun FloatArray.rotatedLeft(offset: Int = +1): FloatArray = copyOf().also { it.rotateLeft(offset) }
fun DoubleArray.rotatedLeft(offset: Int = +1): DoubleArray = copyOf().also { it.rotateLeft(offset) }
fun <T> Array<T>.rotatedLeft(offset: Int = +1): Array<T> = copyOf().also { it.rotateLeft(offset) }
fun <T> List<T>.rotatedLeft(offset: Int = +1): List<T> = toMutableList().also { it.rotateLeft(offset) }

fun BooleanArray.rotatedRight(offset: Int = +1): BooleanArray = copyOf().also { it.rotateRight(offset) }
fun ByteArray.rotatedRight(offset: Int = +1): ByteArray = copyOf().also { it.rotateRight(offset) }
fun CharArray.rotatedRight(offset: Int = +1): CharArray = copyOf().also { it.rotateRight(offset) }
fun ShortArray.rotatedRight(offset: Int = +1): ShortArray = copyOf().also { it.rotateRight(offset) }
fun IntArray.rotatedRight(offset: Int = +1): IntArray = copyOf().also { it.rotateRight(offset) }
fun LongArray.rotatedRight(offset: Int = +1): LongArray = copyOf().also { it.rotateRight(offset) }
fun FloatArray.rotatedRight(offset: Int = +1): FloatArray = copyOf().also { it.rotateRight(offset) }
fun DoubleArray.rotatedRight(offset: Int = +1): DoubleArray = copyOf().also { it.rotateRight(offset) }
fun <T> Array<T>.rotatedRight(offset: Int = +1): Array<T> = copyOf().also { it.rotateRight(offset) }
fun <T> List<T>.rotatedRight(offset: Int = +1): List<T> = toMutableList().also { it.rotateRight(offset) }

private inline fun _rotateRight(size: Int, offset: Int, reverse: (start: Int, end: Int) -> Unit) {
val offset = offset umod size
if (offset == 0) return
check(offset in 1 until size)
reverse(0, size)
reverse(0, offset)
reverse(offset, size)
}
6 changes: 0 additions & 6 deletions kds/src/commonMain/kotlin/com/soywiz/kds/ArrayListExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,6 @@ fun IntArrayList.toDoubleArrayList(): DoubleArrayList {
return out
}

fun <T> MutableList<T>.swap(lIndex: Int, rIndex: Int) {
val temp = this[lIndex]
this[lIndex] = this[rIndex]
this[rIndex] = temp
}

fun <T> List<T>.rotated(offset: Int): List<T> = ArrayList<T>(this.size).also {
for (n in 0 until this.size) it.add(this.getCyclic(n - offset))
}
Expand Down
63 changes: 63 additions & 0 deletions kds/src/commonTest/kotlin/com/soywiz/kds/ArrayExtTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.soywiz.kds

import kotlin.test.*

class ArrayExtTest {
val array = intArrayOf(1, 2, 3, 4)

@Test
fun testSwap() {
assertEquals(listOf(4, 2, 3, 1), array.copyOf().also { it.swap(0, 3) }.toList())
assertEquals(listOf(1, 3, 2, 4), array.copyOf().also { it.swap(1, 2) }.toList())
assertEquals(listOf(3, 2, 1, 4), array.copyOf().also { it.swap(0, 2) }.toList())
assertEquals(listOf(3, 2, 1, 4), mutableListOf(1, 2, 3, 4).also { it.swap(0, 2) }.toList())
}

@Test
fun testRotatedRight() {
assertEquals(listOf(4, 1, 2, 3), array.rotatedRight(+1).toList())
assertEquals(listOf(3, 4, 1, 2), array.rotatedRight(+2).toList())
assertEquals(listOf(2, 3, 4, 1), array.rotatedRight(+3).toList())
assertEquals(listOf(1, 2, 3, 4), array.rotatedRight(+4).toList())
assertEquals(listOf(1, 2, 3, 4), array.rotatedRight(0).toList())
assertEquals(listOf(2, 3, 4, 1), array.rotatedRight(-1).toList())
assertEquals(listOf(3, 4, 1, 2), array.rotatedRight(-2).toList())
assertEquals(listOf(4, 1, 2, 3), array.rotatedRight(-3).toList())
assertEquals(listOf(1, 2, 3, 4), array.rotatedRight(-4).toList())
}

@Test
fun testRotatedLeft() {
assertEquals(listOf(4, 1, 2, 3), array.rotatedLeft(-1).toList())
assertEquals(listOf(3, 4, 1, 2), array.rotatedLeft(-2).toList())
assertEquals(listOf(2, 3, 4, 1), array.rotatedLeft(-3).toList())
assertEquals(listOf(1, 2, 3, 4), array.rotatedLeft(-4).toList())
assertEquals(listOf(1, 2, 3, 4), array.rotatedLeft(0).toList())
assertEquals(listOf(2, 3, 4, 1), array.rotatedLeft(+1).toList())
assertEquals(listOf(3, 4, 1, 2), array.rotatedLeft(+2).toList())
assertEquals(listOf(4, 1, 2, 3), array.rotatedLeft(+3).toList())
assertEquals(listOf(1, 2, 3, 4), array.rotatedLeft(+4).toList())
}

@Test
fun testVariants() {
assertEquals(listOf(3, 4, 1, 2), listOf(1, 2, 3, 4).rotatedRight(-2).toList())
assertEquals(listOf(3, 4, 1, 2), arrayOf<Int>(1, 2, 3, 4).rotatedRight(-2).toList())
assertEquals(listOf(3, 4, 1, 2), byteArrayOf(1, 2, 3, 4).rotatedRight(-2).map { it.toInt() }.toList())
assertEquals(listOf(3, 4, 1, 2), charArrayOf(1.toChar(), 2.toChar(), 3.toChar(), 4.toChar()).rotatedRight(-2).map { it.toInt() }.toList())
assertEquals(listOf(3, 4, 1, 2), shortArrayOf(1, 2, 3, 4).rotatedRight(-2).map { it.toInt() }.toList())
assertEquals(listOf(3, 4, 1, 2), longArrayOf(1, 2, 3, 4).rotatedRight(-2).map { it.toInt() }.toList())
assertEquals(listOf(3, 4, 1, 2), floatArrayOf(1f, 2f, 3f, 4f).rotatedRight(-2).map { it.toInt() }.toList())
assertEquals(listOf(3, 4, 1, 2), doubleArrayOf(1.0, 2.0, 3.0, 4.0).rotatedRight(-2).map { it.toInt() }.toList())

assertEquals(listOf(4, 1, 2, 3), array.rotatedLeft(-1).toList())
assertEquals(listOf(4, 1, 2, 3), listOf(1, 2, 3, 4).rotatedLeft(-1).toList())
assertEquals(listOf(4, 1, 2, 3), arrayOf(1, 2, 3, 4).rotatedLeft(-1).toList())
assertEquals(listOf(4, 1, 2, 3), byteArrayOf(1, 2, 3, 4).rotatedLeft(-1).map { it.toInt() }.toList())
assertEquals(listOf(4, 1, 2, 3), charArrayOf(1.toChar(), 2.toChar(), 3.toChar(), 4.toChar()).rotatedLeft(-1).map { it.toInt() }.toList())
assertEquals(listOf(4, 1, 2, 3), shortArrayOf(1, 2, 3, 4).rotatedLeft(-1).map { it.toInt() }.toList())
assertEquals(listOf(4, 1, 2, 3), longArrayOf(1, 2, 3, 4).rotatedLeft(-1).map { it.toInt() }.toList())
assertEquals(listOf(4, 1, 2, 3), floatArrayOf(1f, 2f, 3f, 4f).rotatedLeft(-1).map { it.toInt() }.toList())
assertEquals(listOf(4, 1, 2, 3), doubleArrayOf(1.0, 2.0, 3.0, 4.0).rotatedLeft(-1).map { it.toInt() }.toList())
}
}