Commit 12105f3b authored by HankG's avatar HankG

Merge branch '13-straight-port-revcomp' into 'develop'

Resolve "Straight port revcomp"

Closes #13

See merge request !8
parents fa11805d 3348e8fe
......@@ -3,7 +3,7 @@
http://benchmarksgame.alioth.debian.org/
Based on Java regex-dna program (#5) contributed by Michael Stover
Contributed by Hank Grabowski
*/
......@@ -31,8 +31,8 @@ object regexredux {
val r = BufferedReader(InputStreamReader(System.`in`,
"US-ASCII"))
val sb = StringBuffer()
var line: String = ""
while ({line = r.readLine(); line}() != null) {
var line: String? = null
while ({line = r?.readLine()?:null; line}() != null) {
sb.append(line)
sb.append("\n")
}
......
/*
* The Computer Language Benchmarks Game
* http://benchmarksgame.alioth.debian.org/
* Based on Java version (8) by Han Kai
*
* Contributed by Hank Grabowski
*/
import java.io.*
import java.util.ArrayList
import java.util.concurrent.Executors
import java.io.IOException
import java.util.concurrent.Callable
object revcomp {
@Throws(Exception::class)
@JvmStatic
fun main(args: Array<String>) {
Strand().use({ strand ->
FileInputStream(FileDescriptor.`in`).use { standIn ->
FileOutputStream(FileDescriptor.out).use { standOut ->
while (strand.readOneStrand(standIn) >= 0) {
strand.reverse()
strand.write(standOut)
strand.reset()
}
}
}
})
}
}
internal class Chunk {
var capacity = 0
var length = 0
val bytes = ByteArray(CHUNK_SIZE)
fun clear() {
capacity = 0
length = 0
}
companion object {
val CHUNK_SIZE = 64 * 1024
}
}
internal class Strand : Closeable {
companion object {
private val NEW_LINE: Byte = '\n'.toByte()
private val ANGLE: Byte = '>'.toByte()
private val LINE_LENGTH = 61
private val map = ByteArray(128)
private val NCPU = Runtime.getRuntime().availableProcessors()
private val executor = Executors.newFixedThreadPool(NCPU)
private var chunkCount = 0
private val chunks = ArrayList<Chunk>()
init {
for (i in map.indices) {
map[i] = i.toByte()
}
map['T'.toInt()] = 'A'.toByte()
map['t'.toInt()] = map['T'.toInt()]
map['A'.toInt()] = 'T'.toByte()
map['a'.toInt()] = map['A'.toInt()]
map['G'.toInt()] = 'C'.toByte()
map['g'.toInt()] = map['G'.toInt()]
map['C'.toInt()] = 'G'.toByte()
map['c'.toInt()] = map['C'.toInt()]
map['V'.toInt()] = 'B'.toByte()
map['v'.toInt()] = map['V'.toInt()]
map['H'.toInt()] = 'D'.toByte()
map['h'.toInt()] = map['H'.toInt()]
map['R'.toInt()] = 'Y'.toByte()
map['r'.toInt()] = map['R'.toInt()]
map['M'.toInt()] = 'K'.toByte()
map['m'.toInt()] = map['M'.toInt()]
map['Y'.toInt()] = 'R'.toByte()
map['y'.toInt()] = map['Y'.toInt()]
map['K'.toInt()] = 'M'.toByte()
map['k'.toInt()] = map['K'.toInt()]
map['B'.toInt()] = 'V'.toByte()
map['b'.toInt()] = map['B'.toInt()]
map['D'.toInt()] = 'H'.toByte()
map['d'.toInt()] = map['D'.toInt()]
map['U'.toInt()] = 'A'.toByte()
map['u'.toInt()] = map['U'.toInt()]
}
}
private fun ensureSize() {
if (chunkCount === chunks.size) {
chunks.add(Chunk())
}
}
private fun isLastChunk(chunk: Chunk): Boolean {
return chunk.length != chunk.capacity
}
private fun correctLentgh(chunk: Chunk, skipFirst: Boolean) {
val bytes = chunk.bytes
val start = if (skipFirst) 1 else 0
val end = chunk.capacity
for (i in start..end - 1) {
if (ANGLE === bytes[i]) {
chunk.length = i
return
}
}
chunk.length = chunk.capacity
}
private fun prepareNextStrand() {
if (chunkCount === 0) {
return
}
val first = chunks[0]
val last = chunks[chunkCount - 1]
if (last.capacity == last.length) {
for (i in 0..chunkCount - 1) {
chunks[i].clear()
}
return
}
System.arraycopy(last.bytes, last.length, first.bytes, 0, last.capacity - last.length)
first.capacity = last.capacity - last.length
correctLentgh(first, true)
for (i in 1..chunkCount - 1) {
chunks[i].clear()
}
}
@Throws(IOException::class)
fun readOneStrand(`is`: InputStream): Int {
while (true) {
ensureSize()
val chunk = chunks[chunkCount]
chunkCount++
if (isLastChunk(chunk)) {
return chunkCount
}
val bytes = chunk.bytes
val readLength = `is`.read(bytes, chunk.length, Chunk.CHUNK_SIZE - chunk.length)
if (chunkCount === 1 && readLength < 0 && chunk.length == 0) {
return -1
}
if (readLength > 0) {
chunk.capacity += readLength
correctLentgh(chunk, chunkCount === 1)
}
if (readLength < 0 || isLastChunk(chunk)) {
return chunkCount
}
}
}
fun reset() {
prepareNextStrand()
chunkCount = 0
}
@Throws(IOException::class)
fun write(out: OutputStream) {
for (i in 0..chunkCount - 1) {
val chunk = chunks[i]
out.write(chunk.bytes, 0, chunk.length)
}
}
@Throws(InterruptedException::class)
fun reverse() {
val sumLength = getSumLength()
val titleLength = getTitleLength()
val dataLength = sumLength - titleLength
val realDataLength = dataLength - ceilDiv(dataLength, LINE_LENGTH)
val leftEndIndex = realDataLength / 2
val rawLeftEndIndex = leftEndIndex + leftEndIndex / (LINE_LENGTH - 1)
val leftEndChunkIndex = ceilDiv(rawLeftEndIndex + titleLength, Chunk.CHUNK_SIZE) - 1
val realLeftEndIndex = (rawLeftEndIndex + titleLength) % Chunk.CHUNK_SIZE - 1
val tasks = ArrayList<Callable<Int>>(NCPU)
val itemCount = ceilDiv(leftEndChunkIndex + 1, NCPU)
for (t in 0..NCPU - 1) {
val start = itemCount * t
val end = Math.min(start + itemCount, leftEndChunkIndex + 1)
tasks.add(Callable<Int>{
for (i in start..end - 1) {
val rawLeftIndex = if (i == 0) 0 else i * Chunk.CHUNK_SIZE - titleLength
val leftIndex = rawLeftIndex - rawLeftIndex / LINE_LENGTH
val rightIndex = realDataLength - leftIndex - 1
val rawRightIndex = rightIndex + rightIndex / (LINE_LENGTH - 1)
val rightChunkIndex = ceilDiv(rawRightIndex + titleLength, Chunk.CHUNK_SIZE) - 1
val realLeftIndex = (rawLeftIndex + titleLength) % Chunk.CHUNK_SIZE
val realRightIndex = (rawRightIndex + titleLength) % Chunk.CHUNK_SIZE
val endIndex = if (leftEndChunkIndex === i)
realLeftEndIndex
else
chunks[i].length - 1
reverse(i, rightChunkIndex, realLeftIndex, realRightIndex, endIndex)
}
0
})
}
executor.invokeAll(tasks)
}
private fun reverse(leftChunkIndex: Int, rightChunkIndex: Int, leftIndex: Int, rightIndex: Int, leftEndIndex: Int) {
var rightChunkIndex = rightChunkIndex
var leftIndex = leftIndex
var rightIndex = rightIndex
val map = Strand.map
val leftChunk = chunks[leftChunkIndex]
var rightChunk = chunks[rightChunkIndex]
val leftBytes = leftChunk.bytes
var rightBytes = rightChunk.bytes
while (leftIndex <= leftEndIndex) {
if (rightIndex < 0) {
rightChunk = chunks[--rightChunkIndex]
rightBytes = rightChunk.bytes
rightIndex = rightChunk.length - 1
}
if (leftBytes[leftIndex] == NEW_LINE) {
leftIndex++
}
if (rightBytes[rightIndex] == NEW_LINE) {
rightIndex--
if (rightIndex < 0) {
rightChunk = chunks[--rightChunkIndex]
rightBytes = rightChunk.bytes
rightIndex = rightChunk.length - 1
}
}
if (leftIndex <= leftEndIndex) {
val lByte = leftBytes[leftIndex]
val rByte = rightBytes[rightIndex]
leftBytes[leftIndex++] = map[rByte.toInt()]
rightBytes[rightIndex--] = map[lByte.toInt()]
}
}
}
private fun ceilDiv(a: Int, b: Int): Int {
return (a + b - 1) / b
}
private fun getSumLength(): Int {
var sumLength = 0
for (i in 0..chunkCount - 1) {
sumLength += chunks[i].length
}
return sumLength
}
private fun getTitleLength(): Int {
val first = chunks[0]
val bytes = first.bytes
for (i in 0..first.length - 1) {
if (bytes[i] == NEW_LINE) {
return i + 1
}
}
return -1
}
@Throws(IOException::class)
override fun close() {
executor.shutdown()
}
}
......@@ -74,6 +74,7 @@ onlydirs =
mandelbrot
pidigits
regexredux
revcomp
......@@ -138,10 +139,10 @@ pidigits = 1 100 1000 2500 5000 10000
[testdata]
knucleotide = ../knucleotide-input.txt
knucleotide = ../knucleotide-input10000.txt
regexdna = ../regexdna-input.txt
regexredux = ../regexdna-input.txt
revcomp = ../revcomp-input.txt
regexredux = ../regexdna-input10000.txt
revcomp = ../revcomp-input1000.txt
......
......@@ -3,7 +3,7 @@
http://benchmarksgame.alioth.debian.org/
Based on Java regex-dna program (#5) contributed by Michael Stover
Contributed by Hank Grabowski
*/
......@@ -31,8 +31,8 @@ object regexredux {
val r = BufferedReader(InputStreamReader(System.`in`,
"US-ASCII"))
val sb = StringBuffer()
var line: String = ""
while ({line = r.readLine(); line}() != null) {
var line: String? = null
while ({line = r?.readLine()?:null; line}() != null) {
sb.append(line)
sb.append("\n")
}
......
Revised BSD license
This is a specific instance of the Open Source Initiative (OSI) BSD license template
http://www.opensource.org/licenses/bsd-license.php
Copyright © 2004-2008 Brent Fulgham, 2005-2017 Isaac Gouy
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of "The Computer Language Benchmarks Game" nor the name of "The Computer Language Shootout Benchmarks" nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/*
* The Computer Language Benchmarks Game
* http://benchmarksgame.alioth.debian.org/
* contributed by Han Kai
*/
import java.io.Closeable;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class revcomp {
public static void main(String[] args) throws Exception {
try (Strand strand = new Strand();
FileInputStream standIn = new FileInputStream(FileDescriptor.in);
FileOutputStream standOut = new FileOutputStream(FileDescriptor.out);) {
while (strand.readOneStrand(standIn) >= 0) {
strand.reverse();
strand.write(standOut);
strand.reset();
}
}
}
}
class Chunk {
public static final int CHUNK_SIZE = 64 * 1024;
public int capacity = 0;
public int length = 0;
public final byte[] bytes = new byte[CHUNK_SIZE];
public void clear() {
capacity = 0;
length = 0;
}
}
class Strand implements Closeable {
private static final byte NEW_LINE = '\n';
private static final byte ANGLE = '>';
private static final int LINE_LENGTH = 61;
private static final byte[] map = new byte[128];
static {
for (int i = 0; i < map.length; i++) {
map[i] = (byte) i;
}
map['t'] = map['T'] = 'A';
map['a'] = map['A'] = 'T';
map['g'] = map['G'] = 'C';
map['c'] = map['C'] = 'G';
map['v'] = map['V'] = 'B';
map['h'] = map['H'] = 'D';
map['r'] = map['R'] = 'Y';
map['m'] = map['M'] = 'K';
map['y'] = map['Y'] = 'R';
map['k'] = map['K'] = 'M';
map['b'] = map['B'] = 'V';
map['d'] = map['D'] = 'H';
map['u'] = map['U'] = 'A';
}
private static int NCPU = Runtime.getRuntime().availableProcessors();
private ExecutorService executor = Executors.newFixedThreadPool(NCPU);
private int chunkCount = 0;
private final ArrayList<Chunk> chunks = new ArrayList<Chunk>();
private void ensureSize() {
if (chunkCount == chunks.size()) {
chunks.add(new Chunk());
}
}
private boolean isLastChunk(Chunk chunk) {
return chunk.length != chunk.capacity;
}
private void correctLentgh(Chunk chunk, boolean skipFirst) {
final byte[] bytes = chunk.bytes;
final int start = skipFirst ? 1 : 0;
final int end = chunk.capacity;
for (int i = start; i < end; i++) {
if (ANGLE == bytes[i]) {
chunk.length = i;
return;
}
}
chunk.length = chunk.capacity;
}
private void prepareNextStrand() {
if (chunkCount == 0) {
return;
}
Chunk first = chunks.get(0);
Chunk last = chunks.get(chunkCount - 1);
if (last.capacity == last.length) {
for (int i = 0; i < chunkCount; i++) {
chunks.get(i).clear();
}
return;
}
System.arraycopy(last.bytes, last.length, first.bytes, 0, last.capacity - last.length);
first.capacity = last.capacity - last.length;
correctLentgh(first, true);
for (int i = 1; i < chunkCount; i++) {
chunks.get(i).clear();
}
}
public int readOneStrand(InputStream is) throws IOException {
while (true) {
ensureSize();
Chunk chunk = chunks.get(chunkCount);
chunkCount++;
if (isLastChunk(chunk)) {
return chunkCount;
}
byte[] bytes = chunk.bytes;
int readLength = is.read(bytes, chunk.length, Chunk.CHUNK_SIZE - chunk.length);
if (chunkCount == 1 && readLength < 0 && chunk.length == 0) {
return -1;
}
if (readLength > 0) {
chunk.capacity += readLength;
correctLentgh(chunk, chunkCount == 1);
}
if (readLength < 0 || isLastChunk(chunk)) {
return chunkCount;
}
}
}
public void reset() {
prepareNextStrand();
chunkCount = 0;
}
public void write(OutputStream out) throws IOException {
for (int i = 0; i < chunkCount; i++) {
Chunk chunk = chunks.get(i);
out.write(chunk.bytes, 0, chunk.length);
}
}
public void reverse() throws InterruptedException {
final int sumLength = getSumLength();
final int titleLength = getTitleLength();
final int dataLength = sumLength - titleLength;
final int realDataLength = dataLength - ceilDiv(dataLength, LINE_LENGTH);
final int leftEndIndex = realDataLength / 2;
final int rawLeftEndIndex = leftEndIndex + leftEndIndex / (LINE_LENGTH - 1);
final int leftEndChunkIndex = ceilDiv(rawLeftEndIndex + titleLength, Chunk.CHUNK_SIZE) - 1;
final int realLeftEndIndex = (rawLeftEndIndex + titleLength) % Chunk.CHUNK_SIZE - 1;
final List<Callable<Void>> tasks = new ArrayList<Callable<Void>>(NCPU);
final int itemCount = ceilDiv(leftEndChunkIndex + 1, NCPU);
for (int t = 0; t < NCPU; t++) {
final int start = itemCount * t;
final int end = Math.min(start + itemCount, leftEndChunkIndex + 1);
Callable<Void> task = () -> {
for (int i = start; i < end; i++) {
int rawLeftIndex = i == 0 ? 0 : i * Chunk.CHUNK_SIZE - titleLength;
int leftIndex = rawLeftIndex - rawLeftIndex / LINE_LENGTH;
int rightIndex = realDataLength - leftIndex - 1;
int rawRightIndex = rightIndex + rightIndex / (LINE_LENGTH - 1);
int leftChunkIndex = i;
int rightChunkIndex = ceilDiv(rawRightIndex + titleLength, Chunk.CHUNK_SIZE) - 1;
int realLeftIndex = (rawLeftIndex + titleLength) % Chunk.CHUNK_SIZE;
int realRightIndex = (rawRightIndex + titleLength) % Chunk.CHUNK_SIZE;
int endIndex = leftEndChunkIndex == leftChunkIndex ? realLeftEndIndex
: chunks.get(leftChunkIndex).length - 1;
reverse(leftChunkIndex, rightChunkIndex, realLeftIndex, realRightIndex, endIndex);
}
return null;
};
tasks.add(task);
}
executor.invokeAll(tasks);
}
private void reverse(int leftChunkIndex, int rightChunkIndex, int leftIndex, int rightIndex, int leftEndIndex) {
final byte[] map = Strand.map;
Chunk leftChunk = chunks.get(leftChunkIndex);
Chunk rightChunk = chunks.get(rightChunkIndex);
byte[] leftBytes = leftChunk.bytes;
byte[] rightBytes = rightChunk.bytes;
while (leftIndex <= leftEndIndex) {
if (rightIndex < 0) {
rightChunk = chunks.get(--rightChunkIndex);
rightBytes = rightChunk.bytes;
rightIndex = rightChunk.length - 1;
}
if (leftBytes[leftIndex] == NEW_LINE) {
leftIndex++;
}
if (rightBytes[rightIndex] == NEW_LINE) {
rightIndex--;
if (rightIndex < 0) {
rightChunk = chunks.get(--rightChunkIndex);
rightBytes = rightChunk.bytes;
rightIndex = rightChunk.length - 1;
}
}
if (leftIndex <= leftEndIndex) {
byte lByte = leftBytes[leftIndex];
byte rByte = rightBytes[rightIndex];
leftBytes[leftIndex++] = map[rByte];
rightBytes[rightIndex--] = map[lByte];
}
}
}
private int ceilDiv(int a, int b) {
return (a + b - 1) / b;
}
private int getSumLength() {
int sumLength = 0;
for (int i = 0; i < chunkCount; i++) {
sumLength += chunks.get(i).length;
}
return sumLength;
}
private int getTitleLength() {
Chunk first = chunks.get(0);
byte[] bytes = first.bytes;
for (int i = 0; i < first.length; i++) {
if (bytes[i] == NEW_LINE) {
return (i + 1);
}
}
return -1;
}
@Override
public void close() throws IOException {
executor.shutdown();
}
}
/*
* The Computer Language Benchmarks Game
* http://benchmarksgame.alioth.debian.org/
* Based on Java version (8) by Han Kai
*
* Contributed by Hank Grabowski
*/
import java.io.*
import java.util.ArrayList
import java.util.concurrent.Executors
import java.io.IOException
import java.util.concurrent.Callable
object revcomp {
@Throws(Exception::class)
@JvmStatic
fun main(args: Array<String>) {
Strand().use({ strand ->
FileInputStream(FileDescriptor.`in`).use { standIn ->
FileOutputStream(FileDescriptor.out).use { standOut ->
while (strand.readOneStrand(standIn) >= 0) {
strand.reverse()
strand.write(standOut)
strand.reset()
}