InputReader

class InputReader @JvmOverloads constructor(stream: InputStream = DEFAULT_STREAM, bufferSize: Int = DEFAULT_BUFFER_SIZE)

A very fast input reader to read data from any InputStream (System.in in particular). I primarily use this input reader for competitive programming to get an edge on the time required to read input. That being said, this input reader assumes you know what data types you are reading in and will not do any validation of input whatsoever!

Author

William Fiset, william.alexandre.fiset@gmail.com

Constructors

Link copied to clipboard
constructor(stream: InputStream = DEFAULT_STREAM, bufferSize: Int = DEFAULT_BUFFER_SIZE)
constructor(bufferSize: Int)

Create an InputReader that reads from standard input.

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
fun close()

Closes the input stream

Link copied to clipboard
fun nextByte(): Byte

Reads a signed 8 bit integer written as text (e.g. "-12") from the input stream. Values outside the byte range overflow silently.

Link copied to clipboard

Read an array of n byte values

Link copied to clipboard

Read a 1-based byte array of size n+1

Link copied to clipboard
fun nextByteMatrix(rows: Int, cols: Int): Array<ByteArray?>

Read a two dimensional matrix of bytes of size rows x cols

Link copied to clipboard
fun nextByteMatrix1(rows: Int, cols: Int): Array<ByteArray?>

Read a 1-based two dimensional matrix of bytes of size rows x cols

Link copied to clipboard

Reads a double from the input stream. The result is always identical to Double.parseDouble.

Link copied to clipboard

Read a double array of size n

Link copied to clipboard

Read a 1-based double array of size n+1

Link copied to clipboard

Quickly read an array of doubles

Link copied to clipboard

Quickly read a 1-based double array of size n+1

Link copied to clipboard

Reads a double value from the input stream, trading up to 1 ulp of accuracy for speed on high precision input. For values with at most ~15 significant digits (most input) this is identical to nextDouble(): same exact result, same speed. For 16+ significant digits, such as the full precision output of Double.toString, nextDouble() falls back to Double.parseDouble to stay exact, while this method keeps the fast path and may be off by 1 ulp (~2x faster).

Link copied to clipboard

Read a two dimensional matrix of doubles of size rows x cols

Link copied to clipboard

Read a 1-based two dimensional matrix of doubles of size rows x cols

Link copied to clipboard

Quickly read a two dimensional matrix of doubles of size rows x cols

Link copied to clipboard

Quickly read a 1-based two dimensional matrix of doubles of size rows x cols

Link copied to clipboard
fun nextInt(): Int

Reads a 32 bit signed integer from input stream.

Link copied to clipboard

Read an integer array of size n

Link copied to clipboard

Read a 1-based integer array of size n+1

Link copied to clipboard
fun nextIntMatrix(rows: Int, cols: Int): Array<IntArray?>

Read a two dimensional matrix of ints of size rows x cols

Link copied to clipboard
fun nextIntMatrix1(rows: Int, cols: Int): Array<IntArray?>

Read a 1-based two dimensional matrix of ints of size rows x cols

Link copied to clipboard

Reads a line from the input stream.

Link copied to clipboard
fun nextLong(): Long

Reads a 64 bit signed long from input stream.

Link copied to clipboard

Read a long array of size n

Link copied to clipboard

Read a 1-based long array of size n+1

Link copied to clipboard
fun nextLongMatrix(rows: Int, cols: Int): Array<LongArray?>

Read a two dimensional matrix of longs of size rows x cols

Link copied to clipboard
fun nextLongMatrix1(rows: Int, cols: Int): Array<LongArray?>

Read a 1-based two dimensional matrix of longs of size rows x cols

Link copied to clipboard

Reads a string of characters from the input stream. The delimiter separating a string of characters is set to be: any ASCII value <= 32 meaning any spaces, new lines, tabs... or any non-ASCII byte (bytes are compared as signed values). Returns null at the end of the stream.

Link copied to clipboard

Read a string array of size n

Link copied to clipboard

Read a 1-based string array of size n+1

Link copied to clipboard
fun nextStringMatrix(rows: Int, cols: Int): Array<Array<String?>?>

Read a two dimensional matrix of Strings of size rows x cols

Link copied to clipboard
fun nextStringMatrix1(rows: Int, cols: Int): Array<Array<String?>?>

Read a 1-based two dimensional matrix of Strings of size rows x cols