Add non-boxing BufferView access
This commit is contained in:
parent
1315a8cd34
commit
f6b576071d
@ -0,0 +1,37 @@
|
|||||||
|
package space.kscience.kmath.structures
|
||||||
|
|
||||||
|
import space.kscience.kmath.misc.UnstableKMathAPI
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Non-boxing access to primitive [Double]
|
||||||
|
*/
|
||||||
|
@OptIn(UnstableKMathAPI::class)
|
||||||
|
public fun Buffer<Double>.getDouble(index: Int): Double = if (this is BufferView) {
|
||||||
|
val originIndex = originIndex(index)
|
||||||
|
if( originIndex>=0) {
|
||||||
|
origin.getDouble(originIndex)
|
||||||
|
} else {
|
||||||
|
get(index)
|
||||||
|
}
|
||||||
|
} else if (this is DoubleBuffer) {
|
||||||
|
array[index]
|
||||||
|
} else {
|
||||||
|
get(index)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Non-boxing access to primitive [Int]
|
||||||
|
*/
|
||||||
|
@OptIn(UnstableKMathAPI::class)
|
||||||
|
public fun Buffer<Int>.getInt(index: Int): Int = if (this is BufferView) {
|
||||||
|
val originIndex = originIndex(index)
|
||||||
|
if( originIndex>=0) {
|
||||||
|
origin.getInt(originIndex)
|
||||||
|
} else {
|
||||||
|
get(index)
|
||||||
|
}
|
||||||
|
} else if (this is IntBuffer) {
|
||||||
|
array[index]
|
||||||
|
} else {
|
||||||
|
get(index)
|
||||||
|
}
|
@ -3,7 +3,7 @@
|
|||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package space.kscience.kmath.stat
|
package space.kscience.kmath.random
|
||||||
|
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlin.contracts.InvocationKind
|
import kotlin.contracts.InvocationKind
|
@ -3,7 +3,7 @@
|
|||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package space.kscience.kmath.stat
|
package space.kscience.kmath.random
|
||||||
|
|
||||||
import space.kscience.kmath.chains.BlockingDoubleChain
|
import space.kscience.kmath.chains.BlockingDoubleChain
|
||||||
import space.kscience.kmath.chains.Chain
|
import space.kscience.kmath.chains.Chain
|
@ -3,7 +3,7 @@
|
|||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package space.kscience.kmath.stat
|
package space.kscience.kmath.random
|
||||||
|
|
||||||
import space.kscience.kmath.structures.DoubleBuffer
|
import space.kscience.kmath.structures.DoubleBuffer
|
||||||
import kotlin.random.Random
|
import kotlin.random.Random
|
@ -3,26 +3,33 @@ package space.kscience.kmath.stat
|
|||||||
import space.kscience.kmath.structures.Buffer
|
import space.kscience.kmath.structures.Buffer
|
||||||
import space.kscience.kmath.structures.asIterable
|
import space.kscience.kmath.structures.asIterable
|
||||||
|
|
||||||
public class Rank<T: Comparable<T>>: BlockingStatistic<T, IntArray> {
|
/**
|
||||||
override fun evaluateBlocking(data: Buffer<T>): IntArray {
|
* Rank statistics
|
||||||
// https://www.geeksforgeeks.org/rank-elements-array/
|
*/
|
||||||
val permutations = ArrayList<Pair<T, Int>>(data.size)
|
public class Rank<T : Comparable<T>> : BlockingStatistic<T, IntArray> {
|
||||||
data.asIterable().mapIndexedTo(permutations) { i, v -> v to i }
|
override fun evaluateBlocking(data: Buffer<T>): IntArray = Companion.evaluate(data)
|
||||||
permutations.sortBy { it.first }
|
|
||||||
var rank = 1
|
public companion object {
|
||||||
var i = 0
|
public fun <T : Comparable<T>> evaluate(data: Buffer<T>): IntArray {
|
||||||
val r = IntArray(data.size) { 0 }
|
// https://www.geeksforgeeks.org/rank-elements-array/
|
||||||
while (i < data.size) {
|
val permutations = ArrayList<Pair<T, Int>>(data.size)
|
||||||
var j = i
|
data.asIterable().mapIndexedTo(permutations) { i, v -> v to i }
|
||||||
while (j < data.size - 1 && permutations[j].first == permutations[j + 1]) ++j
|
permutations.sortBy { it.first }
|
||||||
val n = j - i + 1
|
var rank = 1
|
||||||
(0 until n).map { k ->
|
var i = 0
|
||||||
val idx = permutations[i + k].second
|
val r = IntArray(data.size)
|
||||||
r[idx] = rank + ((n - 1) * 0.5f).toInt()
|
while (i < data.size) {
|
||||||
|
var j = i
|
||||||
|
while (j < data.size - 1 && permutations[j].first == permutations[j + 1]) ++j
|
||||||
|
val n = j - i + 1
|
||||||
|
(0 until n).map { k ->
|
||||||
|
val idx = permutations[i + k].second
|
||||||
|
r[idx] = rank + ((n - 1) * 0.5f).toInt()
|
||||||
|
}
|
||||||
|
rank += n
|
||||||
|
i += n
|
||||||
}
|
}
|
||||||
rank += n
|
return r
|
||||||
i += n
|
|
||||||
}
|
}
|
||||||
return r
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user