Advertisement
Guest User

FastInputReader

a guest
Aug 1st, 2016
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.88 KB | None | 0 0
  1. class FastInputReader {
  2.     private final InputStream in;
  3.     private final String delim;
  4.     private final byte[] buffer = new byte[4096];
  5.     private int bufferSize = 0;
  6.     private int bufferPointer = 0;
  7.     private byte[] input = new byte[32];
  8.     private int inputPointer = 0;
  9.     private int inputLength = 0;
  10.     private byte lastDelim = (byte)'\n';
  11.    
  12.     public FastInputReader() {
  13.         in = new FileInputStream(FileDescriptor.in);
  14.         delim=" \t\n";
  15.     }
  16.    
  17.     public FastInputReader(String delim) {
  18.         in = new FileInputStream(FileDescriptor.in);
  19.         this.delim=delim;
  20.     }
  21.    
  22.     public FastInputReader(InputStream is) {
  23.         in = is;
  24.         delim=" \t\n";
  25.     }
  26.    
  27.     public FastInputReader(InputStream is, String delim) {
  28.         in = is;
  29.         this.delim=delim;
  30.     }
  31.    
  32.     public FastInputReader(File F) throws FileNotFoundException {
  33.         in = new FileInputStream(F);
  34.         delim=" \t\n";
  35.     }
  36.    
  37.     public FastInputReader(File F, String delim) throws FileNotFoundException {
  38.         in = new FileInputStream(F);
  39.         this.delim=delim;
  40.     }
  41.  
  42.     private void nextBytesInternal() throws IOException {
  43.         while(true){
  44.             byte c = read();
  45.             for(int i=0;i<delim.length();i++) {
  46.                 if(c == (byte)delim.charAt(i)) {
  47.                     inputLength = inputPointer;
  48.                     inputPointer = 0;
  49.                     lastDelim = c;
  50.                     return;
  51.                 }
  52.             }
  53.             input[inputPointer++] = c;
  54.         }
  55.     }
  56.  
  57.     public String next() throws IOException {
  58.         nextBytesInternal();
  59.         return new String(input, 0, inputLength, StandardCharsets.US_ASCII);
  60.     }
  61.  
  62.     public byte nextByte() throws IOException {
  63.         nextBytesInternal();
  64.         byte val = 0;
  65.         byte mul = 1;
  66.         for(int i = 0; i < inputLength; ++i) {
  67.             int c = input[i] & 0xFF;
  68.             if(c == '-') {
  69.                 mul = -1;
  70.             }
  71.             if('0' <= c && c <= '9') {
  72.                 val = (byte) (val * 10 + c - '0');
  73.             }
  74.         }
  75.         return (byte) (val * mul);
  76.     }
  77.    
  78.     public byte[] nextBytes() throws IOException {
  79.         nextBytesInternal();
  80.         byte[] newArray = new byte[inputLength];
  81.         if(inputLength < 24) {
  82.             for(int i = 0; i < inputLength; ++i) {
  83.                 newArray[i] = input[i];
  84.             }
  85.         }
  86.         else {
  87.             System.arraycopy(input, 0, newArray, 0, inputLength);
  88.         }
  89.         return newArray;
  90.     }
  91.  
  92.     public char nextChar() throws IOException {
  93.         nextBytesInternal();
  94.         return (char)input[0];
  95.     }
  96.    
  97.     public double nextDouble() throws IOException {
  98.         nextBytesInternal();
  99.         double val=0;
  100.         byte mul = 1;
  101.         int i=0;
  102.         for(; i < inputLength; ++i) {
  103.             int c = input[i] & 0xFF;
  104.             if(c == '-') {
  105.                 mul = -1;
  106.             }
  107.             if(c == '.') {
  108.                 break;
  109.             }
  110.             if('0' <= c && c <= '9') {
  111.                 val = val * 10 + c - '0';
  112.             }
  113.         }
  114.         long base = 1;
  115.         for(; i < inputLength; ++i) {
  116.             int c = input[i] & 0xFF;
  117.             if('0' <= c && c <= '9') {
  118.                 base *= 10 ;
  119.                 val = val + (double)(c - '0') / base;
  120.             }
  121.         }
  122.         return val * mul;
  123.     }
  124.    
  125.     public float nextFloat() throws IOException {
  126.         nextBytesInternal();
  127.         float val=0;
  128.         byte mul = 1;
  129.         int i=0;
  130.         for(; i < inputLength; ++i) {
  131.             int c = input[i] & 0xFF;
  132.             if(c == '-') {
  133.                 mul = -1;
  134.             }
  135.             if(c == '.') {
  136.                 break;
  137.             }
  138.             if('0' <= c && c <= '9') {
  139.                 val = val * 10 + c - '0';
  140.             }
  141.         }
  142.         long base = 1;
  143.         for(; i < inputLength; ++i) {
  144.             int c = input[i] & 0xFF;
  145.             if('0' <= c && c <= '9') {
  146.                 base *= 10 ;
  147.                 val = val + (float)(c - '0') / base;
  148.             }
  149.         }
  150.         return val * mul;
  151.     }
  152.    
  153.     public int nextInt() throws IOException {
  154.         nextBytesInternal();
  155.         int val = 0;
  156.         byte mul = 1;
  157.         for(int i = 0; i < inputLength; ++i) {
  158.             int c = input[i] & 0xFF;
  159.             if(c == '-') {
  160.                 mul = -1;
  161.             }
  162.             if('0' <= c && c <= '9') {
  163.                 val = val * 10 + c - '0';
  164.             }
  165.         }
  166.         return val * mul;
  167.     }
  168.  
  169.     public String nextLine() throws IOException {
  170.         while(lastDelim!='\n') {
  171.             lastDelim = read();
  172.         }
  173.         while(true){
  174.             byte c = read();
  175.             if(c == (byte)'\n') {
  176.                 inputLength = inputPointer;
  177.                 inputPointer = 0;
  178.                 lastDelim = '\n';
  179.                 return new String(input, 0, inputLength, StandardCharsets.US_ASCII);
  180.             }
  181.             input[inputPointer++] = c;
  182.         }
  183.     }
  184.    
  185.     public long nextLong() throws IOException {
  186.         nextBytesInternal();
  187.         long val = 0;
  188.         byte mul = 1;
  189.         for(int i = 0; i < inputLength; ++i) {
  190.             int c = input[i] & 0xFF;
  191.             if(c == '-') {
  192.                 mul = -1;
  193.             }
  194.             if('0' <= c && c <= '9') {
  195.                 val = val * 10 + c - '0';
  196.             }
  197.         }
  198.         return val * mul;
  199.     }
  200.    
  201.     public short nextShort() throws IOException {
  202.         nextBytesInternal();
  203.         short val = 0;
  204.         byte mul = 1;
  205.         for(int i = 0; i < inputLength; ++i) {
  206.             int c = input[i] & 0xFF;
  207.             if(c == '-') {
  208.                 mul = -1;
  209.             }
  210.             if('0' <= c && c <= '9') {
  211.                 val = (short) (val * 10 + c - '0');
  212.             }
  213.         }
  214.         return (short) (val * mul);
  215.     }
  216.    
  217.     private byte read() throws IOException {
  218.         if(bufferPointer >= bufferSize) {
  219.             bufferSize = in.read(buffer, 0, 4096);
  220.             bufferPointer = 0;
  221.         }
  222.         if(inputPointer >= input.length) {
  223.             byte[] newInput = new byte[inputPointer * 2 + 1];
  224.             if(input.length < 24) {
  225.                 for(int i = 0; i < input.length; ++i) {
  226.                     newInput[i] = input[i];
  227.                 }
  228.             }
  229.             else {
  230.                 System.arraycopy(input, 0, newInput, 0, input.length);
  231.             }
  232.             input = newInput;
  233.         }
  234.         return buffer[bufferPointer++];
  235.     }
  236. }
  237. // </editor-fold>
  238.  
  239. // <editor-fold defaultstate="collapsed" desc=" Pair ">
  240. class Pair<T1, T2> {
  241.     public T1 first;
  242.     public T2 second;
  243.    
  244.     public Pair() {
  245.         first=null;
  246.         second=null;
  247.     }
  248.    
  249.     public Pair(T1 f, T2 s) {
  250.         first=f;
  251.         second=s;
  252.     }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement