Search in sources :

Example 56 with IntStream

use of java.util.stream.IntStream in project jdk8u_jdk by JetBrains.

the class BitSetStreamTest method testBitsetStream.

@Test(dataProvider = "cases")
public void testBitsetStream(String name, IntStream data) {
    BitSet bs = new BitSet();
    long setBits = data.distinct().peek(i -> bs.set(i)).count();
    assertEquals(bs.cardinality(), setBits);
    assertEquals(bs.cardinality(), bs.stream().reduce(0, (s, i) -> s + 1));
    PrimitiveIterator.OfInt it = bs.stream().iterator();
    for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) {
        assertTrue(it.hasNext());
        assertEquals(it.nextInt(), i);
    }
    assertFalse(it.hasNext());
}
Also used : IntStream(java.util.stream.IntStream) DataProvider(org.testng.annotations.DataProvider) System(java.lang.System) Assert.fail(org.testng.Assert.fail) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) Random(java.util.Random) Integer(java.lang.Integer) OptionalInt(java.util.OptionalInt) Collectors(java.util.stream.Collectors) PrimitiveIterator(java.util.PrimitiveIterator) Assert.assertTrue(org.testng.Assert.assertTrue) Object(java.lang.Object) BitSet(java.util.BitSet) Assert.assertFalse(org.testng.Assert.assertFalse) IntSupplier(java.util.function.IntSupplier) PrimitiveIterator(java.util.PrimitiveIterator) BitSet(java.util.BitSet) Test(org.testng.annotations.Test)

Example 57 with IntStream

use of java.util.stream.IntStream in project Gargoyle by callakrsos.

the class CodeAnalysisJavaTextArea method initialize.

@FXML
public void initialize() {
    choMethod = new ChoiceBox<>();
    choMethod.setConverter(new StringConverter<SourceAnalysisDVO>() {

        @Override
        public String toString(SourceAnalysisDVO d) {
            return d.getMethodName();
        }

        @Override
        public SourceAnalysisDVO fromString(String string) {
            return null;
        }
    });
    // jdk1.8.73이후버젼부터 사용가능
    choMethod.setOnAction(event -> {
        SourceAnalysisDVO selectedItem = choMethod.getSelectionModel().getSelectedItem();
        String methodName = selectedItem.getMethodName();
        int methodBlockStart = selectedItem.getMethodBlockStart();
        int finallyBlockIdx = methodBlockStart - 1;
        String string = newInstance.getReadLines().get(finallyBlockIdx);
        if (!string.contains(methodName)) {
            for (int i = methodBlockStart - 2; i < methodBlockStart + 1; i++) {
                if (i < 0)
                    continue;
                if (i == (methodBlockStart - 1))
                    continue;
                string = newInstance.getReadLines().get(i);
                if (string.contains(methodName)) {
                    finallyBlockIdx = i;
                    break;
                }
            }
        }
        IntStream mapToInt = Stream.of(txtJavaTextArea.getText().split("\n")).limit(finallyBlockIdx).mapToInt(mapper -> {
            return mapper.length() + 1;
        });
        int length = string.split("\t").length - 1;
        int sum = mapToInt.sum() + length;
        System.out.println(string);
        txtJavaTextArea.selectRange(sum, sum + methodName.length());
    });
    this.setTop(new HBox(5, new Label("Method Name : "), choMethod));
}
Also used : HBox(javafx.scene.layout.HBox) Label(javafx.scene.control.Label) IntStream(java.util.stream.IntStream) SourceAnalysisDVO(com.kyj.fx.voeditor.visual.words.spec.auto.msword.vo.SourceAnalysisDVO) FXML(javafx.fxml.FXML)

Example 58 with IntStream

use of java.util.stream.IntStream in project SoftUni by kostovhg.

the class Main method main.

public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    int[] ints = { 1, 2, 3, 4 };
    IntStream intStream = IntStream.of(ints);
    List<Integer> list = new ArrayList<>();
    IntStream mappedIntStream = list.stream().mapToInt(n -> Integer.valueOf(n));
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) IntStream(java.util.stream.IntStream)

Example 59 with IntStream

use of java.util.stream.IntStream in project MindsEye by SimiaCryptus.

the class ImgConcatLayer method evalAndFree.

@Nullable
@Override
public Result evalAndFree(@Nonnull final Result... inObj) {
    if (!CudaSystem.isEnabled())
        return getCompatibilityLayer().evalAndFree(inObj);
    // assert Arrays.stream(this.bias).allMatch(Double::isFinite);
    // assert Arrays.stream(inObj).flatMapToDouble(input->input.data.stream().flatMapToDouble(x-> Arrays.stream(x.getData()))).allMatch(v->Double.isFinite(v));
    int[] dimensions = inObj[0].getData().getDimensions();
    assert 3 == dimensions.length;
    @Nonnull final int[] outputDimensions = Arrays.copyOf(dimensions, dimensions.length);
    final int length = inObj[0].getData().length();
    assert Arrays.stream(inObj).allMatch(x -> {
        @Nonnull int[] d = x.getData().getDimensions();
        return 3 == d.length && d[0] == outputDimensions[0] && d[1] == outputDimensions[1] && x.getData().length() == length;
    });
    outputDimensions[2] = Arrays.stream(inObj).mapToInt(x -> x.getData().getDimensions()[2]).sum();
    if (0 < maxBands && outputDimensions[2] > maxBands) {
        outputDimensions[2] = maxBands;
    }
    return new Result(CudaSystem.run(gpu -> {
        final long outputSize = ((long) length * outputDimensions[2] * outputDimensions[1] * outputDimensions[0] * precision.size);
        @Nonnull final CudaMemory cudaOutput = gpu.allocate(outputSize, MemoryType.Managed.normalize(), true);
        IntStream stream = IntStream.range(0, inObj.length);
        // if (!CoreSettings.INSTANCE.isConservative() && parallel) stream = stream.parallel();
        stream.forEach(i -> {
            assert CudaDevice.isThreadDeviceId(gpu.getDeviceId());
            final TensorList input = inObj[i].getData();
            @Nonnull final int[] inputDimensions = input.getDimensions();
            assert inputDimensions[0] == outputDimensions[0];
            assert inputDimensions[1] == outputDimensions[1];
            int bandOffset = IntStream.range(0, i).map(j -> inObj[j].getData().getDimensions()[2]).sum();
            if (maxBands > 0)
                bandOffset = Math.min(bandOffset, maxBands);
            int inputBands = inputDimensions[2];
            if (maxBands > 0)
                inputBands = Math.min(inputBands, maxBands - bandOffset);
            if (inputBands > 0) {
                @Nullable final CudaTensor cudaInput = gpu.getTensor(input, precision, MemoryType.Device, false);
                assert inputBands > 0;
                assert maxBands <= 0 || inputBands <= maxBands;
                assert inputBands <= inputDimensions[2];
                @Nonnull final CudaDevice.CudaTensorDescriptor outputDescriptor = gpu.newTensorDescriptor(// 
                precision, // 
                length, // 
                inputBands, // 
                outputDimensions[1], // 
                outputDimensions[0], // 
                outputDimensions[2] * outputDimensions[1] * outputDimensions[0], // 
                outputDimensions[1] * outputDimensions[0], // 
                outputDimensions[0], 1);
                @Nonnull final CudaDevice.CudaTensorDescriptor inputDescriptor = gpu.newTensorDescriptor(// 
                precision, // 
                length, // 
                inputBands, // 
                inputDimensions[1], // 
                inputDimensions[0], // 
                cudaInput.descriptor.nStride, // 
                cudaInput.descriptor.cStride, // 
                cudaInput.descriptor.hStride, cudaInput.descriptor.wStride);
                int byteOffset = outputDescriptor.cStride * bandOffset * precision.size;
                CudaMemory cudaInputMemory = cudaInput.getMemory(gpu);
                gpu.cudnnTransformTensor(precision.getPointer(1.0), inputDescriptor.getPtr(), cudaInputMemory.getPtr(), precision.getPointer(0.0), outputDescriptor.getPtr(), cudaOutput.getPtr().withByteOffset(byteOffset));
                assert CudaDevice.isThreadDeviceId(gpu.getDeviceId());
                cudaInputMemory.dirty();
                cudaOutput.dirty();
                cudaInputMemory.freeRef();
                Stream.<ReferenceCounting>of(cudaInput, outputDescriptor, inputDescriptor).forEach(ReferenceCounting::freeRef);
            }
        });
        CudaDevice.CudaTensorDescriptor outDesc = gpu.newTensorDescriptor(precision, length, outputDimensions[2], outputDimensions[1], outputDimensions[0]);
        return CudaTensorList.wrap(CudaTensor.wrap(cudaOutput, outDesc, precision), length, outputDimensions, precision);
    }, Arrays.stream(inObj).map(Result::getData).toArray()), (@Nonnull final DeltaSet<Layer> buffer, @Nonnull final TensorList delta) -> {
        assert delta.getDimensions()[0] == outputDimensions[0];
        assert delta.getDimensions()[1] == outputDimensions[1];
        assert delta.getDimensions()[2] == outputDimensions[2];
        if (!Arrays.equals(delta.getDimensions(), outputDimensions)) {
            throw new AssertionError(Arrays.toString(delta.getDimensions()) + " != " + Arrays.toString(outputDimensions));
        }
        // outputBuffer.freeRef();
        // assert error.stream().flatMapToDouble(x-> Arrays.stream(x.getData())).allMatch(Double::isFinite);
        @Nonnull IntStream stream = IntStream.range(0, inObj.length);
        if (!CoreSettings.INSTANCE.isSingleThreaded() && parallel)
            stream = stream.parallel();
        stream.forEach(i -> {
            final Result input = inObj[i];
            int[] inputDimentions = input.getData().getDimensions();
            assert 3 == inputDimentions.length;
            assert delta.length() == input.getData().length();
            assert inputDimentions[0] == outputDimensions[0];
            assert inputDimentions[1] == outputDimensions[1];
            int bandOffset = IntStream.range(0, i).map(j -> inObj[j].getData().getDimensions()[2]).sum();
            int inputBands = maxBands <= 0 ? inputDimentions[2] : Math.min(inputDimentions[2], maxBands - bandOffset);
            if (inputBands > 0 && input.isAlive()) {
                assert inputBands <= inputDimentions[2];
                assert inputBands <= outputDimensions[2];
                final TensorList passbackTensorList = CudaSystem.run(gpu -> {
                    final CudaTensor result;
                    synchronized (gpu) {
                        result = gpu.getTensor(delta, precision, MemoryType.Device, true);
                    }
                    @Nullable final CudaTensor cudaDelta = result;
                    CudaMemory cudaDeltaMemory = cudaDelta.getMemory(gpu);
                    try {
                        if (inputDimentions[2] == inputBands) {
                            @Nonnull final CudaDevice.CudaTensorDescriptor viewDescriptor = gpu.newTensorDescriptor(// 
                            precision, // 
                            length, // 
                            inputDimentions[2], // 
                            inputDimentions[1], // 
                            inputDimentions[0], // 
                            cudaDelta.descriptor.nStride, // 
                            cudaDelta.descriptor.cStride, // 
                            cudaDelta.descriptor.hStride, cudaDelta.descriptor.wStride);
                            int byteOffset = cudaDelta.descriptor.cStride * bandOffset * precision.size;
                            CudaMemory ptr = cudaDeltaMemory.withByteOffset(byteOffset);
                            CudaTensor cudaTensor = CudaTensor.wrap(ptr, viewDescriptor, precision);
                            Stream.<ReferenceCounting>of(cudaDelta).forEach(ReferenceCounting::freeRef);
                            return CudaTensorList.wrap(cudaTensor, length, inputDimentions, precision);
                        } else {
                            @Nonnull final CudaDevice.CudaTensorDescriptor passbackTransferDescriptor = gpu.newTensorDescriptor(// 
                            precision, // 
                            length, // 
                            inputBands, // 
                            inputDimentions[1], // 
                            inputDimentions[0], // 
                            inputDimentions[2] * inputDimentions[1] * inputDimentions[0], // 
                            inputDimentions[1] * inputDimentions[0], // 
                            inputDimentions[0], 1);
                            @Nonnull final CudaDevice.CudaTensorDescriptor passbackDescriptor = gpu.newTensorDescriptor(// 
                            precision, // 
                            length, // 
                            inputDimentions[2], // 
                            inputDimentions[1], // 
                            inputDimentions[0], // 
                            inputDimentions[2] * inputDimentions[1] * inputDimentions[0], // 
                            inputDimentions[1] * inputDimentions[0], // 
                            inputDimentions[0], 1);
                            @Nonnull final CudaDevice.CudaTensorDescriptor deltaViewDescriptor = gpu.newTensorDescriptor(// 
                            precision, // 
                            length, // 
                            inputBands, // 
                            inputDimentions[1], // 
                            inputDimentions[0], // 
                            cudaDelta.descriptor.nStride, // 
                            cudaDelta.descriptor.cStride, // 
                            cudaDelta.descriptor.hStride, cudaDelta.descriptor.wStride);
                            @Nonnull final CudaMemory cudaBackprop = gpu.allocate((long) passbackDescriptor.nStride * length * precision.size, MemoryType.Managed.normalize(), inputBands == inputDimentions[2]);
                            int byteOffset = cudaDelta.descriptor.cStride * bandOffset * precision.size;
                            gpu.cudnnTransformTensor(precision.getPointer(1.0), deltaViewDescriptor.getPtr(), cudaDeltaMemory.getPtr().withByteOffset(byteOffset), precision.getPointer(0.0), passbackTransferDescriptor.getPtr(), cudaBackprop.getPtr());
                            cudaBackprop.dirty();
                            cudaDeltaMemory.dirty();
                            Stream.<ReferenceCounting>of(cudaDelta, deltaViewDescriptor, passbackTransferDescriptor).forEach(ReferenceCounting::freeRef);
                            return CudaTensorList.wrap(CudaTensor.wrap(cudaBackprop, passbackDescriptor, precision), length, inputDimentions, precision);
                        }
                    } finally {
                        cudaDeltaMemory.freeRef();
                    }
                });
                input.accumulate(buffer, passbackTensorList);
            }
        // assert passbackTensorList.stream().flatMapToDouble(x-> Arrays.stream(x.getData())).allMatch(v->Double.isFinite(v));
        });
    }) {

        @Override
        protected void _free() {
            for (@Nonnull Result result : inObj) {
                result.freeRef();
                result.getData().freeRef();
            }
        }

        @Override
        public boolean isAlive() {
            return Arrays.stream(inObj).anyMatch(x -> x.isAlive());
        }
    };
}
Also used : IntStream(java.util.stream.IntStream) JsonObject(com.google.gson.JsonObject) Arrays(java.util.Arrays) CudaDevice(com.simiacryptus.mindseye.lang.cudnn.CudaDevice) CudaTensor(com.simiacryptus.mindseye.lang.cudnn.CudaTensor) CudaMemory(com.simiacryptus.mindseye.lang.cudnn.CudaMemory) CoreSettings(com.simiacryptus.mindseye.lang.CoreSettings) CudaTensorList(com.simiacryptus.mindseye.lang.cudnn.CudaTensorList) Result(com.simiacryptus.mindseye.lang.Result) DataSerializer(com.simiacryptus.mindseye.lang.DataSerializer) Precision(com.simiacryptus.mindseye.lang.cudnn.Precision) List(java.util.List) LayerBase(com.simiacryptus.mindseye.lang.LayerBase) Stream(java.util.stream.Stream) CudaSystem(com.simiacryptus.mindseye.lang.cudnn.CudaSystem) TensorList(com.simiacryptus.mindseye.lang.TensorList) Map(java.util.Map) MemoryType(com.simiacryptus.mindseye.lang.cudnn.MemoryType) Layer(com.simiacryptus.mindseye.lang.Layer) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) ReferenceCounting(com.simiacryptus.mindseye.lang.ReferenceCounting) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) CudaTensor(com.simiacryptus.mindseye.lang.cudnn.CudaTensor) CudaDevice(com.simiacryptus.mindseye.lang.cudnn.CudaDevice) Nonnull(javax.annotation.Nonnull) CudaMemory(com.simiacryptus.mindseye.lang.cudnn.CudaMemory) DeltaSet(com.simiacryptus.mindseye.lang.DeltaSet) CudaTensorList(com.simiacryptus.mindseye.lang.cudnn.CudaTensorList) TensorList(com.simiacryptus.mindseye.lang.TensorList) Result(com.simiacryptus.mindseye.lang.Result) ReferenceCounting(com.simiacryptus.mindseye.lang.ReferenceCounting) IntStream(java.util.stream.IntStream) Nullable(javax.annotation.Nullable) Nullable(javax.annotation.Nullable)

Example 60 with IntStream

use of java.util.stream.IntStream in project MindsEye by SimiaCryptus.

the class TestUtil method shuffle.

/**
 * Shuffle int stream.
 *
 * @param stream the stream
 * @return the int stream
 */
public static IntStream shuffle(@Nonnull IntStream stream) {
    // http://primes.utm.edu/lists/small/10000.txt
    long coprimeA = 41387;
    long coprimeB = 9967;
    long ringSize = coprimeA * coprimeB - 1;
    @Nonnull IntToLongFunction fn = x -> (x * coprimeA * coprimeA) % ringSize;
    @Nonnull LongToIntFunction inv = x -> (int) ((x * coprimeB * coprimeB) % ringSize);
    @Nonnull IntUnaryOperator conditions = x -> {
        assert x < ringSize;
        assert x >= 0;
        return x;
    };
    return stream.map(conditions).mapToLong(fn).sorted().mapToInt(inv);
}
Also used : Arrays(java.util.Arrays) ScheduledFuture(java.util.concurrent.ScheduledFuture) DoubleStatistics(com.simiacryptus.util.data.DoubleStatistics) IntUnaryOperator(java.util.function.IntUnaryOperator) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) TrainingMonitor(com.simiacryptus.mindseye.opt.TrainingMonitor) Map(java.util.Map) ImageIO(javax.imageio.ImageIO) Layer(com.simiacryptus.mindseye.lang.Layer) URI(java.net.URI) Graph(guru.nidi.graphviz.model.Graph) LongToIntFunction(java.util.function.LongToIntFunction) StochasticComponent(com.simiacryptus.mindseye.layers.java.StochasticComponent) BufferedImage(java.awt.image.BufferedImage) UUID(java.util.UUID) ComponentEvent(java.awt.event.ComponentEvent) WindowAdapter(java.awt.event.WindowAdapter) DAGNode(com.simiacryptus.mindseye.network.DAGNode) Collectors(java.util.stream.Collectors) WindowEvent(java.awt.event.WindowEvent) Executors(java.util.concurrent.Executors) List(java.util.List) Stream(java.util.stream.Stream) ScalarStatistics(com.simiacryptus.util.data.ScalarStatistics) LoggingWrapperLayer(com.simiacryptus.mindseye.layers.java.LoggingWrapperLayer) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) IntStream(java.util.stream.IntStream) MonitoringWrapperLayer(com.simiacryptus.mindseye.layers.java.MonitoringWrapperLayer) ActionListener(java.awt.event.ActionListener) ScatterPlot(smile.plot.ScatterPlot) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LinkSource(guru.nidi.graphviz.model.LinkSource) Tensor(com.simiacryptus.mindseye.lang.Tensor) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) JsonUtil(com.simiacryptus.util.io.JsonUtil) MutableNode(guru.nidi.graphviz.model.MutableNode) Charset(java.nio.charset.Charset) Factory(guru.nidi.graphviz.model.Factory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) WeakReference(java.lang.ref.WeakReference) LinkTarget(guru.nidi.graphviz.model.LinkTarget) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) LongSummaryStatistics(java.util.LongSummaryStatistics) PrintStream(java.io.PrintStream) Logger(org.slf4j.Logger) PlotCanvas(smile.plot.PlotCanvas) RankDir(guru.nidi.graphviz.attribute.RankDir) IOException(java.io.IOException) FileFilter(javax.swing.filechooser.FileFilter) ActionEvent(java.awt.event.ActionEvent) PercentileStatistics(com.simiacryptus.util.data.PercentileStatistics) File(java.io.File) java.awt(java.awt) ComponentAdapter(java.awt.event.ComponentAdapter) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) MonitoredObject(com.simiacryptus.util.MonitoredObject) IntToLongFunction(java.util.function.IntToLongFunction) Link(guru.nidi.graphviz.model.Link) Step(com.simiacryptus.mindseye.opt.Step) Comparator(java.util.Comparator) javax.swing(javax.swing) Nonnull(javax.annotation.Nonnull) LongToIntFunction(java.util.function.LongToIntFunction) IntToLongFunction(java.util.function.IntToLongFunction) IntUnaryOperator(java.util.function.IntUnaryOperator)

Aggregations

IntStream (java.util.stream.IntStream)96 List (java.util.List)19 Test (org.junit.jupiter.api.Test)17 ArrayList (java.util.ArrayList)15 Stream (java.util.stream.Stream)12 Test (org.junit.Test)12 Arrays (java.util.Arrays)11 Map (java.util.Map)10 Collectors (java.util.stream.Collectors)9 Random (java.util.Random)7 DoubleStream (java.util.stream.DoubleStream)6 LongStream (java.util.stream.LongStream)6 Function (java.util.function.Function)5 Pattern (java.util.regex.Pattern)5 DecimalBoxFieldDefinition (org.kie.workbench.common.forms.fields.shared.fieldTypes.basic.decimalBox.definition.DecimalBoxFieldDefinition)5 MultipleSubFormFieldDefinition (org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.multipleSubform.definition.MultipleSubFormFieldDefinition)5 SubFormFieldDefinition (org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.subForm.definition.SubFormFieldDefinition)5 FieldDefinition (org.kie.workbench.common.forms.model.FieldDefinition)5 LayoutRow (org.uberfire.ext.layout.editor.api.editor.LayoutRow)5 LayoutTemplate (org.uberfire.ext.layout.editor.api.editor.LayoutTemplate)5