public Spell(String name, int manaCost, int maxLevel, int row, int col, String descriptionFormat, IntToDoubleFunction[] functions, Object[] requirements, SpellEffect spellEffect) { this.name = name; this.manaCost = manaCost; this.maxLevel = maxLevel; this.row = row; this.col = col; this.descriptionFormat = descriptionFormat; this.functions = functions; this.requirements = new HashMap<Spell, Integer>(); if (requirements != null) { for (int k = 0; k < requirements.length; k += 2) { this.requirements.put((Spell) requirements[k], (int) requirements[k + 1]); } } this.spellEffect = spellEffect; this.spellEffect.functions = functions; }
@Override public final DoubleStream mapToDouble(IntToDoubleFunction mapper) { Objects.requireNonNull(mapper); return new DoublePipeline.StatelessOp<Integer>(this, StreamShape.INT_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { @Override Sink<Integer> opWrapSink(int flags, Sink<Double> sink) { return new Sink.ChainedInt<Double>(sink) { @Override public void accept(int t) { downstream.accept(mapper.applyAsDouble(t)); } }; } }; }
/** {@inheritDoc} */ @Override public Vector assign(IntToDoubleFunction fun) { assert fun != null; if (sto.isArrayBased()) { ensureReadOnly(); Arrays.setAll(sto.data(), fun); } else { int len = size(); for (int i = 0; i < len; i++) storageSet(i, fun.applyAsDouble(i)); } return this; }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullCache() { // given final ConcurrentMap<Integer, Double> cache = null; final IntToDoubleFunction function = input -> 123; final IntFunction<Integer> keyFunction = Integer::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide an empty map instead of NULL."); // then new ConcurrentMapBasedIntToDoubleFunctionMemoizer<>(cache, keyFunction, function); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullFunction() { // given final ConcurrentMap<Integer, Double> cache = new ConcurrentHashMap<>(); final IntToDoubleFunction function = null; final IntFunction<Integer> keyFunction = Integer::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage( "Cannot memoize a NULL IntToDoubleFunction - provide an actual IntToDoubleFunction to fix this."); // then new ConcurrentMapBasedIntToDoubleFunctionMemoizer<>(cache, keyFunction, function); }
/** * */ @Test public void shouldUseSetCacheKeyAndValue() { // given final ConcurrentMap<Integer, Double> cache = new ConcurrentHashMap<>(); final IntToDoubleFunction function = input -> 123; final IntFunction<Integer> keyFunction = Integer::valueOf; // when final ConcurrentMapBasedIntToDoubleFunctionMemoizer<Integer> memoizer = new ConcurrentMapBasedIntToDoubleFunctionMemoizer<>( cache, keyFunction, function); // then memoizer.applyAsDouble(123); Assert.assertFalse("Cache is still empty after memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoization key does not match expectations", 123, memoizer.viewCacheForTest().keySet().iterator().next().intValue()); Assert.assertEquals("Memoization value does not match expectations", 123D, memoizer.viewCacheForTest().values().iterator().next().doubleValue(), 0.0D); }
/** * */ @Test public void shouldUseCallWrappedFunction() { // given final ConcurrentMap<Integer, Double> cache = new ConcurrentHashMap<>(); final IntToDoubleFunction function = Mockito.mock(IntToDoubleFunction.class); final IntFunction<Integer> keyFunction = Integer::valueOf; // when final ConcurrentMapBasedIntToDoubleFunctionMemoizer<Integer> memoizer = new ConcurrentMapBasedIntToDoubleFunctionMemoizer<>( cache, keyFunction, function); // then memoizer.applyAsDouble(123); Mockito.verify(function).applyAsDouble(123); }
/** * Create an input device from the supplied mapping functions. * @param axisToValue the function that maps an integer to a double value for the axis * @param buttonNumberToSwitch the function that maps an integer to whether the button is pressed * @param padToValue the function that maps an integer to the directional axis output * @return the resulting input device; never null */ public static InputDevice create( IntToDoubleFunction axisToValue, IntToBooleanFunction buttonNumberToSwitch, IntToIntFunction padToValue ) { return new InputDevice() { @Override public ContinuousRange getAxis(int axis) { return ()->axisToValue.applyAsDouble(axis); } @Override public Switch getButton(int button) { return ()->buttonNumberToSwitch.applyAsBoolean(button); } @Override public DirectionalAxis getDPad(int pad) { return ()->padToValue.applyAsInt(pad); } }; }
/** * Create a new PowerPanel from functions that supply the current for each channel, total current, voltage, and temperature. * * @param currentForChannel the function that returns the current for a given channel; may not be null * @param totalCurrent the function that returns total current; may not be null * @param voltage the function that returns voltage; may not be null * @param temperature the function that returns temperature; may not be null * @return the power panel; never null * @see Hardware#powerPanel() */ public static PowerPanel create(IntToDoubleFunction currentForChannel, DoubleSupplier totalCurrent, DoubleSupplier voltage, DoubleSupplier temperature) { return new PowerPanel() { @Override public CurrentSensor getCurrentSensor(int channel) { return () -> currentForChannel.applyAsDouble(channel); } @Override public CurrentSensor getTotalCurrentSensor() { return totalCurrent::getAsDouble; } @Override public VoltageSensor getVoltageSensor() { return voltage::getAsDouble; } @Override public TemperatureSensor getTemperatureSensor() { return voltage::getAsDouble; } }; }
@Test(dataProvider = "double") public void testSetAllDouble(String name, int size, IntToDoubleFunction generator, double[] expected) { double[] result = new double[size]; Arrays.setAll(result, generator); assertDoubleArrayEquals(result, expected, 0.05, "setAll(double[], IntToDoubleFunction) case " + name + " failed."); // ensure fresh array result = new double[size]; Arrays.parallelSetAll(result, generator); assertDoubleArrayEquals(result, expected, 0.05, "setAll(double[], IntToDoubleFunction) case " + name + " failed."); }
/** * Apply an int -> double function to this range, producing a double[] * * @param lambda the int -> double function */ public double[] mapToDouble(final IntToDoubleFunction lambda) { JointCallingUtils.nonNull(lambda, "the lambda function cannot be null"); final double[] result = new double[size()]; for (int i = from; i < to; i++) { result[i - from] = lambda.applyAsDouble(i); } return result; }
/** * Sums the values of an int -> double function applied to this range * * @param lambda the int -> double function */ public double sum(final IntToDoubleFunction lambda) { JointCallingUtils.nonNull(lambda, "the lambda function cannot be null"); double result = 0; for (int i = from; i < to; i++) { result += lambda.applyAsDouble(i); } return result; }
/** * Finds best, in terms of score sum, combined match where each word occurs * at most once and doesn't overlap with any other * @param wordMatches - matches of words * @param distanceCoef - multiplication coefficient applied to score of * resulting combined submatch, accepting * distance (in chars) between last two words */ static public Seq findBestCombinedMatch(Multimap<Integer, ScoredMatch> wordMatches, IntToDoubleFunction distanceCoef) { List<Seq> words = new ArrayList<>(); wordMatches.entries().forEach(e -> words.add(Seq.of(e.getKey(), e.getValue()))); Collections.sort(words, Comparator.comparing(Seq::effectiveStart)); Seq best = null; List<Seq> seqs = new ArrayList<>(); for (Seq w : words) { int k = seqs.size(); for (int i = 0; i < k; i++) { Seq s = seqs.get(i); Seq ns = s.append(w, distanceCoef.applyAsDouble(w.start - s.end)); // continued sequence if (ns != null) { seqs.add(ns); if (best == null || best.score < ns.score) best = ns; } } if (!w.singleWord) throw new IllegalArgumentException(); seqs.add(w); if (best == null || best.score < w.score) best = w; } return best; }
/** * */ @Test public void shouldMemoizeIntToDoubleFunctionWithKeyFunction() { // given final IntToDoubleFunction function = a -> 123D; final IntFunction<String> keyFunction = a -> "key"; // when final IntToDoubleFunction memoize = CaffeineMemoize.intToDoubleFunction(function, keyFunction); // then Assert.assertNotNull("Memoized IntToDoubleFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeIntToDoubleFunction() { // given final IntToDoubleFunction function = a -> 123D; // when final IntToDoubleFunction memoize = CaffeineMemoize.intToDoubleFunction(function); // then Assert.assertNotNull("Memoized IntToDoubleFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeIntToDoubleFunctionWithLambda() { // given // when final IntToDoubleFunction memoize = CaffeineMemoize.intToDoubleFunction(a -> 123D); // then Assert.assertNotNull("Memoized IntToDoubleFunction is NULL", memoize); }
GuavaCacheBasedIntToDoubleFunctionMemoizer( final Cache<KEY, Double> cache, final IntFunction<KEY> keyFunction, final IntToDoubleFunction function) { super(cache); this.keyFunction = keyFunction; this.function = function; }
/** * */ @Test public void shouldMemoizeIntToDoubleFunctionWithKeyFunction() { // given final IntToDoubleFunction function = a -> 123D; final IntFunction<Integer> keyFunction = Integer::valueOf; // when final IntToDoubleFunction memoize = GuavaMemoize.intToDoubleFunction(function, keyFunction); // then Assert.assertNotNull("Memoized IntToDoubleFunction is NULL", memoize); }
/** * */ @Test public void shouldAcceptLoadingCache() { // given final IntToDoubleFunction function = a -> 123; final IntFunction<Integer> keyFunction = Integer::valueOf; final Cache<Integer, Double> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedIntToDoubleFunctionMemoizer<Integer> memoizer = new GuavaCacheBasedIntToDoubleFunctionMemoizer<>( cache, keyFunction, function); // then Assert.assertNotNull(memoizer); }
/** * */ @Test public void shouldTransformInput() { // given final IntToDoubleFunction function = a -> 123.456D; final IntFunction<Integer> keyFunction = Integer::valueOf; final Cache<Integer, Double> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedIntToDoubleFunctionMemoizer<Integer> memoizer = new GuavaCacheBasedIntToDoubleFunctionMemoizer<>( cache, keyFunction, function); // then Assert.assertEquals("Memoized value does not match expectation", 123.456D, memoizer.applyAsDouble(789), 0.0D); }
/** * */ @Test public void shouldMemoizeIntToDoubleFunctionWithLambda() { // given // when final IntToDoubleFunction memoize = GuavaMemoize.intToDoubleFunction(a -> 123D); // then Assert.assertNotNull("Memoized IntToDoubleFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeIntToDoubleFunction() { // given final IntToDoubleFunction function = a -> 123D; // when final IntToDoubleFunction memoize = GuavaMemoize.intToDoubleFunction(function); // then Assert.assertNotNull("Memoized IntToDoubleFunction is NULL", memoize); }
JCacheBasedIntToDoubleFunctionMemoizer( final Cache<KEY, Double> cache, final IntFunction<KEY> keyFunction, final IntToDoubleFunction biFunction) { super(cache); this.keyFunction = keyFunction; this.biFunction = biFunction; }
/** * */ @Test public void shouldMemoizeIntToDoubleFunctionWithKeyFunction() { // given final IntToDoubleFunction function = a -> 123; final IntFunction<String> keyFunction = a -> "key"; // when final IntToDoubleFunction memoize = JCacheMemoize.intToDoubleFunction(function, keyFunction); // then Assert.assertNotNull("Memoized IntToDoubleFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeIntToDoubleFunctionWithLambda() { // given // when final IntToDoubleFunction memoize = JCacheMemoize.intToDoubleFunction(a -> 123); // then Assert.assertNotNull("Memoized IntToDoubleFunction is NULL", memoize); }
/** * */ @Test public void shouldMemoizeBiFunction() { // given final IntToDoubleFunction function = first -> 123; final IntFunction<String> keyfunction = a -> "key"; try (final Cache<String, Double> cache = JCacheMemoize.createCache(ToDoubleBiFunction.class)) { // when final JCacheBasedIntToDoubleFunctionMemoizer<String> loader = new JCacheBasedIntToDoubleFunctionMemoizer<>( cache, keyfunction, function); // then Assert.assertEquals("Memoized value does not match expectation", 123D, loader.applyAsDouble(123), 0.0D); } }
/** * */ @Test public void shouldMemoizeIntToDoubleFunction() { // given final IntToDoubleFunction function = a -> 123; // when final IntToDoubleFunction memoize = JCacheMemoize.intToDoubleFunction(function); // then Assert.assertNotNull("Memoized IntToDoubleFunction is NULL", memoize); }
@SuppressWarnings(CompilerWarnings.NLS) ConcurrentMapBasedIntToDoubleFunctionMemoizer( final ConcurrentMap<KEY, Double> cache, final IntFunction<KEY> keyFunction, final IntToDoubleFunction function) { super(cache); this.keyFunction = keyFunction; this.function = requireNonNull(function, "Cannot memoize a NULL IntToDoubleFunction - provide an actual IntToDoubleFunction to fix this."); }