/** * Creates an new instance. * * @param cpuLoadEstimator estimates CPU load in terms of cycles * @param ramLoadEstimator estimates RAM load in terms of bytes * @param diskLoadEstimator estimates disk accesses in terms of bytes * @param networkLoadEstimator estimates network in terms of bytes * @param resourceUtilizationEstimator degree to which the load profile can utilize available resources * @param overheadMillis overhead that this load profile incurs * @param configurationKey from that this instances was perceived */ public NestableLoadProfileEstimator(LoadEstimator cpuLoadEstimator, LoadEstimator ramLoadEstimator, LoadEstimator diskLoadEstimator, LoadEstimator networkLoadEstimator, ToDoubleBiFunction<long[], long[]> resourceUtilizationEstimator, long overheadMillis, String configurationKey) { this.cpuLoadEstimator = cpuLoadEstimator; this.ramLoadEstimator = ramLoadEstimator; this.diskLoadEstimator = diskLoadEstimator; this.networkLoadEstimator = networkLoadEstimator; this.resourceUtilizationEstimator = resourceUtilizationEstimator; this.overheadMillis = overheadMillis; this.configurationKey = configurationKey; }
/** * */ @Test public void shouldMemoizeBiFunction() { // given final ToDoubleBiFunction<String, String> function = (first, second) -> 123; final BiFunction<String, String, String> keyfunction = hashCodeKeyFunction(); try (final Cache<String, Double> cache = JCacheMemoize.createCache(ToDoubleBiFunction.class)) { // when final JCacheBasedToDoubleBiFunctionMemoizer<String, String, String> loader = new JCacheBasedToDoubleBiFunctionMemoizer<>( cache, keyfunction, function); // then Assert.assertEquals("Memoized value does not match expectation", 123D, loader.applyAsDouble("first", "second"), 0.0D); } }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullCache() { // given final ConcurrentMap<String, Double> cache = null; final BiFunction<String, String, String> keyFunction = (a, b) -> "key"; final ToDoubleBiFunction<String, String> biFunction = (a, b) -> 123.456D; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide an empty map instead of NULL."); // then new ConcurrentMapBasedToDoubleBiFunctionMemoizer<>(cache, keyFunction, biFunction); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullKeyBiFunction() { // given final ConcurrentMap<String, Double> cache = new ConcurrentHashMap<>(); final BiFunction<String, String, String> keyFunction = null; final ToDoubleBiFunction<String, String> biFunction = (a, b) -> 123.456D; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide a key function, might just be 'MemoizationDefaults.hashCodeKeyFunction()'."); // then new ConcurrentMapBasedToDoubleBiFunctionMemoizer<>(cache, keyFunction, biFunction); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullBiFunction() { // given final ConcurrentMap<String, Double> cache = new ConcurrentHashMap<>(); final BiFunction<String, String, String> keyFunction = (a, b) -> "key"; final ToDoubleBiFunction<String, String> biFunction = null; // when thrown.expect(NullPointerException.class); thrown.expectMessage( "Cannot memoize a NULL ToDoubleBiFunction - provide an actual ToDoubleBiFunction to fix this."); // then new ConcurrentMapBasedToDoubleBiFunctionMemoizer<>(cache, keyFunction, biFunction); }
/** * */ @Test public void shouldUseSetCacheKeyAndValue() { // given final ConcurrentMap<String, Double> cache = new ConcurrentHashMap<>(); final BiFunction<String, String, String> keyFunction = (a, b) -> "key"; final ToDoubleBiFunction<String, String> biFunction = (a, b) -> 123.456D; // when final ConcurrentMapBasedToDoubleBiFunctionMemoizer<String, String, String> memoizer = new ConcurrentMapBasedToDoubleBiFunctionMemoizer<>( cache, keyFunction, biFunction); // then memoizer.applyAsDouble("123.456", "789.123"); Assert.assertFalse("Cache is still empty after memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoization key does not match expectations", "key", memoizer.viewCacheForTest().keySet().iterator().next()); Assert.assertEquals("Memoization value does not match expectations", 123.456D, memoizer.viewCacheForTest().values().iterator().next().doubleValue(), 0.0D); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldUseCallWrappedBiFunction() { // given final ConcurrentMap<String, Double> cache = new ConcurrentHashMap<>(); final BiFunction<String, String, String> keyFunction = (a, b) -> "key"; final ToDoubleBiFunction<String, String> biFunction = Mockito.mock(ToDoubleBiFunction.class); // when final ConcurrentMapBasedToDoubleBiFunctionMemoizer<String, String, String> memoizer = new ConcurrentMapBasedToDoubleBiFunctionMemoizer<>( cache, keyFunction, biFunction); // then memoizer.applyAsDouble("123.456", "789.123"); Mockito.verify(biFunction).applyAsDouble("123.456", "789.123"); }
public static double integrate2d(final ToDoubleBiFunction<Double, Double> getIntegrand, final double xLowerBound, final double xUpperBound, final int xNumPoints, final double yLowerBound, final double yUpperBound, final int yNumPoints){ final GaussIntegrator xIntegrator = integratorFactory.legendre(xNumPoints, xLowerBound, xUpperBound); final GaussIntegrator yIntegrator = integratorFactory.legendre(yNumPoints, yLowerBound, yUpperBound); final double[] xIntegrationWeights = new IndexRange(0, xNumPoints).mapToDouble(xIntegrator::getWeight); final double[] xAbscissas = new IndexRange(0, xNumPoints).mapToDouble(xIntegrator::getPoint); final double[] yIntegrationWeights = new IndexRange(0, yNumPoints).mapToDouble(yIntegrator::getWeight); final double[] yAbscissas = new IndexRange(0, yNumPoints).mapToDouble(yIntegrator::getPoint); double integral = 0; for (int i = 0; i < xNumPoints; i++) { final double x = xAbscissas[i]; for (int j = 0; j < yNumPoints; j++) { final double y = yAbscissas[j]; final double integrand = getIntegrand.applyAsDouble(x, y); integral += xIntegrationWeights[i] * yIntegrationWeights[j] * integrand; } } return integral; }
@Override public double sum(ToDoubleBiFunction<? super Integer, ? super T> mapper) { ToDoubleFunction<T> function; if (!isParallel()) { // use fast sequential counting function = new ToDoubleFunction<T>() { int i = 0; @Override public double applyAsDouble(T t) { return mapper.applyAsDouble(i++, t); } }; } else { // retrieve each index individually function = new ToDoubleFunction<T>() { List<T> list = stream().collect(Collectors.toList()); @Override public double applyAsDouble(T t) { return mapper.applyAsDouble(list.indexOf(t), t); } }; } return stream().collect(Collectors.summingDouble(function)); }
@Test public void testCheckedToDoubleBiFunction() { final CheckedToDoubleBiFunction<Object, Object> toDoubleBiFunction = (t, u) -> { throw new Exception(t + ":" + u); }; ToDoubleBiFunction<Object, Object> f1 = Unchecked.toDoubleBiFunction(toDoubleBiFunction); ToDoubleBiFunction<Object, Object> f2 = CheckedToDoubleBiFunction.unchecked(toDoubleBiFunction); ToDoubleBiFunction<Object, Object> f3 = Sneaky.toDoubleBiFunction(toDoubleBiFunction); ToDoubleBiFunction<Object, Object> f4 = CheckedToDoubleBiFunction.sneaky(toDoubleBiFunction); assertToDoubleBiFunction(f1, UncheckedException.class); assertToDoubleBiFunction(f2, UncheckedException.class); assertToDoubleBiFunction(f3, Exception.class); assertToDoubleBiFunction(f4, Exception.class); }
MapReduceMappingsToDoubleTask (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, MapReduceMappingsToDoubleTask<K,V> nextRight, ToDoubleBiFunction<? super K, ? super V> transformer, double basis, DoubleBinaryOperator reducer) { super(p, b, i, f, t); this.nextRight = nextRight; this.transformer = transformer; this.basis = basis; this.reducer = reducer; }
public final void compute() { final ToDoubleBiFunction<? super K, ? super V> transformer; final DoubleBinaryOperator reducer; if ((transformer = this.transformer) != null && (reducer = this.reducer) != null) { double r = this.basis; for (int i = baseIndex, f, h; batch > 0 && (h = ((f = baseLimit) + i) >>> 1) > i;) { addToPendingCount(1); (rights = new MapReduceMappingsToDoubleTask<K,V> (this, batch >>>= 1, baseLimit = h, f, tab, rights, transformer, r, reducer)).fork(); } for (Node<K,V> p; (p = advance()) != null; ) r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key, p.val)); result = r; CountedCompleter<?> c; for (c = firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") MapReduceMappingsToDoubleTask<K,V> t = (MapReduceMappingsToDoubleTask<K,V>)c, s = t.rights; while (s != null) { t.result = reducer.applyAsDouble(t.result, s.result); s = t.rights = s.nextRight; } } } }
/** * @param scorer 打分算法 * @param mapFunction 一个Node,获得tag-score健值对 * @param consumer set选定的标签 */ public SimpleViterbi( ToDoubleBiFunction<Map.Entry<TagType, Integer>, Map.Entry<TagType, Integer>> scorer, Function<ObjType, Map<TagType, Integer>> mapFunction, BiConsumer<ObjType, TagType> consumer ) { this.consumer = consumer; this.scorer = scorer; this.mapFunction = mapFunction; }
@Override public final void compute() { final ToDoubleBiFunction<? super K, ? super V> transformer; final DoubleBinaryOperator reducer; if ((transformer = this.transformer) != null && (reducer = this.reducer) != null) { double r = this.basis; for (int i = this.baseIndex, f, h; this.batch > 0 && (h = ((f = this.baseLimit) + i) >>> 1) > i;) { this.addToPendingCount(1); (this.rights = new MapReduceMappingsToDoubleTask<K,V> (this, this.batch >>>= 1, this.baseLimit = h, f, this.tab, this.rights, transformer, r, reducer)).fork(); } for (Node<K,V> p; (p = this.advance()) != null; ) { r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key, p.val)); } this.result = r; CountedCompleter<?> c; for (c = this.firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") MapReduceMappingsToDoubleTask<K,V> t = (MapReduceMappingsToDoubleTask<K,V>)c, s = t.rights; while (s != null) { t.result = reducer.applyAsDouble(t.result, s.result); s = t.rights = s.nextRight; } } } }
MapReduceMappingsToDoubleTask(final BulkTask<K, V, ?> p, final int b, final int i, final int f, final Node<K, V>[] t, final MapReduceMappingsToDoubleTask<K, V> nextRight, final ToDoubleBiFunction<? super K, ? super V> transformer, final double basis, final DoubleBinaryOperator reducer) { super(p, b, i, f, t); this.nextRight = nextRight; this.transformer = transformer; this.basis = basis; this.reducer = reducer; }
@Override public final void compute() { final ToDoubleBiFunction<? super K, ? super V> transformer; final DoubleBinaryOperator reducer; if ((((transformer = this.transformer)) != null) && (((reducer = this.reducer)) != null)) { double r = this.basis; for (int i = this.baseIndex, f, h; (this.batch > 0) && (((h = (((f = this.baseLimit)) + i) >>> 1)) > i); ) { this.addToPendingCount(1); (this.rights = new MapReduceMappingsToDoubleTask<>(this, this.batch >>>= 1, this.baseLimit = h, f, this.tab, this.rights, transformer, r, reducer)).fork(); } for (Node<K, V> p; (p = this.advance()) != null; ) { r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key, p.val)); } this.result = r; CountedCompleter<?> c; for (c = this.firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") final MapReduceMappingsToDoubleTask<K, V> t = (MapReduceMappingsToDoubleTask<K, V>) c; @SuppressWarnings("unchecked") MapReduceMappingsToDoubleTask<K, V> s = t.rights; while (s != null) { t.result = reducer.applyAsDouble(t.result, s.result); s = t.rights = s.nextRight; } } } }
/** * Parses a mathematical expression and provides it as a {@link ToDoubleFunction}. * * @param expression a mathematical expression * @return a {@link ToLongBiFunction} wrapping the expression */ private static ToDoubleBiFunction<long[], long[]> compileResourceUsage(String expression) { final Expression expr = ExpressionBuilder.parse(expression).specify(baseContext); return (inCards, outCards) -> { Context mathContext = createMathContext(null, inCards, outCards); return expr.evaluate(mathContext); }; }
/** * Creates an new instance. * * @param cpuLoadEstimator estimates CPU load in terms of cycles * @param ramLoadEstimator estimates RAM load in terms of bytes * @param diskLoadEstimator estimates disk accesses in terms of bytes * @param networkLoadEstimator estimates network in terms of bytes * @param resourceUtilizationEstimator degree to which the load profile can utilize available resources * @param overheadMillis overhead that this load profile incurs */ public NestableLoadProfileEstimator(LoadEstimator cpuLoadEstimator, LoadEstimator ramLoadEstimator, LoadEstimator diskLoadEstimator, LoadEstimator networkLoadEstimator, ToDoubleBiFunction<long[], long[]> resourceUtilizationEstimator, long overheadMillis) { this( cpuLoadEstimator, ramLoadEstimator, diskLoadEstimator, networkLoadEstimator, resourceUtilizationEstimator, overheadMillis, null ); }
/** * */ @Test public void shouldMemoizeToDoubleBiFunctionWithKeyFunction() { // given final ToDoubleBiFunction<Double, Double> function = (first, second) -> first.doubleValue() + second.doubleValue(); final BiFunction<Double, Double, String> keyFunction = hashCodeKeyFunction(); // when final ToDoubleBiFunction<Double, Double> memoize = CaffeineMemoize.toDoubleBiFunction(function, keyFunction); // then Assert.assertNotNull("Memoized ToDoubleBiFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeToDoubleBiFunction() { // given final ToDoubleBiFunction<Double, Double> function = (first, second) -> first.doubleValue() + second.doubleValue(); // when final ToDoubleBiFunction<Double, Double> memoize = CaffeineMemoize.toDoubleBiFunction(function); // then Assert.assertNotNull("Memoized ToDoubleBiFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeToDoubleBiFunctionWithLambda() { // given // when final ToDoubleBiFunction<Double, Double> memoize = CaffeineMemoize .toDoubleBiFunction((first, second) -> first.doubleValue() + second.doubleValue()); // then Assert.assertNotNull("Memoized ToDoubleBiFunction is NULL", memoize); }
GuavaCacheBasedToDoubleBiFunctionMemoizer( final Cache<KEY, Double> cache, final BiFunction<FIRST, SECOND, KEY> keyFunction, final ToDoubleBiFunction<FIRST, SECOND> biFunction) { super(cache); this.keyFunction = keyFunction; this.biFunction = biFunction; }
/** * */ @Test public void shouldMemoizeToDoubleBiFunctionWithKeyFunction() { // given final ToDoubleBiFunction<String, String> function = (a, b) -> 123; final BiFunction<String, String, String> keyFunction = (a, b) -> "key"; // when final ToDoubleBiFunction<String, String> memoize = GuavaMemoize.toDoubleBiFunction(function, keyFunction); // then Assert.assertNotNull("Memoized ToDoubleBiFunction is NULL", memoize); }
/** * */ @Test public void shouldAcceptCacheAndKeyFunctionAndBiFunction() { // given final ToDoubleBiFunction<String, String> biFunction = (first, second) -> 123; final BiFunction<String, String, String> keyFunction = (first, second) -> second + first; final Cache<String, Double> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedToDoubleBiFunctionMemoizer<String, String, String> memoizer = new GuavaCacheBasedToDoubleBiFunctionMemoizer<>( cache, keyFunction, biFunction); // then Assert.assertNotNull(memoizer); }
/** * */ @Test public void shouldTransformInput() { // given final ToDoubleBiFunction<String, String> biFunction = (first, second) -> 123; final BiFunction<String, String, String> keyFunction = (first, second) -> second + first; final Cache<String, Double> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedToDoubleBiFunctionMemoizer<String, String, String> memoizer = new GuavaCacheBasedToDoubleBiFunctionMemoizer<>( cache, keyFunction, biFunction); // then Assert.assertEquals("firstsecond", 123D, memoizer.applyAsDouble("first", "second"), 0.0D); }
/** * */ @Test public void shouldMemoizeToDoubleBiFunctionWithLambda() { // given // when final ToDoubleBiFunction<String, String> memoize = GuavaMemoize.toDoubleBiFunction((a, b) -> 123); // then Assert.assertNotNull("Memoized ToDoubleBiFunction is NULL", memoize); }