From d615bc8a42e4d23c82e93688fd7083e105833b8c Mon Sep 17 00:00:00 2001 From: Sergei Lebedev Date: Thu, 22 Sep 2016 14:01:04 +0300 Subject: [PATCH] Dropped Guava we didn't use much of its functionality anyway --- CHANGES | 1 + build.gradle | 1 - gradle/wrapper/gradle-wrapper.jar | Bin 52818 -> 52928 bytes gradle/wrapper/gradle-wrapper.properties | 2 +- src/main/kotlin/org/jetbrains/bio/npy/Npy.kt | 85 ++++++++---------- src/main/kotlin/org/jetbrains/bio/npy/Npz.kt | 11 ++- .../org/jetbrains/bio/npy/SplitBuffer.kt | 16 ++-- .../kotlin/org/jetbrains/bio/npy/Support.kt | 32 ++++++- .../org/jetbrains/bio/npy/NpyFileTest.kt | 4 +- 9 files changed, 82 insertions(+), 70 deletions(-) diff --git a/CHANGES b/CHANGES index 4680ce5..f47fc37 100644 --- a/CHANGES +++ b/CHANGES @@ -9,6 +9,7 @@ Version 0.3.1 - Changed 'NpyFile.write' and 'NpzFile.Writer.write' to work in constant amount of memory. - Allowed writing arrays using a non-native byte order. +- Dropped Guava dependency. Version 0.3.0 ------------- diff --git a/build.gradle b/build.gradle index 29b18e1..2d9a106 100644 --- a/build.gradle +++ b/build.gradle @@ -28,7 +28,6 @@ repositories { dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" - compile 'com.google.guava:guava:19.0' testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version" testCompile 'junit:junit:4.+' diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index deedc7fa5e6310eac3148a7dd0b1f069b07364cb..6ffa237849ef3607e39c3b334a92a65367962071 100644 GIT binary patch delta 3904 zcmaJ@2{@G78=v{cnk^N@V626>mZ4+`30Wg+vKNs_A^S4KEnA6m$(E2cgoqZTj4aug zB6THHM2S+g{m*>k>Y00==l?y=dCvKM=l8z9_q=Bv$i_8a!Eu-w;TSexFf1&X_wK}G zj_s_lGN`N6PSQ+Px8`}rx9I!P7mN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BHWN{z)3~2&pb1V?@O2h9Juw-Gsnj>d*H}bu15Fl^A a2gqCd`J39WpNJl2!-@R{BHB$n;{O0=Lu#}D diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index c3ae1e7..af5b279 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,4 +1,4 @@ -#Wed Sep 21 12:59:54 MSK 2016 +#Thu Sep 22 13:26:27 MSK 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME diff --git a/src/main/kotlin/org/jetbrains/bio/npy/Npy.kt b/src/main/kotlin/org/jetbrains/bio/npy/Npy.kt index ea8b9d2..a5c2c6e 100644 --- a/src/main/kotlin/org/jetbrains/bio/npy/Npy.kt +++ b/src/main/kotlin/org/jetbrains/bio/npy/Npy.kt @@ -1,10 +1,5 @@ package org.jetbrains.bio.npy -import com.google.common.base.Charsets -import com.google.common.base.MoreObjects -import com.google.common.base.Strings -import com.google.common.collect.Iterables -import com.google.common.primitives.* import java.nio.ByteBuffer import java.nio.ByteOrder import java.nio.channels.FileChannel @@ -66,18 +61,11 @@ object NpyFile { else -> impossible() } + metaUnpadded.length - Strings.padEnd(metaUnpadded, - metaUnpadded.length + (16 - totalUnpadded % 16), - ' ') + metaUnpadded.padEnd(metaUnpadded.length + (16 - totalUnpadded % 16), ' ') .toByteArray(Charsets.US_ASCII) } - /** - * Allocates a [ByteBuffer] for the contents described by the header. - * - * The returned buffer already contains the serialized header and is - * guaranteed to be in correct [ByteOrder]. - */ + /** Allocates a [ByteBuffer] for this header. */ fun allocate(): ByteBuffer { val total = MAGIC.size + 2 + when (major to minor) { 1 to 0 -> 2 @@ -92,7 +80,10 @@ object NpyFile { put(minor.toByte()) when (major to minor) { - 1 to 0 -> putShort(Shorts.checkedCast(meta.size.toLong())) + 1 to 0 -> { + check(meta.size <= Short.MAX_VALUE) + putShort(meta.size.toShort()) + } 2 to 0 -> putInt(meta.size) } @@ -251,7 +242,7 @@ object NpyFile { write(path, allocate(data, shape)) } - private fun write(path: Path, chunks: Iterable) { + private fun write(path: Path, chunks: Sequence) { FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE).use { @@ -264,57 +255,55 @@ object NpyFile { } } - internal fun allocate(data: BooleanArray, shape: IntArray): Iterable { + internal fun allocate(data: BooleanArray, shape: IntArray): Sequence { val header = Header(order = null, type = 'b', bytes = 1, shape = shape) - return Iterables.concat(listOf(header.allocate()), - BooleanArraySplitBuffer(data)) + return sequenceOf(header.allocate()) + BooleanArraySplitBuffer(data) } - internal fun allocate(data: ByteArray, shape: IntArray): Iterable { + internal fun allocate(data: ByteArray, shape: IntArray): Sequence { val header = Header(order = null, type = 'i', bytes = 1, shape = shape) - return listOf(header.allocate(), ByteBuffer.wrap(data)) + return sequenceOf(header.allocate()) + ByteBuffer.wrap(data) } internal fun allocate(data: ShortArray, shape: IntArray, - order: ByteOrder): Iterable { - val header = Header(order = order, type = 'i', bytes = Shorts.BYTES, shape = shape) - return Iterables.concat(listOf(header.allocate()), - ShortArraySplitBuffer(data, order)) + order: ByteOrder): Sequence { + val header = Header(order = order, type = 'i', + bytes = java.lang.Short.BYTES, shape = shape) + return sequenceOf(header.allocate()) + ShortArraySplitBuffer(data, order) } internal fun allocate(data: IntArray, shape: IntArray, - order: ByteOrder): Iterable { - val header = Header(order = order, type = 'i', bytes = Ints.BYTES, shape = shape) - return Iterables.concat(listOf(header.allocate()), - IntArraySplitBuffer(data, order)) + order: ByteOrder): Sequence { + val header = Header(order = order, type = 'i', + bytes = java.lang.Integer.BYTES, shape = shape) + return sequenceOf(header.allocate()) + IntArraySplitBuffer(data, order) } internal fun allocate(data: LongArray, shape: IntArray, - order: ByteOrder): Iterable { - val header = Header(order = order, type = 'i', bytes = Longs.BYTES, shape = shape) - return Iterables.concat(listOf(header.allocate()), - LongArraySplitBuffer(data, order)) + order: ByteOrder): Sequence { + val header = Header(order = order, type = 'i', + bytes = java.lang.Long.BYTES, shape = shape) + return sequenceOf(header.allocate()) + LongArraySplitBuffer(data, order) } internal fun allocate(data: FloatArray, shape: IntArray, - order: ByteOrder): Iterable { - val header = Header(order = order, type = 'f', bytes = Floats.BYTES, shape = shape) - return Iterables.concat(listOf(header.allocate()), - FloatArraySplitBuffer(data, order)) + order: ByteOrder): Sequence { + val header = Header(order = order, type = 'f', + bytes = java.lang.Float.BYTES, shape = shape) + return sequenceOf(header.allocate()) + FloatArraySplitBuffer(data, order) } internal fun allocate(data: DoubleArray, shape: IntArray, - order: ByteOrder): Iterable { - val header = Header(order = order, type = 'f', bytes = Doubles.BYTES, shape = shape) - return Iterables.concat(listOf(header.allocate()), - DoubleArraySplitBuffer(data, order)) + order: ByteOrder): Sequence { + val header = Header(order = order, type = 'f', + bytes = java.lang.Double.BYTES, shape = shape) + return sequenceOf(header.allocate()) + DoubleArraySplitBuffer(data, order) } - internal fun allocate(data: Array, shape: IntArray): Iterable { + internal fun allocate(data: Array, shape: IntArray): Sequence { val bytes = data.asSequence().map { it.length }.max() ?: 0 val header = Header(order = null, type = 'S', bytes = bytes, shape = shape) - return Iterables.concat(listOf(header.allocate()), - StringArraySplitBuffer(data)) + return sequenceOf(header.allocate()) + StringArraySplitBuffer(data) } } @@ -342,10 +331,10 @@ class NpyArray( @Suppress("unchecked_cast") fun asStringArray() = data as Array - override fun toString() = MoreObjects.toStringHelper(this) - .add("data", Arrays.deepToString(arrayOf(data)) + override fun toString() = StringJoiner(", ", "NpyArray{", "}") + .add("data=" + Arrays.deepToString(arrayOf(data)) .removeSurrounding("[", "]")) - .add("shape", Arrays.toString(shape)) + .add("shape=" + Arrays.toString(shape)) .toString() } @@ -371,4 +360,4 @@ internal fun npyExample() { println(NpyFile.read(path)) // => NpyArray{data=[1, 2, 3, 4, 5, 6], shape=[2, 3]} -} +} \ No newline at end of file diff --git a/src/main/kotlin/org/jetbrains/bio/npy/Npz.kt b/src/main/kotlin/org/jetbrains/bio/npy/Npz.kt index 10a99b9..93f685a 100644 --- a/src/main/kotlin/org/jetbrains/bio/npy/Npz.kt +++ b/src/main/kotlin/org/jetbrains/bio/npy/Npz.kt @@ -1,6 +1,5 @@ package org.jetbrains.bio.npy -import com.google.common.base.MoreObjects import java.io.Closeable import java.nio.ByteBuffer import java.nio.ByteOrder @@ -147,7 +146,7 @@ object NpzFile { writeEntry(name) { NpyFile.allocate(data, shape) } } - private inline fun writeEntry(name: String, block: () -> Iterable) { + private inline fun writeEntry(name: String, block: () -> Sequence) { val chunks = block() val entry = ZipEntry(name + ".npy").apply { if (compressed) { @@ -192,10 +191,10 @@ object NpzFile { /** A stripped down NPY header for an array in NPZ. */ class NpzEntry(val name: String, val type: Class<*>, val shape: IntArray) { - override fun toString() = MoreObjects.toStringHelper(this) - .add("name", name) - .add("type", type) - .add("shape", Arrays.toString(shape)) + override fun toString() = StringJoiner(", ", "NpzEntry{", "}") + .add("name=" + name) + .add("type=" + type) + .add("shape=" + Arrays.toString(shape)) .toString() } diff --git a/src/main/kotlin/org/jetbrains/bio/npy/SplitBuffer.kt b/src/main/kotlin/org/jetbrains/bio/npy/SplitBuffer.kt index a3f2c89..175f117 100644 --- a/src/main/kotlin/org/jetbrains/bio/npy/SplitBuffer.kt +++ b/src/main/kotlin/org/jetbrains/bio/npy/SplitBuffer.kt @@ -1,7 +1,5 @@ package org.jetbrains.bio.npy -import com.google.common.collect.UnmodifiableIterator -import com.google.common.primitives.* import java.nio.ByteBuffer import java.nio.ByteOrder @@ -32,7 +30,7 @@ internal abstract class ArraySplitBuffer( /** Number of elements in the array. */ private val size: Int, /** Byte order for the produced buffers. */ - private val order: ByteOrder) : Iterable { + private val order: ByteOrder) : Sequence { abstract val bytes: Int /** @@ -42,7 +40,7 @@ internal abstract class ArraySplitBuffer( */ abstract fun ByteBuffer.fill(data: T, offset: Int, size: Int) - override fun iterator() = object : UnmodifiableIterator() { + override fun iterator() = object : Iterator { private var offset = 0 // into the [data]. private var step = DEFAULT_BUFFER_SIZE / bytes // Only allocated 'cache' if the [data] is bigger than [step]. @@ -84,7 +82,7 @@ internal class BooleanArraySplitBuffer(data: BooleanArray) : internal class ShortArraySplitBuffer(data: ShortArray, order: ByteOrder) : ArraySplitBuffer(data, data.size, order) { - override val bytes: Int get() = Shorts.BYTES + override val bytes: Int get() = java.lang.Short.BYTES override fun ByteBuffer.fill(data: ShortArray, offset: Int, size: Int) { asShortBuffer().put(data, offset, size) @@ -93,7 +91,7 @@ internal class ShortArraySplitBuffer(data: ShortArray, order: ByteOrder) : internal class IntArraySplitBuffer(data: IntArray, order: ByteOrder) : ArraySplitBuffer(data, data.size, order) { - override val bytes: Int get() = Ints.BYTES + override val bytes: Int get() = java.lang.Integer.BYTES override fun ByteBuffer.fill(data: IntArray, offset: Int, size: Int) { asIntBuffer().put(data, offset, size) @@ -102,7 +100,7 @@ internal class IntArraySplitBuffer(data: IntArray, order: ByteOrder) : internal class LongArraySplitBuffer(data: LongArray, order: ByteOrder) : ArraySplitBuffer(data, data.size, order) { - override val bytes: Int get() = Longs.BYTES + override val bytes: Int get() = java.lang.Long.BYTES override fun ByteBuffer.fill(data: LongArray, offset: Int, size: Int) { asLongBuffer().put(data, offset, size) @@ -111,7 +109,7 @@ internal class LongArraySplitBuffer(data: LongArray, order: ByteOrder) : internal class FloatArraySplitBuffer(data: FloatArray, order: ByteOrder) : ArraySplitBuffer(data, data.size, order) { - override val bytes: Int get() = Floats.BYTES + override val bytes: Int get() = java.lang.Float.BYTES override fun ByteBuffer.fill(data: FloatArray, offset: Int, size: Int) { asFloatBuffer().put(data, offset, size) @@ -120,7 +118,7 @@ internal class FloatArraySplitBuffer(data: FloatArray, order: ByteOrder) : internal class DoubleArraySplitBuffer(data: DoubleArray, order: ByteOrder) : ArraySplitBuffer(data, data.size, order) { - override val bytes: Int get() = Doubles.BYTES + override val bytes: Int get() = java.lang.Double.BYTES override fun ByteBuffer.fill(data: DoubleArray, offset: Int, size: Int) { asDoubleBuffer().put(data, offset, size) diff --git a/src/main/kotlin/org/jetbrains/bio/npy/Support.kt b/src/main/kotlin/org/jetbrains/bio/npy/Support.kt index 9ed9aa8..267e8c7 100644 --- a/src/main/kotlin/org/jetbrains/bio/npy/Support.kt +++ b/src/main/kotlin/org/jetbrains/bio/npy/Support.kt @@ -1,7 +1,5 @@ package org.jetbrains.bio.npy -import com.google.common.collect.Iterators -import com.google.common.collect.PeekingIterator import java.util.* /** A marker function for "impossible" `when` branches. */ @@ -92,7 +90,7 @@ private fun tokenize(s: String): PeekingIterator { best } - return Iterators.peekingIterator(tokens.iterator()) + return PeekingIterator(tokens.iterator()) } /** Consume a token and return it. */ @@ -109,4 +107,32 @@ private fun PeekingIterator.tryEat(expected: Token): SpannedToken? } else { null } +} + +// XXX doesn't support iterators which might yield null. +private class PeekingIterator(private val it: Iterator): Iterator { + private var hasPeeked = false + private var item: T? = null + + override fun hasNext() = hasPeeked || it.hasNext() + + override fun next(): T { + return if (hasPeeked) { + val result = item + hasPeeked = false + item = null + result!! + } else { + it.next() + } + } + + fun peek(): T { + if (!hasPeeked) { + item = it.next() + hasPeeked = true + } + + return item!! + } } \ No newline at end of file diff --git a/src/test/kotlin/org/jetbrains/bio/npy/NpyFileTest.kt b/src/test/kotlin/org/jetbrains/bio/npy/NpyFileTest.kt index 6ab9d28..b19924c 100644 --- a/src/test/kotlin/org/jetbrains/bio/npy/NpyFileTest.kt +++ b/src/test/kotlin/org/jetbrains/bio/npy/NpyFileTest.kt @@ -1,6 +1,5 @@ package org.jetbrains.bio.npy -import com.google.common.primitives.Shorts import org.junit.Assert.assertArrayEquals import org.junit.Test import org.junit.runner.RunWith @@ -100,7 +99,8 @@ class NpyFileHeaderTest { val header = NpyFile.Header(major = 1, minor = 0, type = 'i', bytes = 4, shape = intArrayOf(42)) - val headerSize = NpyFile.Header.MAGIC.size + 2 + Shorts.BYTES + header.meta.size + val headerSize = NpyFile.Header.MAGIC.size + 2 + + java.lang.Short.BYTES + header.meta.size assertTrue(headerSize % 16 == 0) }