Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.RandomAccessFile;
- import java.nio.ByteBuffer;
- import java.nio.channels.FileChannel;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class test {
- private static int[] a;
- static private int readInt(ByteBuffer buffer) throws IOException {
- int res = 0;
- boolean started = false;
- for (int pos = buffer.position(); pos < buffer.limit(); pos++) {
- int v = buffer.get() - '0';
- if (v >= 0 && v < 10) {
- if (!started) started = true;
- res = res * 10 + v;
- } else if (started)
- break;
- }
- return res;
- }
- public void process(String filename) throws IOException{
- long time = -System.nanoTime();
- int n = 5105042;
- a = new int[n];
- RandomAccessFile ra = new RandomAccessFile(filename, "r");
- long size = ra.length();
- FileChannel channel = ra.getChannel();
- ByteBuffer buffer = ByteBuffer.allocate((int) size);
- channel.read(buffer);
- buffer.position(0);
- Map<Integer,List<Integer>> g = new HashMap<Integer, List<Integer>>();
- for(int i = 0; i < n; i++) a[i] = readInt(buffer);
- for(int i = 0; i < n/2; i++){
- int src = a[i/2];
- int dst = a[i/2+1];
- List<Integer> edges = g.get(src);
- if (edges == null){
- edges = new ArrayList<Integer>();
- g.put(src, edges);
- }
- edges.add(dst);
- }
- long rtime = System.nanoTime()+time;
- time += System.nanoTime();
- System.out.println("Reading time (ms) : " + rtime/1E6);
- System.out.println("Total time (ms) : " + time/1E6);
- }
- static public void main(String[] args) throws IOException {
- new test().process(args[0]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement