Created
March 28, 2017 15:52
-
-
Save eldargab/92c9f53927f89d81bcd8a15f163030ee to your computer and use it in GitHub Desktop.
Scanner for programming contests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FastScanner implements Iterator<String>, Iterable<String>, Closeable { | |
private Reader reader; | |
private StringBuilder sb = new StringBuilder(); | |
private char[] buf = new char[4096]; | |
private char current; | |
private int beg = 0; | |
private int end = 0; | |
public FastScanner(Reader reader) { | |
this.reader = reader; | |
} | |
public FastScanner(File f) { | |
try { | |
this.reader = new FileReader(f); | |
} catch (FileNotFoundException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public FastScanner() { | |
this.reader = new InputStreamReader(System.in); | |
} | |
public FastScanner(String s) { | |
this.reader = new StringReader(s); | |
} | |
@Override | |
public Iterator<String> iterator() { | |
return this; | |
} | |
@Override | |
public boolean hasNext() { | |
if (sb.length() > 0) return true; | |
while (read()) { | |
if (Character.isWhitespace(current)) continue; | |
sb.append(current); | |
return true; | |
} | |
return false; | |
} | |
@Override | |
public String next() { | |
if (!hasNext()) return null; | |
while (read()) { | |
if (Character.isWhitespace(current)) break; | |
sb.append(current); | |
} | |
String s = sb.toString(); | |
sb.setLength(0); | |
return s; | |
} | |
private boolean read() { | |
while (beg >= end) { | |
try { | |
int len = reader.read(buf); | |
if (len < 0) return false; | |
beg = 0; | |
end = beg + len; | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
current = buf[beg]; | |
beg += 1; | |
return true; | |
} | |
public int nextInt() { | |
return Integer.parseInt(next()); | |
} | |
public long nextLong() { | |
return Long.parseLong(next()); | |
} | |
public double nextDouble() { | |
return Double.parseDouble(next()); | |
} | |
public Stream<String> stream() { | |
return StreamSupport.stream(this.spliterator(), false); | |
} | |
public Stream<String> stream(int size) { | |
return StreamSupport.stream(Spliterators.spliterator(this.iterator(), size, 0), false).limit(size); | |
} | |
public IntStream intStream() { | |
return stream().mapToInt(Integer::parseInt); | |
} | |
public IntStream intStream(int size) { | |
return stream(size).mapToInt(Integer::parseInt); | |
} | |
public LongStream longStream() { | |
return stream().mapToLong(Long::parseLong); | |
} | |
public LongStream longStream(int size) { | |
return stream(size).mapToLong(Long::parseLong); | |
} | |
public DoubleStream doubleStream() { | |
return stream().mapToDouble(Double::parseDouble); | |
} | |
public DoubleStream doubleStream(int size) { | |
return stream(size).mapToDouble(Double::parseDouble); | |
} | |
public int[] nextIntArray(int size) { | |
int[] a = new int[size]; | |
for (int i = 0; i < size; i++) { | |
a[i] = nextInt(); | |
} | |
return a; | |
} | |
public long[] nextLongArray(int size) { | |
long[] a = new long[size]; | |
for (int i = 0; i < size; i++) { | |
a[i] = nextLong(); | |
} | |
return a; | |
} | |
public double[] nextDoubleArray(int size) { | |
double[] a = new double[size]; | |
for (int i = 0; i < size; i++) { | |
a[i] = nextDouble(); | |
} | |
return a; | |
} | |
@Override | |
public void close() throws IOException { | |
reader.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment