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());
}
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));
}
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));
}
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());
}
};
}
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);
}
Aggregations