forked from kscience/kmath
Revert "Added internal mapping functionality to buffers"
This commit is contained in:
parent
87e8566157
commit
58e939e0cf
@ -32,6 +32,10 @@ class BoxingNDField<T, F : Field<T>>(
|
|||||||
return BufferedNDFieldElement(
|
return BufferedNDFieldElement(
|
||||||
this,
|
this,
|
||||||
buildBuffer(arg.strides.linearSize) { offset -> elementContext.transform(arg.buffer[offset]) })
|
buildBuffer(arg.strides.linearSize) { offset -> elementContext.transform(arg.buffer[offset]) })
|
||||||
|
|
||||||
|
// val buffer = arg.buffer.transform { _, value -> elementContext.transform(value) }
|
||||||
|
// return BufferedNDFieldElement(this, buffer)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun mapIndexed(
|
override fun mapIndexed(
|
||||||
@ -47,6 +51,10 @@ class BoxingNDField<T, F : Field<T>>(
|
|||||||
arg.buffer[offset]
|
arg.buffer[offset]
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// val buffer =
|
||||||
|
// arg.buffer.transform { offset, value -> elementContext.transform(arg.strides.index(offset), value) }
|
||||||
|
// return BufferedNDFieldElement(this, buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun combine(
|
override fun combine(
|
||||||
|
@ -31,11 +31,6 @@ interface Buffer<T> {
|
|||||||
fun contentEquals(other: Buffer<*>): Boolean =
|
fun contentEquals(other: Buffer<*>): Boolean =
|
||||||
asSequence().mapIndexed { index, value -> value == other[index] }.all { it }
|
asSequence().mapIndexed { index, value -> value == other[index] }.all { it }
|
||||||
|
|
||||||
/**
|
|
||||||
* Map the contents of the buffer to new buffer of the same type
|
|
||||||
*/
|
|
||||||
fun transform(transformation: (index: Int, value: T) -> T): Buffer<T>
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -78,8 +73,6 @@ interface MutableBuffer<T> : Buffer<T> {
|
|||||||
*/
|
*/
|
||||||
fun copy(): MutableBuffer<T>
|
fun copy(): MutableBuffer<T>
|
||||||
|
|
||||||
fun transformInPlace(transformation: (index: Int, value: T) -> T)
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
/**
|
/**
|
||||||
* Create a boxing mutable buffer of given type
|
* Create a boxing mutable buffer of given type
|
||||||
@ -112,9 +105,6 @@ inline class ListBuffer<T>(private val list: List<T>) : Buffer<T> {
|
|||||||
override fun get(index: Int): T = list[index]
|
override fun get(index: Int): T = list[index]
|
||||||
|
|
||||||
override fun iterator(): Iterator<T> = list.iterator()
|
override fun iterator(): Iterator<T> = list.iterator()
|
||||||
|
|
||||||
override fun transform(transformation: (index: Int, value: T) -> T): ListBuffer<T> =
|
|
||||||
list.mapIndexed(transformation).asBuffer()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> List<T>.asBuffer() = ListBuffer(this)
|
fun <T> List<T>.asBuffer() = ListBuffer(this)
|
||||||
@ -131,15 +121,7 @@ inline class MutableListBuffer<T>(private val list: MutableList<T>) : MutableBuf
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun iterator(): Iterator<T> = list.iterator()
|
override fun iterator(): Iterator<T> = list.iterator()
|
||||||
|
|
||||||
override fun transform(transformation: (index: Int, value: T) -> T): ListBuffer<T> =
|
|
||||||
list.mapIndexed(transformation).asBuffer()
|
|
||||||
|
|
||||||
override fun copy(): MutableBuffer<T> = MutableListBuffer(ArrayList(list))
|
override fun copy(): MutableBuffer<T> = MutableListBuffer(ArrayList(list))
|
||||||
|
|
||||||
override fun transformInPlace(transformation: (index: Int, value: T) -> T) = list.forEachIndexed { index, value ->
|
|
||||||
list[index] = transformation(index, value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> MutableList<T>.asBuffer() = MutableListBuffer(this)
|
fun <T> MutableList<T>.asBuffer() = MutableListBuffer(this)
|
||||||
@ -158,14 +140,6 @@ class ArrayBuffer<T>(private val array: Array<T>) : MutableBuffer<T> {
|
|||||||
override fun iterator(): Iterator<T> = array.iterator()
|
override fun iterator(): Iterator<T> = array.iterator()
|
||||||
|
|
||||||
override fun copy(): MutableBuffer<T> = ArrayBuffer(array.copyOf())
|
override fun copy(): MutableBuffer<T> = ArrayBuffer(array.copyOf())
|
||||||
|
|
||||||
override fun transform(transformation: (index: Int, value: T) -> T): ArrayBuffer<T> =
|
|
||||||
Array(size) { index -> transformation(index, get(index)) }.asBuffer()
|
|
||||||
|
|
||||||
|
|
||||||
override fun transformInPlace(transformation: (index: Int, value: T) -> T) = array.forEachIndexed { index, value ->
|
|
||||||
array[index] = transformation(index, value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> Array<T>.asBuffer() = ArrayBuffer(this)
|
fun <T> Array<T>.asBuffer() = ArrayBuffer(this)
|
||||||
@ -183,13 +157,6 @@ inline class DoubleBuffer(private val array: DoubleArray) : MutableBuffer<Double
|
|||||||
|
|
||||||
override fun copy(): MutableBuffer<Double> = DoubleBuffer(array.copyOf())
|
override fun copy(): MutableBuffer<Double> = DoubleBuffer(array.copyOf())
|
||||||
|
|
||||||
override fun transform(transformation: (index: Int, value: Double) -> Double): DoubleBuffer =
|
|
||||||
DoubleArray(size) { index -> transformation(index, get(index)) }.asBuffer()
|
|
||||||
|
|
||||||
override fun transformInPlace(transformation: (index: Int, value: Double) -> Double) =
|
|
||||||
array.forEachIndexed { index, value ->
|
|
||||||
array[index] = transformation(index, value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun DoubleArray.asBuffer() = DoubleBuffer(this)
|
fun DoubleArray.asBuffer() = DoubleBuffer(this)
|
||||||
@ -207,13 +174,6 @@ inline class ShortBuffer(private val array: ShortArray) : MutableBuffer<Short> {
|
|||||||
|
|
||||||
override fun copy(): MutableBuffer<Short> = ShortBuffer(array.copyOf())
|
override fun copy(): MutableBuffer<Short> = ShortBuffer(array.copyOf())
|
||||||
|
|
||||||
override fun transform(transformation: (index: Int, value: Short) -> Short): ShortBuffer =
|
|
||||||
ShortArray(size) { index -> transformation(index, get(index)) }.asBuffer()
|
|
||||||
|
|
||||||
override fun transformInPlace(transformation: (index: Int, value: Short) -> Short) =
|
|
||||||
array.forEachIndexed { index, value ->
|
|
||||||
array[index] = transformation(index, value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ShortArray.asBuffer() = ShortBuffer(this)
|
fun ShortArray.asBuffer() = ShortBuffer(this)
|
||||||
@ -231,13 +191,6 @@ inline class IntBuffer(private val array: IntArray) : MutableBuffer<Int> {
|
|||||||
|
|
||||||
override fun copy(): MutableBuffer<Int> = IntBuffer(array.copyOf())
|
override fun copy(): MutableBuffer<Int> = IntBuffer(array.copyOf())
|
||||||
|
|
||||||
override fun transform(transformation: (index: Int, value: Int) -> Int): IntBuffer =
|
|
||||||
IntArray(size) { index -> transformation(index, get(index)) }.asBuffer()
|
|
||||||
|
|
||||||
override fun transformInPlace(transformation: (index: Int, value: Int) -> Int) =
|
|
||||||
array.forEachIndexed { index, value ->
|
|
||||||
array[index] = transformation(index, value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun IntArray.asBuffer() = IntBuffer(this)
|
fun IntArray.asBuffer() = IntBuffer(this)
|
||||||
@ -255,13 +208,6 @@ inline class LongBuffer(private val array: LongArray) : MutableBuffer<Long> {
|
|||||||
|
|
||||||
override fun copy(): MutableBuffer<Long> = LongBuffer(array.copyOf())
|
override fun copy(): MutableBuffer<Long> = LongBuffer(array.copyOf())
|
||||||
|
|
||||||
override fun transform(transformation: (index: Int, value: Long) -> Long): LongBuffer =
|
|
||||||
LongArray(size) { index -> transformation(index, get(index)) }.asBuffer()
|
|
||||||
|
|
||||||
override fun transformInPlace(transformation: (index: Int, value: Long) -> Long) =
|
|
||||||
array.forEachIndexed { index, value ->
|
|
||||||
array[index] = transformation(index, value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun LongArray.asBuffer() = LongBuffer(this)
|
fun LongArray.asBuffer() = LongBuffer(this)
|
||||||
@ -272,8 +218,6 @@ inline class ReadOnlyBuffer<T>(private val buffer: MutableBuffer<T>) : Buffer<T>
|
|||||||
override fun get(index: Int): T = buffer.get(index)
|
override fun get(index: Int): T = buffer.get(index)
|
||||||
|
|
||||||
override fun iterator(): Iterator<T> = buffer.iterator()
|
override fun iterator(): Iterator<T> = buffer.iterator()
|
||||||
|
|
||||||
override fun transform(transformation: (index: Int, value: T) -> T): Buffer<T> = buffer.transform(transformation)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -292,10 +236,6 @@ class VirtualBuffer<T>(override val size: Int, private val generator: (Int) -> T
|
|||||||
super.contentEquals(other)
|
super.contentEquals(other)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO this composition could become very compex very fast, replace it by boxed generator?
|
|
||||||
override fun transform(transformation: (index: Int, value: T) -> T): Buffer<T> =
|
|
||||||
VirtualBuffer(size) { index -> transformation(index, generator(index)) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -200,7 +200,7 @@ fun <T> ndStructure(strides: Strides, bufferFactory: BufferFactory<T> = Buffer.C
|
|||||||
* Inline create NDStructure with non-boxing buffer implementation if it is possible
|
* Inline create NDStructure with non-boxing buffer implementation if it is possible
|
||||||
*/
|
*/
|
||||||
inline fun <reified T : Any> inlineNDStructure(strides: Strides, crossinline initializer: (IntArray) -> T) =
|
inline fun <reified T : Any> inlineNDStructure(strides: Strides, crossinline initializer: (IntArray) -> T) =
|
||||||
BufferNDStructure(strides, Buffer.Companion.auto(strides.linearSize) { i -> initializer(strides.index(i)) })
|
BufferNDStructure(strides, Buffer.auto(strides.linearSize) { i -> initializer(strides.index(i)) })
|
||||||
|
|
||||||
fun <T> ndStructure(shape: IntArray, bufferFactory: BufferFactory<T> = Buffer.Companion::boxing, initializer: (IntArray) -> T) =
|
fun <T> ndStructure(shape: IntArray, bufferFactory: BufferFactory<T> = Buffer.Companion::boxing, initializer: (IntArray) -> T) =
|
||||||
ndStructure(DefaultStrides(shape), bufferFactory, initializer)
|
ndStructure(DefaultStrides(shape), bufferFactory, initializer)
|
||||||
|
Loading…
Reference in New Issue
Block a user