Advertisement
super_tycoon

lpath unsafe java

Dec 20th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. import sun.misc.Unsafe;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.lang.reflect.Field;
  6. import java.util.Arrays;
  7.  
  8. /**
  9.  * Uses unsafe and compacts the node data into a dense representation
  10.  *
  11.  * Nodes representation:
  12.  *
  13.  * int rowlength
  14.  *
  15.  * dest | cost | 0 0 0 0 0 ... 0 0
  16.  * dest | cost | x x x x x ... 0 0
  17.  *
  18.  * Testing for zero is marginally faster, exploit this instead of checking against array position against rowlength
  19.  *
  20.  *
  21.  */
  22. public class jv{
  23.  
  24.     final static byte ZERO = (byte) 0;
  25.     final static byte ONE = (byte) 1;
  26.  
  27.     static final Unsafe UNSAFE = getUnsafe();
  28.  
  29.     static int numNodes;
  30.     static int rowlength;
  31.     static final long ADR_nodes;
  32.     static final long ADR_visited;
  33.  
  34.     static {
  35.         ADR_nodes = readPlaces();
  36.  
  37.         ADR_visited = UNSAFE.allocateMemory(numNodes);
  38.  
  39.         for (int i = 0; i < numNodes; i++) {
  40.             UNSAFE.putByte(ADR_visited + i, ZERO);
  41.         }
  42.     }
  43.  
  44.     public static void main(final String[] args) throws Exception{
  45.         final long start = System.currentTimeMillis();
  46.         final int len = getLongestPath(0);
  47.         final long duration = System.currentTimeMillis() - start;
  48.         System.out.printf("%d LANGUAGE Java %d\n", len, duration);
  49.     }
  50.  
  51.     static long readPlaces() {
  52.         try (BufferedReader in = new BufferedReader(new FileReader("agraph"))) {
  53.             final String s = in.readLine();
  54.             numNodes = Integer.parseInt(s);
  55.             final int[][] nodes = new int[numNodes][];
  56.             for(int i = 0; i < numNodes; i++){
  57.                 nodes[i]= new int[0];
  58.             }
  59.  
  60.             rowlength = 0;
  61.  
  62.             while (in.ready()) {
  63.                 final String ln = in.readLine();
  64.                 final String[] nums = ln.split("[ \t]+");
  65.                 if(nums.length != 3){
  66.                     break;
  67.                 }
  68.                 final int node = Integer.parseInt(nums[0]);
  69.                 final int neighbour = Integer.parseInt(nums[1]);
  70.                 final int cost = Integer.parseInt(nums[2]);
  71.  
  72.                 final int index = nodes[node].length;
  73.                 final int[] replacement = Arrays.copyOf(nodes[node], index + 2);
  74.                 replacement[index] = neighbour;
  75.                 replacement[index+1] = cost;
  76.  
  77.                 nodes[node] = replacement;
  78.  
  79.                 rowlength = Math.max(rowlength, index + 2);
  80.             }
  81.  
  82.             // convert nodes to dense format and write to raw memory
  83.  
  84.             rowlength += 2;
  85.             rowlength *= 4;
  86.  
  87.             final long address = UNSAFE.allocateMemory(numNodes * rowlength);
  88.  
  89.             for (int i = 0; i < numNodes * rowlength; i++) {
  90.                 UNSAFE.putByte(address + i, ZERO);
  91.             }
  92.  
  93.             for (int i = 0; i < nodes.length; i++) {
  94.                 for (int j = 0; j < nodes[i].length; j++) {
  95.                     UNSAFE.putInt(address + (i * rowlength) + (j * 4), nodes[i][j]);
  96.                 }
  97.             }
  98.  
  99.             return address;
  100.         } catch (Exception e) {
  101.             return 0;
  102.         }
  103.     }
  104.  
  105.     static int getLongestPath(final int nodeID){
  106.         UNSAFE.putByte(ADR_visited + nodeID, ONE);
  107.         int dist, max=0;
  108.  
  109.         final long ADR_row = ADR_nodes + (rowlength * nodeID);
  110.  
  111.         int cost;
  112.         for (int i = 0; (cost = UNSAFE.getInt(ADR_row + i + 4)) != 0; i+=8) {
  113.  
  114.             final int dest = UNSAFE.getInt(ADR_row + i);
  115.  
  116.             if (UNSAFE.getByte(ADR_visited + dest) == 0) {
  117.                 dist = cost + getLongestPath(dest);
  118.                 if (dist > max) {
  119.                     max = dist;
  120.                 }
  121.             }
  122.         }
  123.  
  124.         UNSAFE.putByte(ADR_visited + nodeID, ZERO);
  125.         return max;
  126.     }
  127.  
  128.     static Unsafe getUnsafe() {
  129.         try {
  130.             Field singleoneInstanceField = Unsafe.class.getDeclaredField("theUnsafe");
  131.             singleoneInstanceField.setAccessible(true);
  132.             return (Unsafe) singleoneInstanceField.get(null);
  133.         } catch (Exception e) {
  134.             return null;
  135.         }
  136.     }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement