Merge remote-tracking branch 'upstream/master' into BAEL-2564
This commit is contained in:
@@ -13,3 +13,4 @@
|
||||
- [How to Invert an Array in Java](http://www.baeldung.com/java-invert-array)
|
||||
- [Array Operations in Java](http://www.baeldung.com/java-common-array-operations)
|
||||
- [Intersection Between two Integer Arrays](https://www.baeldung.com/java-array-intersection)
|
||||
- [Sorting Arrays in Java](https://www.baeldung.com/java-sorting-arrays)
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.array.conversions;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class FloatToByteArray {
|
||||
|
||||
/**
|
||||
* convert float into byte array using Float API floatToIntBits
|
||||
* @param value
|
||||
* @return byte[]
|
||||
*/
|
||||
public static byte[] floatToByteArray(float value) {
|
||||
int intBits = Float.floatToIntBits(value);
|
||||
return new byte[] {(byte) (intBits >> 24), (byte) (intBits >> 16), (byte) (intBits >> 8), (byte) (intBits) };
|
||||
}
|
||||
|
||||
/**
|
||||
* convert byte array into float using Float API intBitsToFloat
|
||||
* @param bytes
|
||||
* @return float
|
||||
*/
|
||||
public static float byteArrayToFloat(byte[] bytes) {
|
||||
int intBits = bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF);
|
||||
return Float.intBitsToFloat(intBits);
|
||||
}
|
||||
|
||||
/**
|
||||
* convert float into byte array using ByteBuffer
|
||||
* @param value
|
||||
* @return byte[]
|
||||
*/
|
||||
public static byte[] floatToByteArrayWithByteBuffer(float value) {
|
||||
return ByteBuffer.allocate(4).putFloat(value).array();
|
||||
}
|
||||
|
||||
/**
|
||||
* convert byte array into float using ByteBuffer
|
||||
* @param bytes
|
||||
* @return float
|
||||
*/
|
||||
public static float byteArrayToFloatWithByteBuffer(byte[] bytes) {
|
||||
return ByteBuffer.wrap(bytes).getFloat();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.arrays;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
import org.openjdk.jmh.infra.Blackhole;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class ParallelPrefixBenchmark {
|
||||
private static final int ARRAY_SIZE = 200_000_000;
|
||||
|
||||
@State(Scope.Benchmark)
|
||||
public static class BigArray {
|
||||
|
||||
int[] data;
|
||||
|
||||
@Setup(Level.Iteration)
|
||||
public void prepare() {
|
||||
data = new int[ARRAY_SIZE];
|
||||
for(int j = 0; j< ARRAY_SIZE; j++) {
|
||||
data[j] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
@TearDown(Level.Iteration)
|
||||
public void destroy() {
|
||||
data = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void largeArrayLoopSum(BigArray bigArray, Blackhole blackhole) {
|
||||
for (int i = 0; i < ARRAY_SIZE - 1; i++) {
|
||||
bigArray.data[i + 1] += bigArray.data[i];
|
||||
}
|
||||
blackhole.consume(bigArray.data);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void largeArrayParallelPrefixSum(BigArray bigArray, Blackhole blackhole) {
|
||||
Arrays.parallelPrefix(bigArray.data, (left, right) -> left + right);
|
||||
blackhole.consume(bigArray.data);
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.array.conversions;
|
||||
|
||||
import static com.baeldung.array.conversions.FloatToByteArray.byteArrayToFloat;
|
||||
import static com.baeldung.array.conversions.FloatToByteArray.byteArrayToFloatWithByteBuffer;
|
||||
import static com.baeldung.array.conversions.FloatToByteArray.floatToByteArray;
|
||||
import static com.baeldung.array.conversions.FloatToByteArray.floatToByteArrayWithByteBuffer;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FloatToByteArrayUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAFloat_thenConvertToByteArray() {
|
||||
assertArrayEquals(new byte[] { 63, -116, -52, -51}, floatToByteArray(1.1f));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAByteArray_thenConvertToFloat() {
|
||||
assertEquals(1.1f, byteArrayToFloat(new byte[] { 63, -116, -52, -51}), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAFloat_thenConvertToByteArrayUsingByteBuffer() {
|
||||
assertArrayEquals(new byte[] { 63, -116, -52, -51}, floatToByteArrayWithByteBuffer(1.1f));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAByteArray_thenConvertToFloatUsingByteBuffer() {
|
||||
assertEquals(1.1f, byteArrayToFloatWithByteBuffer(new byte[] { 63, -116, -52, -51}), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAFloat_thenConvertToByteArray_thenConvertToFloat() {
|
||||
float floatToConvert = 200.12f;
|
||||
byte[] byteArray = floatToByteArray(floatToConvert);
|
||||
assertEquals(200.12f, byteArrayToFloat(byteArray), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAFloat_thenConvertToByteArrayWithByteBuffer_thenConvertToFloatWithByteBuffer() {
|
||||
float floatToConvert = 30100.42f;
|
||||
byte[] byteArray = floatToByteArrayWithByteBuffer(floatToConvert);
|
||||
assertEquals(30100.42f, byteArrayToFloatWithByteBuffer(byteArray), 0);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.baeldung.arrays;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -9,6 +11,7 @@ import org.junit.rules.ExpectedException;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class ArraysUnitTest {
|
||||
@@ -150,4 +153,52 @@ public class ArraysUnitTest {
|
||||
exception.expect(UnsupportedOperationException.class);
|
||||
rets.add("the");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntArray_whenPrefixAdd_thenAllAccumulated() {
|
||||
int[] arri = new int[] { 1, 2, 3, 4};
|
||||
Arrays.parallelPrefix(arri, (left, right) -> left + right);
|
||||
assertThat(arri, is(new int[] { 1, 3, 6, 10}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringArray_whenPrefixConcat_thenAllMerged() {
|
||||
String[] arrs = new String[] { "1", "2", "3" };
|
||||
Arrays.parallelPrefix(arrs, (left, right) -> left + right);
|
||||
assertThat(arrs, is(new String[] { "1", "12", "123" }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPrefixAddWithRange_thenRangeAdded() {
|
||||
int[] arri = new int[] { 1, 2, 3, 4, 5 };
|
||||
Arrays.parallelPrefix(arri, 1, 4, (left, right) -> left + right);
|
||||
assertThat(arri, is(new int[] { 1, 2, 5, 9, 5 }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPrefixNonAssociative_thenError() {
|
||||
boolean consistent = true;
|
||||
Random r = new Random();
|
||||
for (int k = 0; k < 100_000; k++) {
|
||||
int[] arrA = r.ints(100, 1, 5).toArray();
|
||||
int[] arrB = Arrays.copyOf(arrA, arrA.length);
|
||||
|
||||
Arrays.parallelPrefix(arrA, this::nonassociativeFunc);
|
||||
|
||||
for (int i = 1; i < arrB.length; i++) {
|
||||
arrB[i] = nonassociativeFunc(arrB[i - 1], arrB[i]);
|
||||
}
|
||||
consistent = Arrays.equals(arrA, arrB);
|
||||
if(!consistent) break;
|
||||
}
|
||||
assertFalse(consistent);
|
||||
}
|
||||
|
||||
/**
|
||||
* non-associative int binary operator
|
||||
*/
|
||||
private int nonassociativeFunc(int left, int right) {
|
||||
return left + right*left;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user