Advertisement
Guest User

Untitled

a guest
Mar 11th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.14 KB | None | 0 0
  1. package local.test;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Set;
  7. import java.util.concurrent.ConcurrentHashMap;
  8. import java.util.concurrent.ForkJoinPool;
  9. import java.util.concurrent.RecursiveAction;
  10.  
  11. public class LinkedWords {
  12. //        static final int SIZE = 10_000;
  13.         static final int SIZE = 300_000;
  14. //    static final int SIZE = 1_000_000;
  15.  
  16. //    static final int SEQUENTIAL_THRESHOLD = 500;
  17. //    static final int SEQUENTIAL_THRESHOLD = 1_000;
  18.     static final int SEQUENTIAL_THRESHOLD = 5_000;
  19. //    static final int SEQUENTIAL_THRESHOLD = 10_000;
  20. //    static final int SEQUENTIAL_THRESHOLD = 50_000;
  21. //    static final int SEQUENTIAL_THRESHOLD = 100_000;
  22. //    static final int SEQUENTIAL_THRESHOLD = Integer.MAX_VALUE;
  23.  
  24.  
  25.     public static void main(String[] args) {
  26.         String word = "0";
  27.  
  28.         ArrayList<String> words = new ArrayList<>();
  29.         for (int i = 0; i < SIZE; i++) {
  30.             words.add((i % 100) + "");
  31.         }
  32.  
  33.         long t1 = System.currentTimeMillis();
  34.         Set<String> linkedWords = getLinkedWords(word, words);
  35.         System.out.println(System.currentTimeMillis() - t1);
  36.  
  37.         long t2 = System.currentTimeMillis();
  38.         Set<String> linkedWordsForeach = getLinkedWordsForeach(word, words);
  39.         System.out.println(System.currentTimeMillis() - t2);
  40.  
  41.         long t3 = System.currentTimeMillis();
  42.         Set<String> linkedWordsRecursiveAction = getLinkedWordsRecursiveAction(word, words);
  43.         System.out.println(System.currentTimeMillis() - t3);
  44.  
  45.         long t4 = System.currentTimeMillis();
  46.         Set<String> linkedWordsRecursiveAction2 = getLinkedWordsRecursiveAction2(word, words);
  47.         System.out.println(System.currentTimeMillis() - t4);
  48.     }
  49.  
  50.     private static Set<String> getLinkedWords(String s, List<String> words) {
  51.         Set<String> tmp = new HashSet<String>();
  52.         for (int i = 0; i < words.size() - 1; i++) {
  53.             if (s.equals(words.get(i))) {
  54.                 tmp.add(words.get(i + 1));
  55.             }
  56.         }
  57.         return tmp;
  58.     }
  59.  
  60.     private static Set<String> getLinkedWordsForeach(String s, List<String> words) {
  61.         Set<String> tmp = new HashSet<String>();
  62.         String prev = null;
  63.         for (String word : words) {
  64.             if (prev != null && prev.equals(s)) {
  65.                 tmp.add(word);
  66.             }
  67.             prev = word;
  68.         }
  69.         return tmp;
  70.     }
  71.  
  72.     private static Set<String> getLinkedWordsRecursiveAction(String s, ArrayList<String> words) {
  73.         ConcurrentHashMap.KeySetView<String, Boolean> acc = ConcurrentHashMap.newKeySet();
  74.         ForkJoinPool forkJoinPool = ForkJoinPool.commonPool();
  75.         forkJoinPool.invoke(new LinkedWordsAction(s, words, 0, words.size(), acc));
  76.         return acc;
  77.     }
  78.  
  79.     private static class LinkedWordsAction extends RecursiveAction {
  80.         final String word;
  81.         final ArrayList<String> words;
  82.  
  83.         final int low;
  84.         final int high;
  85.  
  86.         final Set<String> acc;
  87.  
  88.         public LinkedWordsAction(String word, ArrayList<String> words, int low, int high, Set<String> acc) {
  89.             this.word = word;
  90.             this.words = words;
  91.             this.low = low;
  92.             this.high = high;
  93.             this.acc = acc;
  94.         }
  95.  
  96.         @Override
  97.         protected void compute() {
  98.             if (high - low <= SEQUENTIAL_THRESHOLD) {
  99.                 for (int i = low; i < high; ++i) {
  100.                     if (word.equals(words.get(i))) {
  101.                         acc.add(words.get(i + 1));
  102.                     }
  103.                 }
  104.             } else {
  105.                 int mid = low + (high - low) / 2;
  106.                 LinkedWordsAction left = new LinkedWordsAction(word, words, low, mid, acc);
  107.                 LinkedWordsAction right = new LinkedWordsAction(word, words, mid, high, acc);
  108.                 invokeAll(left, right);
  109.                 if (words.get(mid - 1).equals(word)) {
  110.                     acc.add(words.get(mid));
  111.                 }
  112.             }
  113.  
  114.         }
  115.     }
  116.  
  117.     private static Set<String> getLinkedWordsRecursiveAction2(String s, ArrayList<String> words) {
  118.         ConcurrentHashMap.KeySetView<String, Boolean> acc = ConcurrentHashMap.newKeySet();
  119.         ForkJoinPool forkJoinPool = ForkJoinPool.commonPool();
  120.         forkJoinPool.invoke(new LinkedWordsAction2(s, words, 0, words.size(), acc, null));
  121.         return acc;
  122.     }
  123.  
  124.     private static class LinkedWordsAction2 extends RecursiveAction {
  125.         final String word;
  126.         final ArrayList<String> words;
  127.  
  128.         final int low;
  129.         final int high;
  130.  
  131.         final Set<String> acc;
  132.  
  133.         LinkedWordsAction2 next;
  134.  
  135.         public LinkedWordsAction2(String word, ArrayList<String> words, int low, int high, Set<String> acc, LinkedWordsAction2 next) {
  136.             this.word = word;
  137.             this.words = words;
  138.             this.low = low;
  139.             this.high = high;
  140.             this.acc = acc;
  141.             this.next = next;
  142.         }
  143.  
  144.         @Override
  145.         protected void compute() {
  146.             int l = low;
  147.             int h = high;
  148.             LinkedWordsAction2 right = null;
  149.             while (h - l > SEQUENTIAL_THRESHOLD && getSurplusQueuedTaskCount() <= 3) {
  150.                 int mid = (l + h) >>> 1;
  151.                 right = new LinkedWordsAction2(word, words, mid, h, acc, right);
  152.                 right.fork();
  153.                 h = mid;
  154.             }
  155.             atLeaf(l, h);
  156.             while (right != null) {
  157.                 if (right.tryUnfork()) // directly calculate if not stolen
  158.                     right.atLeaf(right.low, right.high);
  159.                 else {
  160.                     right.join();
  161.                 }
  162.                 if (words.get(right.low - 1).equals(word)) {
  163.                     acc.add(words.get(right.low));
  164.                 }
  165.                 right = right.next;
  166.             }
  167.         }
  168.  
  169.         private void atLeaf(int l, int h) {
  170.             for (int i = l; i < h; ++i) {
  171.                 if (word.equals(words.get(i))) {
  172.                     acc.add(words.get(i + 1));
  173.                 }
  174.             }
  175.         }
  176.     }
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement