You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1003 B
Java
47 lines
1003 B
Java
package _12._1.lukas;
|
|
|
|
import provided._12._1.Fibonacci;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
// eats up the heap
|
|
public class FibonacciDynamicRecursivePersistentParallel extends Fibonacci {
|
|
private final Map<Integer, Long> memory = new HashMap<>();
|
|
|
|
{
|
|
this.memory.put(0, 0L);
|
|
this.memory.put(1, 1L);
|
|
}
|
|
|
|
@Override
|
|
public long calculate(int n) {
|
|
if(n < 0) {
|
|
throw new IllegalArgumentException("n must not be negative. Negafibonacci numbers are not supported.");
|
|
}
|
|
if(this.memory.containsKey(n)) {
|
|
return this.memory.get(n);
|
|
}
|
|
|
|
final long result;
|
|
{
|
|
final long[] parts = new long[2];
|
|
Thread big = new Thread(() -> parts[0] = calculate(n - 1));
|
|
Thread small = new Thread(() -> parts[1] = calculate(n - 2));
|
|
big.start();
|
|
small.start();
|
|
|
|
try {
|
|
big.join();
|
|
small.join();
|
|
result = parts[0] + parts[1];
|
|
} catch(InterruptedException e) {
|
|
throw new IllegalStateException(e);
|
|
}
|
|
}
|
|
|
|
this.memory.put(n, result);
|
|
return result;
|
|
}
|
|
}
|