/** * Gets the best goods wish for a carrier unit. * * @param aiUnit The carrier {@code AIUnit}. * @param goodsType The {@code GoodsType} to wish for. * @return The best {@code GoodsWish} for the unit. */ public GoodsWish getBestGoodsWish(AIUnit aiUnit, GoodsType goodsType) { final Unit carrier = aiUnit.getUnit(); final ToDoubleFunction<GoodsWish> wishValue = cacheDouble(gw -> { int turns = carrier.getTurnsToReach(carrier.getLocation(), gw.getDestination()); return (turns >= Unit.MANY_TURNS) ? -1.0 : (double)gw.getValue() / turns; }); final Comparator<GoodsWish> comp = Comparator.comparingDouble(wishValue); List<GoodsWish> wishes = goodsWishes.get(goodsType); return (wishes == null) ? null : maximize(wishes, gw -> wishValue.applyAsDouble(gw) > 0.0, comp); }
/** * Maps the specified column to doubles using the mapper function provided * @param frame the frame reference * @param colKey the column key to apply mapper function to * @param mapper the mapper function to apply * @return the newly created content, with update column */ @SuppressWarnings("unchecked") final XDataFrameContent<R,C> mapToDoubles(XDataFrame<R,C> frame, C colKey, ToDoubleFunction<DataFrameValue<R,C>> mapper) { if (!isColumnStore()) { throw new DataFrameException("Cannot apply columns of a transposed DataFrame"); } else { final int rowCount = rowKeys.size(); final boolean parallel = frame.isParallel(); final int colIndex = colKeys.getIndexForKey(colKey); return new XDataFrameContent<>(rowKeys, colKeys, true, Mapper.apply(data, parallel, (index, array) -> { if (index != colIndex) { return array; } else { final int colOrdinal = colKeys.getOrdinalForKey(colKey); final Array<?> targetValues = Array.of(Double.class, array.length()); final Cursor cursor = new Cursor(frame, rowKeys.isEmpty() ? -1 : 0, colOrdinal); for (int i = 0; i < rowCount; ++i) { cursor.atRowOrdinal(i); final double value = mapper.applyAsDouble(cursor); targetValues.setDouble(cursor.rowIndex, value); } return targetValues; } })); } }
private static <A extends Annotation, E extends Throwable> double callDouble( AnnotationInterceptor<A> annotationInterceptor, int annotationId, A[] annotations, CallContext context, Arguments currentArguments, ToDoubleFunction<Arguments> terminalInvokeFun) throws E { A annotation = annotations[annotationId]; if (annotationId == annotations.length - 1) { // last annotation return annotationInterceptor.onCall(annotation, context, new SimpleDoubleInterceptionHandler(currentArguments, terminalInvokeFun)); } else { return annotationInterceptor.onCall(annotation, context, new SimpleDoubleInterceptionHandler(currentArguments, (args) -> callDouble(annotationInterceptor, annotationId + 1, annotations, context, args, terminalInvokeFun))); } }
/** * Returns a {@code Collector} that produces the sum of a double-valued * function applied to the input elements. If no elements are present, * the result is 0. * * <p>The sum returned can vary depending upon the order in which * values are recorded, due to accumulated rounding error in * addition of values of differing magnitudes. Values sorted by increasing * absolute magnitude tend to yield more accurate results. If any recorded * value is a {@code NaN} or the sum is at any point a {@code NaN} then the * sum will be {@code NaN}. * * @param <T> the type of the input elements * @param mapper a function extracting the property to be summed * @return a {@code Collector} that produces the sum of a derived property */ public static <T> Collector<T, ?, Double> summingDouble(ToDoubleFunction<? super T> mapper) { /* * In the arrays allocated for the collect operation, index 0 * holds the high-order bits of the running sum, index 1 holds * the low-order bits of the sum computed via compensated * summation, and index 2 holds the simple sum used to compute * the proper result if the stream contains infinite values of * the same sign. */ return new CollectorImpl<>( () -> new double[3], (a, t) -> { double val = mapper.applyAsDouble(t); sumWithCompensation(a, val); a[2] += val;}, (a, b) -> { sumWithCompensation(a, b[0]); a[2] += b[2]; return sumWithCompensation(a, b[1]); }, a -> computeFinalSum(a), CH_NOID); }
public final void compute() { final ToDoubleFunction<? 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 MapReduceValuesToDoubleTask<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.val)); result = r; CountedCompleter<?> c; for (c = firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") MapReduceValuesToDoubleTask<K,V> t = (MapReduceValuesToDoubleTask<K,V>)c, s = t.rights; while (s != null) { t.result = reducer.applyAsDouble(t.result, s.result); s = t.rights = s.nextRight; } } } }
@Override protected <T> io.micrometer.core.instrument.Gauge newGauge(Meter.Id id, T obj, ToDoubleFunction<T> f) { final WeakReference<T> ref = new WeakReference<>(obj); Gauge<Double> gauge = () -> { T obj2 = ref.get(); return obj2 != null ? f.applyAsDouble(ref.get()) : Double.NaN; }; registry.register(hierarchicalName(id), gauge); return new DropwizardGauge(id, gauge); }
@Override protected <T> FunctionTimer newFunctionTimer(Meter.Id id, T obj, ToLongFunction<T> countFunction, ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnits) { DropwizardFunctionTimer ft = new DropwizardFunctionTimer<>(id, clock, obj, countFunction, totalTimeFunction, totalTimeFunctionUnits, getBaseTimeUnit()); registry.register(hierarchicalName(id), ft.getDropwizardMeter()); return ft; }
@Override() public final Z applyDoubles(ToDoubleFunction<DataFrameValue<R,C>> mapper) { return forEachValue(value -> { final double result = mapper.applyAsDouble(value); value.setDouble(result); }); }
public final void compute() { final ToDoubleFunction<? super K> 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 MapReduceKeysToDoubleTask<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)); result = r; CountedCompleter<?> c; for (c = firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") MapReduceKeysToDoubleTask<K,V> t = (MapReduceKeysToDoubleTask<K,V>)c, s = t.rights; while (s != null) { t.result = reducer.applyAsDouble(t.result, s.result); s = t.rights = s.nextRight; } } } }
/** * A timer that tracks monotonically increasing functions for count and totalTime. */ public <T> FunctionTimer timer(String name, Iterable<Tag> tags, T obj, ToLongFunction<T> countFunction, ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnits) { return globalRegistry.more().timer(name, tags, obj, countFunction, totalTimeFunction, totalTimeFunctionUnits); }
private Builder(String name, T obj, ToLongFunction<T> countFunction, ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnits) { this.name = name; this.obj = obj; this.countFunction = countFunction; this.totalTimeFunction = totalTimeFunction; this.totalTimeFunctionUnits = totalTimeFunctionUnits; }
private void counter(MeterRegistry registry, String name, String description, ToDoubleFunction<Statistics> f, String... extraTags) { FunctionCounter.builder(name, stats, f) .tags(tags) .tags(extraTags) .description(description) .register(registry); }
private void buildStatusCounter(MeterRegistry reg, String status, ToDoubleFunction<StatisticsHandler> consumer) { FunctionCounter.builder("jetty.responses", statisticsHandler, consumer) .tags(tags) .description("Number of requests with response status") .tags("status", status) .register(reg); }
/** * Creates an DOUBLE mapper that wraps to function provided * @param function the function to wrap * @param <I> the input type * @return the newly created mapper */ public static <I,O> Function1<I,Double> toDouble(ToDoubleFunction<I> function) { return new Function1<I,Double>(FunctionStyle.DOUBLE) { @Override public final double applyAsDouble(I value) { return function.applyAsDouble(value); } }; }
StatsdFunctionTimer(Id id, T obj, ToLongFunction<T> countFunction, ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnits, TimeUnit baseTimeUnit, StatsdLineBuilder lineBuilder, Subscriber<String> publisher) { super(id, obj, countFunction, totalTimeFunction, totalTimeFunctionUnits, baseTimeUnit); this.lineBuilder = lineBuilder; this.publisher = publisher; }
@Override public final Array<Double> mapToDoubles(ToDoubleFunction<ArrayValue<T>> mapper) { final Array<Double> result = Array.of(Double.class, length()); final MapValues<Double> action = new MapValues<>(0, length() - 1, mapper, result); if (isParallel()) { ForkJoinPool.commonPool().invoke(action); return result; } else { action.compute(); return result; } }
@SuppressWarnings("unchecked") @Override protected <T> io.micrometer.core.instrument.Gauge newGauge(Meter.Id id, T obj, ToDoubleFunction<T> f) { MicrometerCollector collector = collectorByName(id, Collector.Type.GAUGE); Gauge gauge = new DefaultGauge(id, obj, f); List<String> tagValues = tagValues(id); collector.add((conventionName, tagKeys) -> Stream.of( new Collector.MetricFamilySamples.Sample(conventionName, tagKeys, tagValues, gauge.value()) )); return gauge; }
@Override public final Array<T> applyDoubles(ToDoubleFunction<ArrayValue<T>> function) { final int length = length(); if (length > 0) { final ApplyValues action = new ApplyValues(0, length - 1, function); if (isParallel()) { ForkJoinPool.commonPool().invoke(action); } else { action.compute(); } } return this; }
@Override protected <T> FunctionCounter newFunctionCounter(Meter.Id id, T obj, ToDoubleFunction<T> f) { MicrometerCollector collector = collectorByName(id, Collector.Type.COUNTER); FunctionCounter fc = new CumulativeFunctionCounter<>(id, obj, f); List<String> tagValues = tagValues(id); collector.add((conventionName, tagKeys) -> Stream.of( new Collector.MetricFamilySamples.Sample(conventionName, tagKeys, tagValues, fc.count()) )); return fc; }
/** * Creates a subsource for a specific motor in a drive base. * * @param source the source for the drive base data * @param motorName the motor name (eg "Left Motor" or "Right Motor") * @param getter the getter (eg {@code DifferentialDriveData::getLeftSpeed}) * @param setter the setter (eg {@code DifferentialDriveData::withLeftSpeed}) */ protected DataSource<SpeedControllerData> motorSource(DataSource<T> source, String motorName, ToDoubleFunction<T> getter, BiFunction<T, Double, T> setter) { return new SubSource<>( SpeedControllerType.Instance, source, d -> setter.apply(dataOrDefault.get(), d == null ? 0.0 : d.getValue()), d -> new SpeedControllerData(motorName, d == null ? 0.0 : getter.applyAsDouble(d)) ); }
private static <E extends Throwable> double callDouble(int interceptorId, Interceptor[] interceptors, CallContext context, Arguments currentArguments, ToDoubleFunction<Arguments> terminalInvokeFun) throws E { Interceptor interceptor = interceptors[interceptorId]; if (interceptorId == interceptors.length - 1) { // last interceptor return interceptor.onCall(context, new SimpleDoubleInterceptionHandler(currentArguments, terminalInvokeFun)); } else { return interceptor.onCall(context, new SimpleDoubleInterceptionHandler(currentArguments, (args) -> callDouble(interceptorId + 1, interceptors, context, args, terminalInvokeFun))); } }
public final void compute() { final ToDoubleFunction<Map.Entry<K,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 MapReduceEntriesToDoubleTask<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)); result = r; CountedCompleter<?> c; for (c = firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") MapReduceEntriesToDoubleTask<K,V> t = (MapReduceEntriesToDoubleTask<K,V>)c, s = t.rights; while (s != null) { t.result = reducer.applyAsDouble(t.result, s.result); s = t.rights = s.nextRight; } } } }
MapReduceValuesToDoubleTask (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, MapReduceValuesToDoubleTask<K,V> nextRight, ToDoubleFunction<? 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; }
MapReduceKeysToDoubleTask (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, MapReduceKeysToDoubleTask<K,V> nextRight, ToDoubleFunction<? super K> transformer, double basis, DoubleBinaryOperator reducer) { super(p, b, i, f, t); this.nextRight = nextRight; this.transformer = transformer; this.basis = basis; this.reducer = reducer; }
MapReduceEntriesToDoubleTask (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, MapReduceEntriesToDoubleTask<K,V> nextRight, ToDoubleFunction<Map.Entry<K,V>> transformer, double basis, DoubleBinaryOperator reducer) { super(p, b, i, f, t); this.nextRight = nextRight; this.transformer = transformer; this.basis = basis; this.reducer = reducer; }
@Override public DoubleStream mapToDouble(ToDoubleFunction arg0) { return stream.mapToDouble(arg0); }
@Override public DoubleStream mapToDouble(final ToDoubleFunction<? super T> mapper) { return stream.mapToDouble(mapper); }
public static <T> CalculatedDoubleValues calculateDouble(Collection<T> collection, ToDoubleFunction<T> toDouble) { return calculate(collection.stream().mapToDouble(toDouble)); }
@Override public DataFrame<R,C> mapToDoubles(C colKey, ToDoubleFunction<DataFrameValue<R,C>> mapper) { return new XDataFrame<>(content().mapToDoubles(this, colKey, mapper), isParallel()); }
public CumulativeFunctionCounter(Meter.Id id, T obj, ToDoubleFunction<T> f) { super(id); this.ref = new WeakReference<>(obj); this.f = f; }
static <T> Builder<T> builder(String name, T obj, ToDoubleFunction<T> f) { return new Builder<>(name, obj, f); }
private Builder(String name, T obj, ToDoubleFunction<T> f) { this.name = name; this.obj = obj; this.f = f; }