Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

Add *ArrayList.getAt variant that doesn't implement the interface to prevent boxing #11

Merged
merged 2 commits into from
Apr 28, 2020
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
24 changes: 18 additions & 6 deletions kds/src/commonMain/kotlin/com/soywiz/kds/ArrayList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ class IntArrayList(capacity: Int = 7) : List<Int> {
fun add(values: IntArrayList) = add(values.data, 0, values.size)
fun add(values: Iterable<Int>) = run { for (v in values) add(v) }

override operator fun get(index: Int): Int = data[index]
@Deprecated("Try to use getAt instead to prevent boxing", ReplaceWith("getAt(index)"))
override operator fun get(index: Int): Int = getAt(index)

/** Gets an item of the list without boxing */
fun getAt(index: Int): Int = data[index]

operator fun set(index: Int, value: Int) = run {
if (index >= length) {
Expand Down Expand Up @@ -123,7 +127,7 @@ class IntArrayList(capacity: Int = 7) : List<Int> {
sb.append('[')
for (n in 0 until size) {
if (n != 0) sb.append(", ")
sb.append(this[n])
sb.append(this.getAt(n))
}
sb.append(']')
}.toString()
Expand Down Expand Up @@ -185,7 +189,11 @@ class DoubleArrayList(capacity: Int = 7) : List<Double> {
fun add(values: DoubleArrayList) = add(values.data, 0, values.size)
fun add(values: Iterable<Double>) = run { for (v in values) add(v) }

override operator fun get(index: Int): Double = data[index]
@Deprecated("Try to use getAt instead to prevent boxing", ReplaceWith("getAt(index)"))
override operator fun get(index: Int): Double = getAt(index)

/** Gets an item of the list without boxing */
fun getAt(index: Int): Double = data[index]

operator fun set(index: Int, value: Double) = run {
if (index >= length) {
Expand Down Expand Up @@ -251,7 +259,7 @@ class DoubleArrayList(capacity: Int = 7) : List<Double> {
sb.append('[')
for (n in 0 until size) {
if (n != 0) sb.append(", ")
sb.append(this[n])
sb.append(this.getAt(n))
}
sb.append(']')
}.toString()
Expand Down Expand Up @@ -313,7 +321,11 @@ class FloatArrayList(capacity: Int = 7) : List<Float> {
fun add(values: FloatArrayList) = add(values.data, 0, values.size)
fun add(values: Iterable<Float>) = run { for (v in values) add(v) }

override operator fun get(index: Int): Float = data[index]
@Deprecated("Try to use getAt instead to prevent boxing", ReplaceWith("getAt(index)"))
override operator fun get(index: Int): Float = getAt(index)

/** Gets an item of the list without boxing */
fun getAt(index: Int): Float = data[index]

operator fun set(index: Int, value: Float) = run {
if (index >= length) {
Expand Down Expand Up @@ -379,7 +391,7 @@ class FloatArrayList(capacity: Int = 7) : List<Float> {
sb.append('[')
for (n in 0 until size) {
if (n != 0) sb.append(", ")
sb.append(this[n])
sb.append(this.getAt(n))
}
sb.append(']')
}.toString()
Expand Down
6 changes: 3 additions & 3 deletions kds/src/commonMain/kotlin/com/soywiz/kds/ArrayListExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ private object IntArrayListSortOps : SortOps<IntArrayList>() {
}

private object DoubleArrayListSortOps : SortOps<DoubleArrayList>() {
override fun compare(subject: DoubleArrayList, l: Int, r: Int): Int = subject[l].compareTo(subject[r])
override fun compare(subject: DoubleArrayList, l: Int, r: Int): Int = subject.getAt(l).compareTo(subject.getAt(r))
override fun swap(subject: DoubleArrayList, indexL: Int, indexR: Int) {
val l = subject[indexL]
val r = subject[indexR]
val l = subject.getAt(indexL)
val r = subject.getAt(indexR)
subject[indexR] = l
subject[indexL] = r
}
Expand Down
12 changes: 6 additions & 6 deletions kds/src/commonMain/kotlin/com/soywiz/kds/_Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ inline fun mapWhileDouble(cond: (index: Int) -> Boolean, gen: (Int) -> Double):

fun <T> List<T>.getCyclic(index: Int) = this[index umod this.size]
fun <T> Array<T>.getCyclic(index: Int) = this[index umod this.size]
fun IntArrayList.getCyclic(index: Int) = this[index umod this.size]
fun FloatArrayList.getCyclic(index: Int) = this[index umod this.size]
fun DoubleArrayList.getCyclic(index: Int) = this[index umod this.size]
fun IntArrayList.getCyclic(index: Int) = this.getAt(index umod this.size)
fun FloatArrayList.getCyclic(index: Int) = this.getAt(index umod this.size)
fun DoubleArrayList.getCyclic(index: Int) = this.getAt(index umod this.size)

fun <T> Array2<T>.getCyclic(x: Int, y: Int) = this[x umod this.width, y umod this.height]
fun IntArray2.getCyclic(x: Int, y: Int) = this[x umod this.width, y umod this.height]
Expand Down Expand Up @@ -41,9 +41,9 @@ fun IntArray.binarySearch(v: Int, fromIndex: Int = 0, toIndex: Int = size): BSea

fun FloatArray.binarySearch(v: Float, fromIndex: Int = 0, toIndex: Int = size): BSearchResult = BSearchResult(genericBinarySearch(fromIndex, toIndex) { this[it].compareTo(v) })
fun DoubleArray.binarySearch(v: Double, fromIndex: Int = 0, toIndex: Int = size): BSearchResult = BSearchResult(genericBinarySearch(fromIndex, toIndex) { this[it].compareTo(v) })
fun IntArrayList.binarySearch(v: Int, fromIndex: Int = 0, toIndex: Int = size): BSearchResult = BSearchResult(genericBinarySearch(fromIndex, toIndex) { this[it].compareTo(v) })
fun FloatArrayList.binarySearch(v: Int, fromIndex: Int = 0, toIndex: Int = size): BSearchResult = BSearchResult(genericBinarySearch(fromIndex, toIndex) { this[it].compareTo(v) })
fun DoubleArrayList.binarySearch(v: Double, fromIndex: Int = 0, toIndex: Int = size): BSearchResult = BSearchResult(genericBinarySearch(fromIndex, toIndex) { this[it].compareTo(v) })
fun IntArrayList.binarySearch(v: Int, fromIndex: Int = 0, toIndex: Int = size): BSearchResult = BSearchResult(genericBinarySearch(fromIndex, toIndex) { this.getAt(it).compareTo(v) })
fun FloatArrayList.binarySearch(v: Int, fromIndex: Int = 0, toIndex: Int = size): BSearchResult = BSearchResult(genericBinarySearch(fromIndex, toIndex) { this.getAt(it).compareTo(v) })
fun DoubleArrayList.binarySearch(v: Double, fromIndex: Int = 0, toIndex: Int = size): BSearchResult = BSearchResult(genericBinarySearch(fromIndex, toIndex) { this.getAt(it).compareTo(v) })

inline fun genericBinarySearch(
fromIndex: Int,
Expand Down
8 changes: 6 additions & 2 deletions template/src/main/kotlin/com/soywiz/kds/TGenArrayList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ class TGenArrayList<TGen>(capacity: Int = 7) : List<TGen> {
fun add(values: TGenArrayList<TGen>) = add(values.data, 0, values.size)
fun add(values: Iterable<TGen>) = run { for (v in values) add(v) }

override operator fun get(index: Int): TGen = data[index]
@Deprecated("Try to use getAt instead to prevent boxing", ReplaceWith("getAt(index)"))
override operator fun get(index: Int): TGen = getAt(index)

/** Gets an item of the list without boxing */
fun getAt(index: Int): TGen = data[index]

operator fun set(index: Int, value: TGen) = run {
if (index >= length) {
Expand Down Expand Up @@ -121,7 +125,7 @@ class TGenArrayList<TGen>(capacity: Int = 7) : List<TGen> {
sb.append('[')
for (n in 0 until size) {
if (n != 0) sb.append(", ")
sb.append(this[n])
sb.append(this.getAt(n))
}
sb.append(']')
}.toString()
Expand Down