Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Untitled

a guest Apr 9th, 2020 171 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ru.ifmo.rain.naumov.concurrent;
  2.  
  3. import info.kgeorgiy.java.advanced.concurrent.ScalarIP;
  4. import info.kgeorgiy.java.advanced.mapper.ParallelMapper;
  5.  
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.Comparator;
  9. import java.util.List;
  10. import java.util.concurrent.atomic.AtomicInteger;
  11. import java.util.function.Function;
  12. import java.util.function.Predicate;
  13.  
  14. import static java.lang.Math.*;
  15.  
  16.  
  17. public class IterativeParallelism implements ScalarIP {
  18.  
  19.     public IterativeParallelism(ParallelMapper mapper) {
  20.  
  21.     }
  22.  
  23.     public IterativeParallelism() {
  24.  
  25.     }
  26.  
  27.     private <T, R> R computeParallel(int numberOfThreads, List<? extends T> values,
  28.                                      Function<List<? extends T>, R> valueToResult,
  29.                                      Function<List<? extends R>, R> collectResults) throws InterruptedException {
  30.  
  31.         if (numberOfThreads <= 0) {
  32.             throw new IllegalArgumentException("number of threads <= 0");
  33.         }
  34.  
  35.         numberOfThreads = min(numberOfThreads, values.size());
  36.         List<Thread> threads = new ArrayList<>();
  37.         List<R> results = new ArrayList<>(Collections.nCopies(numberOfThreads, null));
  38.  
  39.         final int segmentSize = (int) Math.ceil((double) values.size() / (double) numberOfThreads);
  40.         int cutoff = values.size() % numberOfThreads;
  41.         int segmentIndex = 0, threadIndex = 0;
  42.         while (numberOfThreads-- > 0) {
  43.             final int finalThreadIndex = threadIndex;
  44.             final int finalSegmentIndex = min(segmentIndex, values.size() - 1);
  45.             final int finalSegmentRight = min(finalSegmentIndex + segmentSize + (cutoff-- > 0 ? 1 : 0), values.size());
  46.             threads.add(new Thread(() ->
  47.                     results.set(finalThreadIndex, valueToResult.apply(values.subList(finalSegmentIndex, finalSegmentRight)))
  48.             ));
  49.             segmentIndex = finalSegmentRight;
  50.             threadIndex += 1;
  51.         }
  52.         for (Thread t : threads) {
  53.             t.start();
  54.         }
  55.         for (Thread t : threads) {
  56.             t.join();
  57.         }
  58.         return collectResults.apply(results);
  59.     }
  60.  
  61.     @Override
  62.     public <T> T maximum(int threads, List<? extends T> values, Comparator<? super T> comparator) throws InterruptedException {
  63.         if (values.isEmpty()) {
  64.             throw new IllegalArgumentException("empty list");
  65.         }
  66.         return computeParallel(threads, values, (elements) -> Collections.max(elements, comparator),
  67.                 (results) -> Collections.max(results, comparator));
  68.     }
  69.  
  70.     @Override
  71.     public <T> T minimum(int threads, List<? extends T> values, Comparator<? super T> comparator) throws InterruptedException {
  72.         return maximum(threads, values, Collections.reverseOrder(comparator));
  73.     }
  74.  
  75.     @Override
  76.     public <T> boolean all(int threads, List<? extends T> values, Predicate<? super T> predicate) throws InterruptedException {
  77.         return computeParallel(threads, values, (elements) -> elements.stream().allMatch(predicate),
  78.                 (results) -> results.stream().allMatch(Predicate.isEqual(true)));
  79.     }
  80.  
  81.     @Override
  82.     public <T> boolean any(int threads, List<? extends T> values, Predicate<? super T> predicate) throws InterruptedException {
  83.         return computeParallel(threads, values, (elements) -> elements.stream().anyMatch(predicate),
  84.                 (results) -> results.stream().anyMatch(Predicate.isEqual(true)));
  85.     }
  86. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top